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

[Beyond EVM] performant StateDB implementation - part 1 #5166

Merged
merged 15 commits into from
Jan 4, 2024
Merged
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