Skip to content

Commit

Permalink
Remove inheritance. Remove hard coded block numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Jul 26, 2024
1 parent 27f45f7 commit dd13e28
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 83 deletions.
35 changes: 13 additions & 22 deletions rlp/berlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"github.com/Fantom-foundation/Substate/types"
)

const berlinBlock = 37_455_223

// IsBerlinFork returns true if block is part of the berlin fork block range
func IsBerlinFork(block uint64) bool {
return block >= berlinBlock && block < londonBlock
}

// berlinRLP represents legacy RLP structure between Berlin and London fork starting at berlinBlock ending at londonBlock
type berlinRLP struct {
InputAlloc WorldState
Expand Down Expand Up @@ -53,21 +46,19 @@ type berlinMessage struct {
// toMessage transforms m into RLP format which is compatible with the currently used Geth fork.
func (m berlinMessage) toMessage() *Message {
return &Message{
londonMessage: londonMessage{
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: new(big.Int).Set(m.Value),
Data: m.Data,
InitCodeHash: m.InitCodeHash,
AccessList: m.AccessList,
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: new(big.Int).Set(m.Value),
Data: m.Data,
InitCodeHash: m.InitCodeHash,
AccessList: m.AccessList,

// Same behavior as AccessListTx.gasFeeCap() and AccessListTx.gasTipCap()
GasFeeCap: m.GasPrice,
GasTipCap: m.GasPrice,
},
// Same behavior as AccessListTx.gasFeeCap() and AccessListTx.gasTipCap()
GasFeeCap: m.GasPrice,
GasTipCap: m.GasPrice,
}
}
42 changes: 19 additions & 23 deletions rlp/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@ type legacyMessage struct {
// toMessage transforms m into RLP format which is compatible with the currently used Geth fork.
func (m legacyMessage) toMessage() *Message {
return &Message{
londonMessage: londonMessage{
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: new(big.Int).Set(m.Value),
Data: m.Data,
InitCodeHash: m.InitCodeHash,
AccessList: nil, // access list was not present before berlin fork?
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: new(big.Int).Set(m.Value),
Data: m.Data,
InitCodeHash: m.InitCodeHash,
AccessList: nil, // access list was not present before berlin fork?

// Same behavior as AccessListTx.gasFeeCap() and AccessListTx.gasTipCap()
GasFeeCap: m.GasPrice,
GasTipCap: m.GasPrice,
},
// Same behavior as AccessListTx.gasFeeCap() and AccessListTx.gasTipCap()
GasFeeCap: m.GasPrice,
GasTipCap: m.GasPrice,
}
}

Expand All @@ -74,13 +72,11 @@ type legacyEnv struct {
// toEnv transforms e into RLP format which is compatible with the currently used Geth fork.
func (e legacyEnv) toEnv() *Env {
return &Env{
londonEnv: londonEnv{
Coinbase: e.Coinbase,
Difficulty: e.Difficulty,
GasLimit: e.GasLimit,
Number: e.Number,
Timestamp: e.Timestamp,
BlockHashes: e.BlockHashes,
},
Coinbase: e.Coinbase,
Difficulty: e.Difficulty,
GasLimit: e.GasLimit,
Number: e.Number,
Timestamp: e.Timestamp,
BlockHashes: e.BlockHashes,
}
}
63 changes: 38 additions & 25 deletions rlp/london.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"github.com/Fantom-foundation/Substate/types"
)

const londonBlock = 37_534_833

// IsLondonFork returns true if block is part of the london fork block range
func IsLondonFork(block uint64) bool {
return block >= londonBlock
}

func NewLondonRLP(substate *substate.Substate) *londonRLP {
return &londonRLP{
InputSubstate: NewWorldState(substate.InputSubstate),
Expand Down Expand Up @@ -46,32 +39,36 @@ func (r londonRLP) toRLP() *RLP {

func newLondonEnv(env *substate.Env) londonEnv {
e := londonEnv{
Coinbase: env.Coinbase,
Difficulty: env.Difficulty,
GasLimit: env.GasLimit,
Number: env.Number,
Timestamp: env.Timestamp,
Coinbase: env.Coinbase,
Difficulty: env.Difficulty,
GasLimit: env.GasLimit,
Number: env.Number,
Timestamp: env.Timestamp,
BlockHashes: createBlockHashes(env.BlockHashes),
}

e.BaseFee = nil
if env.BaseFee != nil {
baseFeeHash := types.BigToHash(env.BaseFee)
e.BaseFee = &baseFeeHash
}

return e
}

func createBlockHashes(m map[uint64]types.Hash) (blockHashes [][2]types.Hash) {
var sortedNum64 []uint64
for num64 := range env.BlockHashes {
for num64 := range m {
sortedNum64 = append(sortedNum64, num64)
}

for _, num64 := range sortedNum64 {
num := types.BigToHash(new(big.Int).SetUint64(num64))
blockHash := env.BlockHashes[num64]
blockHash := m[num64]
pair := [2]types.Hash{num, blockHash}
e.BlockHashes = append(e.BlockHashes, pair)
}

e.BaseFee = nil
if env.BaseFee != nil {
baseFeeHash := types.BigToHash(env.BaseFee)
e.BaseFee = &baseFeeHash
blockHashes = append(blockHashes, pair)
}

return e
return blockHashes
}

type londonEnv struct {
Expand All @@ -88,7 +85,13 @@ type londonEnv struct {
// toEnv transforms m into RLP format which is compatible with the currently used Geth fork.
func (e londonEnv) toEnv() *Env {
return &Env{
londonEnv: e,
Coinbase: e.Coinbase,
Difficulty: e.Difficulty,
GasLimit: e.GasLimit,
Number: e.Number,
Timestamp: e.Timestamp,
BlockHashes: e.BlockHashes,
BaseFee: e.BaseFee,
}
}

Expand Down Expand Up @@ -140,6 +143,16 @@ type londonMessage struct {
// toMessage transforms m into RLP format which is compatible with the currently used Geth fork.
func (m londonMessage) toMessage() *Message {
return &Message{
londonMessage: m,
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
From: m.From,
To: m.To,
Value: m.Value,
Data: m.Data,
InitCodeHash: m.InitCodeHash,
AccessList: m.AccessList,
GasFeeCap: m.GasFeeCap,
GasTipCap: m.GasFeeCap,
}
}
6 changes: 3 additions & 3 deletions rlp/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Decode(val []byte) (*RLP, error) {

var legacy legacySubstateRLP
err = rlp.DecodeBytes(val, &legacy)
if err != nil {
return nil, err
if err == nil {
return legacy.toRLP(), nil
}

// cancun
Expand All @@ -54,7 +54,7 @@ func Decode(val []byte) (*RLP, error) {
return &substateRLP, nil
}

return legacy.toRLP(), nil
return nil, err
}

// ToSubstate transforms every attribute of r from RLP to substate.Substate.
Expand Down
22 changes: 20 additions & 2 deletions rlp/rlp_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import (

func NewEnv(env *substate.Env) *Env {
e := &Env{
londonEnv: newLondonEnv(env),
Coinbase: env.Coinbase,
Difficulty: env.Difficulty,
GasLimit: env.GasLimit,
Number: env.Number,
Timestamp: env.Timestamp,
BlockHashes: createBlockHashes(env.BlockHashes),
}

e.BaseFee = nil
if env.BaseFee != nil {
baseFee := types.BigToHash(env.BaseFee)
e.BaseFee = &baseFee
}

e.BlobBaseFee = nil
Expand All @@ -22,7 +33,14 @@ func NewEnv(env *substate.Env) *Env {
}

type Env struct {
londonEnv
Coinbase types.Address
Difficulty *big.Int
GasLimit uint64
Number uint64
Timestamp uint64
BlockHashes [][2]types.Hash

BaseFee *types.Hash `rlp:"nil"` // missing in substate DB from Geth <= v1.10.3
BlobBaseFee *types.Hash `rlp:"nil"` // missing in substate DB before Cancun
}

Expand Down
39 changes: 32 additions & 7 deletions rlp/rlp_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,43 @@ import (
"github.com/syndtr/goleveldb/leveldb"
)

func NewMessage(message *substate.Message) *Message {
m := &Message{
londonMessage: newLondonMessage(message),
BlobGasFeeCap: message.BlobGasFeeCap,
BlobHashes: message.BlobHashes,
func NewMessage(sm *substate.Message) *Message {
mess := &Message{
Nonce: sm.Nonce,
CheckNonce: sm.CheckNonce,
GasPrice: sm.GasPrice,
Gas: sm.Gas,
From: sm.From,
To: sm.To,
Value: new(big.Int).Set(sm.Value),
Data: sm.Data,
AccessList: sm.AccessList,
GasFeeCap: sm.GasFeeCap,
GasTipCap: sm.GasTipCap,
BlobGasFeeCap: sm.BlobGasFeeCap,
BlobHashes: sm.BlobHashes,
}

return m
return mess
}

type Message struct {
londonMessage
Nonce uint64
CheckNonce bool
GasPrice *big.Int
Gas uint64

From types.Address
To *types.Address `rlp:"nil"` // nil means contract creation
Value *big.Int
Data []byte

InitCodeHash *types.Hash `rlp:"nil"` // NOT nil for contract creation

AccessList types.AccessList // missing in substate DB from Geth v1.9.x

GasFeeCap *big.Int // missing in substate DB from Geth <= v1.10.3
GasTipCap *big.Int // missing in substate DB from Geth <= v1.10.3

BlobGasFeeCap *big.Int // missing in substate DB from Geth before Cancun
BlobHashes []types.Hash // missing in substate DB from Geth before Cancun
Expand Down
22 changes: 21 additions & 1 deletion rlp/rlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
hash1 = types.Hash{0x01}
)

func Test_DecodeLondon(t *testing.T) {
func Test_Decode(t *testing.T) {
london := RLP{
Message: &Message{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: &Env{},
Expand All @@ -34,6 +34,26 @@ func Test_DecodeLondon(t *testing.T) {
}
}

func Test_DecodeLondon(t *testing.T) {
london := londonRLP{
Message: londonMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Env: londonEnv{},
Result: &Result{}}
b, err := rlp.EncodeToBytes(london)
if err != nil {
t.Fatal(err)
}

res, err := Decode(b)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(res.Message.Data, []byte{1}) {
t.Fatal("incorrect data")
}
}

func Test_DecodeBerlin(t *testing.T) {
berlin := berlinRLP{
Message: &berlinMessage{Data: []byte{1}, Value: big.NewInt(1), GasPrice: big.NewInt(1)},
Expand Down

0 comments on commit dd13e28

Please sign in to comment.