-
Notifications
You must be signed in to change notification settings - Fork 2
/
evm.go
505 lines (464 loc) · 17 KB
/
evm.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
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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 go-ethereum library is 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"bytes"
"errors"
"math"
"math/big"
"sort"
"github.com/PositionExchange/posichain/block"
consensus_engine "github.com/PositionExchange/posichain/consensus/engine"
"github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/core/vm"
"github.com/PositionExchange/posichain/internal/params"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/shard"
staking "github.com/PositionExchange/posichain/staking"
stakingTypes "github.com/PositionExchange/posichain/staking/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
)
// ChainContext supports retrieving headers and consensus parameters from the
// current blockchain to be used during transaction processing.
type ChainContext interface {
// Engine retrieves the chain's consensus engine.
Engine() consensus_engine.Engine
// GetHeader returns the hash corresponding to their hash.
GetHeader(common.Hash, uint64) *block.Header
// ReadDelegationsByDelegator returns the validators list of a delegator
ReadDelegationsByDelegator(common.Address) (stakingTypes.DelegationIndexes, error)
// ReadDelegationsByDelegatorAt reads the addresses of validators delegated by a delegator at a given block
ReadDelegationsByDelegatorAt(delegator common.Address, blockNum *big.Int) (m stakingTypes.DelegationIndexes, err error)
// ReadValidatorSnapshot returns the snapshot of validator at the beginning of current epoch.
ReadValidatorSnapshot(common.Address) (*stakingTypes.ValidatorSnapshot, error)
// ReadValidatorList returns the list of all validators
ReadValidatorList() ([]common.Address, error)
// Config returns chain config
Config() *params.ChainConfig
ShardID() uint32 // this is implemented by blockchain.go already
}
// NewEVMContext creates a new context for use in the EVM.
func NewEVMContext(msg Message, header *block.Header, chain ChainContext, author *common.Address) vm.Context {
// If we don't have an explicit author (i.e. not mining), extract from the header
var beneficiary common.Address
if author == nil {
beneficiary = common.Address{} // Ignore error, we're past header validation
} else {
beneficiary = *author
}
vrf := common.Hash{}
if len(header.Vrf()) >= 32 {
vrfAndProof := header.Vrf()
copy(vrf[:], vrfAndProof[:32])
}
return vm.Context{
CanTransfer: CanTransfer,
Transfer: Transfer,
GetHash: GetHashFn(header, chain),
GetVRF: GetVRFFn(header, chain),
IsValidator: IsValidator,
Origin: msg.From(),
GasPrice: new(big.Int).Set(msg.GasPrice()),
Coinbase: beneficiary,
GasLimit: header.GasLimit(),
BlockNumber: header.Number(),
EpochNumber: header.Epoch(),
Time: header.Time(),
VRF: vrf,
TxType: 0,
CreateValidator: CreateValidatorFn(header, chain),
EditValidator: EditValidatorFn(header, chain),
Delegate: DelegateFn(header, chain),
Undelegate: UndelegateFn(header, chain),
CollectRewards: CollectRewardsFn(header, chain),
CalculateMigrationGas: CalculateMigrationGasFn(chain),
ShardID: chain.ShardID(),
NumShards: shard.Schedule.InstanceForEpoch(header.Epoch()).NumShards(),
}
}
// HandleStakeMsgFn returns a function which accepts
// (1) the chain state database
// (2) the processed staking parameters
// the function can then be called through the EVM context
func CreateValidatorFn(ref *block.Header, chain ChainContext) vm.CreateValidatorFunc {
// moved from state_transition.go to here, with some modifications
return func(db vm.StateDB, rosettaTracer vm.RosettaTracer, createValidator *stakingTypes.CreateValidator) error {
wrapper, err := VerifyAndCreateValidatorFromMsg(
db, chain, ref.Epoch(), ref.Number(), createValidator,
)
if err != nil {
return err
}
if err := db.UpdateValidatorWrapper(wrapper.Address, wrapper); err != nil {
return err
}
db.SetValidatorFlag(createValidator.ValidatorAddress)
db.SubBalance(createValidator.ValidatorAddress, createValidator.Amount)
//add rosetta log
if rosettaTracer != nil {
rosettaTracer.AddRosettaLog(
vm.CALL,
&vm.RosettaLogAddressItem{
Account: &createValidator.ValidatorAddress,
},
&vm.RosettaLogAddressItem{
Account: &createValidator.ValidatorAddress,
SubAccount: &createValidator.ValidatorAddress,
Metadata: map[string]interface{}{"type": "delegation"},
},
createValidator.Amount,
)
}
return nil
}
}
func EditValidatorFn(ref *block.Header, chain ChainContext) vm.EditValidatorFunc {
// moved from state_transition.go to here, with some modifications
return func(db vm.StateDB, rosettaTracer vm.RosettaTracer, editValidator *stakingTypes.EditValidator) error {
wrapper, err := VerifyAndEditValidatorFromMsg(
db, chain, ref.Epoch(), ref.Number(), editValidator,
)
if err != nil {
return err
}
return db.UpdateValidatorWrapper(wrapper.Address, wrapper)
}
}
func DelegateFn(ref *block.Header, chain ChainContext) vm.DelegateFunc {
// moved from state_transition.go to here, with some modifications
return func(db vm.StateDB, rosettaTracer vm.RosettaTracer, delegate *stakingTypes.Delegate) error {
delegations, err := chain.ReadDelegationsByDelegatorAt(delegate.DelegatorAddress, big.NewInt(0).Sub(ref.Number(), big.NewInt(1)))
if err != nil {
return err
}
updatedValidatorWrappers, balanceToBeDeducted, fromLockedTokens, err := VerifyAndDelegateFromMsg(
db, ref.Epoch(), delegate, delegations, chain.Config())
if err != nil {
return err
}
for _, wrapper := range updatedValidatorWrappers {
if err := db.UpdateValidatorWrapperWithRevert(wrapper.Address, wrapper); err != nil {
return err
}
}
db.SubBalance(delegate.DelegatorAddress, balanceToBeDeducted)
if rosettaTracer != nil && balanceToBeDeducted != big.NewInt(0) {
//add rosetta log
rosettaTracer.AddRosettaLog(
vm.CALL,
&vm.RosettaLogAddressItem{
Account: &delegate.DelegatorAddress,
},
&vm.RosettaLogAddressItem{
Account: &delegate.DelegatorAddress,
SubAccount: &delegate.ValidatorAddress,
Metadata: map[string]interface{}{"type": "delegation"},
},
balanceToBeDeducted,
)
}
if len(fromLockedTokens) > 0 {
sortedKeys := []common.Address{}
for key := range fromLockedTokens {
sortedKeys = append(sortedKeys, key)
}
sort.SliceStable(sortedKeys, func(i, j int) bool {
return bytes.Compare(sortedKeys[i][:], sortedKeys[j][:]) < 0
})
// Add log if everything is good
for _, key := range sortedKeys {
redelegatedToken, ok := fromLockedTokens[key]
if !ok {
return errors.New("Key missing for delegation receipt")
}
encodedRedelegationData := []byte{}
addrBytes := key.Bytes()
encodedRedelegationData = append(encodedRedelegationData, addrBytes...)
encodedRedelegationData = append(encodedRedelegationData, redelegatedToken.Bytes()...)
// The data field format is:
// [first 20 bytes]: Validator address from which the locked token is used for redelegation.
// [rest of the bytes]: the bigInt serialized bytes for the token amount.
db.AddLog(&types.Log{
Address: delegate.DelegatorAddress,
Topics: []common.Hash{staking.DelegateTopic},
Data: encodedRedelegationData,
BlockNumber: ref.Number().Uint64(),
})
//add rosetta log
if rosettaTracer != nil {
// copy from address
fromAccount := common.BytesToAddress(key.Bytes())
rosettaTracer.AddRosettaLog(
vm.CALL,
&vm.RosettaLogAddressItem{
Account: &delegate.DelegatorAddress,
SubAccount: &fromAccount,
Metadata: map[string]interface{}{"type": "undelegation"},
},
&vm.RosettaLogAddressItem{
Account: &delegate.DelegatorAddress,
SubAccount: &delegate.ValidatorAddress,
Metadata: map[string]interface{}{"type": "delegation"},
},
redelegatedToken,
)
}
}
}
return nil
}
}
func UndelegateFn(ref *block.Header, chain ChainContext) vm.UndelegateFunc {
// moved from state_transition.go to here, with some modifications
return func(db vm.StateDB, rosettaTracer vm.RosettaTracer, undelegate *stakingTypes.Undelegate) error {
wrapper, err := VerifyAndUndelegateFromMsg(db, ref.Epoch(), undelegate)
if err != nil {
return err
}
//add rosetta log
if rosettaTracer != nil {
rosettaTracer.AddRosettaLog(
vm.CALL,
&vm.RosettaLogAddressItem{
Account: &undelegate.DelegatorAddress,
SubAccount: &undelegate.ValidatorAddress,
Metadata: map[string]interface{}{"type": "delegation"},
},
&vm.RosettaLogAddressItem{
Account: &undelegate.DelegatorAddress,
SubAccount: &undelegate.ValidatorAddress,
Metadata: map[string]interface{}{"type": "undelegation"},
},
undelegate.Amount,
)
}
return db.UpdateValidatorWrapperWithRevert(wrapper.Address, wrapper)
}
}
func CollectRewardsFn(ref *block.Header, chain ChainContext) vm.CollectRewardsFunc {
return func(db vm.StateDB, rosettaTracer vm.RosettaTracer, collectRewards *stakingTypes.CollectRewards) error {
if chain == nil {
return errors.New("[CollectRewards] No chain context provided")
}
delegations, err := chain.ReadDelegationsByDelegatorAt(collectRewards.DelegatorAddress, big.NewInt(0).Sub(ref.Number(), big.NewInt(1)))
if err != nil {
return err
}
updatedValidatorWrappers, totalRewards, err := VerifyAndCollectRewardsFromDelegation(
db, delegations,
)
if err != nil {
return err
}
for _, wrapper := range updatedValidatorWrappers {
if err := db.UpdateValidatorWrapperWithRevert(wrapper.Address, wrapper); err != nil {
return err
}
}
db.AddBalance(collectRewards.DelegatorAddress, totalRewards)
// Add log if everything is good
db.AddLog(&types.Log{
Address: collectRewards.DelegatorAddress,
Topics: []common.Hash{staking.CollectRewardsTopic},
Data: totalRewards.Bytes(),
BlockNumber: ref.Number().Uint64(),
})
//add rosetta log
if rosettaTracer != nil {
rosettaTracer.AddRosettaLog(
vm.CALL,
nil,
&vm.RosettaLogAddressItem{
Account: &collectRewards.DelegatorAddress,
},
totalRewards,
)
}
return nil
}
}
//func MigrateDelegationsFn(ref *block.Header, chain ChainContext) vm.MigrateDelegationsFunc {
// return func(db vm.StateDB, migrationMsg *stakingTypes.MigrationMsg) ([]interface{}, error) {
// // get existing delegations
// fromDelegations, err := chain.ReadDelegationsByDelegator(migrationMsg.From)
// if err != nil {
// return nil, err
// }
// // get list of modified wrappers
// wrappers, delegates, err := VerifyAndMigrateFromMsg(db, migrationMsg, fromDelegations)
// if err != nil {
// return nil, err
// }
// // add to state db
// for _, wrapper := range wrappers {
// if err := db.UpdateValidatorWrapperWithRevert(wrapper.Address, wrapper); err != nil {
// return nil, err
// }
// }
// return delegates, nil
// }
//}
// calculate the gas for migration; no checks done here similar to other functions
// the checks are handled by staking_verifier.go, ex, if you try to delegate to an address
// who is not a validator - you will be charged all gas passed in two steps
// - 22k initially when gas is calculated
// - remainder when the tx inevitably is a no-op
// i have followed the same logic here, this only produces an error if can't read from db
func CalculateMigrationGasFn(chain ChainContext) vm.CalculateMigrationGasFunc {
return func(db vm.StateDB, migrationMsg *stakingTypes.MigrationMsg, homestead bool, istanbul bool) (uint64, error) {
var gas uint64 = 0
delegations, err := chain.ReadDelegationsByDelegator(migrationMsg.From)
if err != nil {
return 0, err
}
for i := range delegations {
delegationIndex := &delegations[i]
wrapper, err := db.ValidatorWrapper(delegationIndex.ValidatorAddress, true, false)
if err != nil {
return 0, err
}
if uint64(len(wrapper.Delegations)) <= delegationIndex.Index {
utils.Logger().Warn().
Str("validator", delegationIndex.ValidatorAddress.String()).
Uint64("delegation index", delegationIndex.Index).
Int("delegations length", len(wrapper.Delegations)).
Msg("Delegation index out of bound")
return 0, errors.New("Delegation index out of bound")
}
foundDelegation := &wrapper.Delegations[delegationIndex.Index]
// no need to migrate if amount and undelegations are 0
if foundDelegation.Amount.Cmp(common.Big0) == 0 && len(foundDelegation.Undelegations) == 0 {
continue
}
delegate := stakingTypes.Delegate{
DelegatorAddress: migrationMsg.From,
ValidatorAddress: delegationIndex.ValidatorAddress,
Amount: foundDelegation.Amount,
}
encoded, err := rlp.EncodeToBytes(delegate)
if err != nil {
return 0, err
}
thisGas, err := vm.IntrinsicGas(
encoded,
false,
homestead,
istanbul,
false, // isValidatorCreation
)
if err != nil {
return 0, err
}
// overflow when gas + thisGas > Math.MaxUint64
// or Math.MaxUint64 < gas + thisGas
// or Math.MaxUint64 - gas < thisGas
if (math.MaxUint64 - gas) < thisGas {
return 0, vm.ErrOutOfGas
}
gas += thisGas
}
if gas != 0 {
return gas, nil
} else {
// base gas fee if nothing to do, for example, when
// there are no delegations to migrate
return vm.IntrinsicGas(
[]byte{},
false,
homestead,
istanbul,
false, // isValidatorCreation
)
}
}
}
// GetHashFn returns a GetHashFunc which retrieves header hashes by number
func GetHashFn(ref *block.Header, chain ChainContext) func(n uint64) common.Hash {
var cache map[uint64]common.Hash
return func(n uint64) common.Hash {
// If there's no hash cache yet, make one
if cache == nil {
cache = map[uint64]common.Hash{
ref.Number().Uint64() - 1: ref.ParentHash(),
}
}
// Try to fulfill the request from the cache
if hash, ok := cache[n]; ok {
return hash
}
// Not cached, iterate the blocks and cache the hashes
for header := chain.GetHeader(ref.ParentHash(), ref.Number().Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash(), header.Number().Uint64()-1) {
cache[header.Number().Uint64()-1] = header.ParentHash()
if n == header.Number().Uint64()-1 {
return header.ParentHash()
}
}
return common.Hash{}
}
}
// GetVRFFn returns a GetVRFFn which retrieves header vrf by number
func GetVRFFn(ref *block.Header, chain ChainContext) func(n uint64) common.Hash {
var cache map[uint64]common.Hash
return func(n uint64) common.Hash {
// If there's no hash cache yet, make one
if cache == nil {
curVRF := common.Hash{}
if len(ref.Vrf()) >= 32 {
vrfAndProof := ref.Vrf()
copy(curVRF[:], vrfAndProof[:32])
}
cache = map[uint64]common.Hash{
ref.Number().Uint64(): curVRF,
}
}
// Try to fulfill the request from the cache
if hash, ok := cache[n]; ok {
return hash
}
// Not cached, iterate the blocks and cache the hashes
for header := chain.GetHeader(ref.ParentHash(), ref.Number().Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash(), header.Number().Uint64()-1) {
curVRF := common.Hash{}
if len(header.Vrf()) >= 32 {
vrfAndProof := header.Vrf()
copy(curVRF[:], vrfAndProof[:32])
}
cache[header.Number().Uint64()] = curVRF
if n == header.Number().Uint64() {
return curVRF
}
}
return common.Hash{}
}
}
// CanTransfer checks whether there are enough funds in the address' account to make a transfer.
// This does not take the necessary gas in to account to make the transfer valid.
func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
return db.GetBalance(addr).Cmp(amount) >= 0
}
// IsValidator determines whether it is a validator address or not
func IsValidator(db vm.StateDB, addr common.Address) bool {
return db.IsValidator(addr)
}
// Transfer subtracts amount from sender and adds amount to recipient using the given Db
func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int, txType types.TransactionType) {
if txType == types.SameShardTx || txType == types.SubtractionOnly {
db.SubBalance(sender, amount)
}
if txType == types.SameShardTx {
db.AddBalance(recipient, amount)
}
}