-
Notifications
You must be signed in to change notification settings - Fork 27
/
worldUpdate.go
118 lines (104 loc) · 3.33 KB
/
worldUpdate.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
package worldmock
import (
"errors"
"math/big"
"github.com/ElrondNetwork/elrond-go/core/vmcommon"
)
// UpdateBalance sets a new balance to an account
func (b *MockWorld) UpdateBalance(address []byte, newBalance *big.Int) error {
acct := b.AcctMap.GetAccount(address)
if acct == nil {
return errors.New("method UpdateBalance expects an existing address")
}
acct.Balance = newBalance
return nil
}
// UpdateBalanceWithDelta changes balance of an account by a given amount
func (b *MockWorld) UpdateBalanceWithDelta(address []byte, balanceDelta *big.Int) error {
acct := b.AcctMap.GetAccount(address)
if acct == nil {
return errors.New("method UpdateBalanceWithDelta expects an existing address")
}
acct.Balance = big.NewInt(0).Add(acct.Balance, balanceDelta)
return nil
}
// UpdateWorldStateBefore performs gas payment, before transaction
func (b *MockWorld) UpdateWorldStateBefore(
fromAddr []byte,
gasLimit uint64,
gasPrice uint64) error {
acct := b.AcctMap.GetAccount(fromAddr)
if acct == nil {
return errors.New("method UpdateWorldStateBefore expects an existing address")
}
acct.Nonce++
gasPayment := big.NewInt(0).Mul(
big.NewInt(0).SetUint64(gasLimit),
big.NewInt(0).SetUint64(gasPrice))
if acct.Balance.Cmp(gasPayment) < 0 {
return errors.New("not enough balance to pay gas upfront")
}
acct.Balance.Sub(acct.Balance, gasPayment)
return nil
}
// UpdateAccounts should be called after the VM test has run, to update world state
func (b *MockWorld) UpdateAccounts(
outputAccounts map[string]*vmcommon.OutputAccount,
accountsToDelete [][]byte) error {
for _, modAcct := range outputAccounts {
b.UpdateAccountFromOutputAccount(modAcct)
}
// commit ESDT data
for _, acct := range b.AcctMap {
for _, esdtData := range acct.ESDTData {
esdtData.Balance = esdtData.Balance.Add(esdtData.Balance, esdtData.BalanceDelta)
esdtData.BalanceDelta = big.NewInt(0)
}
}
for _, delAddr := range accountsToDelete {
b.AcctMap.DeleteAccount(delAddr)
}
return nil
}
// UpdateAccountFromOutputAccount updates a single account from a transaction output.
func (b *MockWorld) UpdateAccountFromOutputAccount(modAcct *vmcommon.OutputAccount) {
acct := b.AcctMap.GetAccount(modAcct.Address)
if acct == nil {
acct = b.AcctMap.CreateAccount(modAcct.Address)
acct.OwnerAddress = modAcct.CodeDeployerAddress
b.AcctMap.PutAccount(acct)
}
acct.Exists = true
if modAcct.BalanceDelta != nil {
acct.Balance = big.NewInt(0).Add(acct.Balance, modAcct.BalanceDelta)
} else {
acct.Balance = modAcct.Balance
}
if modAcct.Nonce > acct.Nonce {
acct.Nonce = modAcct.Nonce
}
if len(modAcct.Code) > 0 {
// TODO: set CodeMetadata according to code metdata coming from VM
acct.SetCode(modAcct.Code, &vmcommon.CodeMetadata{
Payable: true,
Upgradeable: true,
Readable: true,
})
}
if len(modAcct.OutputTransfers) > 0 && len(modAcct.OutputTransfers[0].Data) > 0 {
acct.AsyncCallData = string(modAcct.OutputTransfers[0].Data)
}
for _, stu := range modAcct.StorageUpdates {
acct.Storage[string(stu.Offset)] = stu.Data
}
}
// RollbackChanges should be called after the VM test has run, if the tx has failed
func (b *MockWorld) RollbackChanges() {
// discard ESDT deltas
// so they don't interfere with future txs
for _, acct := range b.AcctMap {
for _, esdtData := range acct.ESDTData {
esdtData.BalanceDelta = big.NewInt(0)
}
}
}