Commit 2b57c303 authored by protolambda's avatar protolambda

op-e2e: add random withdrawals to fake-proof-of-stake

parent 73c75128
package geth package geth
import ( import (
"math/rand"
"time" "time"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -12,6 +12,9 @@ import ( ...@@ -12,6 +12,9 @@ import (
"github.com/ethereum/go-ethereum/eth/catalyst" "github.com/ethereum/go-ethereum/eth/catalyst"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum-optimism/optimism/op-service/testutils"
) )
// fakePoS is a testing-only utility to attach to Geth, // fakePoS is a testing-only utility to attach to Geth,
...@@ -22,6 +25,8 @@ type fakePoS struct { ...@@ -22,6 +25,8 @@ type fakePoS struct {
log log.Logger log log.Logger
blockTime uint64 blockTime uint64
withdrawalsIndex uint64
finalizedDistance uint64 finalizedDistance uint64
safeDistance uint64 safeDistance uint64
...@@ -33,6 +38,7 @@ func (f *fakePoS) Start() error { ...@@ -33,6 +38,7 @@ func (f *fakePoS) Start() error {
if advancing, ok := f.clock.(*clock.AdvancingClock); ok { if advancing, ok := f.clock.(*clock.AdvancingClock); ok {
advancing.Start() advancing.Start()
} }
withdrawalsRNG := rand.New(rand.NewSource(450368975843)) // avoid generating the same address as any test
f.sub = event.NewSubscription(func(quit <-chan struct{}) error { f.sub = event.NewSubscription(func(quit <-chan struct{}) error {
// poll every half a second: enough to catch up with any block time when ticks are missed // poll every half a second: enough to catch up with any block time when ticks are missed
t := f.clock.NewTicker(time.Second / 2) t := f.clock.NewTicker(time.Second / 2)
...@@ -64,6 +70,17 @@ func (f *fakePoS) Start() error { ...@@ -64,6 +70,17 @@ func (f *fakePoS) Start() error {
// We're a long way behind, let's skip some blocks... // We're a long way behind, let's skip some blocks...
newBlockTime = uint64(f.clock.Now().Unix()) newBlockTime = uint64(f.clock.Now().Unix())
} }
// create some random withdrawals
withdrawals := make([]*types.Withdrawal, withdrawalsRNG.Intn(4))
for i := 0; i < len(withdrawals); i++ {
withdrawals[i] = &types.Withdrawal{
Index: f.withdrawalsIndex + uint64(i),
Validator: withdrawalsRNG.Uint64() % 100_000_000, // 100 million fake validators
Address: testutils.RandomAddress(withdrawalsRNG),
// in gwei, consensus-layer quirk. withdraw non-zero value up to 50 ETH
Amount: uint64(withdrawalsRNG.Intn(50_000_000_000) + 1),
}
}
res, err := f.engineAPI.ForkchoiceUpdatedV2(engine.ForkchoiceStateV1{ res, err := f.engineAPI.ForkchoiceUpdatedV2(engine.ForkchoiceStateV1{
HeadBlockHash: head.Hash(), HeadBlockHash: head.Hash(),
SafeBlockHash: safe.Hash(), SafeBlockHash: safe.Hash(),
...@@ -72,7 +89,7 @@ func (f *fakePoS) Start() error { ...@@ -72,7 +89,7 @@ func (f *fakePoS) Start() error {
Timestamp: newBlockTime, Timestamp: newBlockTime,
Random: common.Hash{}, Random: common.Hash{},
SuggestedFeeRecipient: head.Coinbase, SuggestedFeeRecipient: head.Coinbase,
Withdrawals: make([]*types.Withdrawal, 0), Withdrawals: withdrawals,
}) })
if err != nil { if err != nil {
f.log.Error("failed to start building L1 block", "err", err) f.log.Error("failed to start building L1 block", "err", err)
...@@ -109,6 +126,10 @@ func (f *fakePoS) Start() error { ...@@ -109,6 +126,10 @@ func (f *fakePoS) Start() error {
f.log.Error("failed to make built L1 block canonical", "err", err) f.log.Error("failed to make built L1 block canonical", "err", err)
continue continue
} }
// Increment global withdrawals index in the CL.
// The EL doesn't really care about the value,
// but it's nice to mock something consistent with the CL specs.
f.withdrawalsIndex += uint64(len(withdrawals))
case <-quit: case <-quit:
return nil return nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment