From 88359d2e3c7eca85de9c686fecf1cac7693ed55b Mon Sep 17 00:00:00 2001 From: MiniFrenchBread <103425574+MiniFrenchBread@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:04:56 +0800 Subject: [PATCH] test: util --- precompiles/testutil/suite.go | 88 ++++++++++++++++++++++++++++ x/dasigners/v1/keeper/keeper_test.go | 4 +- x/dasigners/v1/testutil/suite.go | 20 ++++++- 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 precompiles/testutil/suite.go diff --git a/precompiles/testutil/suite.go b/precompiles/testutil/suite.go new file mode 100644 index 00000000..1b6a9ce3 --- /dev/null +++ b/precompiles/testutil/suite.go @@ -0,0 +1,88 @@ +package testutil + +import ( + "strings" + + "github.com/0glabs/0g-chain/app" + "github.com/0glabs/0g-chain/chaincfg" + dasignersprecompile "github.com/0glabs/0g-chain/precompiles/dasigners" + "github.com/0glabs/0g-chain/x/bep3/types" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + emtests "github.com/evmos/ethermint/tests" + evmkeeper "github.com/evmos/ethermint/x/evm/keeper" + "github.com/evmos/ethermint/x/evm/statedb" + "github.com/stretchr/testify/suite" + + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/evmos/ethermint/crypto/ethsecp256k1" + + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +type PrecompileTestSuite struct { + suite.Suite + + StakingKeeper *stakingkeeper.Keeper + App app.TestApp + Ctx sdk.Context + QueryClient types.QueryClient + Addresses []sdk.AccAddress + + EvmKeeper *evmkeeper.Keeper + EthSigner ethtypes.Signer + Statedb *statedb.StateDB +} + +type TestSigner struct { + Addr common.Address + HexAddr string + PrivKey cryptotypes.PrivKey + Signer keyring.Signer +} + +func GenSigner() *TestSigner { + var s TestSigner + addr, priv := emtests.NewAddrKey() + s.PrivKey = priv + s.Addr = addr + s.HexAddr = dasignersprecompile.ToLowerHexWithoutPrefix(s.Addr) + s.Signer = emtests.NewSigner(priv) + return &s +} + +func (suite *PrecompileTestSuite) SetupTest() { + chaincfg.SetSDKConfig() + suite.App = app.NewTestApp() + suite.App.InitializeFromGenesisStates() + suite.StakingKeeper = suite.App.GetStakingKeeper() + + // make block header + privkey, _ := ethsecp256k1.GenerateKey() + consAddress := sdk.ConsAddress(privkey.PubKey().Address()) + key, err := privkey.ToECDSA() + suite.Assert().NoError(err) + hexAddr := strings.ToLower(crypto.PubkeyToAddress(key.PublicKey).Hex()[2:]) + valAddr, err := sdk.ValAddressFromHex(hexAddr) + suite.Assert().NoError(err) + suite.Ctx = suite.App.NewContext(true, tmproto.Header{Height: 1, ChainID: app.TestChainId, ProposerAddress: consAddress}) + newValidator, err := stakingtypes.NewValidator(valAddr, privkey.PubKey(), stakingtypes.Description{}) + suite.Assert().NoError(err) + err = suite.StakingKeeper.SetValidatorByConsAddr(suite.Ctx, newValidator) + suite.Assert().NoError(err) + suite.StakingKeeper.SetValidator(suite.Ctx, newValidator) + + _, accAddresses := app.GeneratePrivKeyAddressPairs(10) + suite.Addresses = accAddresses + + suite.EvmKeeper = suite.App.GetEvmKeeper() + + suite.EthSigner = ethtypes.LatestSignerForChainID(suite.EvmKeeper.ChainID()) + + suite.Statedb = statedb.New(suite.Ctx, suite.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.Ctx.HeaderHash().Bytes()))) +} diff --git a/x/dasigners/v1/keeper/keeper_test.go b/x/dasigners/v1/keeper/keeper_test.go index a4057d22..fda2c698 100644 --- a/x/dasigners/v1/keeper/keeper_test.go +++ b/x/dasigners/v1/keeper/keeper_test.go @@ -113,7 +113,7 @@ func (suite *KeeperTestSuite) testUpdateSocket(signer *types.Signer) { )) } -func (suite *KeeperTestSuite) testRegisterEpochInvalidSignature(signer *types.Signer) { +func (suite *KeeperTestSuite) testRegisterEpochInvalidSignature() { sk := big.NewInt(2) hash := types.EpochRegistrationHash(common.HexToAddress(signer1), 1, big.NewInt(8888)) signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk) @@ -322,7 +322,7 @@ func (suite *KeeperTestSuite) Test_Keeper() { signerOne := suite.testRegisterSignerSuccess() suite.testQuerySigner(signerOne) suite.testUpdateSocket(signerOne) - suite.testRegisterEpochInvalidSignature(signerOne) + suite.testRegisterEpochInvalidSignature() suite.testRegisterEpochSuccess() suite.secondSigner() suite.newEpoch(params) diff --git a/x/dasigners/v1/testutil/suite.go b/x/dasigners/v1/testutil/suite.go index 4e10faef..1191cba7 100644 --- a/x/dasigners/v1/testutil/suite.go +++ b/x/dasigners/v1/testutil/suite.go @@ -1,10 +1,13 @@ package testutil import ( + "strings" + "cosmossdk.io/math" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/suite" "github.com/0glabs/0g-chain/app" @@ -34,7 +37,22 @@ func (suite *Suite) SetupTest() { suite.App.InitializeFromGenesisStates() suite.Keeper = suite.App.GetDASignersKeeper() suite.StakingKeeper = suite.App.GetStakingKeeper() - suite.Ctx = suite.App.NewContext(true, tmproto.Header{Height: 1, ChainID: app.TestChainId}) + + // make block header + privkey, _ := ethsecp256k1.GenerateKey() + consAddress := sdk.ConsAddress(privkey.PubKey().Address()) + key, err := privkey.ToECDSA() + suite.Assert().NoError(err) + hexAddr := strings.ToLower(crypto.PubkeyToAddress(key.PublicKey).Hex()[2:]) + valAddr, err := sdk.ValAddressFromHex(hexAddr) + suite.Assert().NoError(err) + suite.Ctx = suite.App.NewContext(true, tmproto.Header{Height: 1, ChainID: app.TestChainId, ProposerAddress: consAddress}) + newValidator, err := stakingtypes.NewValidator(valAddr, privkey.PubKey(), stakingtypes.Description{}) + suite.Assert().NoError(err) + err = suite.StakingKeeper.SetValidatorByConsAddr(suite.Ctx, newValidator) + suite.Assert().NoError(err) + suite.StakingKeeper.SetValidator(suite.Ctx, newValidator) + _, accAddresses := app.GeneratePrivKeyAddressPairs(10) suite.Addresses = accAddresses