From a671f5140c1563d4807a494e658a489491bf0cb8 Mon Sep 17 00:00:00 2001 From: MiniFrenchBread <103425574+MiniFrenchBread@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:16:55 +0800 Subject: [PATCH] test: types --- x/dasigners/v1/keeper/abci_test.go | 8 ++-- x/dasigners/v1/types/hash_test.go | 22 ++++++++++ x/dasigners/v1/types/msg_test.go | 63 +++++++++++++++++++++++++++++ x/dasigners/v1/types/signer_test.go | 34 ++++++++++++++++ 4 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 x/dasigners/v1/types/hash_test.go create mode 100644 x/dasigners/v1/types/msg_test.go create mode 100644 x/dasigners/v1/types/signer_test.go diff --git a/x/dasigners/v1/keeper/abci_test.go b/x/dasigners/v1/keeper/abci_test.go index a2e43c8d..5e469ccd 100644 --- a/x/dasigners/v1/keeper/abci_test.go +++ b/x/dasigners/v1/keeper/abci_test.go @@ -13,11 +13,11 @@ import ( "github.com/stretchr/testify/suite" ) -type ModuleTestSuite struct { +type AbciTestSuite struct { testutil.Suite } -func (suite *ModuleTestSuite) TestBeginBlock_NotContinuous() { +func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() { suite.App.InitializeFromGenesisStates() dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) params := suite.Keeper.GetParams(suite.Ctx) @@ -26,7 +26,7 @@ func (suite *ModuleTestSuite) TestBeginBlock_NotContinuous() { }, "block height is not continuous") } -func (suite *ModuleTestSuite) TestBeginBlock_Success() { +func (suite *AbciTestSuite) TestBeginBlock_Success() { suite.App.InitializeFromGenesisStates() dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) suite.Keeper.SetParams(suite.Ctx, types.Params{ @@ -134,5 +134,5 @@ func (suite *ModuleTestSuite) TestBeginBlock_Success() { } func TestModuleTestSuite(t *testing.T) { - suite.Run(t, new(ModuleTestSuite)) + suite.Run(t, new(AbciTestSuite)) } diff --git a/x/dasigners/v1/types/hash_test.go b/x/dasigners/v1/types/hash_test.go new file mode 100644 index 00000000..ba731007 --- /dev/null +++ b/x/dasigners/v1/types/hash_test.go @@ -0,0 +1,22 @@ +package types_test + +import ( + "math/big" + "testing" + + "github.com/0glabs/0g-chain/x/dasigners/v1/types" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/assert" +) + +func Test_PubkeyRegistrationHash(t *testing.T) { + hash := types.PubkeyRegistrationHash(common.HexToAddress("0x9685C4EB29309820CDC62663CC6CC82F3D42E964"), big.NewInt(8888)) + assert.Equal(t, hash.X.String(), "17347288745752564851578145205408924577042674846071448492673629564958667746090") + assert.Equal(t, hash.Y.String(), "21456041422468658262738002909407073439935597271458862589356790821116767485654") +} + +func Test_EpochRegistrationHash(t *testing.T) { + hash := types.EpochRegistrationHash(common.HexToAddress("0x9685C4EB29309820CDC62663CC6CC82F3D42E964"), 1, big.NewInt(8888)) + assert.Equal(t, hash.X.String(), "13283083124528531674735853832182424672122091139683454761857829308708073730285") + assert.Equal(t, hash.Y.String(), "21773064143788270772276852950775943855438706734263253481317981346601766662828") +} diff --git a/x/dasigners/v1/types/msg_test.go b/x/dasigners/v1/types/msg_test.go new file mode 100644 index 00000000..d75529f3 --- /dev/null +++ b/x/dasigners/v1/types/msg_test.go @@ -0,0 +1,63 @@ +package types_test + +import ( + "math/big" + "testing" + + "github.com/0glabs/0g-chain/crypto/bn254util" + "github.com/0glabs/0g-chain/x/dasigners/v1/testutil" + "github.com/0glabs/0g-chain/x/dasigners/v1/types" + "github.com/consensys/gnark-crypto/ecc/bn254" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/suite" +) + +type MsgTestSuite struct { + testutil.Suite +} + +func (suite *MsgTestSuite) Test_MsgRegisterSigner() { + 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("0x9685C4EB29309820CDC62663CC6CC82F3D42E964"), big.NewInt(8888)) + signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk) + msg := &types.MsgRegisterSigner{ + Signer: &types.Signer{ + Account: "9685C4EB29309820CDC62663CC6CC82F3D42E964", + Socket: "0.0.0.0:1234", + PubkeyG1: bn254util.SerializeG1(pkG1), + PubkeyG2: bn254util.SerializeG2(pkG2), + }, + Signature: bn254util.SerializeG1(signature), + } + suite.Assert().EqualValues(len(msg.GetSigners()), 1) + suite.Assert().EqualValues(msg.GetSigners()[0].String(), "0g1j6zuf6efxzvzpnwxye3ucmxg9u7596ty686hna") + suite.Assert().NoError(msg.ValidateBasic()) +} + +func (suite *MsgTestSuite) Test_MsgUpdateSocket() { + msg := &types.MsgUpdateSocket{ + Account: "9685C4EB29309820CDC62663CC6CC82F3D42E964", + Socket: "0.0.0.0:1234", + } + suite.Assert().EqualValues(len(msg.GetSigners()), 1) + suite.Assert().EqualValues(msg.GetSigners()[0].String(), "0g1j6zuf6efxzvzpnwxye3ucmxg9u7596ty686hna") + suite.Assert().NoError(msg.ValidateBasic()) +} + +func (suite *MsgTestSuite) Test_MsgRegisterNextEpoch() { + hash := types.EpochRegistrationHash(common.HexToAddress("0x9685C4EB29309820CDC62663CC6CC82F3D42E964"), 1, big.NewInt(8888)) + signature := new(bn254.G1Affine).ScalarMultiplication(hash, big.NewInt(1)) + msg := &types.MsgRegisterNextEpoch{ + Account: "9685C4EB29309820CDC62663CC6CC82F3D42E964", + Signature: bn254util.SerializeG1(signature), + } + suite.Assert().EqualValues(len(msg.GetSigners()), 1) + suite.Assert().EqualValues(msg.GetSigners()[0].String(), "0g1j6zuf6efxzvzpnwxye3ucmxg9u7596ty686hna") + suite.Assert().NoError(msg.ValidateBasic()) +} + +func TestSuite(t *testing.T) { + suite.Run(t, new(MsgTestSuite)) +} diff --git a/x/dasigners/v1/types/signer_test.go b/x/dasigners/v1/types/signer_test.go new file mode 100644 index 00000000..ae2a40f1 --- /dev/null +++ b/x/dasigners/v1/types/signer_test.go @@ -0,0 +1,34 @@ +package types_test + +import ( + fmt "fmt" + "math/big" + "testing" + + "github.com/0glabs/0g-chain/crypto/bn254util" + "github.com/0glabs/0g-chain/x/dasigners/v1/types" + "github.com/consensys/gnark-crypto/ecc/bn254" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/assert" +) + +func Test_ValidateSignature(t *testing.T) { + sk := big.NewInt(1) + pkG1 := new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), sk) + pkG2 := new(bn254.G2Affine).ScalarMultiplication(bn254util.GetG2Generator(), sk) + signer := types.Signer{ + Account: "9685C4EB29309820CDC62663CC6CC82F3D42E964", + Socket: "0.0.0.0:1234", + PubkeyG1: bn254util.SerializeG1(pkG1), + PubkeyG2: bn254util.SerializeG2(pkG2), + } + 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) +}