forked from thetatoken/theta-protocol-ledger
/
executils.go
306 lines (259 loc) · 9.13 KB
/
executils.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
package execution
import (
"encoding/hex"
"math/big"
"github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/common/result"
"github.com/thetatoken/theta/core"
"github.com/thetatoken/theta/ledger/state"
"github.com/thetatoken/theta/ledger/types"
)
// --------------------------------- Execution Utilities -------------------------------------
// TODO: need to implement the following two functions
// // Read genesis file.
// func ReadGenesisFile() (genDoc *ttypes.GenesisDoc, err error) {
// cfg, err := tcmd.ParseConfig()
// if err != nil {
// return
// }
// return ReadGenesisFileByPath(cfg.GenesisFile())
// }
// func ReadGenesisFileByPath(path string) (genDoc *ttypes.GenesisDoc, err error) {
// genDocJSON, err := ioutil.ReadFile(path)
// if err != nil {
// return
// }
// genDoc, err = ttypes.GenesisDocFromJSON(genDocJSON)
// return
// }
// getValidatorAddresses returns the validator set
func getValidatorSet(ledger core.Ledger, valMgr core.ValidatorManager) *core.ValidatorSet {
currentBlock := ledger.GetCurrentBlock()
if currentBlock == nil {
panic("ledger.currentBlock is nil")
}
parentBlkHash := currentBlock.Parent
validatorSet := valMgr.GetNextValidatorSet(parentBlkHash)
return validatorSet
}
// getValidatorAddresses returns validators' addresses
func getValidatorAddresses(validatorSet *core.ValidatorSet) []common.Address {
validators := validatorSet.Validators()
validatorAddresses := make([]common.Address, len(validators))
for i, v := range validators {
validatorAddresses[i] = v.Address
}
return validatorAddresses
}
func isAValidator(address common.Address, validatorAddresses []common.Address) result.Result {
proposerIsAValidator := false
for _, validatorAddr := range validatorAddresses {
if address == validatorAddr {
proposerIsAValidator = true
break
}
}
if !proposerIsAValidator {
return result.Error("The coinbaseTx proposer is not a validator")
}
return result.OK
}
// The accounts from the TxInputs must either already have
// crypto.PubKey.(type) != nil, (it must be known),
// or it must be specified in the TxInput.
func getInputs(view *state.StoreView, ins []types.TxInput) (map[string]*types.Account, result.Result) {
accounts := map[string]*types.Account{}
for _, in := range ins {
// Account shouldn't be duplicated
if _, ok := accounts[string(in.Address[:])]; ok {
return nil, result.Error("getInputs - Duplicated address: %v", in.Address)
}
acc, success := getAccount(view, in.Address)
if success.IsError() {
return nil, result.Error("getInputs - Unknown address: %v", in.Address)
}
accounts[string(in.Address[:])] = acc
}
return accounts, result.OK
}
func getInput(view *state.StoreView, in types.TxInput) (*types.Account, result.Result) {
return getOrMakeInputImpl(view, in, false)
}
func getOrMakeInput(view *state.StoreView, in types.TxInput) (*types.Account, result.Result) {
return getOrMakeInputImpl(view, in, true)
}
// This function guarantees the public key of the retrieved account is not empty
func getOrMakeInputImpl(view *state.StoreView, in types.TxInput, makeNewAccount bool) (*types.Account, result.Result) {
acc, success := getOrMakeAccountImpl(view, in.Address, makeNewAccount)
if success.IsError() {
return nil, result.Error("getOrMakeInputImpl - Unknown address: %v", in.Address)
}
return acc, result.OK
}
func getAccount(view *state.StoreView, address common.Address) (*types.Account, result.Result) {
return getOrMakeAccountImpl(view, address, false)
}
func getOrMakeAccount(view *state.StoreView, address common.Address) *types.Account {
acc, _ := getOrMakeAccountImpl(view, address, true)
// Impossible to have error.
return acc
}
func getOrMakeAccountImpl(view *state.StoreView, address common.Address, makeNewAccount bool) (*types.Account, result.Result) {
acc := view.GetAccount(address)
if acc == nil {
if !makeNewAccount {
return nil, result.Error("getOrMakeAccountImpl - Unknown address: %v", address)
}
acc = types.NewAccount(address)
acc.LastUpdatedBlockHeight = view.Height()
}
acc.UpdateToHeight(view.Height())
return acc, result.OK
}
func getOrMakeOutputs(view *state.StoreView, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, result.Result) {
if accounts == nil {
accounts = make(map[string]*types.Account)
}
for _, out := range outs {
// Account shouldn't be duplicated
if _, ok := accounts[string(out.Address[:])]; ok {
return nil, result.Error("getOrMakeOutputs - Duplicated address: %v", out.Address)
}
acc := getOrMakeAccount(view, out.Address)
accounts[string(out.Address[:])] = acc
}
return accounts, result.OK
}
// Validate inputs basic structure
func validateInputsBasic(ins []types.TxInput) result.Result {
for _, in := range ins {
// Check TxInput basic
if res := in.ValidateBasic(); res.IsError() {
return res
}
}
return result.OK
}
// Validate inputs and compute total amount of coins
func validateInputsAdvanced(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput, blockHeight uint64) (total types.Coins, res result.Result) {
total = types.NewCoins(0, 0)
for _, in := range ins {
acc := accounts[string(in.Address[:])]
if acc == nil {
panic("validateInputsAdvanced() expects account in accounts")
}
res = validateInputAdvanced(acc, signBytes, in, blockHeight)
if res.IsError() {
return
}
// Good. Add amount to total
total = total.Plus(in.Coins)
}
return total, result.OK
}
func validateInputAdvanced(acc *types.Account, signBytes []byte, in types.TxInput, blockHeight uint64) result.Result {
// Check sequence/coins
seq, balance := acc.Sequence, acc.Balance
if seq+1 != in.Sequence {
return result.Error("ValidateInputAdvanced: Got %v, expected %v. (acc.seq=%v)",
in.Sequence, seq+1, acc.Sequence).WithErrorCode(result.CodeInvalidSequence)
}
// Check amount
if !balance.IsGTE(in.Coins) {
return result.Error("Insufficient fund: balance is %v, tried to send %v",
balance, in.Coins).WithErrorCode(result.CodeInsufficientFund)
}
// Check signatures
signatureValid := in.Signature.Verify(signBytes, acc.Address)
if blockHeight >= common.HeightTxWrapperExtension {
signBytesV2 := types.ChangeEthereumTxWrapper(signBytes, 2)
signatureValid = signatureValid || in.Signature.Verify(signBytesV2, acc.Address)
}
if !signatureValid {
return result.Error("Signature verification failed, SignBytes: %v",
hex.EncodeToString(signBytes)).WithErrorCode(result.CodeInvalidSignature)
}
return result.OK
}
func validateOutputsBasic(outs []types.TxOutput) result.Result {
for _, out := range outs {
// Check TxOutput basic
if res := out.ValidateBasic(); res.IsError() {
return res
}
}
return result.OK
}
func sumOutputs(outs []types.TxOutput) types.Coins {
total := types.NewCoins(0, 0)
for _, out := range outs {
total = total.Plus(out.Coins)
}
return total
}
// Note: Since totalInput == totalOutput + fee, the transaction fee is charged implicitly
// by the following adjustByInputs() function. No special handling needed
func adjustByInputs(view *state.StoreView, accounts map[string]*types.Account, ins []types.TxInput) {
for _, in := range ins {
acc := accounts[string(in.Address[:])]
if acc == nil {
panic("adjustByInputs() expects account in accounts")
}
if !acc.Balance.IsGTE(in.Coins) {
panic("adjustByInputs() expects sufficient funds")
}
acc.Balance = acc.Balance.Minus(in.Coins)
acc.Sequence++
view.SetAccount(in.Address, acc)
}
}
func adjustByOutputs(view *state.StoreView, accounts map[string]*types.Account, outs []types.TxOutput) {
for _, out := range outs {
acc := accounts[string(out.Address[:])]
if acc == nil {
panic("adjustByOutputs() expects account in accounts")
}
acc.Balance = acc.Balance.Plus(out.Coins)
view.SetAccount(out.Address, acc)
}
}
func sanityCheckForGasPrice(gasPrice *big.Int, blockHeight uint64) bool {
if gasPrice == nil {
return false
}
minimumGasPrice := types.GetMinimumGasPrice(blockHeight)
if gasPrice.Cmp(minimumGasPrice) < 0 {
return false
}
return true
}
func sanityCheckForFee(fee types.Coins, blockHeight uint64) (minimumFee *big.Int, success bool) {
fee = fee.NoNil()
minimumFee = types.GetMinimumTransactionFeeTFuelWei(blockHeight)
success = (fee.ThetaWei.Cmp(types.Zero) == 0 && fee.TFuelWei.Cmp(minimumFee) >= 0)
return minimumFee, success
}
func sanityCheckForSendTxFee(fee types.Coins, numAccountsAffected uint64, blockHeight uint64) (minimumFee *big.Int, success bool) {
fee = fee.NoNil()
minimumFee = types.GetSendTxMinimumTransactionFeeTFuelWei(numAccountsAffected, blockHeight)
success = (fee.ThetaWei.Cmp(types.Zero) == 0 && fee.TFuelWei.Cmp(minimumFee) >= 0)
return minimumFee, success
}
func chargeFee(account *types.Account, fee types.Coins) bool {
if !account.Balance.IsGTE(fee) {
return false
}
account.Balance = account.Balance.Minus(fee)
return true
}
func getBlockHeight(ledgerState *state.LedgerState) uint64 {
blockHeight := ledgerState.Height() + 1
return blockHeight
}
func getRegularTxGas(ledgerState *state.LedgerState) uint64 {
blockHeight := getBlockHeight(ledgerState)
if blockHeight < common.HeightJune2021FeeAdjustment {
return types.GasRegularTx
}
return types.GasRegularTxJune2021
}