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

added unit tests for rlp decoding #53

Merged
merged 8 commits into from
Mar 27, 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
6 changes: 3 additions & 3 deletions db/substate_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err
return nil, nil
}

rlpSubstate, err := rlp.Decode(val, block)
rlpSubstate, err := rlp.Decode(val)
if err != nil {
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %v", block, tx, err)
}
Expand All @@ -89,14 +89,14 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err
}

func (db *substateDB) PutSubstate(ss *substate.Substate) error {
for i, account := range ss.PreState {
for i, account := range ss.InputSubstate {
err := db.PutCode(account.Code)
if err != nil {
return fmt.Errorf("cannot put preState code from substate-account %v block %v, %v tx into db; %v", i, ss.Block, ss.Transaction, err)
}
}

for i, account := range ss.PostState {
for i, account := range ss.OutputSubstate {
err := db.PutCode(account.Code)
if err != nil {
return fmt.Errorf("cannot put postState code from substate-account %v block %v, %v tx into db; %v", i, ss.Block, ss.Transaction, err)
Expand Down
18 changes: 9 additions & 9 deletions db/substate_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var testSubstate = &substate.Substate{
PreState: substate.NewWorldState(),
PostState: substate.NewWorldState(),
InputSubstate: substate.NewWorldState(),
OutputSubstate: substate.NewWorldState(),
Env: &substate.Env{
Coinbase: types.Address{1},
Difficulty: new(big.Int).SetUint64(1),
Expand All @@ -25,7 +25,7 @@ var testSubstate = &substate.Substate{
},
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)),
Result: substate.NewResult(1, []byte{}, []*types.Log{}, types.Address{1}, 1),
Block: 37_534_834,
Block: 37_134_834,
Transaction: 1,
}

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestSubstateDB_HasSubstate(t *testing.T) {
t.Fatal(err)
}

has, err := db.HasSubstate(37_534_834, 1)
has, err := db.HasSubstate(37_134_834, 1)
if err != nil {
t.Fatalf("has substate returned error; %v", err)
}
Expand All @@ -72,7 +72,7 @@ func TestSubstateDB_GetSubstate(t *testing.T) {
t.Fatal(err)
}

ss, err := db.GetSubstate(37_534_834, 1)
ss, err := db.GetSubstate(37_134_834, 1)
if err != nil {
t.Fatalf("get substate returned error; %v", err)
}
Expand All @@ -94,12 +94,12 @@ func TestSubstateDB_DeleteSubstate(t *testing.T) {
t.Fatal(err)
}

err = db.DeleteSubstate(37_534_834, 1)
err = db.DeleteSubstate(37_134_834, 1)
if err != nil {
t.Fatalf("delete substate returned error; %v", err)
}

ss, err := db.GetSubstate(37_534_834, 1)
ss, err := db.GetSubstate(37_134_834, 1)
if err != nil {
t.Fatalf("get substate returned error; %v", err)
}
Expand All @@ -121,8 +121,8 @@ func createDbAndPutSubstate(dbPath string) (*substateDB, error) {
h2 := types.Hash{}
h2.SetBytes(nil)

testSubstate.PreState[types.Address{1}] = substate.NewAccount(1, new(big.Int).SetUint64(1), h1[:])
testSubstate.PostState[types.Address{2}] = substate.NewAccount(2, new(big.Int).SetUint64(2), h2[:])
testSubstate.InputSubstate[types.Address{1}] = substate.NewAccount(1, new(big.Int).SetUint64(1), h1[:])
testSubstate.OutputSubstate[types.Address{2}] = substate.NewAccount(2, new(big.Int).SetUint64(2), h2[:])
testSubstate.Env.BlockHashes[1] = types.BytesToHash([]byte{1})

err = db.PutSubstate(testSubstate)
Expand Down
2 changes: 1 addition & 1 deletion db/substate_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) {
return nil, fmt.Errorf("invalid substate key: %v; %v", key, err)
}

rlpSubstate, err := rlp.Decode(value, block)
rlpSubstate, err := rlp.Decode(value)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions db/substate_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestSubstateIterator_Value(t *testing.T) {
t.Fatal("iterator returned nil")
}

if tx.Block != 37_534_834 {
t.Fatalf("iterator returned transaction with different block number\ngot: %v\n want: %v", tx.Block, 37_534_834)
if tx.Block != 37_134_834 {
t.Fatalf("iterator returned transaction with different block number\ngot: %v\n want: %v", tx.Block, 37_134_834)
}

if tx.Transaction != 1 {
Expand Down
46 changes: 23 additions & 23 deletions rlp/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ func IsBerlinFork(block uint64) bool {
return block >= berlinBlock && block < londonBlock
}

// legacyRLP represents legacy RLP structure between before Berlin fork thus before berlinBlock
type legacyRLP struct {
PreState WorldState
PostState WorldState
Env *legacyEnv
Message *legacyMessage
Result *Result
// legacySubstateRLP represents legacy RLP structure between before Berlin fork thus before berlinBlock
type legacySubstateRLP struct {
InputAlloc WorldState
OutputAlloc WorldState
Env *legacyEnv
Message *legacyMessage
Result *Result
}

func (r legacyRLP) toLondon() *RLP {
func (r legacySubstateRLP) toLondon() *RLP {
return &RLP{
PreState: r.PreState,
PostState: r.PostState,
Env: r.Env.toLondon(),
Message: r.Message.toLondon(),
Result: r.Result,
InputSubstate: r.InputAlloc,
OutputSubstate: r.OutputAlloc,
Env: r.Env.toLondon(),
Message: r.Message.toLondon(),
Result: r.Result,
}
}

Expand Down Expand Up @@ -95,20 +95,20 @@ func (e legacyEnv) toLondon() *Env {

// berlinRLP represents legacy RLP structure between Berlin and London fork starting at berlinBlock ending at londonBlock
type berlinRLP struct {
PreState WorldState
PostState WorldState
Env *legacyEnv
Message *berlinMessage
Result *Result
InputAlloc WorldState
OutputAlloc WorldState
Env *legacyEnv
Message *berlinMessage
Result *Result
}

func (r berlinRLP) toLondon() *RLP {
return &RLP{
PreState: r.PreState,
PostState: r.PostState,
Env: r.Env.toLondon(),
Message: r.Message.toLondon(),
Result: r.Result,
InputSubstate: r.InputAlloc,
OutputSubstate: r.OutputAlloc,
Env: r.Env.toLondon(),
Message: r.Message.toLondon(),
Result: r.Result,
}

}
Expand Down
48 changes: 24 additions & 24 deletions rlp/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ import (

func NewRLP(substate *substate.Substate) *RLP {
return &RLP{
PreState: NewWorldState(substate.PreState),
PostState: NewWorldState(substate.PostState),
Env: NewEnv(substate.Env),
Message: NewMessage(substate.Message),
Result: NewResult(substate.Result),
InputSubstate: NewWorldState(substate.InputSubstate),
OutputSubstate: NewWorldState(substate.OutputSubstate),
Env: NewEnv(substate.Env),
Message: NewMessage(substate.Message),
Result: NewResult(substate.Result),
}
}

type RLP struct {
PreState WorldState
PostState WorldState
Env *Env
Message *Message
Result *Result
InputSubstate WorldState
OutputSubstate WorldState
Env *Env
Message *Message
Result *Result
}

// Decode decodes val into RLP and returns it.
func Decode(val []byte, block uint64) (*RLP, error) {
func Decode(val []byte) (*RLP, error) {
var (
substateRLP RLP
substateRLP = new(RLP)
err error
)
// todo decode does not work
err = rlp.DecodeBytes(val, &substateRLP)

err = rlp.DecodeBytes(val, substateRLP)
if err == nil {
return &substateRLP, nil
return substateRLP, nil
}

var berlin berlinRLP
Expand All @@ -42,7 +42,7 @@ func Decode(val []byte, block uint64) (*RLP, error) {
return berlin.toLondon(), nil
}

var legacy legacyRLP
var legacy legacySubstateRLP
err = rlp.DecodeBytes(val, &legacy)
if err != nil {
return nil, err
Expand All @@ -52,19 +52,19 @@ func Decode(val []byte, block uint64) (*RLP, error) {
}

// ToSubstate transforms every attribute of r from RLP to substate.Substate.
func (r RLP) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error), block uint64, tx int) (*substate.Substate, error) {
func (r *RLP) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error), block uint64, tx int) (*substate.Substate, error) {
msg, err := r.Message.ToSubstate(getHashFunc)
if err != nil {
return nil, err
}

return &substate.Substate{
PreState: r.PreState.ToSubstate(),
PostState: r.PostState.ToSubstate(),
Env: r.Env.ToSubstate(),
Message: msg,
Result: r.Result.ToSubstate(),
Block: block,
Transaction: tx,
InputSubstate: r.InputSubstate.ToSubstate(),
OutputSubstate: r.OutputSubstate.ToSubstate(),
Env: r.Env.ToSubstate(),
Message: msg,
Result: r.Result.ToSubstate(),
Block: block,
Transaction: tx,
}, nil
}
6 changes: 3 additions & 3 deletions rlp/rlp_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/Fantom-foundation/Substate/types"
)

func NewRLPAccount(acc *substate.Account) *Account {
a := &Account{
func NewRLPAccount(acc *substate.Account) *SubstateAccountRLP {
a := &SubstateAccountRLP{
Nonce: acc.Nonce,
Balance: new(big.Int).Set(acc.Balance),
CodeHash: acc.CodeHash(),
Expand All @@ -33,7 +33,7 @@ func NewRLPAccount(acc *substate.Account) *Account {
return a
}

type Account struct {
type SubstateAccountRLP struct {
Nonce uint64
Balance *big.Int
CodeHash types.Hash
Expand Down
15 changes: 8 additions & 7 deletions rlp/rlp_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func NewEnv(env *substate.Env) *Env {
e.BlockHashes = append(e.BlockHashes, pair)
}

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

return e
}
Expand All @@ -45,7 +45,8 @@ type Env struct {
Number uint64
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
}

// ToSubstate transforms e from Env to substate.Env.
Expand All @@ -57,7 +58,7 @@ func (e Env) ToSubstate() *substate.Env {
Number: e.Number,
Timestamp: e.Timestamp,
BlockHashes: make(map[uint64]types.Hash),
BaseFee: new(big.Int).SetBytes(e.BaseFee[:]),
//BaseFee: new(big.Int).SetBytes(e.BaseFee[:]),
}

// iterate through BlockHashes
Expand Down
Loading