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

This commit is contained in:
MiniFrenchBread 2024-08-07 18:16:55 +08:00
parent 7b1b0eecbe
commit a671f5140c
4 changed files with 123 additions and 4 deletions

View File

@ -13,11 +13,11 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
type ModuleTestSuite struct { type AbciTestSuite struct {
testutil.Suite testutil.Suite
} }
func (suite *ModuleTestSuite) TestBeginBlock_NotContinuous() { func (suite *AbciTestSuite) TestBeginBlock_NotContinuous() {
suite.App.InitializeFromGenesisStates() suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
params := suite.Keeper.GetParams(suite.Ctx) params := suite.Keeper.GetParams(suite.Ctx)
@ -26,7 +26,7 @@ func (suite *ModuleTestSuite) TestBeginBlock_NotContinuous() {
}, "block height is not continuous") }, "block height is not continuous")
} }
func (suite *ModuleTestSuite) TestBeginBlock_Success() { func (suite *AbciTestSuite) TestBeginBlock_Success() {
suite.App.InitializeFromGenesisStates() suite.App.InitializeFromGenesisStates()
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState()) dasigners.InitGenesis(suite.Ctx, suite.Keeper, *types.DefaultGenesisState())
suite.Keeper.SetParams(suite.Ctx, types.Params{ suite.Keeper.SetParams(suite.Ctx, types.Params{
@ -134,5 +134,5 @@ func (suite *ModuleTestSuite) TestBeginBlock_Success() {
} }
func TestModuleTestSuite(t *testing.T) { func TestModuleTestSuite(t *testing.T) {
suite.Run(t, new(ModuleTestSuite)) suite.Run(t, new(AbciTestSuite))
} }

View File

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

View File

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

View File

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