forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onflow#5166 from onflow/ramtin/5129-new-evm-stateD…
…B-part1 [Beyond EVM] performant StateDB implementation - part 1
- Loading branch information
Showing
7 changed files
with
1,457 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.