@@ -138,7 +138,7 @@ type (
138
138
// EstimateGasForNonExecution estimates action gas except execution
139
139
EstimateGasForNonExecution (action.Action ) (uint64 , error )
140
140
// EstimateExecutionGasConsumption estimate gas consumption for execution action
141
- EstimateExecutionGasConsumption (ctx context.Context , sc * action.Execution , callerAddr address.Address ) (uint64 , error )
141
+ EstimateExecutionGasConsumption (ctx context.Context , sc * action.Execution , callerAddr address.Address , opts ... protocol. SimulateOption ) (uint64 , error )
142
142
// LogsInBlockByHash filter logs in the block by hash
143
143
LogsInBlockByHash (filter * logfilter.LogFilter , blockHash hash.Hash256 ) ([]* action.Log , error )
144
144
// LogsInRange filter logs among [start, end] blocks
@@ -1513,11 +1513,22 @@ func (core *coreService) EstimateMigrateStakeGasConsumption(ctx context.Context,
1513
1513
GasLimit : g .BlockGasLimitByHeight (header .Height () + 1 ),
1514
1514
Producer : zeroAddr ,
1515
1515
})
1516
+
1516
1517
exec , err := staking .FindProtocol (core .registry ).ConstructExecution (ctx , ms , core .sf )
1517
1518
if err != nil {
1518
1519
return 0 , err
1519
1520
}
1520
- gas , err := core .EstimateExecutionGasConsumption (ctx , exec , caller )
1521
+ gas , err := core .EstimateExecutionGasConsumption (ctx , exec , caller , protocol .WithSimulatePreOpt (func (sm protocol.StateManager ) error {
1522
+ // add amount to the sender account
1523
+ sender , err := accountutil .LoadAccount (sm , caller )
1524
+ if err != nil {
1525
+ return err
1526
+ }
1527
+ if err = sender .AddBalance (exec .Amount ()); err != nil {
1528
+ return err
1529
+ }
1530
+ return accountutil .StoreAccount (sm , caller , sender )
1531
+ }))
1521
1532
if err != nil {
1522
1533
return 0 , err
1523
1534
}
@@ -1529,7 +1540,7 @@ func (core *coreService) EstimateMigrateStakeGasConsumption(ctx context.Context,
1529
1540
}
1530
1541
1531
1542
// EstimateExecutionGasConsumption estimate gas consumption for execution action
1532
- func (core * coreService ) EstimateExecutionGasConsumption (ctx context.Context , sc * action.Execution , callerAddr address.Address ) (uint64 , error ) {
1543
+ func (core * coreService ) EstimateExecutionGasConsumption (ctx context.Context , sc * action.Execution , callerAddr address.Address , opts ... protocol. SimulateOption ) (uint64 , error ) {
1533
1544
ctx = genesis .WithGenesisContext (ctx , core .bc .Genesis ())
1534
1545
state , err := accountutil .AccountState (ctx , core .sf , callerAddr )
1535
1546
if err != nil {
@@ -1552,7 +1563,7 @@ func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, sc
1552
1563
blockGasLimit = g .BlockGasLimitByHeight (core .bc .TipHeight ())
1553
1564
)
1554
1565
sc .SetGasLimit (blockGasLimit )
1555
- enough , receipt , err := core .isGasLimitEnough (ctx , callerAddr , sc )
1566
+ enough , receipt , err := core .isGasLimitEnough (ctx , callerAddr , sc , opts ... )
1556
1567
if err != nil {
1557
1568
return 0 , status .Error (codes .Internal , err .Error ())
1558
1569
}
@@ -1564,7 +1575,7 @@ func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, sc
1564
1575
}
1565
1576
estimatedGas := receipt .GasConsumed
1566
1577
sc .SetGasLimit (estimatedGas )
1567
- enough , _ , err = core .isGasLimitEnough (ctx , callerAddr , sc )
1578
+ enough , _ , err = core .isGasLimitEnough (ctx , callerAddr , sc , opts ... )
1568
1579
if err != nil && err != action .ErrInsufficientFunds {
1569
1580
return 0 , status .Error (codes .Internal , err .Error ())
1570
1581
}
@@ -1574,7 +1585,7 @@ func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, sc
1574
1585
for low <= high {
1575
1586
mid := (low + high ) / 2
1576
1587
sc .SetGasLimit (mid )
1577
- enough , _ , err = core .isGasLimitEnough (ctx , callerAddr , sc )
1588
+ enough , _ , err = core .isGasLimitEnough (ctx , callerAddr , sc , opts ... )
1578
1589
if err != nil && err != action .ErrInsufficientFunds {
1579
1590
return 0 , status .Error (codes .Internal , err .Error ())
1580
1591
}
@@ -1594,6 +1605,7 @@ func (core *coreService) isGasLimitEnough(
1594
1605
ctx context.Context ,
1595
1606
caller address.Address ,
1596
1607
sc * action.Execution ,
1608
+ opts ... protocol.SimulateOption ,
1597
1609
) (bool , * action.Receipt , error ) {
1598
1610
ctx , span := tracer .NewSpan (ctx , "Server.isGasLimitEnough" )
1599
1611
defer span .End ()
@@ -1602,7 +1614,7 @@ func (core *coreService) isGasLimitEnough(
1602
1614
return false , nil , err
1603
1615
}
1604
1616
1605
- _ , receipt , err := core .simulateExecution (ctx , caller , sc , core .dao .GetBlockHash , core .getBlockTime )
1617
+ _ , receipt , err := core .simulateExecution (ctx , caller , sc , core .dao .GetBlockHash , core .getBlockTime , opts ... )
1606
1618
if err != nil {
1607
1619
return false , nil , err
1608
1620
}
@@ -1908,14 +1920,14 @@ func (core *coreService) traceTx(ctx context.Context, txctx *tracers.Context, co
1908
1920
return retval , receipt , tracer , err
1909
1921
}
1910
1922
1911
- func (core * coreService ) simulateExecution (ctx context.Context , addr address.Address , exec * action.Execution , getBlockHash evm.GetBlockHash , getBlockTime evm.GetBlockTime ) ([]byte , * action.Receipt , error ) {
1923
+ func (core * coreService ) simulateExecution (ctx context.Context , addr address.Address , exec * action.Execution , getBlockHash evm.GetBlockHash , getBlockTime evm.GetBlockTime , opts ... protocol. SimulateOption ) ([]byte , * action.Receipt , error ) {
1912
1924
ctx = evm .WithHelperCtx (ctx , evm.HelperContext {
1913
1925
GetBlockHash : getBlockHash ,
1914
1926
GetBlockTime : getBlockTime ,
1915
1927
DepositGasFunc : rewarding .DepositGasWithSGD ,
1916
1928
Sgd : core .sgdIndexer ,
1917
1929
})
1918
- return core .sf .SimulateExecution (ctx , addr , exec )
1930
+ return core .sf .SimulateExecution (ctx , addr , exec , opts ... )
1919
1931
}
1920
1932
1921
1933
func filterReceipts (receipts []* action.Receipt , actHash hash.Hash256 ) * action.Receipt {
0 commit comments