mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-28 08:52:29 +00:00
add unit test for msg_server_register_trust_anchor
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
688f13e171
commit
b0d4ffad7a
@ -114,3 +114,9 @@ func ExtendedKeyPair(cfg chaincfg.Params) (string, string) {
|
|||||||
}
|
}
|
||||||
return xprivKey.String(), xpubKey.String()
|
return xprivKey.String(), xpubKey.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TrustAnchor() machinetypes.TrustAnchor {
|
||||||
|
return machinetypes.TrustAnchor{
|
||||||
|
Pubkey: PubKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,16 +2,28 @@ package keeper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
config "planetmint-go/config"
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"planetmint-go/x/machine/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegisterTrustAnchor) (*types.MsgRegisterTrustAnchorResponse, error) {
|
func (k msgServer) RegisterTrustAnchor(goCtx context.Context, msg *types.MsgRegisterTrustAnchor) (*types.MsgRegisterTrustAnchorResponse, error) {
|
||||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||||
|
|
||||||
// TODO: Handling the message
|
isValidTrustAnchorPubkey := validateExtendedPublicKey(msg.TrustAnchor.Pubkey, config.LiquidNetParams)
|
||||||
_ = ctx
|
if !isValidTrustAnchorPubkey {
|
||||||
|
return nil, errors.New("invalid trust anchor pubkey")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, found := k.GetTrustAnchor(ctx, msg.TrustAnchor.Pubkey)
|
||||||
|
if found {
|
||||||
|
return nil, errors.New("trust anchor is already registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
k.StoreTrustAnchor(ctx, *msg.TrustAnchor, false)
|
||||||
|
|
||||||
return &types.MsgRegisterTrustAnchorResponse{}, nil
|
return &types.MsgRegisterTrustAnchorResponse{}, nil
|
||||||
}
|
}
|
||||||
|
@ -46,3 +46,36 @@ func TestMsgServerAttestMachineInvalidLiquidKey(t *testing.T) {
|
|||||||
_, err := msgServer.AttestMachine(ctx, msg)
|
_, err := msgServer.AttestMachine(ctx, msg)
|
||||||
assert.EqualError(t, err, "invalid liquid key")
|
assert.EqualError(t, err, "invalid liquid key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMsgServerRegisterTrustAnchor(t *testing.T) {
|
||||||
|
_, pk := sample.KeyPair()
|
||||||
|
ta := sample.TrustAnchor()
|
||||||
|
msg := types.NewMsgRegisterTrustAnchor(pk, &ta)
|
||||||
|
msgServer, ctx := setupMsgServer(t)
|
||||||
|
res, err := msgServer.RegisterTrustAnchor(ctx, msg)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, &types.MsgRegisterTrustAnchorResponse{}, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMsgServerRegisterTrustAnchorTwice(t *testing.T) {
|
||||||
|
_, pk := sample.KeyPair()
|
||||||
|
ta := sample.TrustAnchor()
|
||||||
|
msg := types.NewMsgRegisterTrustAnchor(pk, &ta)
|
||||||
|
msgServer, ctx := setupMsgServer(t)
|
||||||
|
res, err := msgServer.RegisterTrustAnchor(ctx, msg)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, &types.MsgRegisterTrustAnchorResponse{}, res)
|
||||||
|
}
|
||||||
|
_, err = msgServer.RegisterTrustAnchor(ctx, msg)
|
||||||
|
assert.EqualError(t, err, "trust anchor is already registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMsgServerRegisterTrustAnchorInvalidPubkey(t *testing.T) {
|
||||||
|
_, pk := sample.KeyPair()
|
||||||
|
ta := sample.TrustAnchor()
|
||||||
|
msg := types.NewMsgRegisterTrustAnchor(pk, &ta)
|
||||||
|
msgServer, ctx := setupMsgServer(t)
|
||||||
|
_, err := msgServer.RegisterTrustAnchor(ctx, msg)
|
||||||
|
assert.EqualError(t, err, "invalid trust anchor pubkey")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user