Skip to content

Commit 8d907b2

Browse files
committed
[[FEAT]] add state committer interface in consensus module(#1268)
1 parent ff7ba75 commit 8d907b2

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

consensus/consensus.go

+9
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,14 @@ func New(cfg *types.Chain33Config) queue.Module {
2727
}
2828
obj := con(mcfg, subcfg)
2929
consensus.QueryData.SetThis(mcfg.Name, reflect.ValueOf(obj))
30+
31+
// init state committer
32+
c := consensus.LoadCommiter(mcfg.Committer)
33+
c.Init(sub[mcfg.Committer])
34+
m, ok := obj.(consensus.Miner)
35+
if !ok {
36+
panic("New consensus: invalid Miner name= " + mcfg.Name)
37+
}
38+
m.SetCommitter(c)
3039
return obj
3140
}

system/consensus/base.go

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Miner interface {
4242
CheckBlock(parent *types.Block, current *types.BlockDetail) error
4343
ProcEvent(msg *queue.Message) bool
4444
CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool
45+
SetCommitter(c Committer)
4546
}
4647

4748
//BaseClient ...
@@ -55,6 +56,7 @@ type BaseClient struct {
5556
currentBlock *types.Block
5657
mulock sync.Mutex
5758
child Miner
59+
committer Committer
5860
minerstartCB func()
5961
isCaughtUp int32
6062
}
@@ -81,6 +83,11 @@ func (bc *BaseClient) SetChild(c Miner) {
8183
bc.child = c
8284
}
8385

86+
// SetCommitter set committer
87+
func (bc *BaseClient) SetCommitter(c Committer) {
88+
bc.committer = c
89+
}
90+
8491
//GetAPI 获取api
8592
func (bc *BaseClient) GetAPI() client.QueueProtocolAPI {
8693
return bc.api
@@ -246,6 +253,7 @@ func (bc *BaseClient) EventLoop() {
246253
block := msg.GetData().(*types.BlockDetail).Block
247254
bc.SetCurrentBlock(block)
248255
bc.child.AddBlock(block)
256+
bc.committer.AddBlock(block)
249257
} else if msg.Ty == types.EventCheckBlock {
250258
block := msg.GetData().(*types.BlockDetail)
251259
err := bc.CheckBlock(block)

system/consensus/consensus.go

+30
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,33 @@ func Load(name string) (create Create, err error) {
3636
}
3737
return nil, types.ErrNotFound
3838
}
39+
40+
// Committer state commiter
41+
type Committer interface {
42+
Init(subCfg []byte)
43+
AddBlock(b *types.Block)
44+
}
45+
46+
var committers = make(map[string]Committer)
47+
48+
// RegCommitter register committer
49+
func RegCommitter(name string, c Committer) {
50+
51+
if c == nil {
52+
panic("RegCommitter: committer is nil")
53+
}
54+
if _, dup := committers[name]; dup {
55+
panic("RegCommitter: duplicate committer " + name)
56+
}
57+
committers[name] = c
58+
}
59+
60+
// LoadCommiter load
61+
func LoadCommiter(name string) Committer {
62+
63+
c, ok := committers[name]
64+
if !ok {
65+
panic("LoadCommiter: unknown commiiter name=" + name)
66+
}
67+
return c
68+
}

types/cfg.go

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ type Mempool struct {
124124
type Consensus struct {
125125
// 共识名称 :solo, ticket, raft, tendermint, para
126126
Name string `json:"name,omitempty"`
127+
// state commiter, rollup
128+
Committer string `json:"committer,omitempty"`
127129
// 创世区块时间(UTC时间)
128130
GenesisBlockTime int64 `json:"genesisBlockTime,omitempty"`
129131
// 是否开启挖矿,开启挖矿才能创建区块

0 commit comments

Comments
 (0)