Commit 76a6573e authored by Danyal Prout's avatar Danyal Prout

Code review feedback

parent f1cb42df
...@@ -621,8 +621,10 @@ func (s *SyncClient) doRequest(ctx context.Context, id peer.ID, expectedBlockNum ...@@ -621,8 +621,10 @@ func (s *SyncClient) doRequest(ctx context.Context, id peer.ID, expectedBlockNum
return fmt.Errorf("failed to read response: %w", err) return fmt.Errorf("failed to read response: %w", err)
} }
expectedBlockTime := s.cfg.TimestampForBlock(expectedBlockNum)
blockVersion := eth.BlockV1 blockVersion := eth.BlockV1
if s.cfg.IsCanyon(expectedBlockNum) { if s.cfg.IsCanyon(expectedBlockTime) {
blockVersion = eth.BlockV2 blockVersion = eth.BlockV2
} }
var res eth.ExecutionPayload var res eth.ExecutionPayload
......
...@@ -41,6 +41,37 @@ func AttributesMatchBlock(attrs *eth.PayloadAttributes, parentHash common.Hash, ...@@ -41,6 +41,37 @@ func AttributesMatchBlock(attrs *eth.PayloadAttributes, parentHash common.Hash,
if *attrs.GasLimit != block.GasLimit { if *attrs.GasLimit != block.GasLimit {
return fmt.Errorf("gas limit does not match. expected %d. got: %d", *attrs.GasLimit, block.GasLimit) return fmt.Errorf("gas limit does not match. expected %d. got: %d", *attrs.GasLimit, block.GasLimit)
} }
if withdrawalErr := checkWithdrawalsMatch(attrs.Withdrawals, block.Withdrawals); withdrawalErr != nil {
return withdrawalErr
}
return nil
}
func checkWithdrawalsMatch(attrWithdrawals *eth.Withdrawals, blockWithdrawals *eth.Withdrawals) error {
if attrWithdrawals == nil && blockWithdrawals == nil {
return nil
}
if attrWithdrawals == nil && blockWithdrawals != nil {
return fmt.Errorf("expected withdrawals in block to be nil, actual %v", *blockWithdrawals)
}
if attrWithdrawals != nil && blockWithdrawals == nil {
return fmt.Errorf("expected withdrawals in block to be non-nil %v, actual nil", *attrWithdrawals)
}
if len(*attrWithdrawals) != len(*blockWithdrawals) {
return fmt.Errorf("expected withdrawals in block to be %d, actual %d", len(*attrWithdrawals), len(*blockWithdrawals))
}
for idx, expected := range *attrWithdrawals {
actual := (*blockWithdrawals)[idx]
if expected != actual {
return fmt.Errorf("expected withdrawal %d to be %v, actual %v", idx, expected, actual)
}
}
return nil return nil
} }
......
package derive
import (
"testing"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/stretchr/testify/require"
)
func TestWithdrawalsMatch(t *testing.T) {
tests := []struct {
attrs *eth.Withdrawals
block *eth.Withdrawals
shouldMatch bool
}{
{
attrs: nil,
block: nil,
shouldMatch: true,
},
{
attrs: &eth.Withdrawals{},
block: nil,
shouldMatch: false,
},
{
attrs: nil,
block: &eth.Withdrawals{},
shouldMatch: false,
},
{
attrs: &eth.Withdrawals{},
block: &eth.Withdrawals{},
shouldMatch: true,
},
{
attrs: &eth.Withdrawals{
{
Index: 1,
},
},
block: &eth.Withdrawals{},
shouldMatch: false,
},
{
attrs: &eth.Withdrawals{
{
Index: 1,
},
},
block: &eth.Withdrawals{
{
Index: 2,
},
},
shouldMatch: false,
},
}
for _, test := range tests {
err := checkWithdrawalsMatch(test.attrs, test.block)
if test.shouldMatch {
require.NoError(t, err)
} else {
require.Error(t, err)
}
}
}
...@@ -125,6 +125,10 @@ func (cfg *Config) ValidateL2Config(ctx context.Context, client L2Client) error ...@@ -125,6 +125,10 @@ func (cfg *Config) ValidateL2Config(ctx context.Context, client L2Client) error
return nil return nil
} }
func (cfg *Config) TimestampForBlock(blockNumber uint64) uint64 {
return cfg.Genesis.L2Time + ((blockNumber - cfg.Genesis.L2.Number) * cfg.BlockTime)
}
func (cfg *Config) TargetBlockNumber(timestamp uint64) (num uint64, err error) { func (cfg *Config) TargetBlockNumber(timestamp uint64) (num uint64, err error) {
// subtract genesis time from timestamp to get the time elapsed since genesis, and then divide that // subtract genesis time from timestamp to get the time elapsed since genesis, and then divide that
// difference by the block time to get the expected L2 block number at the current time. If the // difference by the block time to get the expected L2 block number at the current time. If the
......
...@@ -396,3 +396,54 @@ func TestConfig_Check(t *testing.T) { ...@@ -396,3 +396,54 @@ func TestConfig_Check(t *testing.T) {
}) })
} }
} }
func TestTimestampForBlock(t *testing.T) {
config := randConfig()
tests := []struct {
name string
genesisTime uint64
genesisBlock uint64
blockTime uint64
blockNum uint64
expectedBlockTime uint64
}{
{
name: "FirstBlock",
genesisTime: 100,
genesisBlock: 0,
blockTime: 2,
blockNum: 0,
expectedBlockTime: 100,
},
{
name: "SecondBlock",
genesisTime: 100,
genesisBlock: 0,
blockTime: 2,
blockNum: 1,
expectedBlockTime: 102,
},
{
name: "NBlock",
genesisTime: 100,
genesisBlock: 0,
blockTime: 2,
blockNum: 25,
expectedBlockTime: 150,
},
}
for _, test := range tests {
test := test
t.Run(fmt.Sprintf("TestTimestampForBlock_%s", test.name), func(t *testing.T) {
config.Genesis.L2Time = test.genesisTime
config.Genesis.L2.Number = test.genesisBlock
config.BlockTime = test.blockTime
timestamp := config.TimestampForBlock(test.blockNum)
assert.Equal(t, timestamp, test.expectedBlockTime)
})
}
}
...@@ -355,7 +355,7 @@ func toGethWithdrawals(payload *eth.ExecutionPayload) []*types.Withdrawal { ...@@ -355,7 +355,7 @@ func toGethWithdrawals(payload *eth.ExecutionPayload) []*types.Withdrawal {
return nil return nil
} }
result := []*types.Withdrawal{} result := make([]*types.Withdrawal, 0, len(*payload.Withdrawals))
for _, w := range *payload.Withdrawals { for _, w := range *payload.Withdrawals {
result = append(result, &types.Withdrawal{ result = append(result, &types.Withdrawal{
......
...@@ -26,7 +26,7 @@ const blockV1FixedPart = 32 + 20 + 32 + 32 + 256 + 32 + 8 + 8 + 8 + 8 + 4 + 32 + ...@@ -26,7 +26,7 @@ const blockV1FixedPart = 32 + 20 + 32 + 32 + 256 + 32 + 8 + 8 + 8 + 8 + 4 + 32 +
// V1 + Withdrawals offset // V1 + Withdrawals offset
const blockV2FixedPart = blockV1FixedPart + 4 const blockV2FixedPart = blockV1FixedPart + 4
const withdrawalSize = 8 + 8 + 32 + 8 const withdrawalSize = 8 + 8 + 20 + 8
// MAX_TRANSACTIONS_PER_PAYLOAD in consensus spec // MAX_TRANSACTIONS_PER_PAYLOAD in consensus spec
// https://github.com/ethereum/consensus-specs/blob/ef434e87165e9a4c82a99f54ffd4974ae113f732/specs/bellatrix/beacon-chain.md#execution // https://github.com/ethereum/consensus-specs/blob/ef434e87165e9a4c82a99f54ffd4974ae113f732/specs/bellatrix/beacon-chain.md#execution
...@@ -194,8 +194,8 @@ func marshalWithdrawals(out []byte, withdrawals *Withdrawals) { ...@@ -194,8 +194,8 @@ func marshalWithdrawals(out []byte, withdrawals *Withdrawals) {
offset += 8 offset += 8
binary.LittleEndian.PutUint64(out[offset:offset+8], withdrawal.Validator) binary.LittleEndian.PutUint64(out[offset:offset+8], withdrawal.Validator)
offset += 8 offset += 8
copy(out[offset:offset+32], withdrawal.Address[:]) copy(out[offset:offset+20], withdrawal.Address[:])
offset += 32 offset += 20
binary.LittleEndian.PutUint64(out[offset:offset+8], withdrawal.Amount) binary.LittleEndian.PutUint64(out[offset:offset+8], withdrawal.Amount)
offset += 8 offset += 8
} }
...@@ -335,8 +335,8 @@ func unmarshalWithdrawals(in []byte) (*Withdrawals, error) { ...@@ -335,8 +335,8 @@ func unmarshalWithdrawals(in []byte) (*Withdrawals, error) {
withdrawal.Validator = binary.LittleEndian.Uint64(in[offset : offset+8]) withdrawal.Validator = binary.LittleEndian.Uint64(in[offset : offset+8])
offset += 8 offset += 8
copy(withdrawal.Address[:], in[offset:offset+32]) copy(withdrawal.Address[:], in[offset:offset+20])
offset += 32 offset += 20
withdrawal.Amount = binary.LittleEndian.Uint64(in[offset : offset+8]) withdrawal.Amount = binary.LittleEndian.Uint64(in[offset : offset+8])
offset += 8 offset += 8
......
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