Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change gas for emit event as linear #2760

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions contracts/wasm/testcore/go/testcoreimpl/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const (

MsgDoNothing = "========== doing nothing"
MsgFailOnPurpose = "failing on purpose"
MsgFullPanic = "========== panic FULL ENTRY POINT ========="
MsgFullPanic = "========== panic FULL ENTRY POINT =========="
MsgJustView = "calling empty view entry point"

MsgViewPanic = "========== panic VIEW ========="
MsgViewPanic = "========== panic VIEW =========="
)

func funcCallOnChain(ctx wasmlib.ScFuncContext, f *CallOnChainContext) {
Expand Down Expand Up @@ -242,14 +242,14 @@ func funcWithdrawFromChain(ctx wasmlib.ScFuncContext, f *WithdrawFromChainContex
transfer := wasmlib.ScTransferFromBalances(ctx.Allowance())
ctx.TransferAllowed(ctx.AccountID(), transfer)

// This is just a test contract, but normally these numbers should
// be parameters because there is no way for the contract to figure
// out the gas fees on the other chain, and it's also silly to run
// the costly calculation to determine storage deposit every time
// unless absolutely necessary. Better to just make sure that the
// storage deposit is large enough, since it will be returned anyway.
const gasFee = wasmlib.MinGasFee
const gasReserve = wasmlib.MinGasFee
var gasReserveTransferAccountToChain = wasmlib.MinGasFee
if f.Params.GasReserveTransferAccountToChain().Exists() {
gasReserveTransferAccountToChain = f.Params.GasReserveTransferAccountToChain().Value()
}
var gasReserve = wasmlib.MinGasFee
if f.Params.GasReserve().Exists() {
gasReserve = f.Params.GasReserve().Value()
}
const storageDeposit = wasmlib.StorageDeposit

// note: gasReserve is the gas necessary to run accounts.transferAllowanceTo
Expand All @@ -258,7 +258,7 @@ func funcWithdrawFromChain(ctx wasmlib.ScFuncContext, f *WithdrawFromChainContex
// NOTE: make sure you READ THE DOCS before calling this function
xfer := coreaccounts.ScFuncs.TransferAccountToChain(ctx)
xfer.Params.GasReserve().SetValue(gasReserve)
xfer.Func.TransferBaseTokens(storageDeposit + gasFee + gasReserve).
xfer.Func.TransferBaseTokens(storageDeposit + gasReserveTransferAccountToChain + gasReserve).
AllowanceBaseTokens(withdrawal + storageDeposit + gasReserve).
PostToChain(targetChain)
}
Expand Down
23 changes: 12 additions & 11 deletions contracts/wasm/testcore/rs/testcoreimpl/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::*;

const CONTRACT_NAME_DEPLOYED: &str = "exampleDeployTR";
const MSG_CORE_ONLY_PANIC: &str = "========== core only =========";
const MSG_FULL_PANIC: &str = "========== panic FULL ENTRY POINT =========";
const MSG_VIEW_PANIC: &str = "========== panic VIEW =========";
const MSG_FULL_PANIC: &str = "========== panic FULL ENTRY POINT ==========";
const MSG_VIEW_PANIC: &str = "========== panic VIEW ==========";

pub fn func_call_on_chain(ctx: &ScFuncContext, f: &CallOnChainContext) {
let param_int = f.params.n().value();
Expand Down Expand Up @@ -255,14 +255,15 @@ pub fn func_withdraw_from_chain(ctx: &ScFuncContext, f: &WithdrawFromChainContex
let transfer = ScTransfer::from_balances(&ctx.allowance());
ctx.transfer_allowed(&ctx.account_id(), &transfer);

// This is just a test contract, but normally these numbers should
// be parameters because there is no way for the contract to figure
// out the gas fees on the other chain, and it's also silly to run
// the costly calculation to determine storage deposit every time
// unless absolutely necessary. Better to just make sure that the
// storage deposit is large enough, since it will be returned anyway.
let gas_fee: u64 = MIN_GAS_FEE;
let gas_reserve: u64 = MIN_GAS_FEE;
let mut gas_reserve_transfer_account_to_chain: u64 = MIN_GAS_FEE;
if f.params.gas_reserve_transfer_account_to_chain().exists() {
gas_reserve_transfer_account_to_chain =
f.params.gas_reserve_transfer_account_to_chain().value();
}
let mut gas_reserve: u64 = MIN_GAS_FEE;
if f.params.gas_reserve().exists() {
gas_reserve = f.params.gas_reserve().value();
}
let storage_deposit: u64 = STORAGE_DEPOSIT;

// note: gasReserve is the gas necessary to run accounts.transferAllowanceTo
Expand All @@ -272,7 +273,7 @@ pub fn func_withdraw_from_chain(ctx: &ScFuncContext, f: &WithdrawFromChainContex
let xfer = coreaccounts::ScFuncs::transfer_account_to_chain(ctx);
xfer.params.gas_reserve().set_value(gas_reserve);
xfer.func
.transfer_base_tokens(storage_deposit + gas_fee + gas_reserve)
.transfer_base_tokens(storage_deposit + gas_reserve_transfer_account_to_chain + gas_reserve)
.allowance_base_tokens(withdrawal + storage_deposit + gas_reserve)
.post_to_chain(target_chain);
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/wasm/testcore/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ funcs:
params:
chainID: ChainID
baseTokens: Uint64
gasReserve: Uint64?
gasReserveTransferAccountToChain: Uint64?

views:
checkContextFromViewEP:
Expand Down
17 changes: 11 additions & 6 deletions contracts/wasm/testcore/test/2chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,16 @@ func Test2Chains(t *testing.T) {

// allowance for accounts.transferAccountToChain(): SD + GAS1 + GAS2
xferDeposit := wasmlib.StorageDeposit
xferAllowance := xferDeposit + wasmlib.MinGasFee + wasmlib.MinGasFee
const gasFeeTransferAccountToChain = 10 * wasmlib.MinGasFee
const gasReserve = 10 * wasmlib.MinGasFee
const gasWithdrawFromChain = 10 * wasmlib.MinGasFee
xferAllowance := xferDeposit + gasReserve + gasFeeTransferAccountToChain
f := testcore.ScFuncs.WithdrawFromChain(ctx2.Sign(user))
f.Params.ChainID().SetValue(ctx1.CurrentChainID())
f.Params.BaseTokens().SetValue(withdrawalAmount)
f.Func.TransferBaseTokens(xferAllowance + isc.Million).
f.Params.GasReserve().SetValue(gasReserve)
f.Params.GasReserveTransferAccountToChain().SetValue(gasFeeTransferAccountToChain)
f.Func.TransferBaseTokens(xferAllowance + gasWithdrawFromChain).
AllowanceBaseTokens(xferAllowance).Post()
require.NoError(t, ctx2.Err)

Expand All @@ -153,21 +158,21 @@ func Test2Chains(t *testing.T) {
// chain2.testcore account will be credited with SD+GAS1+GAS2, pay actual GAS1,
// and be debited by SD+GAS2+'withdrawalAmount'
bal1.UpdateFeeBalances(ctxAcc1.GasFee)
bal1.Add(testcore2, xferDeposit+wasmlib.MinGasFee+wasmlib.MinGasFee-ctxAcc1.GasFee-xferDeposit-wasmlib.MinGasFee-withdrawalAmount)
bal1.Add(testcore2, xferDeposit+gasWithdrawFromChain+gasWithdrawFromChain-ctxAcc1.GasFee-xferDeposit-gasReserve-withdrawalAmount)
// verify these changes against the actual chain1 account balances
bal1.VerifyBalances(t)

userL1 -= xferAllowance + isc.Million
userL1 -= xferAllowance + gasWithdrawFromChain
require.Equal(t, userL1, user.Balance())

// The gas fees will be credited to chain1.Originator
bal2.UpdateFeeBalances(withdrawalReceipt.GasFeeCharged)
bal2.UpdateFeeBalances(transferReceipt.GasFeeCharged)
// deduct coretest.WithdrawFromChain() gas fee from user's cool million
bal2.Add(user, isc.Million-withdrawalReceipt.GasFeeCharged)
bal2.Add(user, gasWithdrawFromChain-withdrawalReceipt.GasFeeCharged)
// chain2.accounts1 will be credited with SD+GAS2+'withdrawalAmount', pay actual GAS2,
// and be debited by SD+'withdrawalAmount', leaving zero
bal2.Add(accounts1, xferDeposit+wasmlib.MinGasFee+withdrawalAmount-transferReceipt.GasFeeCharged-xferDeposit-withdrawalAmount)
bal2.Add(accounts1, xferDeposit+gasReserve+withdrawalAmount-transferReceipt.GasFeeCharged-xferDeposit-withdrawalAmount)
// chain2.testcore account receives the withdrawn tokens and storage deposit
bal2.Account += withdrawalAmount + xferDeposit
// verify these changes against the actual chain2 account balances
Expand Down
22 changes: 11 additions & 11 deletions contracts/wasm/testcore/ts/testcoreimpl/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import * as sc from "../testcore/index";

const CONTRACT_NAME_DEPLOYED = "exampleDeployTR";
const MSG_CORE_ONLY_PANIC = "========== core only =========";
const MSG_FULL_PANIC = "========== panic FULL ENTRY POINT =========";
const MSG_VIEW_PANIC = "========== panic VIEW =========";
const MSG_FULL_PANIC = "========== panic FULL ENTRY POINT ==========";
const MSG_VIEW_PANIC = "========== panic VIEW ==========";

export function funcCallOnChain(ctx: wasmlib.ScFuncContext, f: sc.CallOnChainContext): void {
let paramInt = f.params.n().value();
Expand Down Expand Up @@ -228,14 +228,14 @@ export function funcWithdrawFromChain(ctx: wasmlib.ScFuncContext, f: sc.Withdraw
const transfer = wasmlib.ScTransfer.fromBalances(ctx.allowance());
ctx.transferAllowed(ctx.accountID(), transfer);

// This is just a test contract, but normally these numbers should
// be parameters because there is no way for the contract to figure
// out the gas fees on the other chain, and it's also silly to run
// the costly calculation to determine storage deposit every time
// unless absolutely necessary. Better to just make sure that the
// storage deposit is large enough, since it will be returned anyway.
const gasFee: u64 = wasmlib.MinGasFee;
const gasReserve: u64 = wasmlib.MinGasFee;
let gasReserveTransferAccountToChain: u64 = wasmlib.MinGasFee;
if (f.params.gasReserveTransferAccountToChain().exists()) {
gasReserveTransferAccountToChain = f.params.gasReserveTransferAccountToChain().value();
}
let gasReserve: u64 = wasmlib.MinGasFee;
if (f.params.gasReserve().exists()) {
gasReserve = f.params.gasReserve().value();
}
const storageDeposit: u64 = wasmlib.StorageDeposit;

// note: gasReserve is the gas necessary to run accounts.transferAllowanceTo
Expand All @@ -244,7 +244,7 @@ export function funcWithdrawFromChain(ctx: wasmlib.ScFuncContext, f: sc.Withdraw
// NOTE: make sure you READ THE DOCS before calling this function
const xfer = coreaccounts.ScFuncs.transferAccountToChain(ctx);
xfer.params.gasReserve().setValue(gasReserve);
xfer.func.transferBaseTokens(storageDeposit + gasFee + gasReserve)
xfer.func.transferBaseTokens(storageDeposit + gasReserveTransferAccountToChain + gasReserve)
.allowanceBaseTokens(withdrawal + storageDeposit + gasReserve)
.postToChain(targetChain);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/chain/cons/cons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/iotaledger/wasp/packages/vm/core/accounts"
"github.com/iotaledger/wasp/packages/vm/core/coreprocessors"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
"github.com/iotaledger/wasp/packages/vm/gas"
"github.com/iotaledger/wasp/packages/vm/processors"
"github.com/iotaledger/wasp/packages/vm/vmimpl"
)
Expand Down Expand Up @@ -346,7 +347,7 @@ func testChained(t *testing.T, n, f, b int) {
inccounter.FuncIncCounter.Hname(),
dict.New(),
uint64(i*reqPerBlock+ii),
20000,
gas.LimitsDefault.MinGasPerRequest,
).Sign(scClient)
reqs = append(reqs, scRequest)
incTotal++
Expand Down
5 changes: 3 additions & 2 deletions packages/solo/ledgerl1l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ func (fp *foundryParams) CreateFoundry() (uint32, iotago.NativeTokenID, error) {
user = fp.user
}
req := CallParamsFromDict(accounts.Contract.Name, accounts.FuncFoundryCreateNew.Name, par).
WithAllowance(isc.NewAssetsBaseTokens(allowanceForFoundryStorageDeposit))
WithAllowance(isc.NewAssetsBaseTokens(allowanceForFoundryStorageDeposit)).
AddBaseTokens(allowanceForFoundryStorageDeposit)

gas, _, err := fp.ch.EstimateGasOnLedger(req, user, true)
if err != nil {
Expand Down Expand Up @@ -297,7 +298,7 @@ func (ch *Chain) DestroyTokensOnL1(nativeTokenID iotago.NativeTokenID, amount in
return err
}

// DepositAssetsToL2 deposits ftokens on user's on-chain account
// DepositAssetsToL2 deposits ftokens on user's on-chain account, if user is nil, then chain owner is assigned
func (ch *Chain) DepositAssetsToL2(assets *isc.Assets, user *cryptolib.KeyPair) error {
_, err := ch.PostRequestSync(
NewCallParams(accounts.Contract.Name, accounts.FuncDeposit.Name).
Expand Down
4 changes: 2 additions & 2 deletions packages/testutil/testchain/test_chain_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (tcl *TestChainLedger) MakeTxAccountsDeposit(account *cryptolib.KeyPair) []
Metadata: &isc.SendMetadata{
TargetContract: accounts.Contract.Hname(),
EntryPoint: accounts.FuncDeposit.Hname(),
GasBudget: 10_000,
GasBudget: 2 * gas.LimitsDefault.MinGasPerRequest,
},
},
},
Expand Down Expand Up @@ -117,7 +117,7 @@ func (tcl *TestChainLedger) MakeTxDeployIncCounterContract() []isc.Request {
root.ParamName: inccounter.Contract.Name,
inccounter.VarCounter: 0,
}),
GasBudget: 10_000,
GasBudget: 2 * gas.LimitsDefault.MinGasPerRequest,
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/core/accounts/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm"
"github.com/iotaledger/wasp/packages/vm/core/errors/coreerrors"
"github.com/iotaledger/wasp/packages/vm/gas"
)

func CommonAccount() isc.AgentID {
Expand Down Expand Up @@ -175,15 +176,15 @@ func transferAccountToChain(ctx isc.Sandbox) dict.Dict {
assets := allowance.Clone()

// deduct the gas reserve GAS2 from the allowance, if possible
gasReserve := ctx.Params().MustGetUint64(ParamGasReserve, 100)
gasReserve := ctx.Params().MustGetUint64(ParamGasReserve, gas.LimitsDefault.MinGasPerRequest)
if allowance.BaseTokens < gasReserve {
panic(ErrNotEnoughAllowance)
}
allowance.BaseTokens -= gasReserve

// Warning: this will transfer all assets into the accounts core contract's L2 account.
// Be sure everything transfers out again, or assets will be stuck forever.
_ = ctx.TransferAllowedFunds(ctx.AccountID())
ctx.TransferAllowedFunds(ctx.AccountID())

// Send the specified assets, which should include GAS2 and SD, as part of the
// accounts.TransferAllowanceTo() request on the origin chain.
Expand Down
8 changes: 6 additions & 2 deletions packages/vm/core/evm/evmimpl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,15 @@ func registerERC20NativeTokenOnRemoteChain(ctx isc.Sandbox) dict.Dict {
evm.FieldTokenDecimals: codec.EncodeUint8(decimals),
evm.FieldFoundryTokenScheme: codec.EncodeTokenScheme(tokenScheme),
},
// FIXME why does this gas budget is higher than the allowance below
GasBudget: 50 * gas.LimitsDefault.MinGasPerRequest,
},
}
sd := ctx.EstimateRequiredStorageDeposit(req)
ctx.TransferAllowedFunds(ctx.AccountID(), isc.NewAssetsBaseTokens(sd))
req.Assets.AddBaseTokens(sd)
// this request is sent by contract account,
// so we move enough allowance for the gas fee below in the req.Assets.AddBaseTokens() function call
ctx.TransferAllowedFunds(ctx.AccountID(), isc.NewAssetsBaseTokens(sd+10*gas.LimitsDefault.MinGasPerRequest))
req.Assets.AddBaseTokens(sd + 10*gas.LimitsDefault.MinGasPerRequest)
ctx.Send(req)

return nil
Expand Down
6 changes: 5 additions & 1 deletion packages/vm/core/evm/evmtest/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ func (e *soloChainEnv) registerERC20ExternalNativeToken(
evm.FieldTokenTickerSymbol: codec.EncodeString(tokenTickerSymbol),
evm.FieldTokenDecimals: codec.EncodeUint8(tokenDecimals),
evm.FieldTargetAddress: codec.EncodeAddress(e.soloChain.ChainID.AsAddress()),
}).WithMaxAffordableGasBudget().WithAllowance(isc.NewAssetsBaseTokens(1*isc.Million)), fromChain.OriginatorPrivateKey)
}).
// to cover sd and gas fee for the 'FuncRegisterERC20ExternalNativeToken' func call in 'FuncRegisterERC20NativeTokenOnRemoteChain'
WithAllowance(isc.NewAssetsBaseTokens(20*gas.LimitsDefault.MinGasPerRequest)).
WithGasBudget(10*gas.LimitsDefault.MinGasPerRequest),
fromChain.OriginatorPrivateKey)
if err != nil {
return ret, err
}
Expand Down
22 changes: 16 additions & 6 deletions packages/vm/core/testcore/custom_onledger_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/iotaledger/wasp/packages/solo"
"github.com/iotaledger/wasp/packages/testutil/testmisc"
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/vm/core/accounts"
"github.com/iotaledger/wasp/packages/vm/gas"
)

func TestNoSenderFeature(t *testing.T) {
Expand All @@ -26,7 +28,9 @@ func TestNoSenderFeature(t *testing.T) {
// ----------------------------------------------------------------

// mint some NTs and withdraw them
err := ch.DepositAssetsToL2(isc.NewAssetsBaseTokens(10*isc.Million), wallet)
gasFee := 10 * gas.LimitsDefault.MinGasPerRequest
withdrawAmount := 3 * gas.LimitsDefault.MinGasPerRequest
err := ch.DepositAssetsToL2(isc.NewAssetsBaseTokens(withdrawAmount+gasFee), wallet)
require.NoError(t, err)
nativeTokenAmount := big.NewInt(123)
sn, nativeTokenID, err := ch.NewFoundryParams(1234).
Expand All @@ -37,14 +41,20 @@ func TestNoSenderFeature(t *testing.T) {
err = ch.MintTokens(sn, nativeTokenAmount, wallet)
require.NoError(t, err)

// withdraw NTs to L1
req := solo.NewCallParams("accounts", "withdraw").
AddAllowanceBaseTokens(5 * isc.Million).
// withdraw native tokens to L1
allowance := withdrawAmount
baseTokensToSend := allowance + gasFee
_, err = ch.PostRequestOffLedger(solo.NewCallParams(
accounts.Contract.Name, accounts.FuncWithdraw.Name,
).
AddBaseTokens(baseTokensToSend).
AddAllowanceBaseTokens(allowance).
AddAllowanceNativeTokensVect(&iotago.NativeToken{
ID: nativeTokenID,
Amount: nativeTokenAmount,
})
_, err = ch.PostRequestOffLedger(req, wallet)
}).
WithGasBudget(gasFee),
wallet)
require.NoError(t, err)

nft, _, err := ch.Env.MintNFTL1(wallet, addr, []byte("foobar"))
Expand Down
Loading