2020-08-26 05:11:31 -07:00
|
|
|
package ibctesting
|
2020-08-24 03:06:48 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-09-21 09:48:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2020-09-03 13:23:20 -07:00
|
|
|
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/ibc/exported"
|
2020-08-24 03:06:48 -07:00
|
|
|
solomachinetypes "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/solomachine/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Solomachine is a testing helper used to simulate a counterparty
|
|
|
|
// solo machine client.
|
|
|
|
type Solomachine struct {
|
|
|
|
t *testing.T
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
cdc codec.BinaryMarshaler
|
|
|
|
ClientID string
|
|
|
|
PrivateKey crypto.PrivKey
|
|
|
|
PublicKey crypto.PubKey
|
|
|
|
Sequence uint64
|
|
|
|
Time uint64
|
|
|
|
Diversifier string
|
2020-08-24 03:06:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSolomachine returns a new solomachine instance with a generated private/public
|
|
|
|
// key pair and a sequence starting at 1.
|
2020-09-07 05:46:48 -07:00
|
|
|
func NewSolomachine(t *testing.T, cdc codec.BinaryMarshaler, clientID, diversifier string) *Solomachine {
|
2020-09-21 09:48:28 -07:00
|
|
|
privKey := secp256k1.GenPrivKey()
|
2020-08-24 03:06:48 -07:00
|
|
|
|
|
|
|
return &Solomachine{
|
2020-09-07 05:46:48 -07:00
|
|
|
t: t,
|
|
|
|
cdc: cdc,
|
|
|
|
ClientID: clientID,
|
|
|
|
PrivateKey: privKey,
|
|
|
|
PublicKey: privKey.PubKey(),
|
|
|
|
Sequence: 1,
|
|
|
|
Time: 10,
|
|
|
|
Diversifier: diversifier,
|
2020-08-24 03:06:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
// ClientState returns a new solo machine ClientState instance. Default usage does not allow update
|
|
|
|
// after governance proposal
|
2020-08-24 03:06:48 -07:00
|
|
|
func (solo *Solomachine) ClientState() *solomachinetypes.ClientState {
|
2020-09-09 08:14:11 -07:00
|
|
|
return solomachinetypes.NewClientState(solo.Sequence, solo.ConsensusState(), false)
|
2020-08-24 03:06:48 -07:00
|
|
|
}
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
// ConsensusState returns a new solo machine ConsensusState instance
|
2020-08-24 03:06:48 -07:00
|
|
|
func (solo *Solomachine) ConsensusState() *solomachinetypes.ConsensusState {
|
2020-09-21 09:48:28 -07:00
|
|
|
publicKey, err := tx.PubKeyToAny(solo.PublicKey)
|
2020-08-24 03:06:48 -07:00
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
return &solomachinetypes.ConsensusState{
|
2020-09-07 05:46:48 -07:00
|
|
|
PublicKey: publicKey,
|
|
|
|
Diversifier: solo.Diversifier,
|
|
|
|
Timestamp: solo.Time,
|
2020-08-24 03:06:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:23:20 -07:00
|
|
|
// GetHeight returns an exported.Height with Sequence as EpochHeight
|
|
|
|
func (solo *Solomachine) GetHeight() exported.Height {
|
|
|
|
return clienttypes.NewHeight(0, solo.Sequence)
|
|
|
|
}
|
|
|
|
|
2020-08-24 03:06:48 -07:00
|
|
|
// CreateHeader generates a new private/public key pair and creates the
|
|
|
|
// necessary signature to construct a valid solo machine header.
|
2020-08-25 07:17:38 -07:00
|
|
|
func (solo *Solomachine) CreateHeader() *solomachinetypes.Header {
|
2020-08-24 03:06:48 -07:00
|
|
|
// generate new private key and signature for header
|
2020-09-21 09:48:28 -07:00
|
|
|
newPrivKey := secp256k1.GenPrivKey()
|
2020-08-24 03:06:48 -07:00
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
publicKey, err := tx.PubKeyToAny(newPrivKey.PubKey())
|
2020-08-24 03:06:48 -07:00
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
data := &solomachinetypes.HeaderData{
|
|
|
|
NewPubKey: publicKey,
|
|
|
|
NewDiversifier: solo.Diversifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
dataBz, err := solo.cdc.MarshalBinaryBare(data)
|
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
signBytes := &solomachinetypes.SignBytes{
|
|
|
|
Sequence: solo.Sequence,
|
|
|
|
Timestamp: solo.Time,
|
|
|
|
Diversifier: solo.Diversifier,
|
|
|
|
Data: dataBz,
|
|
|
|
}
|
|
|
|
|
|
|
|
signBz, err := solo.cdc.MarshalBinaryBare(signBytes)
|
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
signature, err := solo.PrivateKey.Sign(signBz)
|
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
2020-08-25 07:17:38 -07:00
|
|
|
header := &solomachinetypes.Header{
|
2020-09-07 05:46:48 -07:00
|
|
|
Sequence: solo.Sequence,
|
|
|
|
Timestamp: solo.Time,
|
|
|
|
Signature: signature,
|
|
|
|
NewPublicKey: publicKey,
|
|
|
|
NewDiversifier: solo.Diversifier,
|
2020-08-24 03:06:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// assumes successful header update
|
|
|
|
solo.Sequence++
|
|
|
|
solo.PrivateKey = newPrivKey
|
|
|
|
solo.PublicKey = newPrivKey.PubKey()
|
|
|
|
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:51:13 -07:00
|
|
|
// CreateMisbehaviour constructs testing misbehaviour for the solo machine client
|
2020-08-24 03:06:48 -07:00
|
|
|
// by signing over two different data bytes at the same sequence.
|
2020-08-26 10:51:13 -07:00
|
|
|
func (solo *Solomachine) CreateMisbehaviour() *solomachinetypes.Misbehaviour {
|
2020-08-24 03:06:48 -07:00
|
|
|
dataOne := []byte("DATA ONE")
|
|
|
|
dataTwo := []byte("DATA TWO")
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
signBytes := &solomachinetypes.SignBytes{
|
|
|
|
Sequence: solo.Sequence,
|
|
|
|
Timestamp: solo.Time,
|
|
|
|
Diversifier: solo.Diversifier,
|
|
|
|
Data: dataOne,
|
|
|
|
}
|
|
|
|
|
|
|
|
signBz, err := solo.cdc.MarshalBinaryBare(signBytes)
|
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
sig, err := solo.PrivateKey.Sign(signBz)
|
2020-08-24 03:06:48 -07:00
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
signatureOne := solomachinetypes.SignatureAndData{
|
|
|
|
Signature: sig,
|
|
|
|
Data: dataOne,
|
|
|
|
}
|
|
|
|
|
2020-09-07 05:46:48 -07:00
|
|
|
signBytes = &solomachinetypes.SignBytes{
|
|
|
|
Sequence: solo.Sequence,
|
|
|
|
Timestamp: solo.Time,
|
|
|
|
Diversifier: solo.Diversifier,
|
|
|
|
Data: dataTwo,
|
|
|
|
}
|
|
|
|
|
|
|
|
signBz, err = solo.cdc.MarshalBinaryBare(signBytes)
|
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
sig, err = solo.PrivateKey.Sign(signBz)
|
2020-08-24 03:06:48 -07:00
|
|
|
require.NoError(solo.t, err)
|
|
|
|
|
|
|
|
signatureTwo := solomachinetypes.SignatureAndData{
|
|
|
|
Signature: sig,
|
|
|
|
Data: dataTwo,
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:51:13 -07:00
|
|
|
return &solomachinetypes.Misbehaviour{
|
2020-08-24 03:06:48 -07:00
|
|
|
ClientId: solo.ClientID,
|
|
|
|
Sequence: solo.Sequence,
|
|
|
|
SignatureOne: &signatureOne,
|
|
|
|
SignatureTwo: &signatureTwo,
|
|
|
|
}
|
|
|
|
}
|