Commit 52221b3b authored by vipwzw's avatar vipwzw

update chain33

parent 13e2d54e
......@@ -108,7 +108,15 @@ func (c *KVCreator) AddNoPrefix(key, value []byte) *KVCreator {
//AddListNoPrefix only add KVList
func (c *KVCreator) AddListNoPrefix(list []*types.KeyValue) *KVCreator {
for _, kv := range list {
c.add(kv.Key, kv.Value, false)
c.addnoprefix(kv.Key, kv.Value, true)
}
return c
}
//AddList only add KVList
func (c *KVCreator) AddList(list []*types.KeyValue) *KVCreator {
for _, kv := range list {
c.add(kv.Key, kv.Value, true)
}
return c
}
......
......@@ -28,15 +28,21 @@ func TestKVCreator(t *testing.T) {
{Key: []byte("l1"), Value: []byte("vl1")},
{Key: []byte("l2"), Value: []byte("vl2")},
})
creator.AddListNoPrefix([]*types.KeyValue{
{Key: []byte("l1"), Value: []byte("vl1")},
{Key: []byte("l2"), Value: []byte("vl2")},
})
creator.Add([]byte("c1"), nil)
creator.Add([]byte("l2"), nil)
creator.AddRollbackKV()
assert.Equal(t, 7, len(creator.KVList()))
assert.Equal(t, 9, len(creator.KVList()))
util.SaveKVList(ldb, creator.KVList())
kvs, err := creator.GetRollbackKVList()
assert.Nil(t, err)
assert.Equal(t, 6, len(kvs))
assert.Equal(t, []byte("b"), kvs[5].Value)
assert.Equal(t, 8, len(kvs))
assert.Equal(t, []byte("b"), kvs[7].Value)
assert.Equal(t, []byte(nil), kvs[6].Value)
assert.Equal(t, []byte(nil), kvs[5].Value)
assert.Equal(t, []byte(nil), kvs[4].Value)
assert.Equal(t, []byte(nil), kvs[3].Value)
assert.Equal(t, []byte(nil), kvs[2].Value)
......
......@@ -130,7 +130,12 @@ func RunChain33(name string) {
q := queue.New("channel")
log.Info("loading mempool module")
mem := mempool.New(cfg.Mempool, sub.Mempool)
var mem queue.Module
if !types.IsPara() {
mem = mempool.New(cfg.Mempool, sub.Mempool)
} else {
mem = &util.MockModule{Key: "mempool"}
}
mem.SetQueueClient(q.Client())
log.Info("loading execs module")
......@@ -150,12 +155,15 @@ func RunChain33(name string) {
cs := consensus.New(cfg.Consensus, sub.Consensus)
cs.SetQueueClient(q.Client())
var network *p2p.P2p
if cfg.P2P.Enable {
log.Info("loading p2p module")
log.Info("loading p2p module")
var network queue.Module
if cfg.P2P.Enable && !types.IsPara() {
network = p2p.New(cfg.P2P)
network.SetQueueClient(q.Client())
} else {
network = &util.MockModule{Key: "p2p"}
}
network.SetQueueClient(q.Client())
//jsonrpc, grpc, channel 三种模式
rpcapi := rpc.New(cfg.RPC)
rpcapi.SetQueueClient(q.Client())
......@@ -169,10 +177,8 @@ func RunChain33(name string) {
chain.Close()
log.Info("begin close mempool module")
mem.Close()
if cfg.P2P.Enable {
log.Info("begin close P2P module")
network.Close()
}
log.Info("begin close P2P module")
network.Close()
log.Info("begin close execs module")
exec.Close()
log.Info("begin close store module")
......
......@@ -453,3 +453,25 @@ func PrintKV(kvs []*types.KeyValue) {
fmt.Printf("KV %d %s(%s)\n", i, string(kvs[i].Key), common.ToHex(kvs[i].Value))
}
}
// MockModule struct
type MockModule struct {
Key string
}
// SetQueueClient method
func (m *MockModule) SetQueueClient(client queue.Client) {
go func() {
client.Sub(m.Key)
for msg := range client.Recv() {
msg.Reply(client.NewMessage(m.Key, types.EventReply, &types.Reply{IsOk: false,
Msg: []byte(fmt.Sprintf("mock %s module not handle message %v", m.Key, msg.Ty))}))
}
}()
}
// Wait for ready
func (m *MockModule) Wait() {}
// Close method
func (m *MockModule) Close() {}
......@@ -8,7 +8,7 @@ import (
"testing"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert"
......@@ -187,3 +187,20 @@ func TestDB(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, value, []byte("b"))
}
func TestMockModule(t *testing.T) {
q := queue.New("channel")
client := q.Client()
memKey := "mempool"
mem := &MockModule{Key: memKey}
mem.SetQueueClient(client)
msg := client.NewMessage(memKey, types.EventTx, &types.Transaction{})
client.Send(msg, true)
resp, err := client.Wait(msg)
assert.Nil(t, err)
reply, ok := resp.GetData().(*types.Reply)
assert.Equal(t, ok, true)
assert.Equal(t, reply.GetIsOk(), false)
assert.Equal(t, reply.GetMsg(), []byte("mock mempool module not handle message 1"))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment