Skip to content

Commit

Permalink
Change header encoding mechanism (#300)
Browse files Browse the repository at this point in the history
This changes the way that we detect pre-gingerbread headers when encoding.

Closes celo-org/celo-blockchain-planning#588

Previously we would interpret a `gasLimit` of zero to indicate pre-gingerbread headers, since prior to the gingerbread hardfork celo blocks lacked the `gasLimit` field.

This however caused lots of problems with the go tests (existing and ones that continue to be added) that routinely use blocks where gasLimit has not been set.

So now we detect pre-gingerbread blocks when decoding and set a flag on the struct that is used to select the encoding.
New blocks are assumed to be post gingerbread, which is ok because it's not possible to run op-geth without gingerbread set, because we haven't ported across the pre-gingerbread state transition logic.

I was able to revert some test modifications because of this but this didn't fix the tests that broke during the celo11 rebase (skipped in [this commit](d981efb)), it seems like those problems were caused by something else.
  • Loading branch information
piersy authored Dec 20, 2024
1 parent ef4bd10 commit 57eef86
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 52 deletions.
4 changes: 2 additions & 2 deletions beacon/blsync/block_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
Body: deneb.BeaconBlockBody{
ExecutionPayload: deneb.ExecutionPayload{
BlockNumber: 456,
BlockHash: zrntcommon.Hash32(common.HexToHash("eb8dbaf115360cbada7b6e5420bb8a340b0c7976caf0abd63a2131baf98ce3ac")),
BlockHash: zrntcommon.Hash32(common.HexToHash("905ac721c4058d9ed40b27b6b9c1bdd10d4333e4f3d9769100bf9dfb80e5d1f6")),
},
},
})
Expand All @@ -45,7 +45,7 @@ var (
Body: deneb.BeaconBlockBody{
ExecutionPayload: deneb.ExecutionPayload{
BlockNumber: 457,
BlockHash: zrntcommon.Hash32(common.HexToHash("e77f66ebcf4cc17211cd725ca563646d5d48f39b72de1cc233b8f0b4084fa641")),
BlockHash: zrntcommon.Hash32(common.HexToHash("011703f39c664efc1c6cf5f49ca09b595581eec572d4dfddd3d6179a9e63e655")),
},
},
})
Expand Down
15 changes: 5 additions & 10 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,11 @@ func (g *Genesis) toBlockWithRoot(root common.Hash) *types.Block {
Coinbase: g.Coinbase,
Root: root,
}
// Don't set defaults for gas limit and difficulty for migrated celo chains.
// I.E. when Cel2Time is set & non zero. Since migrated celo chains can
// have gas limit and difficulty unset in the genesis.
if g.Config.Cel2Time == nil || g.Config.IsCel2(0) {
if g.GasLimit == 0 {
head.GasLimit = params.GenesisGasLimit
}
if g.Difficulty == nil && g.Mixhash == (common.Hash{}) {
head.Difficulty = params.GenesisDifficulty
}
if g.GasLimit == 0 {
head.GasLimit = params.GenesisGasLimit
}
if g.Difficulty == nil && g.Mixhash == (common.Hash{}) {
head.Difficulty = params.GenesisDifficulty
} else if g.Difficulty == nil {
// In the case of migrated chains we ensure a zero rather than nil difficulty.
head.Difficulty = new(big.Int)
Expand Down
3 changes: 3 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type Header struct {

// RequestsHash was added by EIP-7685 and is ignored in legacy headers.
RequestsHash *common.Hash `json:"requestsRoot" rlp:"optional"`

// preGingerbread determines whether this is a pre-gingerbread header, which determines how this block will be encoded.
preGingerbread bool
}

// field type overrides for gencodec
Expand Down
11 changes: 2 additions & 9 deletions core/types/celo_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
h.Time = decodedHeader.Time
h.Extra = decodedHeader.Extra
h.Difficulty = new(big.Int)
h.preGingerbread = true
} else {
// After gingerbread
decodedHeader := AfterGingerbreadHeader{}
Expand Down Expand Up @@ -150,14 +151,6 @@ func isPreGingerbreadHeader(buf []byte) (bool, error) {
return contentSize == common.AddressLength, nil
}

// Returns if the header is a gingerbread header by looking at the gas limit.
func (h *Header) IsPreGingerbread() bool {
// We check for a pre gingerbread header by looking for (GasLimit == 0)
// here. We don't use Difficulty because we ensure that headers have a zero
// difficulty, even if it's not set in the rlp encoded form (we do this
// because the go ethereum codebase assumed non nil difficulties) and post
// gingerbread difficulty is hardcoded to zero. Also testing for base fee
// is not reliable because some older eth blocks had no base fee and they
// are used in some tests.
return h.GasLimit == 0
return h.preGingerbread
}
23 changes: 12 additions & 11 deletions core/types/celo_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ func ToMockOldBeforeGingerbreadHeader(h *BeforeGingerbreadHeader) *mockOldBefore

func BeforeGingerbreadHeaderToHeader(h *BeforeGingerbreadHeader) *Header {
return &Header{
ParentHash: h.ParentHash,
Coinbase: h.Coinbase,
Root: h.Root,
TxHash: h.TxHash,
ReceiptHash: h.ReceiptHash,
Bloom: h.Bloom,
Number: h.Number,
GasUsed: h.GasUsed,
Time: h.Time,
Extra: h.Extra,
Difficulty: new(big.Int),
ParentHash: h.ParentHash,
Coinbase: h.Coinbase,
Root: h.Root,
TxHash: h.TxHash,
ReceiptHash: h.ReceiptHash,
Bloom: h.Bloom,
Number: h.Number,
GasUsed: h.GasUsed,
Time: h.Time,
Extra: h.Extra,
Difficulty: new(big.Int),
preGingerbread: true,
}
}

Expand Down
24 changes: 12 additions & 12 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,7 @@ func TestRPCMarshalBlock(t *testing.T) {
}
txs = append(txs, tx)
}
block := types.NewBlock(&types.Header{Number: big.NewInt(100), GasLimit: 10}, &types.Body{Transactions: txs}, nil, blocktest.NewHasher())
block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, &types.Body{Transactions: txs}, nil, blocktest.NewHasher())

