-
Notifications
You must be signed in to change notification settings - Fork 0
/
evm_writer.go
202 lines (181 loc) · 6.7 KB
/
evm_writer.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
package evmwriter
import (
"bytes"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/corex-mn/go-corex/opera/genesis/driver"
)
var (
// ContractAddress is the EvmWriter pre-compiled contract address
ContractAddress = common.HexToAddress("0xd100ec0000000000000000000000000000000000")
// ContractABI is the input ABI used to generate the binding from
ContractABI string = "[{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"acc\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"setBalance\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"acc\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"}],\"name\":\"copyCode\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"acc\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"with\",\"type\":\"address\"}],\"name\":\"swapCode\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"acc\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"key\",\"type\":\"bytes32\"},{\"internalType\":\"bytes32\",\"name\":\"value\",\"type\":\"bytes32\"}],\"name\":\"setStorage\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"acc\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"diff\",\"type\":\"uint256\"}],\"name\":\"incNonce\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
)
var (
setBalanceMethodID []byte
copyCodeMethodID []byte
swapCodeMethodID []byte
setStorageMethodID []byte
incNonceMethodID []byte
)
func init() {
abi, err := abi.JSON(strings.NewReader(ContractABI))
if err != nil {
panic(err)
}
for name, constID := range map[string]*[]byte{
"setBalance": &setBalanceMethodID,
"copyCode": ©CodeMethodID,
"swapCode": &swapCodeMethodID,
"setStorage": &setStorageMethodID,
"incNonce": &incNonceMethodID,
} {
method, exist := abi.Methods[name]
if !exist {
panic("unknown EvmWriter method")
}
*constID = make([]byte, len(method.ID))
copy(*constID, method.ID)
}
}
type PreCompiledContract struct{}
func (_ PreCompiledContract) Run(stateDB vm.StateDB, _ vm.BlockContext, txCtx vm.TxContext, caller common.Address, input []byte, suppliedGas uint64) ([]byte, uint64, error) {
if caller != driver.ContractAddress {
return nil, 0, vm.ErrExecutionReverted
}
if len(input) < 4 {
return nil, 0, vm.ErrExecutionReverted
}
if bytes.Equal(input[:4], setBalanceMethodID) {
input = input[4:]
// setBalance
if suppliedGas < params.CallValueTransferGas {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= params.CallValueTransferGas
if len(input) != 64 {
return nil, 0, vm.ErrExecutionReverted
}
acc := common.BytesToAddress(input[12:32])
input = input[32:]
value := new(big.Int).SetBytes(input[:32])
if acc == txCtx.Origin {
// Origin balance shouldn't decrease during his transaction
return nil, 0, vm.ErrExecutionReverted
}
balance := stateDB.GetBalance(acc)
if balance.Cmp(value) >= 0 {
diff := new(big.Int).Sub(balance, value)
stateDB.SubBalance(acc, diff)
} else {
diff := new(big.Int).Sub(value, balance)
stateDB.AddBalance(acc, diff)
}
} else if bytes.Equal(input[:4], copyCodeMethodID) {
input = input[4:]
// copyCode
if suppliedGas < params.CreateGas {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= params.CreateGas
if len(input) != 64 {
return nil, 0, vm.ErrExecutionReverted
}
accTo := common.BytesToAddress(input[12:32])
input = input[32:]
accFrom := common.BytesToAddress(input[12:32])
code := stateDB.GetCode(accFrom)
if code == nil {
code = []byte{}
}
cost := uint64(len(code)) * (params.CreateDataGas + params.MemoryGas)
if suppliedGas < cost {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= cost
if accTo != accFrom {
stateDB.SetCode(accTo, code)
}
} else if bytes.Equal(input[:4], swapCodeMethodID) {
input = input[4:]
// swapCode
cost := 2 * params.CreateGas
if suppliedGas < cost {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= cost
if len(input) != 64 {
return nil, 0, vm.ErrExecutionReverted
}
acc0 := common.BytesToAddress(input[12:32])
input = input[32:]
acc1 := common.BytesToAddress(input[12:32])
code0 := stateDB.GetCode(acc0)
if code0 == nil {
code0 = []byte{}
}
code1 := stateDB.GetCode(acc1)
if code1 == nil {
code1 = []byte{}
}
cost0 := uint64(len(code0)) * (params.CreateDataGas + params.MemoryGas)
cost1 := uint64(len(code1)) * (params.CreateDataGas + params.MemoryGas)
cost = (cost0 + cost1) / 2 // 50% discount because trie size won't increase after pruning
if suppliedGas < cost {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= cost
if acc0 != acc1 {
stateDB.SetCode(acc0, code1)
stateDB.SetCode(acc1, code0)
}
} else if bytes.Equal(input[:4], setStorageMethodID) {
input = input[4:]
// setStorage
if suppliedGas < params.SstoreSetGasEIP2200 {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= params.SstoreSetGasEIP2200
if len(input) != 96 {
return nil, 0, vm.ErrExecutionReverted
}
acc := common.BytesToAddress(input[12:32])
input = input[32:]
key := common.BytesToHash(input[:32])
input = input[32:]
value := common.BytesToHash(input[:32])
stateDB.SetState(acc, key, value)
} else if bytes.Equal(input[:4], incNonceMethodID) {
input = input[4:]
// incNonce
if suppliedGas < params.CallValueTransferGas {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= params.CallValueTransferGas
if len(input) != 64 {
return nil, 0, vm.ErrExecutionReverted
}
acc := common.BytesToAddress(input[12:32])
input = input[32:]
value := new(big.Int).SetBytes(input[:32])
if acc == txCtx.Origin {
// Origin nonce shouldn't change during his transaction
return nil, 0, vm.ErrExecutionReverted
}
if value.Cmp(common.Big256) >= 0 {
// Don't allow large nonce increasing to prevent a nonce overflow
return nil, 0, vm.ErrExecutionReverted
}
if value.Sign() <= 0 {
return nil, 0, vm.ErrExecutionReverted
}
stateDB.SetNonce(acc, stateDB.GetNonce(acc)+value.Uint64())
} else {
return nil, 0, vm.ErrExecutionReverted
}
return nil, suppliedGas, nil
}