From bfe1c0c42f825c9ca60f6e4edb8c8980a2759963 Mon Sep 17 00:00:00 2001 From: Yang Hau Date: Wed, 21 Jun 2023 20:52:51 -0500 Subject: [PATCH] test: Add governance tests --- packages/solo/req.go | 2 +- packages/vm/core/testcore/governance_test.go | 87 +++++++++++++++++++- packages/vm/vmcontext/runreq.go | 9 +- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/packages/solo/req.go b/packages/solo/req.go index bb62396c71..918eb3a903 100644 --- a/packages/solo/req.go +++ b/packages/solo/req.go @@ -316,7 +316,7 @@ func (ch *Chain) RequestFromParamsToLedger(req *CallParams, keyPair *cryptolib.K return tx, isc.NewRequestID(txid, 0), nil } -// PostRequestSync posts a request synchronously sent by the test program to the smart contract on the same or another chain: +// PostRequestSync posts a request synchronously sent by the test program to the smart contract on the same or another chain: // - creates a request transaction with the request block on it. The sigScheme is used to // sign the inputs of the transaction or OriginatorKeyPair is used if parameter is nil // - adds request transaction to UTXODB diff --git a/packages/vm/core/testcore/governance_test.go b/packages/vm/core/testcore/governance_test.go index b221c18474..a32239b6af 100644 --- a/packages/vm/core/testcore/governance_test.go +++ b/packages/vm/core/testcore/governance_test.go @@ -466,7 +466,7 @@ func TestGovernanceSetMustGetPayoutAgentID(t *testing.T) { env := solo.New(t, &solo.InitOptions{AutoAdjustStorageDeposit: true, Debug: true, PrintStackTrace: true}) ch := env.NewChain() - _, userAddr := env.NewKeyPairWithFunds() + user, userAddr := env.NewKeyPairWithFunds() userAgentID := isc.NewAgentID(userAddr) _, err := ch.PostRequestSync( @@ -488,6 +488,17 @@ func TestGovernanceSetMustGetPayoutAgentID(t *testing.T) { retAgentID, err := codec.DecodeAgentID(retDict.Get(governance.ParamSetPayoutAgentID)) require.NoError(t, err) require.Equal(t, userAgentID, retAgentID) + + _, err = ch.PostRequestSync( + solo.NewCallParams( + governance.Contract.Name, + governance.FuncSetPayoutAgentID.Name, + governance.ParamSetPayoutAgentID, + userAgentID.Bytes(), + ).WithMaxAffordableGasBudget(), + user, + ) + require.ErrorContains(t, err, "unauthorized access") } func TestGovernanceSetGetMinCommonAccountBalance(t *testing.T) { @@ -543,3 +554,77 @@ func TestGovCallsNoBalance(t *testing.T) { ) require.NoError(t, err) } + +func TestGasPayout(t *testing.T) { + env := solo.New(t, &solo.InitOptions{ + AutoAdjustStorageDeposit: true, + Debug: true, + PrintStackTrace: true, + }) + ch := env.NewChain(false) + user1, user1Addr := env.NewKeyPairWithFunds() + user1AgentID := isc.NewAgentID(user1Addr) + + ownerBal1 := ch.L2Assets(ch.OriginatorAgentID) + user1Bal1 := ch.L2Assets(user1AgentID) + transferAmt := uint64(2000) + + _, err := ch.PostRequestSync( + solo.NewCallParams( + accounts.Contract.Name, + accounts.FuncDeposit.Name, + ).AddBaseTokens(transferAmt), + user1, + ) + require.NoError(t, err) + receipt := ch.LastReceipt() + + ownerBal2 := ch.L2Assets(ch.OriginatorAgentID) + commonBal2 := ch.L2CommonAccountAssets() + user1Bal2 := ch.L2Assets(user1AgentID) + + require.Equal(t, ownerBal1.BaseTokens+receipt.GasFeeCharged, ownerBal2.BaseTokens) + require.Equal(t, user1Bal1.BaseTokens+transferAmt-receipt.GasFeeCharged, user1Bal2.BaseTokens) + + _, err = ch.PostRequestOffLedger( + solo.NewCallParams( + governance.Contract.Name, + governance.FuncSetPayoutAgentID.Name, + governance.ParamSetPayoutAgentID, + user1AgentID.Bytes(), + ), + nil, + ) + require.NoError(t, err) + + retDict, err := ch.CallView( + governance.Contract.Name, + governance.ViewGetPayoutAgentID.Name, + ) + require.NoError(t, err) + retAgentID, err := codec.DecodeAgentID(retDict.Get(governance.ParamSetPayoutAgentID)) + require.NoError(t, err) + require.Equal(t, user1AgentID, retAgentID) + + ownerBal3 := ch.L2Assets(ch.OriginatorAgentID) + commonBal3 := ch.L2CommonAccountAssets() + user1Bal3 := ch.L2Assets(user1AgentID) + require.Equal(t, ownerBal2.BaseTokens, ownerBal3.BaseTokens) + require.Equal(t, commonBal2.BaseTokens, commonBal3.BaseTokens) + require.Equal(t, user1Bal2.BaseTokens, user1Bal3.BaseTokens) + + _, err = ch.PostRequestSync( + solo.NewCallParams( + accounts.Contract.Name, + accounts.FuncDeposit.Name, + ).AddBaseTokens(transferAmt), + user1, + ) + require.NoError(t, err) + ownerBal4 := ch.L2Assets(ch.OriginatorAgentID) + commonBal4 := ch.L2CommonAccountAssets() + user1Bal4 := ch.L2Assets(user1AgentID) + require.Equal(t, ownerBal3.BaseTokens, ownerBal4.BaseTokens) + require.Equal(t, commonBal3.BaseTokens, commonBal4.BaseTokens) + require.Equal(t, user1Bal3.BaseTokens+transferAmt, user1Bal4.BaseTokens) +} diff --git a/packages/vm/vmcontext/runreq.go b/packages/vm/vmcontext/runreq.go index 021821e333..b5fa54f80d 100644 --- a/packages/vm/vmcontext/runreq.go +++ b/packages/vm/vmcontext/runreq.go @@ -362,11 +362,12 @@ func (vmctx *VMContext) chargeGasFee() { return } - transferToValidator := &isc.Assets{} - transferToValidator.BaseTokens = sendToValidator sender := vmctx.req.SenderAccount() - - vmctx.mustMoveBetweenAccounts(sender, vmctx.task.ValidatorFeeTarget, transferToValidator) + if sendToValidator != 0 { + transferToValidator := &isc.Assets{} + transferToValidator.BaseTokens = sendToValidator + vmctx.mustMoveBetweenAccounts(sender, vmctx.task.ValidatorFeeTarget, transferToValidator) + } // ensure common account has at least minSD, and transfer the rest of gas fee to payout AgentID // if the payout AgentID is not set in governance contract, then chain owner will be used