-
Notifications
You must be signed in to change notification settings - Fork 175
/
account.go
62 lines (55 loc) · 1.43 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package state
import (
"math/big"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/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)
}