-
Notifications
You must be signed in to change notification settings - Fork 2
/
state_object.go
250 lines (215 loc) · 6.21 KB
/
state_object.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2022 Match Foundation
// This file is part of the Match Network packages.
//
// Match is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Match packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Match packages. If not, see https://github.com/matchain/match/blob/main/LICENSE
package statedb
import (
"bytes"
"math/big"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var emptyCodeHash = crypto.Keccak256(nil)
// Account is the Ethereum consensus representation of accounts.
// These objects are stored in the storage of auth module.
type Account struct {
Nonce uint64
Balance *big.Int
CodeHash []byte
}
// NewEmptyAccount returns an empty account.
func NewEmptyAccount() *Account {
return &Account{
Balance: new(big.Int),
CodeHash: emptyCodeHash,
}
}
// IsContract returns if the account contains contract code.
func (acct Account) IsContract() bool {
return !bytes.Equal(acct.CodeHash, emptyCodeHash)
}
// Storage represents in-memory cache/buffer of contract storage.
type Storage map[common.Hash]common.Hash
// SortedKeys sort the keys for deterministic iteration
func (s Storage) SortedKeys() []common.Hash {
keys := make([]common.Hash, 0, len(s))
for k := range s {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return bytes.Compare(keys[i].Bytes(), keys[j].Bytes()) < 0
})
return keys
}
// stateObject is the state of an acount
type stateObject struct {
db *StateDB
account Account
code []byte
// state storage
originStorage Storage
dirtyStorage Storage
address common.Address
// flags
dirtyCode bool
suicided bool
}
// newObject creates a state object.
func newObject(db *StateDB, address common.Address, account Account) *stateObject {
if account.Balance == nil {
account.Balance = new(big.Int)
}
if account.CodeHash == nil {
account.CodeHash = emptyCodeHash
}
return &stateObject{
db: db,
address: address,
account: account,
originStorage: make(Storage),
dirtyStorage: make(Storage),
}
}
// empty returns whether the account is considered empty.
func (s *stateObject) empty() bool {
return s.account.Nonce == 0 && s.account.Balance.Sign() == 0 && bytes.Equal(s.account.CodeHash, emptyCodeHash)
}
func (s *stateObject) markSuicided() {
s.suicided = true
}
// AddBalance adds amount to s's balance.
// It is used to add funds to the destination account of a transfer.
func (s *stateObject) AddBalance(amount *big.Int) {
if amount.Sign() == 0 {
return
}
s.SetBalance(new(big.Int).Add(s.Balance(), amount))
}
// SubBalance removes amount from s's balance.
// It is used to remove funds from the origin account of a transfer.
func (s *stateObject) SubBalance(amount *big.Int) {
if amount.Sign() == 0 {
return
}
s.SetBalance(new(big.Int).Sub(s.Balance(), amount))
}
// SetBalance update account balance.
func (s *stateObject) SetBalance(amount *big.Int) {
s.db.journal.append(balanceChange{
account: &s.address,
prev: new(big.Int).Set(s.account.Balance),
})
s.setBalance(amount)
}
func (s *stateObject) setBalance(amount *big.Int) {
s.account.Balance = amount
}
//
// Attribute accessors
//
// Returns the address of the contract/account
func (s *stateObject) Address() common.Address {
return s.address
}
// Code returns the contract code associated with this object, if any.
func (s *stateObject) Code() []byte {
if s.code != nil {
return s.code
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
return nil
}
code := s.db.keeper.GetCode(s.db.ctx, common.BytesToHash(s.CodeHash()))
s.code = code
return code
}
// CodeSize returns the size of the contract code associated with this object,
// or zero if none.
func (s *stateObject) CodeSize() int {
return len(s.Code())
}
// SetCode set contract code to account
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
prevcode := s.Code()
s.db.journal.append(codeChange{
account: &s.address,
prevhash: s.CodeHash(),
prevcode: prevcode,
})
s.setCode(codeHash, code)
}
func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
s.code = code
s.account.CodeHash = codeHash[:]
s.dirtyCode = true
}
// SetCode set nonce to account
func (s *stateObject) SetNonce(nonce uint64) {
s.db.journal.append(nonceChange{
account: &s.address,
prev: s.account.Nonce,
})
s.setNonce(nonce)
}
func (s *stateObject) setNonce(nonce uint64) {
s.account.Nonce = nonce
}
// CodeHash returns the code hash of account
func (s *stateObject) CodeHash() []byte {
return s.account.CodeHash
}
// Balance returns the balance of account
func (s *stateObject) Balance() *big.Int {
return s.account.Balance
}
// Nonce returns the nonce of account
func (s *stateObject) Nonce() uint64 {
return s.account.Nonce
}
// GetCommittedState query the committed state
func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
if value, cached := s.originStorage[key]; cached {
return value
}
// If no live objects are available, load it from keeper
value := s.db.keeper.GetState(s.db.ctx, s.Address(), key)
s.originStorage[key] = value
return value
}
// GetState query the current state (including dirty state)
func (s *stateObject) GetState(key common.Hash) common.Hash {
if value, dirty := s.dirtyStorage[key]; dirty {
return value
}
return s.GetCommittedState(key)
}
// SetState sets the contract state
func (s *stateObject) SetState(key common.Hash, value common.Hash) {
// If the new value is the same as old, don't set
prev := s.GetState(key)
if prev == value {
return
}
// New value is different, update and journal the change
s.db.journal.append(storageChange{
account: &s.address,
key: key,
prevalue: prev,
})
s.setState(key, value)
}
func (s *stateObject) setState(key, value common.Hash) {
s.dirtyStorage[key] = value
}