Skip to content

Commit

Permalink
[EIP-4399] Remove newly added Header.Random (reuse mixHash instead) (#…
Browse files Browse the repository at this point in the history
…3069)

* [EIP-4399] Remove newly added Header.Random (reuse mixHash instead)

* add a TODO

* Update mock payload hashes
  • Loading branch information
yperbasis authored Dec 1, 2021
1 parent 3449eed commit 094ab5e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 83 deletions.
8 changes: 5 additions & 3 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
)
Expand All @@ -46,9 +47,10 @@ func NewEVMBlockContext(header *types.Header, getHeader func(hash common.Hash, n

difficulty := new(big.Int)

if header.Eip3675 {
// Turn DIFFICULTY into RANDOM when the merge is done. spec: https://ethereum-magicians.org/t/eip-4399-supplant-difficulty-opcode-with-random/7368
difficulty.SetBytes(header.Random[:])
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// EIP-4399. We use SerenityDifficulty (i.e. 0) as a telltale of Proof-of-Stake blocks.
// TODO: Turn DIFFICULTY into RANDOM when the Merge is done.
difficulty.SetBytes(header.MixDigest[:])
} else {
difficulty.Set(header.Difficulty)
}
Expand Down
35 changes: 0 additions & 35 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ type Header struct {
MixDigest common.Hash `json:"mixHash"`
Nonce BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas"`
Random common.Hash `json:"random"`
Eip1559 bool // to avoid relying on BaseFee != nil for that
Eip3675 bool // to avoid relying on Random != nil for that
Seal []rlp.RawValue // AuRa POA network field
WithSeal bool // to avoid relying on Seal != nil for that
}
Expand Down Expand Up @@ -177,10 +175,6 @@ func (h Header) EncodingSize() int {
encodingSize += baseFeeLen
}

if h.Eip3675 {
encodingSize += common.HashLength + 1
}

return encodingSize
}

Expand Down Expand Up @@ -263,10 +257,6 @@ func (h Header) EncodeRLP(w io.Writer) error {
encodingSize += baseFeeLen
}

if h.Eip3675 {
encodingSize += common.HashLength + 1
}

var b [33]byte
// Prefix
if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil {
Expand Down Expand Up @@ -434,14 +424,6 @@ func (h Header) EncodeRLP(w io.Writer) error {
}
}

if h.Eip3675 {
if _, err := w.Write([]byte{128 + common.HashLength}); err != nil {
return err
}
if _, err := w.Write(h.Random.Bytes()); err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -570,23 +552,6 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
}
h.Eip1559 = true
h.BaseFee = new(big.Int).SetBytes(b)

if b, err = s.Bytes(); err != nil {
if errors.Is(err, rlp.EOL) {
h.Random = common.Hash{}
h.Eip3675 = false
if err := s.ListEnd(); err != nil {
return fmt.Errorf("close header struct (no random): %w", err)
}
return nil
}
return fmt.Errorf("read Random: %w", err)
}
if len(b) != common.HashLength {
return fmt.Errorf("wrong size for Random: %d", len(b))
}
h.Eip3675 = true
h.Random = common.BytesToHash(b)
}
if err := s.ListEnd(); err != nil {
return fmt.Errorf("close header struct: %w", err)
Expand Down
36 changes: 0 additions & 36 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,42 +214,6 @@ func TestUncleHash(t *testing.T) {
}
}

func TestRandom(t *testing.T) {

check := func(f string, got, want interface{}) {
if !reflect.DeepEqual(got, want) {
t.Errorf("%s mismatch: got %v, want %v", f, got, want)
}
}

header := Header{
Difficulty: math.BigPow(11, 11),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000001"),
GasLimit: 12345678,
GasUsed: 1476322,
Time: 9876543,
BaseFee: common.Big1,
Random: common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"),
Eip1559: true,
Eip3675: true,
}
var buf bytes.Buffer
err := header.EncodeRLP(&buf)
if err != nil {
t.Fatalf("err during encododing: %s", err.Error())
}
var decodedHeader Header
decodedHeader.DecodeRLP(rlp.NewStream(&buf, 0))

check("Difficulty", decodedHeader.Difficulty, math.BigPow(11, 11))
check("GasLimit", decodedHeader.GasLimit, uint64(12345678))
check("GasUsed", decodedHeader.GasUsed, uint64(1476322))
check("Coinbase", decodedHeader.Coinbase, common.HexToAddress("0x0000000000000000000000000000000000000001"))
check("BaseFee", decodedHeader.BaseFee, common.Big1)
check("Random", decodedHeader.Random, common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
check("Time", decodedHeader.Time, uint64(9876543))
}

var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))

func BenchmarkEncodeBlock(b *testing.B) {
Expand Down
9 changes: 6 additions & 3 deletions eth/stagedsync/stage_difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/interfaces"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
Expand Down Expand Up @@ -98,12 +99,14 @@ func SpawnDifficultyStage(s *StageState, tx kv.RwTx, cfg DifficultyCfg, ctx cont
return err
}

td.Add(td, header.Difficulty)

if header.Eip3675 {
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// Proof-of-Stake block
// TODO(yperbasis): double check that it's secure
return nil
}

td.Add(td, header.Difficulty)

if td.Cmp(cfg.terminalTotalDifficulty) > 0 {
return rawdb.MarkTransition(tx, blockNum)
}
Expand Down
6 changes: 3 additions & 3 deletions ethdb/privateapi/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
// Hashes
var (
startingHeadHash = common.HexToHash("0x1")
payload1Hash = common.HexToHash("9c344b6c65e0293dd3d45510fef3b6e957a1058c124d88ff607aa59fbd1293a7")
payload2Hash = common.HexToHash("99937164b496a50595aa445a587200fd8a84024e63078871ab3508b6483a8c53")
payload3Hash = common.HexToHash("34f005ca6dee15d637b2c45c3fca124a479daa7bc25c781bba6782ce90aac566")
payload1Hash = common.HexToHash("a1726a8541a53f098b21f58e9d1c63a450b8acbecdce0510162f8257be790320")
payload2Hash = common.HexToHash("a19013b8b5f95ffaa008942fd2f04f72a3ae91f54eb64a3f6f8c6630db742aef")
payload3Hash = common.HexToHash("8aa80e6d2270ae7273e87fa5752d94b94a0f05da60f99fd4b8e36ca767cbae91")
)

// Payloads
Expand Down
4 changes: 1 addition & 3 deletions ethdb/privateapi/ethbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,14 @@ func (s *EthBackendServer) EngineExecutePayloadV1(ctx context.Context, req *type
Coinbase: gointerfaces.ConvertH160toAddress(req.Coinbase),
Root: gointerfaces.ConvertH256ToHash(req.StateRoot),
Bloom: gointerfaces.ConvertH2048ToBloom(req.LogsBloom),
Random: gointerfaces.ConvertH256ToHash(req.Random),
Eip3675: true,
Eip1559: eip1559,
BaseFee: baseFee,
Extra: extra_data.Bytes(),
Number: big.NewInt(int64(req.BlockNumber)),
GasUsed: req.GasUsed,
GasLimit: req.GasLimit,
Time: req.Timestamp,
MixDigest: common.Hash{},
MixDigest: gointerfaces.ConvertH256ToHash(req.Random),
UncleHash: types.EmptyUncleHash,
Difficulty: serenity.SerenityDifficulty,
Nonce: serenity.SerenityNonce,
Expand Down

0 comments on commit 094ab5e

Please sign in to comment.