Skip to content

Commit 2d2691a

Browse files
committed
refactor: introduce partial header and partial block for data from DA before execution
1 parent 20204ab commit 2d2691a

File tree

5 files changed

+60
-31
lines changed

5 files changed

+60
-31
lines changed

core/blockchain.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1835,18 +1835,18 @@ func (bc *BlockChain) BuildAndWriteBlock(parentBlock *types.Block, header *types
18351835
return NonStatTy, err
18361836
}
18371837

1838+
header.ParentHash = parentBlock.Hash()
1839+
18381840
tempBlock := types.NewBlockWithHeader(header).WithBody(txs, nil)
18391841
receipts, logs, gasUsed, err := bc.processor.Process(tempBlock, statedb, bc.vmConfig)
18401842
if err != nil {
18411843
return NonStatTy, err
18421844
}
18431845

18441846
header.GasUsed = gasUsed
1845-
header.ParentHash = parentBlock.Hash()
18461847
header.Root = statedb.GetRootHash()
18471848
// Since we're using Clique consensus, we don't have uncles
18481849
header.UncleHash = types.EmptyUncleHash
1849-
// TODO: extraData and difficulty should be set
18501850

18511851
fullBlock := types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))
18521852

rollup/da_syncer/block_queue.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/scroll-tech/go-ethereum/core/types"
87
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/da"
98
)
109

1110
type BlockQueue struct {
1211
batchQueue *BatchQueue
13-
blocks []*types.Block
12+
blocks []*da.PartialBlock
1413
}
1514

1615
func NewBlockQueue(batchQueue *BatchQueue) *BlockQueue {
1716
return &BlockQueue{
1817
batchQueue: batchQueue,
19-
blocks: []*types.Block{},
18+
blocks: make([]*da.PartialBlock, 0),
2019
}
2120
}
2221

