Skip to content

Commit

Permalink
test: Add governance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Jun 22, 2023
1 parent 9c43772 commit bfe1c0c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/solo/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 86 additions & 1 deletion packages/vm/core/testcore/governance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
9 changes: 5 additions & 4 deletions packages/vm/vmcontext/runreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bfe1c0c

Please sign in to comment.