Skip to content

Commit

Permalink
fix: div by 0 gas ratio error
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemmsilva committed Feb 21, 2024
1 parent f0473bc commit db36152
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/vm/gas/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ var DefaultEVMGasRatio = util.Ratio32{A: 1, B: 1}

func ISCGasBudgetToEVM(iscGasBudget uint64, gasRatio *util.Ratio32) uint64 {
// EVM gas budget = floor(ISC gas budget * B / A)
if gasRatio.A == 0 {
return 0
}
return gasRatio.YFloor64(iscGasBudget)
}

func ISCGasBurnedToEVM(iscGasBurned uint64, gasRatio *util.Ratio32) uint64 {
// estimated EVM gas = ceil(ISC gas burned * B / A)
if gasRatio.A == 0 {
return 0
}
return gasRatio.YCeil64(iscGasBurned)
}

func EVMGasToISC(evmGas uint64, gasRatio *util.Ratio32) uint64 {
// ISC gas burned = ceil(EVM gas * A / B)
if gasRatio.A == 0 {
return 0
}
return gasRatio.XCeil64(evmGas)
}

Expand Down
37 changes: 36 additions & 1 deletion tools/cluster/tests/legacy_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/kv/codec"
"github.com/iotaledger/wasp/packages/legacymigration"
"github.com/iotaledger/wasp/packages/vm/core/accounts"
"github.com/iotaledger/wasp/packages/vm/core/evm/iscmagic"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/tools/cluster/templates"
)

Expand Down Expand Up @@ -71,11 +73,44 @@ func TestMigrationAndBurn(t *testing.T) {
require.NoError(t, err)
migrationEnv := newChainEnv(t, migrationCluster, migrationChain)

// set gas fee to 0x000000
migrationOriginatorClient := migrationChain.Client(migrationEnv.Clu.OriginatorKeyPair)
req, err := migrationOriginatorClient.PostOffLedgerRequest(
context.Background(),
governance.Contract.Hname(),
governance.FuncSetFeePolicy.Hname(),
chainclient.PostRequestParams{
Args: map[kv.Key][]byte{
governance.ParamFeePolicyBytes: lo.Must(iotago.DecodeHex("0x0000000000000000000000000000000000")),
},
},
)
require.NoError(t, err)
_, err = migrationChain.AllNodesMultiClient().WaitUntilRequestProcessedSuccessfully(migrationChain.ChainID, req.ID(), false, 30*time.Second)
require.NoError(t, err)

// fill the migration contract with funds
someWallet, _, err := migrationCluster.NewKeyPairWithFunds()
require.NoError(t, err)
// deposit with transfer == allowance
someWalletMigratorChainClient := migrationChain.Client(someWallet)
transfer := isc.NewAssetsBaseTokens(100 * isc.Million)
migrationContractAgentID := isc.NewContractAgentID(migrationChain.ChainID, legacymigration.Contract.Hname())
depositToAgentID(t, migrationChain, 100*isc.Million, migrationContractAgentID, someWallet)
tx, err := someWalletMigratorChainClient.Post1Request(
accounts.Contract.Hname(),
accounts.FuncTransferAllowanceTo.Hname(),
chainclient.PostRequestParams{
Transfer: transfer,
Args: map[kv.Key][]byte{
accounts.ParamAgentID: codec.EncodeAgentID(migrationContractAgentID),
},
Allowance: transfer,
},
)
require.NoError(t, err)
_, err = migrationChain.AllNodesMultiClient().WaitUntilAllRequestsProcessedSuccessfully(migrationChain.ChainID, tx, false, 30*time.Second)
require.NoError(t, err)

migBalance := migrationEnv.getBalanceOnChain(migrationContractAgentID, isc.BaseTokenID)
require.Positive(t, migBalance)

Expand Down

0 comments on commit db36152

Please sign in to comment.