Skip to content

Commit

Permalink
[[FEAT]] add state committer interface in consensus module(#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
bysomeone committed Dec 30, 2022
1 parent ff7ba75 commit 8d907b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@ func New(cfg *types.Chain33Config) queue.Module {
}
obj := con(mcfg, subcfg)
consensus.QueryData.SetThis(mcfg.Name, reflect.ValueOf(obj))

// init state committer
c := consensus.LoadCommiter(mcfg.Committer)
c.Init(sub[mcfg.Committer])
m, ok := obj.(consensus.Miner)
if !ok {
panic("New consensus: invalid Miner name= " + mcfg.Name)
}
m.SetCommitter(c)
return obj
}
8 changes: 8 additions & 0 deletions system/consensus/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Miner interface {
CheckBlock(parent *types.Block, current *types.BlockDetail) error
ProcEvent(msg *queue.Message) bool
CmpBestBlock(newBlock *types.Block, cmpBlock *types.Block) bool
SetCommitter(c Committer)
}

//BaseClient ...
Expand All @@ -55,6 +56,7 @@ type BaseClient struct {
currentBlock *types.Block
mulock sync.Mutex
child Miner
committer Committer
minerstartCB func()
isCaughtUp int32
}
Expand All @@ -81,6 +83,11 @@ func (bc *BaseClient) SetChild(c Miner) {
bc.child = c
}

// SetCommitter set committer
func (bc *BaseClient) SetCommitter(c Committer) {
bc.committer = c
}

//GetAPI 获取api
func (bc *BaseClient) GetAPI() client.QueueProtocolAPI {
return bc.api
Expand Down Expand Up @@ -246,6 +253,7 @@ func (bc *BaseClient) EventLoop() {
block := msg.GetData().(*types.BlockDetail).Block
bc.SetCurrentBlock(block)
bc.child.AddBlock(block)
bc.committer.AddBlock(block)
} else if msg.Ty == types.EventCheckBlock {
block := msg.GetData().(*types.BlockDetail)
err := bc.CheckBlock(block)
Expand Down
30 changes: 30 additions & 0 deletions system/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,33 @@ func Load(name string) (create Create, err error) {
}
return nil, types.ErrNotFound
}

// Committer state commiter
type Committer interface {
Init(subCfg []byte)
AddBlock(b *types.Block)
}

var committers = make(map[string]Committer)

// RegCommitter register committer
func RegCommitter(name string, c Committer) {

if c == nil {
panic("RegCommitter: committer is nil")
}
if _, dup := committers[name]; dup {
panic("RegCommitter: duplicate committer " + name)
}
committers[name] = c
}

// LoadCommiter load
func LoadCommiter(name string) Committer {

c, ok := committers[name]
if !ok {
panic("LoadCommiter: unknown commiiter name=" + name)
}
return c
}
2 changes: 2 additions & 0 deletions types/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ type Mempool struct {
type Consensus struct {
// 共识名称 :solo, ticket, raft, tendermint, para
Name string `json:"name,omitempty"`
// state commiter, rollup
Committer string `json:"committer,omitempty"`
// 创世区块时间(UTC时间)
GenesisBlockTime int64 `json:"genesisBlockTime,omitempty"`
// 是否开启挖矿,开启挖矿才能创建区块
Expand Down

0 comments on commit 8d907b2

Please sign in to comment.