Skip to content

Commit

Permalink
Merge pull request onflow#5166 from onflow/ramtin/5129-new-evm-stateD…
Browse files Browse the repository at this point in the history
…B-part1

[Beyond EVM] performant StateDB implementation - part 1
  • Loading branch information
ramtinms authored Jan 4, 2024
2 parents 38484eb + 46dc4ce commit eeff3f3
Show file tree
Hide file tree
Showing 7 changed files with 1,457 additions and 0 deletions.
62 changes: 62 additions & 0 deletions fvm/evm/emulator/state/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package state

import (
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)

// Account holds the metadata of an address and provides (de)serialization functionality
//
// Note that code and storage slots of an address is not part of this data structure
type Account struct {
// address
Address gethCommon.Address
// balance of the address
Balance *big.Int
// nonce of the address
Nonce uint64
// hash of the code
// if no code the gethTypes.EmptyCodeHash is stored
CodeHash gethCommon.Hash
// the id of the collection holds storage slots for this account
// this value is nil for EOA accounts
CollectionID []byte
}

// NewAccount constructs a new account
func NewAccount(
address gethCommon.Address,
balance *big.Int,
nonce uint64,
codeHash gethCommon.Hash,
collectionID []byte,
) *Account {
return &Account{
Address: address,
Balance: balance,
Nonce: nonce,
CodeHash: codeHash,
CollectionID: collectionID,
}
}

func (a *Account) HasCode() bool {
return a.CodeHash != gethTypes.EmptyCodeHash
}

// Encode encodes the account
func (a *Account) Encode() ([]byte, error) {
return rlp.EncodeToBytes(a)
}

// DecodeAccount constructs a new account from the encoded data
func DecodeAccount(inp []byte) (*Account, error) {
if len(inp) == 0 {
return nil, nil
}
a := &Account{}
return a, rlp.DecodeBytes(inp, a)
}
28 changes: 28 additions & 0 deletions fvm/evm/emulator/state/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package state_test

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/testutils"
)

func TestAccountEncoding(t *testing.T) {
acc := state.NewAccount(
testutils.RandomCommonAddress(t),
testutils.RandomBigInt(1000),
uint64(2),
common.BytesToHash([]byte{1}),
[]byte{2},
)

encoded, err := acc.Encode()
require.NoError(t, err)

ret, err := state.DecodeAccount(encoded)
require.NoError(t, err)
require.Equal(t, acc, ret)
}
Loading

0 comments on commit eeff3f3

Please sign in to comment.