Compare commits

..

3 Commits

Author SHA1 Message Date
MiniFrenchBread
831e257507 test: dasigners precompile
Some checks are pending
Continuous Integration (Commit) / lint (push) Waiting to run
2024-08-08 18:44:56 +08:00
MiniFrenchBread
88359d2e3c test: util 2024-08-08 18:04:56 +08:00
MiniFrenchBread
dfa3dc0931 test: keeper test 2024-08-08 13:44:10 +08:00
8 changed files with 828 additions and 12 deletions

View File

@ -55,7 +55,7 @@ var (
defaultInitialHeight int64 = 1
)
const TestChainId = "kavatest_2221-1"
const TestChainId = "zgchain_8888-1"
// TestApp is a simple wrapper around an App. It exposes internal keepers for use in integration tests.
// This file also contains test helpers. Ideally they would be in separate package.

View File

@ -0,0 +1,377 @@
package dasigners_test
import (
"fmt"
"math/big"
"strings"
"testing"
"github.com/0glabs/0g-chain/crypto/bn254util"
dasignersprecompile "github.com/0glabs/0g-chain/precompiles/dasigners"
"github.com/0glabs/0g-chain/precompiles/testutil"
"github.com/0glabs/0g-chain/x/dasigners/v1"
"github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/consensys/gnark-crypto/ecc/bn254"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/suite"
"cosmossdk.io/math"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
)
type DASignersTestSuite struct {
testutil.PrecompileTestSuite
abi abi.ABI
addr common.Address
dasigners *dasignersprecompile.DASignersPrecompile
dasignerskeeper dasignerskeeper.Keeper
signerOne *testutil.TestSigner
signerTwo *testutil.TestSigner
}
func (suite *DASignersTestSuite) 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.dasignerskeeper.GetDelegatorBonded(suite.Ctx, accAddr)
suite.StakingKeeper.SetDelegation(suite.Ctx, stakingtypes.Delegation{
DelegatorAddress: accAddr.String(),
ValidatorAddress: valAddr.String(),
Shares: bonded.Add(amount).ToLegacyDec(),
})
}
func (suite *DASignersTestSuite) SetupTest() {
suite.PrecompileTestSuite.SetupTest()
suite.dasignerskeeper = suite.App.GetDASignersKeeper()
suite.addr = common.HexToAddress(dasignersprecompile.PrecompileAddress)
precompiles := suite.EvmKeeper.GetPrecompiles()
precompile, ok := precompiles[suite.addr]
suite.Assert().EqualValues(ok, true)
suite.dasigners = precompile.(*dasignersprecompile.DASignersPrecompile)
suite.signerOne = testutil.GenSigner()
suite.signerTwo = testutil.GenSigner()
abi, err := abi.JSON(strings.NewReader(dasignersprecompile.DASignersABI))
suite.Assert().NoError(err)
suite.abi = abi
}
func (suite *DASignersTestSuite) runTx(input []byte, signer *testutil.TestSigner, gas uint64) ([]byte, error) {
contract := vm.NewPrecompile(vm.AccountRef(signer.Addr), vm.AccountRef(suite.addr), big.NewInt(0), gas)
contract.Input = input
msgEthereumTx := evmtypes.NewTx(suite.EvmKeeper.ChainID(), 0, &suite.addr, big.NewInt(0), gas, big.NewInt(0), big.NewInt(0), big.NewInt(0), input, nil)
msgEthereumTx.From = signer.HexAddr
err := msgEthereumTx.Sign(suite.EthSigner, signer.Signer)
suite.Assert().NoError(err, "failed to sign Ethereum message")
proposerAddress := suite.Ctx.BlockHeader().ProposerAddress
cfg, err := suite.EvmKeeper.EVMConfig(suite.Ctx, proposerAddress, suite.EvmKeeper.ChainID())
suite.Assert().NoError(err, "failed to instantiate EVM config")
msg, err := msgEthereumTx.AsMessage(suite.EthSigner, big.NewInt(0))
suite.Assert().NoError(err, "failed to instantiate Ethereum message")
evm := suite.EvmKeeper.NewEVM(suite.Ctx, msg, cfg, nil, suite.Statedb)
precompiles := suite.EvmKeeper.GetPrecompiles()
evm.WithPrecompiles(precompiles, []common.Address{suite.addr})
return suite.dasigners.Run(evm, contract, false)
}
func (suite *DASignersTestSuite) registerSigner(testSigner *testutil.TestSigner, sk *big.Int) *types.Signer {
pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), sk)
pkG2 := new(bn254.G2Affine).ScalarMultiplication(bn254util.GetG2Generator(), sk)
hash := types.PubkeyRegistrationHash(testSigner.Addr, big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
signer := &types.Signer{
Account: testSigner.HexAddr,
Socket: "0.0.0.0:1234",
PubkeyG1: bn254util.SerializeG1(pkG1),
PubkeyG2: bn254util.SerializeG2(pkG2),
}
input, err := suite.abi.Pack(
"registerSigner",
dasignersprecompile.NewIDASignersSignerDetail(signer),
dasignersprecompile.NewBN254G1Point(bn254util.SerializeG1(signature)),
)
suite.Assert().NoError(err)
oldLogs := suite.Statedb.Logs()
_, err = suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
logs := suite.Statedb.Logs()
suite.Assert().EqualValues(len(logs), len(oldLogs)+2)
_, err = suite.abi.Unpack("SocketUpdated", logs[len(logs)-1].Data)
suite.Assert().NoError(err)
_, err = suite.abi.Unpack("NewSigner", logs[len(logs)-2].Data)
suite.Assert().NoError(err)
return signer
}
func (suite *DASignersTestSuite) updateSocket(testSigner *testutil.TestSigner, signer *types.Signer) {
input, err := suite.abi.Pack(
"updateSocket",
"0.0.0.0:2345",
)
suite.Assert().NoError(err)
oldLogs := suite.Statedb.Logs()
_, err = suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
logs := suite.Statedb.Logs()
suite.Assert().EqualValues(len(logs), len(oldLogs)+1)
_, err = suite.abi.Unpack("SocketUpdated", logs[len(logs)-1].Data)
suite.Assert().NoError(err)
signer.Socket = "0.0.0.0:2345"
}
func (suite *DASignersTestSuite) registerEpoch(testSigner *testutil.TestSigner, sk *big.Int) {
hash := types.EpochRegistrationHash(common.HexToAddress(testSigner.HexAddr), 1, big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
input, err := suite.abi.Pack(
"registerNextEpoch",
dasignersprecompile.NewBN254G1Point(bn254util.SerializeG1(signature)),
)
suite.Assert().NoError(err)
_, err = suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
}
func (suite *DASignersTestSuite) queryEpochNumber(testSigner *testutil.TestSigner) {
input, err := suite.abi.Pack(
"epochNumber",
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["epochNumber"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
suite.Assert().EqualValues(out[0], big.NewInt(1))
}
func (suite *DASignersTestSuite) queryQuorumCount(testSigner *testutil.TestSigner) {
input, err := suite.abi.Pack(
"quorumCount",
big.NewInt(1),
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["quorumCount"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
suite.Assert().EqualValues(out[0], big.NewInt(1))
}
func (suite *DASignersTestSuite) queryGetSigner(testSigner *testutil.TestSigner, answer []*types.Signer) {
input, err := suite.abi.Pack(
"getSigner",
[]common.Address{suite.signerOne.Addr, suite.signerTwo.Addr},
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["getSigner"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
res := make([]dasignersprecompile.IDASignersSignerDetail, 0)
for _, s := range answer {
res = append(res, dasignersprecompile.NewIDASignersSignerDetail(s))
}
suite.Assert().EqualValues(out[0], res)
}
func (suite *DASignersTestSuite) queryIsSigner(testSigner *testutil.TestSigner) {
input, err := suite.abi.Pack(
"isSigner",
suite.signerOne.Addr,
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["isSigner"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
suite.Assert().EqualValues(out[0], true)
}
func (suite *DASignersTestSuite) queryRegisteredEpoch(testSigner *testutil.TestSigner, account common.Address, epoch *big.Int) bool {
input, err := suite.abi.Pack(
"registeredEpoch",
account,
epoch,
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["registeredEpoch"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
return out[0].(bool)
}
func (suite *DASignersTestSuite) queryGetQuorum(testSigner *testutil.TestSigner) []common.Address {
input, err := suite.abi.Pack(
"getQuorum",
big.NewInt(1),
big.NewInt(0),
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["getQuorum"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
return out[0].([]common.Address)
}
func (suite *DASignersTestSuite) queryGetQuorumRow(testSigner *testutil.TestSigner, row uint32) common.Address {
input, err := suite.abi.Pack(
"getQuorumRow",
big.NewInt(1),
big.NewInt(0),
row,
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["getQuorumRow"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
return out[0].(common.Address)
}
func (suite *DASignersTestSuite) queryGetAggPkG1(testSigner *testutil.TestSigner, bitmap []byte) struct {
AggPkG1 dasignersprecompile.BN254G1Point
Total *big.Int
Hit *big.Int
} {
input, err := suite.abi.Pack(
"getAggPkG1",
big.NewInt(1),
big.NewInt(0),
bitmap,
)
suite.Assert().NoError(err)
bz, err := suite.runTx(input, testSigner, 10000000)
suite.Assert().NoError(err)
out, err := suite.abi.Methods["getAggPkG1"].Outputs.Unpack(bz)
suite.Assert().NoError(err)
fmt.Printf("%v\n", out[0])
return struct {
AggPkG1 dasignersprecompile.BN254G1Point
Total *big.Int
Hit *big.Int
}{
AggPkG1: out[0].(dasignersprecompile.BN254G1Point),
Total: out[1].(*big.Int),
Hit: out[2].(*big.Int),
}
}
func (suite *DASignersTestSuite) Test_DASigners() {
// suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.dasignerskeeper, *types.DefaultGenesisState())
// add delegation
params := suite.dasignerskeeper.GetParams(suite.Ctx)
suite.AddDelegation(suite.signerOne.HexAddr, suite.signerOne.HexAddr, keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)))
suite.AddDelegation(suite.signerTwo.HexAddr, suite.signerOne.HexAddr, keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)).Mul(sdk.NewIntFromUint64(2)))
// tx test
signer1 := suite.registerSigner(suite.signerOne, big.NewInt(1))
signer2 := suite.registerSigner(suite.signerTwo, big.NewInt(11))
suite.updateSocket(suite.signerOne, signer1)
suite.updateSocket(suite.signerTwo, signer2)
suite.registerEpoch(suite.signerOne, big.NewInt(1))
suite.registerEpoch(suite.signerTwo, big.NewInt(11))
// move to next epoch
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * 1)
suite.dasignerskeeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
// query test
suite.queryEpochNumber(suite.signerOne)
suite.queryQuorumCount(suite.signerOne)
suite.queryGetSigner(suite.signerOne, []*types.Signer{signer1, signer2})
suite.queryIsSigner(suite.signerOne)
suite.Assert().EqualValues(suite.queryRegisteredEpoch(suite.signerOne, suite.signerOne.Addr, big.NewInt(1)), true)
suite.Assert().EqualValues(suite.queryRegisteredEpoch(suite.signerOne, suite.signerTwo.Addr, big.NewInt(1)), true)
suite.Assert().EqualValues(suite.queryRegisteredEpoch(suite.signerOne, suite.signerOne.Addr, big.NewInt(2)), false)
suite.Assert().EqualValues(suite.queryRegisteredEpoch(suite.signerOne, suite.signerTwo.Addr, big.NewInt(0)), false)
quorum := suite.queryGetQuorum(suite.signerOne)
suite.Assert().EqualValues(len(quorum), params.EncodedSlices)
cnt := map[common.Address]int{suite.signerOne.Addr: 0, suite.signerTwo.Addr: 0}
onePos := len(quorum)
twoPos := len(quorum)
for i, v := range quorum {
suite.Assert().EqualValues(suite.queryGetQuorumRow(suite.signerOne, uint32(i)), v)
cnt[v] += 1
if v == suite.signerOne.Addr {
onePos = min(onePos, i)
} else {
twoPos = min(twoPos, i)
}
}
suite.Assert().EqualValues(cnt[suite.signerOne.Addr], len(quorum)/3)
suite.Assert().EqualValues(cnt[suite.signerTwo.Addr], len(quorum)*2/3)
bitMap := make([]byte, len(quorum)/8)
bitMap[onePos/8] |= 1 << (onePos % 8)
suite.Assert().EqualValues(suite.queryGetAggPkG1(suite.signerOne, bitMap), struct {
AggPkG1 dasignersprecompile.BN254G1Point
Total *big.Int
Hit *big.Int
}{
AggPkG1: dasignersprecompile.NewBN254G1Point(bn254util.SerializeG1(new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(1)))),
Total: big.NewInt(int64(len(quorum))),
Hit: big.NewInt(int64(len(quorum) / 3)),
})
bitMap[twoPos/8] |= 1 << (twoPos % 8)
suite.Assert().EqualValues(suite.queryGetAggPkG1(suite.signerOne, bitMap), struct {
AggPkG1 dasignersprecompile.BN254G1Point
Total *big.Int
Hit *big.Int
}{
AggPkG1: dasignersprecompile.NewBN254G1Point(bn254util.SerializeG1(new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(1+11)))),
Total: big.NewInt(int64(len(quorum))),
Hit: big.NewInt(int64(len(quorum))),
})
}
func TestKeeperSuite(t *testing.T) {
suite.Run(t, new(DASignersTestSuite))
}

View File

@ -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())))
}

View File

@ -18,7 +18,7 @@ type AbciTestSuite struct {
}
func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
suite.App.InitializeFromGenesisStates()
// suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
params := suite.Keeper.GetParams(suite.Ctx)
suite.Require().Panics(func() {
@ -27,7 +27,7 @@ func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
}
func (suite *AbciTestSuite) TestBeginBlock_Success() {
suite.App.InitializeFromGenesisStates()
// suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
suite.Keeper.SetParams(suite.Ctx, types.Params{
TokensPerVote: 10,
@ -133,6 +133,6 @@ func (suite *AbciTestSuite) TestBeginBlock_Success() {
suite.Assert().EqualValues(cnt, 10)
}
func TestModuleTestSuite(t *testing.T) {
func TestAbciSuite(t *testing.T) {
suite.Run(t, new(AbciTestSuite))
}

View File

@ -24,7 +24,7 @@ func (k Keeper) Signer(
return nil, err
}
if !found {
return nil, nil
return nil, types.ErrSignerNotFound
}
response.Signer[i] = &signer
}

View File

@ -0,0 +1,338 @@
package keeper_test
import (
"encoding/hex"
"math/big"
"strings"
"testing"
"github.com/0glabs/0g-chain/crypto/bn254util"
"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"
"github.com/consensys/gnark-crypto/ecc/bn254"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/suite"
)
const (
signer1 = "9685C4EB29309820CDC62663CC6CC82F3D42E964"
signer2 = "9685C4EB29309820CDC62663CC6CC82F3D42E965"
)
type KeeperTestSuite struct {
testutil.Suite
}
func (suite *KeeperTestSuite) testRegisterSignerInvalidSignature() {
sk := big.NewInt(1)
pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), sk)
pkG2 := new(bn254.G2Affine).ScalarMultiplication(bn254util.GetG2Generator(), sk)
hash := types.PubkeyRegistrationHash(common.HexToAddress(signer1), big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, big.NewInt(2))
msg := &types.MsgRegisterSigner{
Signer: &types.Signer{
Account: signer1,
Socket: "0.0.0.0:1234",
PubkeyG1: bn254util.SerializeG1(pkG1),
PubkeyG2: bn254util.SerializeG2(pkG2),
},
Signature: bn254util.SerializeG1(signature),
}
_, err := suite.Keeper.RegisterSigner(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().ErrorIs(err, types.ErrInvalidSignature)
}
func (suite *KeeperTestSuite) testRegisterSignerSuccess() *types.Signer { // resgister signer
sk := big.NewInt(1)
pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), sk)
pkG2 := new(bn254.G2Affine).ScalarMultiplication(bn254util.GetG2Generator(), sk)
hash := types.PubkeyRegistrationHash(common.HexToAddress(signer1), big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
signer := &types.Signer{
Account: signer1,
Socket: "0.0.0.0:1234",
PubkeyG1: bn254util.SerializeG1(pkG1),
PubkeyG2: bn254util.SerializeG2(pkG2),
}
msg := &types.MsgRegisterSigner{
Signer: signer,
Signature: bn254util.SerializeG1(signature),
}
oldEventNum := len(suite.Ctx.EventManager().Events())
_, err := suite.Keeper.RegisterSigner(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().NoError(err)
events := suite.Ctx.EventManager().Events()
suite.Assert().EqualValues(len(events), oldEventNum+1)
suite.Assert().EqualValues(events[len(events)-1], sdk.NewEvent(
types.EventTypeUpdateSigner,
sdk.NewAttribute(types.AttributeKeySigner, signer.Account),
sdk.NewAttribute(types.AttributeKeySocket, signer.Socket),
sdk.NewAttribute(types.AttributeKeyPublicKeyG1, hex.EncodeToString(signer.PubkeyG1)),
sdk.NewAttribute(types.AttributeKeyPublicKeyG2, hex.EncodeToString(signer.PubkeyG2)),
))
return signer
}
func (suite *KeeperTestSuite) testQuerySigner(signer *types.Signer) {
response, err := suite.Keeper.Signer(sdk.WrapSDKContext(suite.Ctx), &types.QuerySignerRequest{
Accounts: []string{signer1},
})
suite.Assert().NoError(err)
suite.Assert().EqualValues(len(response.Signer), 1)
suite.Assert().EqualValues(response.Signer[0], signer)
_, err = suite.Keeper.Signer(sdk.WrapSDKContext(suite.Ctx), &types.QuerySignerRequest{
Accounts: []string{signer1, signer2},
})
suite.Assert().ErrorIs(err, types.ErrSignerNotFound)
}
func (suite *KeeperTestSuite) testUpdateSocket(signer *types.Signer) {
signer.Socket = "0.0.0.0:2345"
msg := &types.MsgUpdateSocket{
Account: signer2,
Socket: signer.Socket,
}
_, err := suite.Keeper.UpdateSocket(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().ErrorIs(err, types.ErrSignerNotFound)
msg.Account = signer.Account
oldEventNum := len(suite.Ctx.EventManager().Events())
_, err = suite.Keeper.UpdateSocket(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().NoError(err, types.ErrSignerNotFound)
events := suite.Ctx.EventManager().Events()
suite.Assert().EqualValues(len(events), oldEventNum+1)
suite.Assert().EqualValues(events[len(events)-1], sdk.NewEvent(
types.EventTypeUpdateSigner,
sdk.NewAttribute(types.AttributeKeySigner, signer.Account),
sdk.NewAttribute(types.AttributeKeySocket, signer.Socket),
sdk.NewAttribute(types.AttributeKeyPublicKeyG1, hex.EncodeToString(signer.PubkeyG1)),
sdk.NewAttribute(types.AttributeKeyPublicKeyG2, hex.EncodeToString(signer.PubkeyG2)),
))
}
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)
msg := &types.MsgRegisterNextEpoch{
Account: signer1,
Signature: bn254util.SerializeG1(signature),
}
_, err := suite.Keeper.RegisterNextEpoch(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().ErrorIs(err, types.ErrInvalidSignature)
}
func (suite *KeeperTestSuite) secondSigner() *types.Signer {
sk := big.NewInt(11)
pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), sk)
pkG2 := new(bn254.G2Affine).ScalarMultiplication(bn254util.GetG2Generator(), sk)
hash := types.PubkeyRegistrationHash(common.HexToAddress(signer2), big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
signer := &types.Signer{
Account: signer2,
Socket: "0.0.0.0:1234",
PubkeyG1: bn254util.SerializeG1(pkG1),
PubkeyG2: bn254util.SerializeG2(pkG2),
}
msg := &types.MsgRegisterSigner{
Signer: signer,
Signature: bn254util.SerializeG1(signature),
}
oldEventNum := len(suite.Ctx.EventManager().Events())
_, err := suite.Keeper.RegisterSigner(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().NoError(err)
events := suite.Ctx.EventManager().Events()
suite.Assert().EqualValues(len(events), oldEventNum+1)
suite.Assert().EqualValues(events[len(events)-1], sdk.NewEvent(
types.EventTypeUpdateSigner,
sdk.NewAttribute(types.AttributeKeySigner, signer.Account),
sdk.NewAttribute(types.AttributeKeySocket, signer.Socket),
sdk.NewAttribute(types.AttributeKeyPublicKeyG1, hex.EncodeToString(signer.PubkeyG1)),
sdk.NewAttribute(types.AttributeKeyPublicKeyG2, hex.EncodeToString(signer.PubkeyG2)),
))
// register epoch
hash = types.EpochRegistrationHash(common.HexToAddress(signer2), 1, big.NewInt(8888))
signature = new(bn254.G1Affine).ScalarMultiplication(hash, sk)
msg2 := &types.MsgRegisterNextEpoch{
Account: signer2,
Signature: bn254util.SerializeG1(signature),
}
_, err = suite.Keeper.RegisterNextEpoch(sdk.WrapSDKContext(suite.Ctx), msg2)
suite.Assert().NoError(err, types.ErrSignerNotFound)
return signer
}
func (suite *KeeperTestSuite) testRegisterEpochSuccess() {
sk := big.NewInt(1)
hash := types.EpochRegistrationHash(common.HexToAddress(signer1), 1, big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
msg := &types.MsgRegisterNextEpoch{
Account: signer1,
Signature: bn254util.SerializeG1(signature),
}
_, err := suite.Keeper.RegisterNextEpoch(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Assert().NoError(err, types.ErrSignerNotFound)
}
func (suite *KeeperTestSuite) newEpoch(params types.Params) {
// 1st ballot of signer1: 1d5df5684184f84a8dbd20b158b6478a6e8eb021b1cf81ac281dd4c7af4370ed30231e1a6a1d76bac5f464f10c7e99afa8df3c4643ca447bfc80f248764ab2ac
// 1st ballot of signer2: 103d29532b47eb7df57049180475d72737f7ab2be4a0f3614aedbb61c8a844a32c76fcbb29b937c56c577121dfd4be8041e2b4acfe2523ae54f0d6f604745b06
// 2nd ballot of signer2: 93a5bb4c22640a155b18e24c0c584f2bc4bdd94ddb786d86ff3c3816d741e67f
// sorted ballots: 2-1, 1-1, 2-2
suite.Ctx = suite.Ctx.WithBlockHeight(int64(params.EpochBlocks) * 1)
suite.Keeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{})
}
func (suite *KeeperTestSuite) queryEpochNumber() {
response, err := suite.Keeper.EpochNumber(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochNumberRequest{})
suite.Assert().NoError(err)
suite.Assert().EqualValues(response.EpochNumber, 1)
}
func (suite *KeeperTestSuite) queryQuorumCount() {
response, err := suite.Keeper.QuorumCount(sdk.WrapSDKContext(suite.Ctx), &types.QueryQuorumCountRequest{
EpochNumber: 1,
})
suite.Assert().NoError(err)
suite.Assert().EqualValues(response.QuorumCount, 1)
}
func (suite *KeeperTestSuite) queryEpochQuorum(params types.Params) {
_, err := suite.Keeper.EpochQuorum(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRequest{
EpochNumber: 1,
QuorumId: 1,
})
suite.Assert().ErrorIs(err, types.ErrQuorumIdOutOfBound)
response, err := suite.Keeper.EpochQuorum(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRequest{
EpochNumber: 1,
QuorumId: 0,
})
suite.Assert().NoError(err)
quorum := make([]string, 0)
for i := 0; i < int(params.EncodedSlices); i += 1 {
if i%3 == 1 {
quorum = append(quorum, strings.ToLower(signer1))
} else {
quorum = append(quorum, strings.ToLower(signer2))
}
}
suite.Assert().EqualValues(response.Quorum.Signers, quorum)
}
func (suite *KeeperTestSuite) queryEpochQuorumRow(params types.Params) {
_, err := suite.Keeper.EpochQuorumRow(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRowRequest{
EpochNumber: 1,
QuorumId: 0,
RowIndex: uint32(params.EncodedSlices),
})
suite.Assert().ErrorIs(err, types.ErrRowIndexOutOfBound)
response, err := suite.Keeper.EpochQuorumRow(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRowRequest{
EpochNumber: 1,
QuorumId: 0,
RowIndex: 0,
})
suite.Assert().NoError(err)
suite.Assert().EqualValues(response.Signer, strings.ToLower(signer2))
response, err = suite.Keeper.EpochQuorumRow(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRowRequest{
EpochNumber: 1,
QuorumId: 0,
RowIndex: 1,
})
suite.Assert().NoError(err)
suite.Assert().EqualValues(response.Signer, strings.ToLower(signer1))
response, err = suite.Keeper.EpochQuorumRow(sdk.WrapSDKContext(suite.Ctx), &types.QueryEpochQuorumRowRequest{
EpochNumber: 1,
QuorumId: 0,
RowIndex: 2,
})
suite.Assert().NoError(err)
suite.Assert().EqualValues(response.Signer, strings.ToLower(signer2))
}
func (suite *KeeperTestSuite) queryAggregatePubkeyG1(params types.Params) {
quorumBitMap := make([]byte, params.EncodedSlices/8-1)
_, err := suite.Keeper.AggregatePubkeyG1(sdk.WrapSDKContext(suite.Ctx), &types.QueryAggregatePubkeyG1Request{
EpochNumber: 1,
QuorumId: 0,
QuorumBitmap: quorumBitMap,
})
suite.Assert().ErrorIs(err, types.ErrQuorumBitmapLengthMismatch)
quorumBitMap = append(quorumBitMap, byte(0))
response, err := suite.Keeper.AggregatePubkeyG1(sdk.WrapSDKContext(suite.Ctx), &types.QueryAggregatePubkeyG1Request{
EpochNumber: 1,
QuorumId: 0,
QuorumBitmap: quorumBitMap,
})
suite.Assert().NoError(err)
pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(0))
suite.Assert().EqualValues(response.AggregatePubkeyG1, bn254util.SerializeG1(pkG1))
suite.Assert().EqualValues(response.Total, params.EncodedSlices)
suite.Assert().EqualValues(response.Hit, 0)
quorumBitMap[0] = byte(1)
response, err = suite.Keeper.AggregatePubkeyG1(sdk.WrapSDKContext(suite.Ctx), &types.QueryAggregatePubkeyG1Request{
EpochNumber: 1,
QuorumId: 0,
QuorumBitmap: quorumBitMap,
})
suite.Assert().NoError(err)
pkG1 = new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(11))
suite.Assert().EqualValues(response.AggregatePubkeyG1, bn254util.SerializeG1(pkG1))
suite.Assert().EqualValues(response.Total, params.EncodedSlices)
suite.Assert().EqualValues(response.Hit, params.EncodedSlices*2/3)
quorumBitMap[0] = byte(2)
response, err = suite.Keeper.AggregatePubkeyG1(sdk.WrapSDKContext(suite.Ctx), &types.QueryAggregatePubkeyG1Request{
EpochNumber: 1,
QuorumId: 0,
QuorumBitmap: quorumBitMap,
})
suite.Assert().NoError(err)
pkG1 = new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(1))
suite.Assert().EqualValues(response.AggregatePubkeyG1, bn254util.SerializeG1(pkG1))
suite.Assert().EqualValues(response.Total, params.EncodedSlices)
suite.Assert().EqualValues(response.Hit, params.EncodedSlices/3)
quorumBitMap[0] = byte(3)
response, err = suite.Keeper.AggregatePubkeyG1(sdk.WrapSDKContext(suite.Ctx), &types.QueryAggregatePubkeyG1Request{
EpochNumber: 1,
QuorumId: 0,
QuorumBitmap: quorumBitMap,
})
suite.Assert().NoError(err)
pkG1 = new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(1+11))
suite.Assert().EqualValues(response.AggregatePubkeyG1, bn254util.SerializeG1(pkG1))
suite.Assert().EqualValues(response.Total, params.EncodedSlices)
suite.Assert().EqualValues(response.Hit, params.EncodedSlices)
}
func (suite *KeeperTestSuite) Test_Keeper() {
// suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
// add delegation
params := suite.Keeper.GetParams(suite.Ctx)
suite.AddDelegation(signer1, signer1, keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)))
suite.AddDelegation(signer2, signer1, keeper.BondedConversionRate.Mul(sdk.NewIntFromUint64(params.TokensPerVote)).Mul(sdk.NewIntFromUint64(2)))
// test
suite.testRegisterSignerInvalidSignature()
signerOne := suite.testRegisterSignerSuccess()
suite.testQuerySigner(signerOne)
suite.testUpdateSocket(signerOne)
suite.testRegisterEpochInvalidSignature()
suite.testRegisterEpochSuccess()
suite.secondSigner()
suite.newEpoch(params)
suite.queryEpochNumber()
suite.queryQuorumCount()
suite.queryEpochQuorum(params)
suite.queryEpochQuorumRow(params)
suite.queryAggregatePubkeyG1(params)
}
func TestKeeperSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

View File

@ -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"
@ -31,9 +34,25 @@ type Suite struct {
func (suite *Suite) SetupTest() {
chaincfg.SetSDKConfig()
suite.App = app.NewTestApp()
suite.App.InitializeFromGenesisStates()
suite.Keeper = suite.App.GetDASignersKeeper()
suite.StakingKeeper = suite.App.GetStakingKeeper()
suite.Ctx = suite.App.NewContext(true, tmproto.Header{})
// 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

View File

@ -1,7 +1,6 @@
package types_test
import (
fmt "fmt"
"math/big"
"testing"
@ -25,10 +24,5 @@ func Test_ValidateSignature(t *testing.T) {
assert.NoError(t, signer.Validate())
hash := types.PubkeyRegistrationHash(common.HexToAddress("0x9685C4EB29309820CDC62663CC6CC82F3D42E964"), big.NewInt(8888))
signature := new(bn254.G1Affine).ScalarMultiplication(hash, big.NewInt(1))
fmt.Printf(
"registration signature G1 X: %v, Y: %v\n ",
signature.X.BigInt(new(big.Int)),
signature.Y.BigInt(new(big.Int)),
)
assert.Equal(t, signer.ValidateSignature(hash, signature), true)
}