Skip to content

Commit b0aca7d

Browse files
committed
[[FEAT]] add consensus commiter plugin rollup(33cn/chain33#1268)
1 parent 3eb1390 commit b0aca7d

File tree

5 files changed

+171
-5
lines changed

5 files changed

+171
-5
lines changed

plugin/consensus/init/init.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package init
22

33
import (
4-
_ "github.com/33cn/plugin/plugin/consensus/dpos" //auto gen
5-
_ "github.com/33cn/plugin/plugin/consensus/para" //auto gen
6-
_ "github.com/33cn/plugin/plugin/consensus/pbft" //auto gen
7-
_ "github.com/33cn/plugin/plugin/consensus/qbft" //auto gen
8-
_ "github.com/33cn/plugin/plugin/consensus/raft" //auto gen
4+
_ "github.com/33cn/plugin/plugin/consensus/dpos" //auto gen
5+
_ "github.com/33cn/plugin/plugin/consensus/para" //auto gen
6+
_ "github.com/33cn/plugin/plugin/consensus/pbft" //auto gen
7+
_ "github.com/33cn/plugin/plugin/consensus/qbft" //auto gen
8+
_ "github.com/33cn/plugin/plugin/consensus/raft" //auto gen
9+
_ "github.com/33cn/plugin/plugin/consensus/rollup"
910
_ "github.com/33cn/plugin/plugin/consensus/tendermint" //auto gen
1011
_ "github.com/33cn/plugin/plugin/consensus/ticket" //auto gen
1112
)

plugin/consensus/rollup/optimistic.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package rollup
2+
3+
import (
4+
"github.com/33cn/chain33/types"
5+
rolluptypes "github.com/33cn/plugin/plugin/dapp/rollup/types"
6+
)
7+
8+
type optimistic struct {
9+
}
10+
11+
func (op *optimistic) GetCommitBatch(blocks []*types.Block) *rolluptypes.CommitBatch {
12+
13+
batch := &rolluptypes.CommitBatch{}
14+
15+
return batch
16+
}

plugin/consensus/rollup/rollup.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package rollup
2+
3+
import (
4+
"context"
5+
"sync"
6+
"time"
7+
8+
"github.com/33cn/chain33/common/log"
9+
"github.com/33cn/chain33/system/consensus"
10+
"github.com/33cn/chain33/types"
11+
"github.com/golang/groupcache/lru"
12+
)
13+
14+
const (
15+
minCommitTxCount = 128
16+
)
17+
18+
var (
19+
rlog = log.New("module", "rollup")
20+
)
21+
22+
func init() {
23+
consensus.RegCommitter("rollup", &RollUp{})
24+
}
25+
26+
// RollUp roll up
27+
type RollUp struct {
28+
nextBuildHeight int64
29+
nextBuildRound int64
30+
cfg Config
31+
op *optimistic
32+
33+
initDone chan struct{}
34+
nextCommitRound int32
35+
36+
currCommitter string
37+
38+
commitCache *lru.Cache
39+
batchCache sync.Map
40+
ctx context.Context
41+
base *consensus.BaseClient
42+
chainCfg *types.Chain33Config
43+
}
44+
45+
// Init init
46+
func (r *RollUp) Init(base *consensus.BaseClient, chainCfg *types.Chain33Config, subCfg []byte) {
47+
48+
if !chainCfg.IsPara() {
49+
return
50+
}
51+
52+
r.chainCfg = chainCfg
53+
r.ctx = base.Context
54+
r.op = &optimistic{}
55+
r.initDone = make(chan struct{})
56+
57+
go r.fetchRollupState()
58+
go r.startRollupRoutine()
59+
}
60+
61+
func (r *RollUp) fetchRollupState() {
62+
63+
r.initDone <- struct{}{}
64+
}
65+
66+
func (r *RollUp) startRollupRoutine() {
67+
68+
<-r.initDone
69+
70+
go r.handleBuildBatch()
71+
go r.handleCommitBatch()
72+
}
73+
74+
func (r *RollUp) handleBuildBatch() {
75+
76+
ticker := time.NewTicker(time.Second * 10)
77+
var blocks []*types.Block
78+
for {
79+
80+
select {
81+
case <-ticker.C:
82+
blocks = r.getNextBatchBlocks(r.nextBuildHeight)
83+
case <-r.ctx.Done():
84+
ticker.Stop()
85+
return
86+
}
87+
// 区块内未达到最低批量数量, 需要继续等待
88+
if blocks == nil {
89+
rlog.Debug("handleBuildBatch", "height", r.nextBuildHeight,
90+
"round", r.nextBuildRound, "msg", "wait more block")
91+
continue
92+
}
93+
94+
batch := r.op.GetCommitBatch(blocks)
95+
r.batchCache.Store(batch.CommitRound, batch)
96+
r.nextBuildRound++
97+
r.nextBuildHeight += int64(len(blocks))
98+
}
99+
}
100+
101+
// 提交共识
102+
func (r *RollUp) handleCommitBatch() {
103+
104+
}
105+
106+
// 同步链上已提交的最新 blockHeight 和 commitRound, 维护batch缓存
107+
func (r *RollUp) syncRollupState() {
108+
109+
}

plugin/consensus/rollup/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package rollup
2+
3+
// Config rollup 配置
4+
type Config struct {
5+
}

plugin/consensus/rollup/util.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package rollup
2+
3+
import (
4+
"github.com/33cn/chain33/types"
5+
)
6+
7+
func (r *RollUp) getNextBatchBlocks(startHeight int64) []*types.Block {
8+
9+
req := &types.ReqBlocks{
10+
Start: startHeight,
11+
End: startHeight + minCommitTxCount,
12+
}
13+
14+
details, err := r.base.GetAPI().GetBlocks(req)
15+
for err != nil {
16+
rlog.Error("getNextBatchBlocks", "req", req.String(), "err", err)
17+
return nil
18+
}
19+
20+
blocks := make([]*types.Block, 0, minCommitTxCount)
21+
txCount := 0
22+
for _, detail := range details.GetItems() {
23+
blocks = append(blocks, detail.GetBlock())
24+
txCount += len(detail.GetBlock().GetTxs())
25+
if txCount >= minCommitTxCount {
26+
break
27+
}
28+
}
29+
30+
if txCount < minCommitTxCount {
31+
return nil
32+
}
33+
34+
return blocks
35+
}

0 commit comments

Comments
 (0)