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

Add BlobBaseFee support. #79

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion db/substate_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var testSubstate = &substate.Substate{
Timestamp: 1,
BaseFee: new(big.Int).SetUint64(1),
},
Message: substate.NewMessage(1, true, new(big.Int).SetUint64(1), 1, types.Address{1}, new(types.Address), new(big.Int).SetUint64(1), []byte{1}, nil, types.AccessList{}, new(big.Int).SetUint64(1), new(big.Int).SetUint64(1)),
Message: substate.NewMessage(1, true, new(big.Int).SetUint64(1), 1, types.Address{1}, new(types.Address), new(big.Int).SetUint64(1), []byte{1}, nil, types.AccessList{}, new(big.Int).SetUint64(1), new(big.Int).SetUint64(1), new(big.Int).SetUint64(1), make([]types.Hash, 0)),
Result: substate.NewResult(1, types.Bloom{}, []*types.Log{}, types.Address{1}, 1),
Block: 37_534_834,
Transaction: 1,
Expand Down
16 changes: 14 additions & 2 deletions rlp/rlp_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func NewEnv(env *substate.Env) *Env {
e.BaseFee = &baseFeeHash
}

e.BlobBaseFee = nil
if env.BlobBaseFee != nil {
blobBaseFee := types.BigToHash(env.BlobBaseFee)
e.BlobBaseFee = &blobBaseFee
}

return e
}

Expand All @@ -46,16 +52,21 @@ type Env struct {
Timestamp uint64
BlockHashes [][2]types.Hash

BaseFee *types.Hash `rlp:"nil"` // missing in substate DB from Geth <= v1.10.3
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
}