var testSuite = []struct {
inclTx bool
Expand All @@ -2828,9 +2828,9 @@ func TestRPCMarshalBlock(t *testing.T) {
want: `{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"hash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x0000000000000000000000000000000000000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -2853,9 +2853,9 @@ func TestRPCMarshalBlock(t *testing.T) {
want: `{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"hash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x0000000000000000000000000000000000000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -2884,9 +2884,9 @@ func TestRPCMarshalBlock(t *testing.T) {
want: `{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"hash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x0000000000000000000000000000000000000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -2900,7 +2900,7 @@ func TestRPCMarshalBlock(t *testing.T) {
"timestamp": "0x0",
"transactions": [
{
"blockHash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"blockHash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"blockNumber": "0x64",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
Expand All @@ -2920,7 +2920,7 @@ func TestRPCMarshalBlock(t *testing.T) {
"yParity": "0x0"
},
{
"blockHash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"blockHash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"blockNumber": "0x64",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
Expand All @@ -2938,7 +2938,7 @@ func TestRPCMarshalBlock(t *testing.T) {
"s": "0x0"
},
{
"blockHash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"blockHash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"blockNumber": "0x64",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
Expand All @@ -2958,7 +2958,7 @@ func TestRPCMarshalBlock(t *testing.T) {
"yParity": "0x0"
},
{
"blockHash": "0x7117849518bf9484dae1b1e0aeb9f37b4fa81550061e2057614bec1a022e4596",
"blockHash": "0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee",
"blockNumber": "0x64",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
Expand Down Expand Up @@ -3032,7 +3032,7 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
Address: common.Address{0x12, 0x34},
Amount: 10,
}
pending = types.NewBlock(&types.Header{Number: big.NewInt(11), Time: 42, GasLimit: 10}, &types.Body{Transactions: types.Transactions{tx}, Withdrawals: types.Withdrawals{withdrawal}}, nil, blocktest.NewHasher())
pending = types.NewBlock(&types.Header{Number: big.NewInt(11), Time: 42}, &types.Body{Transactions: types.Transactions{tx}, Withdrawals: types.Withdrawals{withdrawal}}, nil, blocktest.NewHasher())
)
backend := newTestBackend(t, genBlocks, genesis, ethash.NewFaker(), func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
],
"transactionsRoot": "0xb0893d21a4a44dc26a962a6e91abae66df87fb61ac9c60e936aee89c76331445",
"uncles": []
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -17,7 +17,7 @@
"timestamp": "0x2a",
"transactions": [
{
"blockHash": "0x0a6761c1616a9ef71352fa9b7169f0ca379397c73516f32f9cc5f414663b3412",
"blockHash": "0x6cebd9f966ea686f44b981685e3f0eacea28591a7a86d7fbbe521a86e9f81165",
"blockNumber": "0xb",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
Expand Down Expand Up @@ -46,4 +46,4 @@
}
],
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -29,4 +29,4 @@
}
],
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xa",
"gasLimit": "0x0",
"gasUsed": "0x0",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -16,4 +16,4 @@
"timestamp": "0x2a",
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
}
}

0 comments on commit 57eef86

Please sign in to comment.