test: unit test
Some checks are pending
Continuous Integration (Commit) / lint (push) Waiting to run

This commit is contained in:
MiniFrenchBread 2024-08-13 14:43:15 +08:00
parent da02dc7eb7
commit 3017ed9919
6 changed files with 95 additions and 176 deletions

View File

@ -48,46 +48,6 @@
"name": "NewSigner", "name": "NewSigner",
"type": "event" "type": "event"
}, },
{
"anonymous": false,
"inputs": [
{
"components": [
{
"internalType": "uint256",
"name": "tokensPerVote",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxVotesPerSigner",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxQuorums",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "epochBlocks",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "encodedSlices",
"type": "uint256"
}
],
"indexed": false,
"internalType": "struct IDASigners.Params",
"name": "params",
"type": "tuple"
}
],
"name": "ParamsUpdated",
"type": "event"
},
{ {
"anonymous": false, "anonymous": false,
"inputs": [ "inputs": [

File diff suppressed because one or more lines are too long

View File

@ -370,6 +370,25 @@ func (suite *DASignersTestSuite) Test_DASigners() {
} }
func (suite *DASignersTestSuite) Test_Params() {
input, err := suite.abi.Pack(
"params",
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, suite.signerOne, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["params"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
params := out[0].(dasignersprecompile.IDASignersParams)
expected := types.DefaultGenesisState().Params
suite.Assert().EqualValues(expected.TokensPerVote, params.TokensPerVote.Uint64())
suite.Assert().EqualValues(expected.MaxVotesPerSigner, params.MaxVotesPerSigner.Uint64())
suite.Assert().EqualValues(expected.MaxQuorums, params.MaxQuorums.Uint64())
suite.Assert().EqualValues(expected.EpochBlocks, params.EpochBlocks.Uint64())
suite.Assert().EqualValues(expected.EncodedSlices, params.EncodedSlices.Uint64())
}
func TestKeeperSuite(t *testing.T) { func TestKeeperSuite(t *testing.T) {
suite.Run(t, new(DASignersTestSuite)) suite.Run(t, new(DASignersTestSuite))
} }

View File

@ -27,7 +27,7 @@ type IDASignersSignerDetail = struct {
PkG2 BN254G2Point "json:\"pkG2\"" PkG2 BN254G2Point "json:\"pkG2\""
} }
type IDASignersParams struct { type IDASignersParams = struct {
TokensPerVote *big.Int "json:\"tokensPerVote\"" TokensPerVote *big.Int "json:\"tokensPerVote\""
MaxVotesPerSigner *big.Int "json:\"maxVotesPerSigner\"" MaxVotesPerSigner *big.Int "json:\"maxVotesPerSigner\""
MaxQuorums *big.Int "json:\"maxQuorums\"" MaxQuorums *big.Int "json:\"maxQuorums\""

View File

@ -0,0 +1,71 @@
package keeper_test
import (
"testing"
"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
"github.com/stretchr/testify/suite"
)
type MsgServerTestSuite struct {
testutil.Suite
}
func (suite *MsgServerTestSuite) TestChangeParams() {
govAccAddr := suite.GovKeeper.GetGovernanceAccount(suite.Ctx).GetAddress().String()
testCases := []struct {
name string
req *types.MsgChangeParams
expectErr bool
errMsg string
}{
{
name: "invalid signer",
req: &types.MsgChangeParams{
Authority: suite.Addresses[0].String(),
Params: &types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 3072,
},
},
expectErr: true,
errMsg: "expected gov account as only signer for proposal message",
},
{
name: "success",
req: &types.MsgChangeParams{
Authority: govAccAddr,
Params: &types.Params{
TokensPerVote: 1,
MaxVotesPerSigner: 2048,
MaxQuorums: 1,
EpochBlocks: 100,
EncodedSlices: 2048 * 3,
},
},
expectErr: false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
_, err := suite.Keeper.ChangeParams(suite.Ctx, tc.req)
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.errMsg)
} else {
suite.Require().NoError(err)
params := suite.Keeper.GetParams(suite.Ctx)
suite.Require().EqualValues(*tc.req.Params, params)
}
})
}
}
func TestMsgServerSuite(t *testing.T) {
suite.Run(t, new(MsgServerTestSuite))
}

View File

@ -14,6 +14,7 @@ import (
"github.com/0glabs/0g-chain/chaincfg" "github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/dasigners/v1/keeper" "github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
"github.com/0glabs/0g-chain/x/dasigners/v1/types" "github.com/0glabs/0g-chain/x/dasigners/v1/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/evmos/ethermint/crypto/ethsecp256k1"
) )
@ -24,6 +25,7 @@ type Suite struct {
Keeper keeper.Keeper Keeper keeper.Keeper
StakingKeeper *stakingkeeper.Keeper StakingKeeper *stakingkeeper.Keeper
GovKeeper govkeeper.Keeper
App app.TestApp App app.TestApp
Ctx sdk.Context Ctx sdk.Context
QueryClient types.QueryClient QueryClient types.QueryClient
@ -37,6 +39,7 @@ func (suite *Suite) SetupTest() {
suite.App.InitializeFromGenesisStates() suite.App.InitializeFromGenesisStates()
suite.Keeper = suite.App.GetDASignersKeeper() suite.Keeper = suite.App.GetDASignersKeeper()
suite.StakingKeeper = suite.App.GetStakingKeeper() suite.StakingKeeper = suite.App.GetStakingKeeper()
suite.GovKeeper = suite.App.GetGovKeeper()
// make block header // make block header
privkey, _ := ethsecp256k1.GenerateKey() privkey, _ := ethsecp256k1.GenerateKey()