-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn.go
550 lines (471 loc) · 12.3 KB
/
txn.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package state
import (
"fmt"
"math/big"
"github.com/ethereum/evmc/v10/bindings/go/evmc"
iradix "github.com/hashicorp/go-immutable-radix"
"github.com/umbracle/ethgo"
)
var (
// logIndex is the index of the logs in the trie
logIndex = bytesToHash([]byte{2})
// refundIndex is the index of the refund
refundIndex = bytesToHash([]byte{3})
)
// Txn is a reference of the state
type Txn struct {
snapshot Snapshot
snapshots []*iradix.Tree
txn *iradix.Txn
rev evmc.Revision
}
func NewTxn(snapshot Snapshot) *Txn {
return newTxn(snapshot)
}
func newTxn(snapshot Snapshot) *Txn {
i := iradix.New()
return &Txn{
snapshot: snapshot,
snapshots: []*iradix.Tree{},
txn: i.Txn(),
}
}
// Snapshot takes a snapshot at this point in time
func (txn *Txn) Snapshot() int {
t := txn.txn.CommitOnly()
id := len(txn.snapshots)
txn.snapshots = append(txn.snapshots, t)
return id
}
// RevertToSnapshot reverts to a given snapshot
func (txn *Txn) RevertToSnapshot(id int) {
if id > len(txn.snapshots) {
panic("")
}
tree := txn.snapshots[id]
txn.txn = tree.Txn()
}
// GetAccount returns an account
func (txn *Txn) GetAccount(addr evmc.Address) (*Account, bool) {
object, exists := txn.getStateObject(addr)
if !exists {
return nil, false
}
return object.Account, true
}
func (txn *Txn) getStateObject(addr evmc.Address) (*stateObject, bool) {
// Try to get state from radix tree which holds transient states during block processing first
val, exists := txn.txn.Get(addr[:])
if exists {
obj := val.(*stateObject)
if obj.Deleted {
return nil, false
}
return obj.Copy(), true
}
account, err := txn.snapshot.GetAccount(addr)
if err != nil {
return nil, false
}
if account == nil {
return nil, false
}
obj := &stateObject{
Account: account.Copy(),
Code: account.Code,
}
return obj, true
}
func (txn *Txn) upsertAccount(addr evmc.Address, create bool, f func(object *stateObject)) {
object, exists := txn.getStateObject(addr)
if !exists && create {
object = &stateObject{
Account: &Account{
Balance: big.NewInt(0),
CodeHash: EmptyCodeHash[:],
Root: EmptyRootHash,
},
}
}
// run the callback to modify the account
f(object)
if object != nil {
txn.txn.Insert(addr[:], object)
}
}
func (txn *Txn) AddSealingReward(addr evmc.Address, balance *big.Int) {
txn.upsertAccount(addr, true, func(object *stateObject) {
if object.Suicide {
*object = *newStateObject(txn)
object.Account.Balance.SetBytes(balance.Bytes())
} else {
object.Account.Balance.Add(object.Account.Balance, balance)
}
})
}
// AddBalance adds balance
func (txn *Txn) AddBalance(addr evmc.Address, balance *big.Int) {
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.Balance.Add(object.Account.Balance, balance)
})
}
var errNotEnoughFunds = fmt.Errorf("not enough funds for transfer with given value")
// SubBalance reduces the balance at address addr by amount
func (txn *Txn) SubBalance(addr evmc.Address, amount *big.Int) error {
// If we try to reduce balance by 0, then it's a noop
if amount.Sign() == 0 {
return nil
}
// Check if we have enough balance to deduce amount from
if balance := txn.GetBalance(addr); balance.Cmp(amount) < 0 {
return errNotEnoughFunds
}
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.Balance.Sub(object.Account.Balance, amount)
})
return nil
}
// SetBalance sets the balance
func (txn *Txn) SetBalance(addr evmc.Address, balance *big.Int) {
//fmt.Printf("SET BALANCE: %s %s\n", addr.String(), balance.String())
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.Balance.SetBytes(balance.Bytes())
})
}
// GetBalance returns the balance of an address
func (txn *Txn) GetBalance(addr evmc.Address) *big.Int {
object, exists := txn.getStateObject(addr)
if !exists {
return big.NewInt(0)
}
return object.Account.Balance
}
func (txn *Txn) EmitLog(addr evmc.Address, topics []evmc.Hash, data []byte) {
log := &Log{
Address: addr,
Topics: topics,
Data: append([]byte{}, data...),
}
var logs []*Log
val, exists := txn.txn.Get(logIndex[:])
if !exists {
logs = []*Log{}
} else {
logs = val.([]*Log)
}
logs = append(logs, log)
txn.txn.Insert(logIndex[:], logs)
}
// State
var zeroHash evmc.Hash
func (txn *Txn) isRevision(rev evmc.Revision) bool {
return rev <= txn.rev
}
func (txn *Txn) SetStorage(addr evmc.Address, key evmc.Hash, value evmc.Hash) (status evmc.StorageStatus) {
oldValue := txn.GetState(addr, key)
if oldValue == value {
return evmc.StorageUnchanged
}
current := oldValue // current - storage dirtied by previous lines of this contract
original := txn.GetCommittedState(addr, key) // storage slot before this transaction started
txn.SetState(addr, key, value)
isIstanbul := txn.isRevision(evmc.Istanbul)
legacyGasMetering := !isIstanbul && (txn.isRevision(evmc.Petersburg) || !txn.isRevision(evmc.Constantinople))
if legacyGasMetering {
status = evmc.StorageModified
if oldValue == zeroHash {
return evmc.StorageAdded
} else if value == zeroHash {
txn.AddRefund(15000)
return evmc.StorageDeleted
}
return evmc.StorageModified
}
if original == current {
if original == zeroHash { // create slot (2.1.1)
return evmc.StorageAdded
}
if value == zeroHash { // delete slot (2.1.2b)
txn.AddRefund(15000)
return evmc.StorageDeleted
}
return evmc.StorageModified
}
if original != zeroHash { // Storage slot was populated before this transaction started
if current == zeroHash { // recreate slot (2.2.1.1)
txn.SubRefund(15000)
} else if value == zeroHash { // delete slot (2.2.1.2)
txn.AddRefund(15000)
}
}
if original == value {
if original == zeroHash { // reset to original nonexistent slot (2.2.2.1)
// Storage was used as memory (allocation and deallocation occurred within the same contract)
if isIstanbul {
txn.AddRefund(19200)
} else {
txn.AddRefund(19800)
}
} else { // reset to original existing slot (2.2.2.2)
if isIstanbul {
txn.AddRefund(4200)
} else {
txn.AddRefund(4800)
}
}
}
return evmc.StorageModifiedAgain
}
// SetState change the state of an address
func (txn *Txn) SetState(addr evmc.Address, key, value evmc.Hash) {
txn.upsertAccount(addr, true, func(object *stateObject) {
if object.Txn == nil {
object.Txn = iradix.New().Txn()
}
if value == zeroHash {
object.Txn.Insert(key[:], nil)
} else {
object.Txn.Insert(key[:], value[:])
}
})
}
// GetState returns the state of the address at a given key
func (txn *Txn) GetState(addr evmc.Address, key evmc.Hash) evmc.Hash {
object, exists := txn.getStateObject(addr)
if !exists {
return evmc.Hash{}
}
// Try to get account state from radix tree first
// Because the latest account state should be in in-memory radix tree
// if account state update happened in previous transactions of same block
if object.Txn != nil {
if val, ok := object.Txn.Get(key[:]); ok {
if val == nil {
return evmc.Hash{}
}
return bytesToHash(val.([]byte))
}
}
return txn.snapshot.GetStorage(addr, object.Account.Root, key)
}
// Nonce
// IncrNonce increases the nonce of the address
func (txn *Txn) IncrNonce(addr evmc.Address) {
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.Nonce++
})
}
// SetNonce reduces the balance
func (txn *Txn) SetNonce(addr evmc.Address, nonce uint64) {
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.Nonce = nonce
})
}
// GetNonce returns the nonce of an addr
func (txn *Txn) GetNonce(addr evmc.Address) uint64 {
object, exists := txn.getStateObject(addr)
if !exists {
return 0
}
return object.Account.Nonce
}
// Code
// SetCode sets the code for an address
func (txn *Txn) SetCode(addr evmc.Address, code []byte) {
txn.upsertAccount(addr, true, func(object *stateObject) {
object.Account.CodeHash = ethgo.Keccak256(code)
object.DirtyCode = true
object.Code = code
})
}
func (txn *Txn) GetCode(addr evmc.Address) []byte {
object, exists := txn.getStateObject(addr)
if !exists {
return nil
}
if object.DirtyCode {
return object.Code
}
return object.Code
}
func (txn *Txn) GetCodeSize(addr evmc.Address) int {
return len(txn.GetCode(addr))
}
func (txn *Txn) GetCodeHash(addr evmc.Address) (res evmc.Hash) {
if txn.empty(addr) {
return
}
object, exists := txn.getStateObject(addr)
if !exists {
return
}
copy(res[:], object.Account.CodeHash)
return
}
// Suicide marks the given account as suicided
func (txn *Txn) Suicide(addr evmc.Address) bool {
var suicided bool
txn.upsertAccount(addr, false, func(object *stateObject) {
if object == nil || object.Suicide {
suicided = false
} else {
suicided = true
object.Suicide = true
}
if object != nil {
object.Account.Balance = new(big.Int)
}
})
return suicided
}
// HasSuicided returns true if the account suicided
func (txn *Txn) HasSuicided(addr evmc.Address) bool {
object, exists := txn.getStateObject(addr)
return exists && object.Suicide
}
// Refund
func (txn *Txn) AddRefund(gas uint64) {
refund := txn.GetRefund() + gas
txn.txn.Insert(refundIndex[:], refund)
}
func (txn *Txn) SubRefund(gas uint64) {
refund := txn.GetRefund() - gas
txn.txn.Insert(refundIndex[:], refund)
}
func (txn *Txn) Logs() []*Log {
data, exists := txn.txn.Get(logIndex[:])
if !exists {
return nil
}
txn.txn.Delete(logIndex[:])
return data.([]*Log)
}
func (txn *Txn) GetRefund() uint64 {
data, exists := txn.txn.Get(refundIndex[:])
if !exists {
return 0
}
return data.(uint64)
}
// GetCommittedState returns the state of the address in the trie
func (txn *Txn) GetCommittedState(addr evmc.Address, key evmc.Hash) evmc.Hash {
obj, ok := txn.getStateObject(addr)
if !ok {
return evmc.Hash{}
}
return txn.snapshot.GetStorage(addr, obj.Account.Root, key)
}
func (txn *Txn) TouchAccount(addr evmc.Address) {
txn.upsertAccount(addr, true, func(obj *stateObject) {
})
}
func (txn *Txn) AccountExists(addr evmc.Address) bool {
if txn.rev >= evmc.SpuriousDragon {
return !txn.empty(addr)
}
_, exists := txn.getStateObject(addr)
return exists
}
func (txn *Txn) empty(addr evmc.Address) bool {
obj, exists := txn.getStateObject(addr)
if !exists {
return true
}
return obj.Empty()
}
func newStateObject(txn *Txn) *stateObject {
return &stateObject{
Account: &Account{
Balance: big.NewInt(0),
CodeHash: EmptyCodeHash[:],
Root: EmptyRootHash,
},
}
}
func (txn *Txn) CreateAccount(addr evmc.Address) {
obj := &stateObject{
Account: &Account{
Balance: big.NewInt(0),
CodeHash: EmptyCodeHash[:],
Root: EmptyRootHash,
},
}
prev, ok := txn.getStateObject(addr)
if ok {
obj.Account.Balance.SetBytes(prev.Account.Balance.Bytes())
}
txn.txn.Insert(addr[:], obj)
}
func (txn *Txn) CleanDeleteObjects(deleteEmptyObjects bool) {
remove := [][]byte{}
txn.txn.Root().Walk(func(k []byte, v interface{}) bool {
a, ok := v.(*stateObject)
if !ok {
return false
}
if a.Suicide || a.Empty() && deleteEmptyObjects {
remove = append(remove, k)
}
return false
})
for _, k := range remove {
v, ok := txn.txn.Get(k)
if !ok {
panic("it should not happen")
}
obj, ok := v.(*stateObject)
if !ok {
panic("it should not happen")
}
obj2 := obj.Copy()
obj2.Deleted = true
txn.txn.Insert(k, obj2)
}
// delete refunds
txn.txn.Delete(refundIndex[:])
}
func (txn *Txn) Commit() []*Object {
x := txn.txn.Commit()
// Do a more complex thing for now
objs := []*Object{}
x.Root().Walk(func(k []byte, v interface{}) bool {
a, ok := v.(*stateObject)
if !ok {
// We also have logs, avoid those
return false
}
// k is an array of 20 bytes
var addr evmc.Address
copy(addr[:], k)
var codeHash evmc.Hash
copy(codeHash[:], a.Account.CodeHash)
obj := &Object{
Nonce: a.Account.Nonce,
Address: addr,
Balance: a.Account.Balance,
Root: a.Account.Root,
CodeHash: codeHash,
DirtyCode: a.DirtyCode,
Code: a.Code,
}
if a.Deleted {
obj.Deleted = true
} else {
if a.Txn != nil {
a.Txn.Root().Walk(func(k []byte, v interface{}) bool {
store := &StorageObject{Key: k}
if v == nil {
store.Deleted = true
} else {
store.Val = v.([]byte)
}
obj.Storage = append(obj.Storage, store)
return false
})
}
}
objs = append(objs, obj)
return false
})
return objs
}