mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
fix: abci; test: abci
This commit is contained in:
parent
c1e5ee2277
commit
7b1b0eecbe
@ -6,28 +6,15 @@ import (
|
|||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
||||||
|
|
||||||
"github.com/0glabs/0g-chain/app"
|
"github.com/0glabs/0g-chain/app"
|
||||||
"github.com/0glabs/0g-chain/x/dasigners/v1"
|
"github.com/0glabs/0g-chain/x/dasigners/v1"
|
||||||
"github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
|
"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
|
||||||
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
|
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenesisTestSuite struct {
|
type GenesisTestSuite struct {
|
||||||
suite.Suite
|
testutil.Suite
|
||||||
|
|
||||||
app app.TestApp
|
|
||||||
ctx sdk.Context
|
|
||||||
keeper keeper.Keeper
|
|
||||||
addresses []sdk.AccAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *GenesisTestSuite) SetupTest() {
|
|
||||||
suite.app = app.NewTestApp()
|
|
||||||
suite.keeper = suite.app.GetDASignersKeeper()
|
|
||||||
suite.ctx = suite.app.NewContext(true, tmproto.Header{})
|
|
||||||
_, suite.addresses = app.GeneratePrivKeyAddressPairs(10)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *GenesisTestSuite) TestInitGenesis() {
|
func (suite *GenesisTestSuite) TestInitGenesis() {
|
||||||
@ -135,15 +122,15 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
suite.Run(tc.name, func() {
|
suite.Run(tc.name, func() {
|
||||||
// Setup (note: suite.SetupTest is not run before every suite.Run)
|
// Setup (note: suite.SetupTest is not run before every suite.Run)
|
||||||
suite.app = app.NewTestApp()
|
suite.App = app.NewTestApp()
|
||||||
suite.keeper = suite.app.GetDASignersKeeper()
|
suite.Keeper = suite.App.GetDASignersKeeper()
|
||||||
suite.ctx = suite.app.NewContext(true, tmproto.Header{})
|
suite.Ctx = suite.App.NewContext(true, tmproto.Header{})
|
||||||
|
|
||||||
// Run
|
// Run
|
||||||
var exportedGenState *types.GenesisState
|
var exportedGenState *types.GenesisState
|
||||||
run := func() {
|
run := func() {
|
||||||
dasigners.InitGenesis(suite.ctx, suite.keeper, *tc.genState)
|
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *tc.genState)
|
||||||
exportedGenState = dasigners.ExportGenesis(suite.ctx, suite.keeper)
|
exportedGenState = dasigners.ExportGenesis(suite.Ctx, suite.Keeper)
|
||||||
}
|
}
|
||||||
if tc.expectPass {
|
if tc.expectPass {
|
||||||
suite.Require().NotPanics(run)
|
suite.Require().NotPanics(run)
|
||||||
@ -153,9 +140,9 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
|
|||||||
|
|
||||||
// Check
|
// Check
|
||||||
if tc.expectPass {
|
if tc.expectPass {
|
||||||
expectedJson, err := suite.app.AppCodec().MarshalJSON(tc.genState)
|
expectedJson, err := suite.App.AppCodec().MarshalJSON(tc.genState)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
actualJson, err := suite.app.AppCodec().MarshalJSON(exportedGenState)
|
actualJson, err := suite.App.AppCodec().MarshalJSON(exportedGenState)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
suite.Equal(expectedJson, actualJson)
|
suite.Equal(expectedJson, actualJson)
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
|
|||||||
}
|
}
|
||||||
if len(ballots) >= int(params.EncodedSlices) {
|
if len(ballots) >= int(params.EncodedSlices) {
|
||||||
for i := 0; i+int(params.EncodedSlices) <= len(ballots); i += int(params.EncodedSlices) {
|
for i := 0; i+int(params.EncodedSlices) <= len(ballots); i += int(params.EncodedSlices) {
|
||||||
if int(params.MaxQuorums) < len(quorums.Quorums) {
|
if int(params.MaxQuorums) <= len(quorums.Quorums) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
quorum := types.Quorum{
|
quorum := types.Quorum{
|
||||||
@ -101,10 +101,6 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
|
|||||||
quorum.Signers[i] = ballots[i%n].account
|
quorum.Signers[i] = ballots[i%n].account
|
||||||
}
|
}
|
||||||
quorums.Quorums = append(quorums.Quorums, &quorum)
|
quorums.Quorums = append(quorums.Quorums, &quorum)
|
||||||
} else {
|
|
||||||
quorums.Quorums = append(quorums.Quorums, &types.Quorum{
|
|
||||||
Signers: make([]string, 0),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// save to store
|
// save to store
|
||||||
|
138
x/dasigners/v1/keeper/abci_test.go
Normal file
138
x/dasigners/v1/keeper/abci_test.go
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
package keeper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/0glabs/0g-chain/x/dasigners/v1"
|
||||||
|
"github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
|
||||||
|
"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
|
||||||
|
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
|
||||||
|
abci "github.com/cometbft/cometbft/abci/types"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ModuleTestSuite struct {
|
||||||
|
testutil.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *ModuleTestSuite) TestBeginBlock_NotContinuous() {
|
||||||
|
suite.App.InitializeFromGenesisStates()
|
||||||
|
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
|
||||||
|
params := suite.Keeper.GetParams(suite.Ctx)
|
||||||
|
suite.Require().Panics(func() {
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx.WithBlockHeight(int64(params.EpochBlocks*2)), abci.RequestBeginBlock{})
|
||||||
|
}, "block height is not continuous")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *ModuleTestSuite) TestBeginBlock_Success() {
|
||||||
|
suite.App.InitializeFromGenesisStates()
|
||||||
|
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
|
||||||
|
suite.Keeper.SetParams(suite.Ctx, types.Params{
|
||||||
|
TokensPerVote: 10,
|
||||||
|
MaxVotesPerSigner: 200,
|
||||||
|
MaxQuorums: 10,
|
||||||
|
EpochBlocks: 5760,
|
||||||
|
EncodedSlices: 10,
|
||||||
|
})
|
||||||
|
params := suite.Keeper.GetParams(suite.Ctx)
|
||||||
|
epoch, err := suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
cnt, err := suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 0)
|
||||||
|
// set signer
|
||||||
|
suite.Keeper.SetSigner(suite.Ctx, types.Signer{
|
||||||
|
Account: "0000000000000000000000000000000000000001",
|
||||||
|
Socket: "0.0.0.0:1234",
|
||||||
|
PubkeyG1: common.LeftPadBytes([]byte{1}, 32),
|
||||||
|
PubkeyG2: common.LeftPadBytes([]byte{2}, 64),
|
||||||
|
})
|
||||||
|
suite.Keeper.SetRegistration(suite.Ctx, epoch+1, "0000000000000000000000000000000000000001", common.LeftPadBytes([]byte{1}, 32))
|
||||||
|
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * int64(epoch+1))
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
|
||||||
|
// check quorums
|
||||||
|
lastEpoch := epoch
|
||||||
|
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(epoch, lastEpoch+1)
|
||||||
|
cnt, err = suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 0)
|
||||||
|
// set delegation, 1 ballot
|
||||||
|
suite.AddDelegation("0000000000000000000000000000000000000001", "0000000000000000000000000000000000000001", keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)))
|
||||||
|
// set signer
|
||||||
|
suite.Keeper.SetSigner(suite.Ctx, types.Signer{
|
||||||
|
Account: "0000000000000000000000000000000000000001",
|
||||||
|
Socket: "0.0.0.0:1234",
|
||||||
|
PubkeyG1: common.LeftPadBytes([]byte{1}, 32),
|
||||||
|
PubkeyG2: common.LeftPadBytes([]byte{2}, 64),
|
||||||
|
})
|
||||||
|
suite.Keeper.SetRegistration(suite.Ctx, epoch+1, "0000000000000000000000000000000000000001", common.LeftPadBytes([]byte{1}, 32))
|
||||||
|
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * int64(epoch+1))
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
|
||||||
|
// check quorums
|
||||||
|
lastEpoch = epoch
|
||||||
|
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(epoch, lastEpoch+1)
|
||||||
|
cnt, err = suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 1)
|
||||||
|
// set delegation, 10 ballot
|
||||||
|
suite.AddDelegation(
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)).Mul(sdk.NewIntFromUint64(9)),
|
||||||
|
)
|
||||||
|
suite.Keeper.SetRegistration(suite.Ctx, epoch+1, "0000000000000000000000000000000000000001", common.LeftPadBytes([]byte{1}, 32))
|
||||||
|
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * int64(epoch+1))
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
|
||||||
|
// check quorums
|
||||||
|
lastEpoch = epoch
|
||||||
|
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(epoch, lastEpoch+1)
|
||||||
|
cnt, err = suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 1)
|
||||||
|
// set delegation, 11 ballot
|
||||||
|
suite.AddDelegation(
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)).Mul(sdk.NewIntFromUint64(1)),
|
||||||
|
)
|
||||||
|
suite.Keeper.SetRegistration(suite.Ctx, epoch+1, "0000000000000000000000000000000000000001", common.LeftPadBytes([]byte{1}, 32))
|
||||||
|
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * int64(epoch+1))
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
|
||||||
|
// check quorums
|
||||||
|
lastEpoch = epoch
|
||||||
|
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(epoch, lastEpoch+1)
|
||||||
|
cnt, err = suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 2)
|
||||||
|
// set delegation, 200 ballot
|
||||||
|
suite.AddDelegation(
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
"0000000000000000000000000000000000000001",
|
||||||
|
keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)).Mul(sdk.NewIntFromUint64(200)),
|
||||||
|
)
|
||||||
|
suite.Keeper.SetRegistration(suite.Ctx, epoch+1, "0000000000000000000000000000000000000001", common.LeftPadBytes([]byte{1}, 32))
|
||||||
|
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * int64(epoch+1))
|
||||||
|
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
|
||||||
|
// check quorums
|
||||||
|
lastEpoch = epoch
|
||||||
|
epoch, err = suite.Keeper.GetEpochNumber(suite.Ctx)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(epoch, lastEpoch+1)
|
||||||
|
cnt, err = suite.Keeper.GetQuorumCount(suite.Ctx, epoch)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().EqualValues(cnt, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModuleTestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(ModuleTestSuite))
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package testutil
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cosmossdk.io/math"
|
||||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||||
@ -10,6 +11,8 @@ 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"
|
||||||
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Suite implements a test suite for the module integration tests
|
// Suite implements a test suite for the module integration tests
|
||||||
@ -40,3 +43,27 @@ func (suite *Suite) SetupTest() {
|
|||||||
types.RegisterQueryServer(queryHelper, queryHandler)
|
types.RegisterQueryServer(queryHelper, queryHandler)
|
||||||
suite.QueryClient = types.NewQueryClient(queryHelper)
|
suite.QueryClient = types.NewQueryClient(queryHelper)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *Suite) AddDelegation(from string, to string, amount math.Int) {
|
||||||
|
accAddr, err := sdk.AccAddressFromHexUnsafe(from)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
valAddr, err := sdk.ValAddressFromHex(to)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
validator, found := suite.StakingKeeper.GetValidator(suite.Ctx, valAddr)
|
||||||
|
if !found {
|
||||||
|
consPriv, err := ethsecp256k1.GenerateKey()
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
newValidator, err := stakingtypes.NewValidator(valAddr, consPriv.PubKey(), stakingtypes.Description{})
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
validator = newValidator
|
||||||
|
}
|
||||||
|
validator.Tokens = validator.Tokens.Add(amount)
|
||||||
|
validator.DelegatorShares = validator.DelegatorShares.Add(amount.ToLegacyDec())
|
||||||
|
suite.StakingKeeper.SetValidator(suite.Ctx, validator)
|
||||||
|
bonded := suite.Keeper.GetDelegatorBonded(suite.Ctx, accAddr)
|
||||||
|
suite.StakingKeeper.SetDelegation(suite.Ctx, stakingtypes.Delegation{
|
||||||
|
DelegatorAddress: accAddr.String(),
|
||||||
|
ValidatorAddress: valAddr.String(),
|
||||||
|
Shares: bonded.Add(amount).ToLegacyDec(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user