// ToSubstate transforms e from Env to substate.Env.
func (e Env) ToSubstate() *substate.Env {
var baseFee *big.Int
var baseFee, blobBaseFee *big.Int
if e.BaseFee != nil {
baseFee = e.BaseFee.Big()
}

if e.BlobBaseFee != nil {
blobBaseFee = e.BlobBaseFee.Big()
}

se := &substate.Env{
Coinbase: e.Coinbase,
Difficulty: e.Difficulty,
Expand All @@ -64,6 +75,7 @@ func (e Env) ToSubstate() *substate.Env {
Timestamp: e.Timestamp,
BlockHashes: make(map[uint64]types.Hash),
BaseFee: baseFee,
BlobBaseFee: blobBaseFee,
}

// iterate through BlockHashes
Expand Down
53 changes: 30 additions & 23 deletions rlp/rlp_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import (

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

if m.To == nil {
Expand Down Expand Up @@ -52,22 +54,27 @@ type Message struct {

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
}

// ToSubstate transforms m from Message to substate.Message.
func (m Message) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error)) (*substate.Message, error) {
sm := &substate.Message{
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: m.Value,
Data: m.Data,
AccessList: m.AccessList,
GasFeeCap: m.GasFeeCap,
GasTipCap: m.GasTipCap,
Nonce: m.Nonce,
CheckNonce: m.CheckNonce,
GasPrice: m.GasPrice,
Gas: m.Gas,
From: m.From,
To: m.To,
Value: m.Value,
Data: m.Data,
AccessList: m.AccessList,
GasFeeCap: m.GasFeeCap,
GasTipCap: m.GasTipCap,
BlobGasFeeCap: m.BlobGasFeeCap,
BlobHashes: m.BlobHashes,
}

// if receiver is nil, we have to extract the data from the DB using getHashFunc
Expand Down
8 changes: 7 additions & 1 deletion substate/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Env struct {

// London hard fork, EIP-1559
BaseFee *big.Int // nil if EIP-1559 is not activated
// Cancun hard fork EIP-4844
BlobBaseFee *big.Int // nil if EIP-4844 is not activated
}

func NewEnv(
Expand All @@ -27,6 +29,7 @@ func NewEnv(
number uint64,
timestamp uint64,
baseFee *big.Int,
blobBaseFee *big.Int,
blockHashes map[uint64]types.Hash) *Env {
return &Env{
Coinbase: coinbase,
Expand All @@ -36,6 +39,7 @@ func NewEnv(
Timestamp: timestamp,
BlockHashes: blockHashes,
BaseFee: baseFee,
BlobBaseFee: blobBaseFee,
}
}

Expand All @@ -56,7 +60,8 @@ func (e *Env) Equal(y *Env) bool {
e.Number == y.Number &&
e.Timestamp == y.Timestamp &&
len(e.BlockHashes) == len(y.BlockHashes) &&
e.BaseFee.Cmp(y.BaseFee) == 0
e.BaseFee.Cmp(y.BaseFee) == 0 &&
e.BlobBaseFee.Cmp(y.BlobBaseFee) == 0
if !equal {
return false
}
Expand All @@ -80,6 +85,7 @@ func (e *Env) String() string {
builder.WriteString(fmt.Sprintf("Number: %v\n", e.Number))
builder.WriteString(fmt.Sprintf("Timestamp: %v\n", e.Timestamp))
builder.WriteString(fmt.Sprintf("Base Fee: %v\n", e.BaseFee.String()))
builder.WriteString(fmt.Sprintf("Blob Base Fee: %v\n", e.BlobBaseFee.String()))
builder.WriteString("Block Hashes: \n")

for number, hash := range e.BlockHashes {
Expand Down
18 changes: 18 additions & 0 deletions substate/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@ func TestEnv_EqualBaseFee(t *testing.T) {
t.Fatal("envs BaseFee are same but equal returned false")
}
}

func TestEnv_EqualBlobBaseFee(t *testing.T) {
env := &Env{
BlobBaseFee: new(big.Int).SetUint64(0),
}
comparedEnv := &Env{
BlobBaseFee: new(big.Int).SetUint64(1),
}

if env.Equal(comparedEnv) {
t.Fatal("envs BlobBaseFee are different but equal returned true")
}

comparedEnv.BlobBaseFee = env.BlobBaseFee
if !env.Equal(comparedEnv) {
t.Fatal("envs BlobBaseFee are same but equal returned false")
}
}
43 changes: 29 additions & 14 deletions substate/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"math/big"
"slices"
"strings"

"github.com/Fantom-foundation/Substate/types"
Expand All @@ -30,6 +31,10 @@ type Message struct {
// London hard fork, EIP-1559: Fee market
GasFeeCap *big.Int // GasPrice if EIP-1559 is not activated
GasTipCap *big.Int // GasPrice if EIP-1559 is not activated

// Cancun hard fork, EIP-4844
BlobGasFeeCap *big.Int
BlobHashes []types.Hash
}

func NewMessage(
Expand All @@ -44,20 +49,25 @@ func NewMessage(
dataHash *types.Hash,
accessList types.AccessList,
gasFeeCap *big.Int,
gasTipCap *big.Int) *Message {
gasTipCap *big.Int,
blobGasFeeCap *big.Int,
blobHashes []types.Hash,
) *Message {
return &Message{
Nonce: nonce,
CheckNonce: checkNonce,
GasPrice: gasPrice,
Gas: gas,
From: from,
To: to,
Value: value,
Data: data,
dataHash: dataHash,
AccessList: accessList,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Nonce: nonce,
CheckNonce: checkNonce,
GasPrice: gasPrice,
Gas: gas,
From: from,
To: to,
Value: value,
Data: data,
dataHash: dataHash,
AccessList: accessList,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
BlobGasFeeCap: blobGasFeeCap,
BlobHashes: blobHashes,
}
}

Expand All @@ -83,11 +93,16 @@ func (m *Message) Equal(y *Message) bool {
bytes.Equal(m.Data, y.Data) &&
len(m.AccessList) == len(y.AccessList) &&
m.GasFeeCap.Cmp(y.GasFeeCap) == 0 &&
m.GasTipCap.Cmp(y.GasTipCap) == 0
m.GasTipCap.Cmp(y.GasTipCap) == 0 &&
m.BlobGasFeeCap.Cmp(y.BlobGasFeeCap) == 0
if !equal {
return false
}

if !slices.Equal(m.BlobHashes, y.BlobHashes) {
return false
}

// check AccessList
for i, mTuple := range m.AccessList {
yTuple := y.AccessList[i]
Expand Down
28 changes: 28 additions & 0 deletions substate/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ func TestMessage_EqualGasTipCap(t *testing.T) {
}
}

func TestMessage_EqualBlobGasFeeCap(t *testing.T) {
msg := &Message{BlobGasFeeCap: new(big.Int).SetUint64(0)}
comparedMsg := &Message{BlobGasFeeCap: new(big.Int).SetUint64(1)}

if msg.Equal(comparedMsg) {
t.Fatal("messages BlobGasFeeCap are different but equal returned true")
}

comparedMsg.BlobGasFeeCap = msg.BlobGasFeeCap
if !msg.Equal(comparedMsg) {
t.Fatal("messages BlobGasFeeCap are same but equal returned false")
}
}

func TestMessage_EqualBlobHashes(t *testing.T) {
msg := &Message{BlobHashes: []types.Hash{types.BytesToHash([]byte{0x0})}}
comparedMsg := &Message{BlobHashes: []types.Hash{types.BytesToHash([]byte{0x1})}}

if msg.Equal(comparedMsg) {
t.Fatal("messages BlobHashes are different but equal returned true")
}

comparedMsg.BlobHashes = msg.BlobHashes
if !msg.Equal(comparedMsg) {
t.Fatal("messages BlobHashes are same but equal returned false")
}
}

func TestMessage_DataHashReturnsIfExists(t *testing.T) {
want := types.BytesToHash([]byte{1})
msg := &Message{dataHash: &want}
Expand Down
Loading