-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathemulator.go
714 lines (623 loc) · 20 KB
/
emulator.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package emulator
import (
"errors"
"math/big"
"github.com/holiman/uint256"
"github.com/onflow/atree"
"github.com/onflow/crypto/hash"
gethCommon "github.com/onflow/go-ethereum/common"
gethCore "github.com/onflow/go-ethereum/core"
gethTracing "github.com/onflow/go-ethereum/core/tracing"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethVM "github.com/onflow/go-ethereum/core/vm"
gethCrypto "github.com/onflow/go-ethereum/crypto"
gethParams "github.com/onflow/go-ethereum/params"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
)
// Emulator wraps an EVM runtime where evm transactions
// and direct calls are accepted.
type Emulator struct {
rootAddr flow.Address
ledger atree.Ledger
}
var _ types.Emulator = &Emulator{}
// NewEmulator constructs a new EVM Emulator
func NewEmulator(
ledger atree.Ledger,
rootAddr flow.Address,
) *Emulator {
return &Emulator{
rootAddr: rootAddr,
ledger: ledger,
}
}
func newConfig(ctx types.BlockContext) *Config {
return NewConfig(
WithChainID(ctx.ChainID),
WithBlockNumber(new(big.Int).SetUint64(ctx.BlockNumber)),
WithBlockTime(ctx.BlockTimestamp),
WithCoinbase(ctx.GasFeeCollector.ToCommon()),
WithDirectCallBaseGasUsage(ctx.DirectCallBaseGasUsage),
WithExtraPrecompiledContracts(ctx.ExtraPrecompiledContracts),
WithGetBlockHashFunction(ctx.GetHashFunc),
WithRandom(&ctx.Random),
WithTransactionTracer(ctx.Tracer),
WithBlockTotalGasUsedSoFar(ctx.TotalGasUsedSoFar),
WithBlockTxCountSoFar(ctx.TxCountSoFar),
)
}
// NewReadOnlyBlockView constructs a new read-only block view
func (em *Emulator) NewReadOnlyBlockView(ctx types.BlockContext) (types.ReadOnlyBlockView, error) {
execState, err := state.NewStateDB(em.ledger, em.rootAddr)
return &ReadOnlyBlockView{
state: execState,
}, err
}
// NewBlockView constructs a new block view (mutable)
func (em *Emulator) NewBlockView(ctx types.BlockContext) (types.BlockView, error) {
return &BlockView{
config: newConfig(ctx),
rootAddr: em.rootAddr,
ledger: em.ledger,
}, nil
}
// ReadOnlyBlockView provides a read only view of a block
// could be used for multiple queries against a block
type ReadOnlyBlockView struct {
state types.StateDB
}
// BalanceOf returns the balance of the given address
func (bv *ReadOnlyBlockView) BalanceOf(address types.Address) (*big.Int, error) {
bal := bv.state.GetBalance(address.ToCommon())
return bal.ToBig(), bv.state.Error()
}
// NonceOf returns the nonce of the given address
func (bv *ReadOnlyBlockView) NonceOf(address types.Address) (uint64, error) {
return bv.state.GetNonce(address.ToCommon()), bv.state.Error()
}
// CodeOf returns the code of the given address
func (bv *ReadOnlyBlockView) CodeOf(address types.Address) (types.Code, error) {
return bv.state.GetCode(address.ToCommon()), bv.state.Error()
}
// CodeHashOf returns the code hash of the given address
func (bv *ReadOnlyBlockView) CodeHashOf(address types.Address) ([]byte, error) {
return bv.state.GetCodeHash(address.ToCommon()).Bytes(), bv.state.Error()
}
// BlockView allows mutation of the evm state as part of a block
// current version only accepts only a single interaction per block view.
type BlockView struct {
config *Config
rootAddr flow.Address
ledger atree.Ledger
}
// DirectCall executes a direct call
func (bl *BlockView) DirectCall(call *types.DirectCall) (res *types.Result, err error) {
// construct a new procedure
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
// Set the nonce for the call (needed for some operations like deployment)
call.Nonce = proc.state.GetNonce(call.From.ToCommon())
// Call tx tracer
if proc.evm.Config.Tracer != nil && proc.evm.Config.Tracer.OnTxStart != nil {
proc.evm.Config.Tracer.OnTxStart(proc.evm.GetVMContext(), call.Transaction(), call.From.ToCommon())
defer func() {
if proc.evm.Config.Tracer.OnTxEnd != nil &&
err == nil && res != nil {
proc.evm.Config.Tracer.OnTxEnd(res.Receipt(), res.ValidationError)
}
// call OnLog tracer hook, upon successful call result
if proc.evm.Config.Tracer.OnLog != nil &&
err == nil && res != nil {
for _, log := range res.Logs {
proc.evm.Config.Tracer.OnLog(log)
}
}
}()
}
// re-route based on the sub type
switch call.SubType {
case types.DepositCallSubType:
return proc.mintTo(call)
case types.WithdrawCallSubType:
return proc.withdrawFrom(call)
case types.DeployCallSubType:
if !call.EmptyToField() {
return proc.deployAt(call)
}
fallthrough
default:
return proc.runDirect(call)
}
}
// RunTransaction runs an evm transaction
func (bl *BlockView) RunTransaction(
tx *gethTypes.Transaction,
) (result *types.Result, err error) {
// create a new procedure
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
// constructs a core.message from the tx
msg, err := gethCore.TransactionToMessage(
tx,
GetSigner(bl.config),
proc.config.BlockContext.BaseFee)
if err != nil {
// this is not a fatal error (e.g. due to bad signature)
// not a valid transaction
return types.NewInvalidResult(tx, err), nil
}
// call tracer
if proc.evm.Config.Tracer != nil && proc.evm.Config.Tracer.OnTxStart != nil {
proc.evm.Config.Tracer.OnTxStart(proc.evm.GetVMContext(), tx, msg.From)
}
// run msg
res, err := proc.run(msg, tx.Hash(), tx.Type())
if err != nil {
return nil, err
}
// all commit errors (StateDB errors) has to be returned
res.StateChangeCommitment, err = proc.commit(true)
if err != nil {
return nil, err
}
// call tracer on tx end
if proc.evm.Config.Tracer != nil &&
proc.evm.Config.Tracer.OnTxEnd != nil &&
res != nil {
proc.evm.Config.Tracer.OnTxEnd(res.Receipt(), res.ValidationError)
}
// call OnLog tracer hook, upon successful tx result
if proc.evm.Config.Tracer != nil &&
proc.evm.Config.Tracer.OnLog != nil &&
res != nil {
for _, log := range res.Logs {
proc.evm.Config.Tracer.OnLog(log)
}
}
return res, nil
}
// BatchRunTransactions runs a batch of EVM transactions
func (bl *BlockView) BatchRunTransactions(txs []*gethTypes.Transaction) ([]*types.Result, error) {
batchResults := make([]*types.Result, len(txs))
// create a new procedure
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
for i, tx := range txs {
msg, err := gethCore.TransactionToMessage(
tx,
GetSigner(bl.config),
proc.config.BlockContext.BaseFee)
if err != nil {
batchResults[i] = types.NewInvalidResult(tx, err)
continue
}
// call tracer on tx start
if proc.evm.Config.Tracer != nil && proc.evm.Config.Tracer.OnTxStart != nil {
proc.evm.Config.Tracer.OnTxStart(proc.evm.GetVMContext(), tx, msg.From)
}
// run msg
res, err := proc.run(msg, tx.Hash(), tx.Type())
if err != nil {
return nil, err
}
// all commit errors (StateDB errors) has to be returned
res.StateChangeCommitment, err = proc.commit(false)
if err != nil {
return nil, err
}
// this clears state for any subsequent transaction runs
proc.state.Reset()
// collect result
batchResults[i] = res
// call tracer on tx end
if proc.evm.Config.Tracer != nil &&
proc.evm.Config.Tracer.OnTxEnd != nil &&
res != nil {
proc.evm.Config.Tracer.OnTxEnd(res.Receipt(), res.ValidationError)
}
// call OnLog tracer hook, upon successful tx result
if proc.evm.Config.Tracer != nil &&
proc.evm.Config.Tracer.OnLog != nil &&
res != nil {
for _, log := range res.Logs {
proc.evm.Config.Tracer.OnLog(log)
}
}
}
// finalize after all the batch transactions are executed to save resources
if err := proc.state.Finalize(); err != nil {
return nil, err
}
return batchResults, nil
}
// DryRunTransaction runs an unsigned transaction without persisting the state
func (bl *BlockView) DryRunTransaction(
tx *gethTypes.Transaction,
from gethCommon.Address,
) (*types.Result, error) {
// create a new procedure
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
// convert tx into message
msg, err := gethCore.TransactionToMessage(
tx,
GetSigner(bl.config),
proc.config.BlockContext.BaseFee,
)
// we can ignore invalid signature errors since we don't expect signed transactions
if !errors.Is(err, gethTypes.ErrInvalidSig) {
return nil, err
}
// use the from as the signer
msg.From = from
// we need to skip nonce check for dry run
// Previous we had: msg.SkipAccountChecks = true,
// so we need to set SkipNonceChecks, SkipFromEOACheck both to true.
msg.SkipNonceChecks = true
msg.SkipFromEOACheck = true
// run and return without committing the state changes
return proc.run(msg, tx.Hash(), tx.Type())
}
func (bl *BlockView) newProcedure() (*procedure, error) {
execState, err := state.NewStateDB(bl.ledger, bl.rootAddr)
if err != nil {
return nil, err
}
cfg := bl.config
return &procedure{
config: cfg,
evm: gethVM.NewEVM(
*cfg.BlockContext,
*cfg.TxContext,
execState,
cfg.ChainConfig,
cfg.EVMConfig,
),
state: execState,
}, nil
}
type procedure struct {
config *Config
evm *gethVM.EVM
state types.StateDB
}
// commit commits the changes to the state (with optional finalization)
func (proc *procedure) commit(finalize bool) (hash.Hash, error) {
stateUpdateCommitment, err := proc.state.Commit(finalize)
if err != nil {
// if known types (state errors) don't do anything and return
if types.IsAFatalError(err) || types.IsAStateError(err) || types.IsABackendError(err) {
return stateUpdateCommitment, err
}
// else is a new fatal error
return stateUpdateCommitment, types.NewFatalError(err)
}
return stateUpdateCommitment, nil
}
func (proc *procedure) mintTo(
call *types.DirectCall,
) (*types.Result, error) {
// convert and check value
isValid, value := convertAndCheckValue(call.Value)
if !isValid {
return types.NewInvalidResult(
call.Transaction(),
types.ErrInvalidBalance), nil
}
// create bridge account if not exist
bridge := call.From.ToCommon()
if !proc.state.Exist(bridge) {
proc.state.CreateAccount(bridge)
}
// add balance to the bridge account before transfer
proc.state.AddBalance(bridge, value, gethTracing.BalanceIncreaseWithdrawal)
// check state errors
if err := proc.state.Error(); err != nil {
return nil, err
}
// withdraw the amount and move it to the bridge account
res, err := proc.run(call.Message(), call.Hash(), types.DirectCallTxType)
if err != nil {
return res, err
}
// if any error (invalid or vm) on the internal call, revert and don't commit any change
// this prevents having cases that we add balance to the bridge but the transfer
// fails due to gas, etc.
if res.Invalid() || res.Failed() {
// reset the state to revert the add balances
proc.state.Reset()
return res, nil
}
// commit and finalize the state and return any stateDB error
res.StateChangeCommitment, err = proc.commit(true)
return res, err
}
func (proc *procedure) withdrawFrom(
call *types.DirectCall,
) (*types.Result, error) {
// convert and check value
isValid, value := convertAndCheckValue(call.Value)
if !isValid {
return types.NewInvalidResult(
call.Transaction(),
types.ErrInvalidBalance), nil
}
// check balance is not prone to rounding error
if types.BalanceConversionToUFix64ProneToRoundingError(call.Value) {
return types.NewInvalidResult(
call.Transaction(),
types.ErrWithdrawBalanceRounding), nil
}
// create bridge account if not exist
bridge := call.To.ToCommon()
if !proc.state.Exist(bridge) {
proc.state.CreateAccount(bridge)
}
// withdraw the amount and move it to the bridge account
res, err := proc.run(call.Message(), call.Hash(), types.DirectCallTxType)
if err != nil {
return res, err
}
// if any error (invalid or vm) on the internal call, revert and don't commit any change
// this prevents having cases that we deduct the balance from the account
// but doesn't return it as a vault.
if res.Invalid() || res.Failed() {
// reset the state to revert the add balances
proc.state.Reset()
return res, nil
}
// now deduct the balance from the bridge
proc.state.SubBalance(bridge, value, gethTracing.BalanceIncreaseWithdrawal)
// commit and finalize the state and return any stateDB error
res.StateChangeCommitment, err = proc.commit(true)
return res, err
}
// deployAt deploys a contract at the given target address
// behavior should be similar to what evm.create internal method does with
// a few differences, we don't need to check for previous forks given this
// functionality was not available to anyone, we don't need to
// follow snapshoting, given we do commit/revert style in this code base.
// in the future we might optimize this method accepting deploy-ready byte codes
// and skip interpreter call, gas calculations and many checks.
func (proc *procedure) deployAt(
call *types.DirectCall,
) (*types.Result, error) {
// convert and check value
isValid, castedValue := convertAndCheckValue(call.Value)
if !isValid {
return types.NewInvalidResult(
call.Transaction(),
types.ErrInvalidBalance), nil
}
txHash := call.Hash()
res := &types.Result{
TxType: types.DirectCallTxType,
TxHash: txHash,
}
if proc.evm.Config.Tracer != nil {
tracer := proc.evm.Config.Tracer
if tracer.OnEnter != nil {
tracer.OnEnter(0, byte(gethVM.CREATE2), call.From.ToCommon(), call.To.ToCommon(), call.Data, call.GasLimit, call.Value)
}
if tracer.OnGasChange != nil {
tracer.OnGasChange(0, call.GasLimit, gethTracing.GasChangeCallInitialBalance)
}
defer func() {
if call.GasLimit != 0 && tracer.OnGasChange != nil {
tracer.OnGasChange(call.GasLimit, 0, gethTracing.GasChangeCallLeftOverReturned)
}
if tracer.OnExit != nil {
var reverted bool
if res.VMError != nil && !errors.Is(res.VMError, gethVM.ErrCodeStoreOutOfGas) {
reverted = true
}
tracer.OnExit(0, res.ReturnedData, call.GasLimit-res.GasConsumed, gethVM.VMErrorFromErr(res.VMError), reverted)
}
}()
}
addr := call.To.ToCommon()
// pre check 1 - check balance of the source
if call.Value.Sign() != 0 &&
!proc.evm.Context.CanTransfer(proc.state, call.From.ToCommon(), castedValue) {
res.SetValidationError(gethCore.ErrInsufficientFundsForTransfer)
return res, nil
}
// pre check 2 - ensure there's no existing eoa or contract is deployed at the address
contractHash := proc.state.GetCodeHash(addr)
if proc.state.GetNonce(addr) != 0 ||
(contractHash != (gethCommon.Hash{}) && contractHash != gethTypes.EmptyCodeHash) {
res.VMError = gethVM.ErrContractAddressCollision
return res, nil
}
callerCommon := call.From.ToCommon()
// setup caller if doesn't exist
if !proc.state.Exist(callerCommon) {
proc.state.CreateAccount(callerCommon)
}
// increment the nonce for the caller
proc.state.SetNonce(callerCommon, proc.state.GetNonce(callerCommon)+1)
// setup account
proc.state.CreateAccount(addr)
proc.state.SetNonce(addr, 1) // (EIP-158)
if call.Value.Sign() > 0 {
proc.evm.Context.Transfer( // transfer value
proc.state,
callerCommon,
addr,
uint256.MustFromBig(call.Value),
)
}
// run code through interpreter
// this would check for errors and computes the final bytes to be stored under account
var err error
inter := gethVM.NewEVMInterpreter(proc.evm)
contract := gethVM.NewContract(
gethVM.AccountRef(callerCommon),
gethVM.AccountRef(addr),
castedValue,
call.GasLimit)
contract.SetCallCode(&addr, gethCrypto.Keccak256Hash(call.Data), call.Data)
// update access list (Berlin)
proc.state.AddAddressToAccessList(addr)
ret, err := inter.Run(contract, nil, false)
gasCost := uint64(len(ret)) * gethParams.CreateDataGas
res.GasConsumed = gasCost
// handle errors
if err != nil {
// for all errors except this one consume all the remaining gas (Homestead)
if err != gethVM.ErrExecutionReverted {
res.GasConsumed = call.GasLimit
}
res.VMError = err
return res, nil
}
// update gas usage
if gasCost > call.GasLimit {
// consume all the remaining gas (Homestead)
res.GasConsumed = call.GasLimit
res.VMError = gethVM.ErrCodeStoreOutOfGas
return res, nil
}
// check max code size (EIP-158)
if len(ret) > gethParams.MaxCodeSize {
// consume all the remaining gas (Homestead)
res.GasConsumed = call.GasLimit
res.VMError = gethVM.ErrMaxCodeSizeExceeded
return res, nil
}
// reject code starting with 0xEF (EIP-3541)
if len(ret) >= 1 && ret[0] == 0xEF {
// consume all the remaining gas (Homestead)
res.GasConsumed = call.GasLimit
res.VMError = gethVM.ErrInvalidCode
return res, nil
}
res.DeployedContractAddress = &call.To
res.CumulativeGasUsed = proc.config.BlockTotalGasUsedSoFar + res.GasConsumed
proc.state.SetCode(addr, ret)
res.StateChangeCommitment, err = proc.commit(true)
return res, err
}
func (proc *procedure) runDirect(
call *types.DirectCall,
) (*types.Result, error) {
// run the msg
res, err := proc.run(
call.Message(),
call.Hash(),
types.DirectCallTxType,
)
if err != nil {
return nil, err
}
// commit and finalize the state and return any stateDB error
res.StateChangeCommitment, err = proc.commit(true)
return res, err
}
// run runs a geth core.message and returns the
// results, any validation or execution errors
// are captured inside the result, the remaining
// return errors are errors requires extra handling
// on upstream (e.g. backend errors).
func (proc *procedure) run(
msg *gethCore.Message,
txHash gethCommon.Hash,
txType uint8,
) (*types.Result, error) {
var err error
res := types.Result{
TxType: txType,
TxHash: txHash,
}
// Negative values are not acceptable
// although we check this condition on direct calls
// its worth an extra check here given some calls are
// coming from batch run, etc.
if msg.Value.Sign() < 0 {
res.SetValidationError(types.ErrInvalidBalance)
return &res, nil
}
// set the origin on the TxContext
proc.evm.TxContext.Origin = msg.From
// reset precompile tracking in case
proc.config.PCTracker.Reset()
// Set gas pool based on block gas limit
// if the block gas limit is set to anything than max
// we need to update this code.
gasPool := (*gethCore.GasPool)(&proc.config.BlockContext.GasLimit)
// transit the state
execResult, err := gethCore.NewStateTransition(
proc.evm,
msg,
gasPool,
).TransitionDb()
if err != nil {
// if the error is a fatal error or a non-fatal state error or a backend err return it
// this condition should never happen given all StateDB errors are withheld for the commit time.
if types.IsAFatalError(err) || types.IsAStateError(err) || types.IsABackendError(err) {
return nil, err
}
// otherwise is a validation error (pre-check failure)
// no state change, wrap the error and return
res.SetValidationError(err)
return &res, nil
}
txIndex := proc.config.BlockTxCountSoFar
// if pre-checks are passed, the exec result won't be nil
if execResult != nil {
res.GasConsumed = execResult.UsedGas
res.GasRefund = proc.state.GetRefund()
res.Index = uint16(txIndex)
res.CumulativeGasUsed = execResult.UsedGas + proc.config.BlockTotalGasUsedSoFar
res.PrecompiledCalls, err = proc.config.PCTracker.CapturedCalls()
if err != nil {
return nil, err
}
// we need to capture the returned value no matter the status
// if the tx is reverted the error message is returned as returned value
res.ReturnedData = execResult.ReturnData
// Update proc context
proc.config.BlockTotalGasUsedSoFar = res.CumulativeGasUsed
proc.config.BlockTxCountSoFar += 1
if !execResult.Failed() { // collect vm errors
// If the transaction has created a contract,
// store the creation address in the receipt
if msg.To == nil {
deployedAddress := types.NewAddress(gethCrypto.CreateAddress(msg.From, msg.Nonce))
res.DeployedContractAddress = &deployedAddress
}
// collect logs
res.Logs = proc.state.Logs(
proc.config.BlockContext.BlockNumber.Uint64(),
txHash,
txIndex,
)
} else {
// execResult.Err is VM errors (we don't return it as error)
res.VMError = execResult.Err
}
}
return &res, nil
}
func convertAndCheckValue(input *big.Int) (isValid bool, converted *uint256.Int) {
// check for negative input
if input.Sign() < 0 {
return false, nil
}
// convert value into uint256
value, overflow := uint256.FromBig(input)
if overflow {
return true, nil
}
return true, value
}