Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package common
import (
"bytes"
"database/sql/driver"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -65,6 +66,13 @@ func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
// If b is larger than len(h), b will be cropped from the left.
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }

// Uint64ToHash sets the lowest 8 bytes of hash to u in big endian format.
func Uint64ToHash(u uint64) Hash {
var h Hash
binary.BigEndian.PutUint64(h[24:], u)
return h
}

// Less compares two hashes.
func (h Hash) Less(other Hash) bool {
return bytes.Compare(h[:], other[:]) < 0
Expand Down
47 changes: 47 additions & 0 deletions consensus/misc/eip4788.go
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) {
Copy link
Member

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.

Copy link
Member Author

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

// 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
}
63 changes: 63 additions & 0 deletions consensus/misc/eip4788_test.go
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 json file, so we can share it cross-client?

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
}
}
}
26 changes: 26 additions & 0 deletions consensus/misc/testdata/eip4788_beaconroot.json
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"
}
]
9 changes: 9 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be in Prepare in the consensus interface?

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down
7 changes: 7 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

  • only accept BeaconRoot after the fork time,
  • require the BeaconRoot after the fork time,

As for RLP, similarly, we need to be careful, and strict.

Copy link
Contributor

Choose a reason for hiding this comment

The 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...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its specified yet

Copy link
Member

Choose a reason for hiding this comment

The 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:

ethereum/EIPs#7297

}

// field type overrides for gencodec
Expand Down Expand Up @@ -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.
Expand Down
Loading