-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
161 lines (127 loc) · 3.02 KB
/
state.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package state
import (
"bytes"
"fmt"
"math/big"
iradix "github.com/hashicorp/go-immutable-radix"
"github.com/umbracle/fastrlp"
"github.com/0xDaoChain/dao-chain/crypto"
"github.com/0xDaoChain/dao-chain/types"
)
type State interface {
NewSnapshotAt(types.Hash) (Snapshot, error)
NewSnapshot() Snapshot
GetCode(hash types.Hash) ([]byte, bool)
}
type Snapshot interface {
readSnapshot
Commit(objs []*Object) (Snapshot, []byte)
}
// Account is the account reference in the ethereum state
type Account struct {
Nonce uint64
Balance *big.Int
Root types.Hash
CodeHash []byte
}
func (a *Account) MarshalWith(ar *fastrlp.Arena) *fastrlp.Value {
v := ar.NewArray()
v.Set(ar.NewUint(a.Nonce))
v.Set(ar.NewBigInt(a.Balance))
v.Set(ar.NewBytes(a.Root.Bytes()))
v.Set(ar.NewBytes(a.CodeHash))
return v
}
var accountParserPool fastrlp.ParserPool
func (a *Account) UnmarshalRlp(b []byte) error {
p := accountParserPool.Get()
defer accountParserPool.Put(p)
v, err := p.Parse(b)
if err != nil {
return err
}
elems, err := v.GetElems()
if err != nil {
return err
}
if len(elems) < 4 {
return fmt.Errorf("incorrect number of elements to decode account, expected 4 but found %d", len(elems))
}
// nonce
if a.Nonce, err = elems[0].GetUint64(); err != nil {
return err
}
// balance
if a.Balance == nil {
a.Balance = new(big.Int)
}
if err = elems[1].GetBigInt(a.Balance); err != nil {
return err
}
// root
if err = elems[2].GetHash(a.Root[:]); err != nil {
return err
}
// codeHash
if a.CodeHash, err = elems[3].GetBytes(a.CodeHash[:0]); err != nil {
return err
}
return nil
}
func (a *Account) String() string {
return fmt.Sprintf("%d %s", a.Nonce, a.Balance.String())
}
func (a *Account) Copy() *Account {
aa := new(Account)
aa.Balance = big.NewInt(1).SetBytes(a.Balance.Bytes())
aa.Nonce = a.Nonce
aa.CodeHash = a.CodeHash
aa.Root = a.Root
return aa
}
var emptyCodeHash = crypto.Keccak256(nil)
// StateObject is the internal representation of the account
type StateObject struct {
Account *Account
Code []byte
Suicide bool
Deleted bool
DirtyCode bool
Txn *iradix.Txn
}
func (s *StateObject) Empty() bool {
return s.Account.Nonce == 0 && s.Account.Balance.Sign() == 0 && bytes.Equal(s.Account.CodeHash, emptyCodeHash)
}
// Copy makes a copy of the state object
func (s *StateObject) Copy() *StateObject {
ss := new(StateObject)
// copy account
ss.Account = s.Account.Copy()
ss.Suicide = s.Suicide
ss.Deleted = s.Deleted
ss.DirtyCode = s.DirtyCode
ss.Code = s.Code
if s.Txn != nil {
ss.Txn = s.Txn.CommitOnly().Txn()
}
return ss
}
// Object is the serialization of the radix object (can be merged to StateObject?).
type Object struct {
Address types.Address
CodeHash types.Hash
Balance *big.Int
Root types.Hash
Nonce uint64
Deleted bool
// TODO: Move this to executor
DirtyCode bool
Code []byte
Storage []*StorageObject
}
// StorageObject is an entry in the storage
type StorageObject struct {
Deleted bool
Key []byte
Val []byte
}