-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: implement EIP-4788 BeaconRoot precompile #27289
Changes from all commits
e851002
6f11378
d6d0d6d
10390cf
3d4ecc3
f115bf6
2bd0102
bd221a9
4a08450
564bfee
8cf4db1
9a11599
8a030c9
8057030
7ac6529
2ce4c01
b544bd6
ba8ea61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package misc | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
// ApplyBeaconRoot adds the beacon root from the header to the state. | ||
func ApplyBeaconRoot(header *types.Header, state *state.StateDB) { | ||
// If EIP-4788 is enabled, we need to store the block root | ||
timeKey, time, rootKey, root := calcBeaconRootIndices(header) | ||
state.SetState(params.BeaconRootsStorageAddress, timeKey, time) | ||
state.SetState(params.BeaconRootsStorageAddress, rootKey, root) | ||
// We also need to ensure that the BeaconRoot address has nonzero nonce. | ||
if state.GetNonce(params.BeaconRootsStorageAddress) == 0 { | ||
state.SetNonce(params.BeaconRootsStorageAddress, 1) | ||
} | ||
} | ||
|
||
func calcBeaconRootIndices(header *types.Header) (timeKey, time, rootKey, root common.Hash) { | ||
// timeKey -> header.Time | ||
timeIndex := header.Time % params.HistoricalRootsModulus | ||
timeKey = common.Uint64ToHash(timeIndex) | ||
time = common.Uint64ToHash(header.Time) | ||
// rootKey -> header.BeaconRoot | ||
rootKey = common.Uint64ToHash(timeIndex + params.HistoricalRootsModulus) | ||
root = *header.BeaconRoot | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package misc | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
type tcInput struct { | ||
HeaderTime uint64 | ||
HeaderBeaconRoot common.Hash | ||
|
||
TimeKey common.Hash | ||
Time common.Hash | ||
RootKey common.Hash | ||
Root common.Hash | ||
} | ||
|
||
func TestCalcBeaconRootIndices(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good start. Mind if I move the actual test vectors of it out to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure would be nice to have some more test cases |
||
data, err := os.ReadFile("./testdata/eip4788_beaconroot.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var tests []tcInput | ||
if err := json.Unmarshal(data, &tests); err != nil { | ||
t.Fatal(err) | ||
} | ||
for i, tc := range tests { | ||
header := types.Header{Time: tc.HeaderTime, BeaconRoot: &tc.HeaderBeaconRoot} | ||
haveTimeKey, haveTime, haveRootKey, haveRoot := calcBeaconRootIndices(&header) | ||
if haveTimeKey != tc.TimeKey { | ||
t.Errorf("test %d: invalid time key: \nhave %v\nwant %v", i, haveTimeKey, tc.TimeKey) | ||
} | ||
if haveTime != tc.Time { | ||
t.Errorf("test %d: invalid time: \nhave %v\nwant %v", i, haveTime, tc.Time) | ||
} | ||
if haveRootKey != tc.RootKey { | ||
t.Errorf("test %d: invalid root key: \nhave %v\nwant %v", i, haveRootKey, tc.RootKey) | ||
} | ||
if haveRoot != tc.Root { | ||
t.Errorf("test %d: invalid root: \nhave %v\nwant %v", i, haveRoot, tc.Root) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[ | ||
{ | ||
"HeaderTime": 0, | ||
"HeaderBeaconRoot": "0x0100000000000000000000000000000000000000000000000000000000000000", | ||
"TimeKey": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"Time": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"RootKey": "0x0000000000000000000000000000000000000000000000000000000000018000", | ||
"Root": "0x0100000000000000000000000000000000000000000000000000000000000000" | ||
}, | ||
{ | ||
"HeaderTime": 120265298769267, | ||
"HeaderBeaconRoot": "0xfffefc0000000000000000000000000000000000000000000000000000000000", | ||
"TimeKey": "0x000000000000000000000000000000000000000000000000000000000000f573", | ||
"Time": "0x00000000000000000000000000000000000000000000000000006d6172697573", | ||
"RootKey": "0x0000000000000000000000000000000000000000000000000000000000027573", | ||
"Root": "0xfffefc0000000000000000000000000000000000000000000000000000000000" | ||
}, | ||
{ | ||
"HeaderTime": 18446744073709551615, | ||
"HeaderBeaconRoot": "0xfffefcffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
"TimeKey": "0x000000000000000000000000000000000000000000000000000000000000ffff", | ||
"Time": "0x000000000000000000000000000000000000000000000000ffffffffffffffff", | ||
"RootKey": "0x0000000000000000000000000000000000000000000000000000000000027fff", | ||
"Root": "0xfffefcffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,15 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg | |
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { | ||
misc.ApplyDAOHardFork(statedb) | ||
} | ||
if p.config.IsCancun(blockNumber, block.Time()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify, @lightclient, I don't understand what you are suggesting |
||
if header.BeaconRoot == nil { | ||
return nil, nil, 0, errors.New("expected beacon root post-cancun") | ||
} | ||
misc.ApplyBeaconRoot(header, statedb) | ||
} else if header.BeaconRoot != nil { | ||
return nil, nil, 0, errors.New("beacon root set pre-cancun") | ||
} | ||
|
||
var ( | ||
context = NewEVMBlockContext(header, p.bc, nil) | ||
vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,9 @@ type Header struct { | |
|
||
// ExcessBlobGas was added by EIP-4844 and is ignored in legacy headers. | ||
ExcessBlobGas *uint64 `json:"excessBlobGas" rlp:"optional"` | ||
|
||
// BeaconRoot was added by EIP-4788 and is ignored in legacy headers. | ||
BeaconRoot *common.Hash `json:"beaconRoot" rlp:"optional"` | ||
Comment on lines
+93
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding it like this delegates the responsibility of "when to start storing data for EIP-4788" entirely to the CL layer. When/of they start including a beaconroot, we start storing it into the state. IMO we should
As for RLP, similarly, we need to be careful, and strict. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the order between BeaconRoot and DataGasUsed specified? Both are activated in the same fork... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think its specified yet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be at the end of all the 4844 stuff; I was trying to specify this abstractly to avoid thrash from other EIPs but I can see how this is unclear so I made it explicit: |
||
} | ||
|
||
// field type overrides for gencodec | ||
|
@@ -378,6 +381,10 @@ func (b *Block) BlobGasUsed() *uint64 { | |
return blobGasUsed | ||
} | ||
|
||
func (b *Block) BeaconRoot() *common.Hash { | ||
return b.header.BeaconRoot | ||
} | ||
|
||
func (b *Block) Header() *Header { return CopyHeader(b.header) } | ||
|
||
// Body returns the non-header content of the block. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is kinda superfluous. It's just doing a bunch of one-line conversions. I think it is actually more comprehensible all in the same function, otherwise takes a bit of time to realize almost not computation is happening here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Martin proposed spinning this out, so it can be tested easier, I'll add some test cases, so it actually makes sense to move this