23-
func (bq *BlockQueue) NextBlock(ctx context.Context) (*types.Block, error) {
22+
func (bq *BlockQueue) NextBlock(ctx context.Context) (*da.PartialBlock, error) {
2423
for len(bq.blocks) == 0 {
2524
err := bq.getBlocksFromBatch(ctx)
2625
if err != nil {

rollup/da_syncer/da/commitV0.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/binary"
55
"fmt"
66
"io"
7-
"math/big"
87

98
"github.com/scroll-tech/da-codec/encoding"
109
"github.com/scroll-tech/da-codec/encoding/codecv0"
@@ -84,20 +83,13 @@ func (c *CommitBatchDAV0) BatchIndex() uint64 {
8483
return c.batchIndex
8584
}
8685

87-
func (c *CommitBatchDAV0) Blocks() ([]*types.Block, error) {
88-
var blocks []*types.Block
86+
func (c *CommitBatchDAV0) Blocks() ([]*PartialBlock, error) {
87+
var blocks []*PartialBlock
8988
l1TxPointer := 0
9089

9190
curL1TxIndex := c.parentTotalL1MessagePopped
9291
for _, chunk := range c.chunks {
9392
for blockId, daBlock := range chunk.Blocks {
94-
// create header
95-
header := types.Header{
96-
Number: big.NewInt(0).SetUint64(daBlock.BlockNumber),
97-
Time: daBlock.Timestamp,
98-
BaseFee: daBlock.BaseFee,
99-
GasLimit: daBlock.GasLimit,
100-
}
10193
// create txs
10294
// var txs types.Transactions
10395
txs := make(types.Transactions, 0, daBlock.NumTransactions)
@@ -110,7 +102,17 @@ func (c *CommitBatchDAV0) Blocks() ([]*types.Block, error) {
110102
curL1TxIndex += uint64(daBlock.NumL1Messages)
111103
// insert l2 txs
112104
txs = append(txs, chunk.Transactions[blockId]...)
113-
block := types.NewBlockWithHeader(&header).WithBody(txs, make([]*types.Header, 0))
105+
106+
block := NewPartialBlock(
107+
&PartialHeader{
108+
Number: daBlock.BlockNumber,
109+
Time: daBlock.Timestamp,
110+
BaseFee: daBlock.BaseFee,
111+
GasLimit: daBlock.GasLimit,
112+
//TODO: Difficulty: new(big.Int).SetUint64(10),
113+
//TODO: ExtraData: []byte{1, 2, 3, 4, 5, 6, 7, 8},
114+
},
115+
txs)
114116
blocks = append(blocks, block)
115117
}
116118
}

rollup/da_syncer/da/da.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package da
22

33
import (
4+
"math/big"
5+
46
"github.com/scroll-tech/go-ethereum/core/types"
57
)
68

@@ -29,7 +31,39 @@ type Entry interface {
2931

3032
type EntryWithBlocks interface {
3133
Entry
32-
Blocks() ([]*types.Block, error)
34+
Blocks() ([]*PartialBlock, error)
3335
}
3436

3537
type Entries []Entry
38+
39+
type PartialHeader struct {
40+
Number uint64
41+
Time uint64
42+
BaseFee *big.Int
43+
GasLimit uint64
44+
Difficulty *big.Int
45+
ExtraData []byte
46+
}
47+
48+
func (h *PartialHeader) ToHeader() *types.Header {
49+
return &types.Header{
50+
Number: big.NewInt(0).SetUint64(h.Number),
51+
Time: h.Time,
52+
BaseFee: h.BaseFee,
53+
GasLimit: h.GasLimit,
54+
Difficulty: h.Difficulty,
55+
Extra: h.ExtraData,
56+
}
57+
}
58+
59+
type PartialBlock struct {
60+
PartialHeader *PartialHeader
61+
Transactions types.Transactions
62+
}
63+
64+
func NewPartialBlock(partialHeader *PartialHeader, txs types.Transactions) *PartialBlock {
65+
return &PartialBlock{
66+
PartialHeader: partialHeader,
67+
Transactions: txs,
68+
}
69+
}

rollup/da_syncer/da_syncer.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package da_syncer
22

33
import (
44
"fmt"
5-
"math/big"
65

7-
"github.com/scroll-tech/go-ethereum/common"
86
"github.com/scroll-tech/go-ethereum/core"
9-
"github.com/scroll-tech/go-ethereum/core/types"
107
"github.com/scroll-tech/go-ethereum/log"
8+
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/da"
119
)
1210

1311
type DASyncer struct {
@@ -20,23 +18,19 @@ func NewDASyncer(blockchain *core.BlockChain) *DASyncer {
2018
}
2119
}
2220

23-
func (s *DASyncer) SyncOneBlock(block *types.Block) error {
21+
func (s *DASyncer) SyncOneBlock(block *da.PartialBlock) error {
2422
parentBlock := s.blockchain.CurrentBlock()
25-
if big.NewInt(0).Add(parentBlock.Number(), common.Big1).Cmp(block.Number()) != 0 {
26-
return fmt.Errorf("not consecutive block, number: %d", block.Number())
23+
if parentBlock.NumberU64()+1 != block.PartialHeader.Number {
24+
return fmt.Errorf("not consecutive block, number: %d, chain height: %d", block.PartialHeader.Number, parentBlock.NumberU64())
2725
}
2826

29-
header := block.Header()
30-
header.Difficulty = common.Big1
31-
header.BaseFee = nil // TODO: after Curie we need to fill this correctly
32-
header.ParentHash = parentBlock.Hash()
33-
34-
if _, err := s.blockchain.BuildAndWriteBlock(parentBlock, header, block.Transactions()); err != nil {
35-
return fmt.Errorf("failed building and writing block, number: %d, error: %v", block.Number(), err)
27+
if _, err := s.blockchain.BuildAndWriteBlock(parentBlock, block.PartialHeader.ToHeader(), block.Transactions); err != nil {
28+
return fmt.Errorf("failed building and writing block, number: %d, error: %v", block.PartialHeader.Number, err)
3629
}
3730

3831
if s.blockchain.CurrentBlock().Header().Number.Uint64()%100 == 0 {
3932
log.Info("inserted block", "blockhain height", s.blockchain.CurrentBlock().Header().Number, "block hash", s.blockchain.CurrentBlock().Header().Hash(), "root", s.blockchain.CurrentBlock().Header().Root)
4033
}
34+
4135
return nil
4236
}

0 commit comments

Comments
 (0)