Skip to content

Commit

Permalink
x/ibc: move solo machine sequence to client state (cosmos#7271)
Browse files Browse the repository at this point in the history
* update proto

* move solo machine sequence to client state

Co-authored-by: Christopher Goes <[email protected]>
  • Loading branch information
colin-axner and cwgoes authored Sep 9, 2020
1 parent 93dd06a commit ec74416
Show file tree
Hide file tree
Showing 30 changed files with 829 additions and 767 deletions.
1 change: 1 addition & 0 deletions client/grpc/reflection/reflection.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/grpc/simulate/simulate.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions proto/ibc/lightclients/solomachine/v1/solomachine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import "google/protobuf/any.proto";
// state and if the client is frozen.
message ClientState {
option (gogoproto.goproto_getters) = false;
// latest sequence of the client state
uint64 sequence = 1;
// frozen sequence of the solo machine
uint64 frozen_sequence = 1 [(gogoproto.moretags) = "yaml:\"frozen_sequence\""];
ConsensusState consensus_state = 2 [(gogoproto.moretags) = "yaml:\"consensus_state\""];
uint64 frozen_sequence = 2 [(gogoproto.moretags) = "yaml:\"frozen_sequence\""];
ConsensusState consensus_state = 3 [(gogoproto.moretags) = "yaml:\"consensus_state\""];
// when set to true, will allow governance to update a solo machine client.
// The client will be unfrozen if it is frozen.
bool allow_update_after_proposal = 3 [(gogoproto.moretags) = "yaml:\"allow_update_after_proposal\""];
bool allow_update_after_proposal = 4 [(gogoproto.moretags) = "yaml:\"allow_update_after_proposal\""];
}

// ConsensusState defines a solo machine consensus state
// ConsensusState defines a solo machine consensus state. The sequence of a consensus state
// is contained in the "height" key used in storing the consensus state.
message ConsensusState {
option (gogoproto.goproto_getters) = false;
// current sequence of the consensus state
uint64 sequence = 1;
// public key of the solo machine
cosmos.base.crypto.v1beta1.PublicKey public_key = 2 [(gogoproto.moretags) = "yaml:\"public_key\""];
cosmos.base.crypto.v1beta1.PublicKey public_key = 1 [(gogoproto.moretags) = "yaml:\"public_key\""];
// diversifier allows the same public key to be re-used across different solo machine clients
// (potentially on different chains) without being considered misbehaviour.
string diversifier = 3;
uint64 timestamp = 4;
string diversifier = 2;
uint64 timestamp = 3;
}

// Header defines a solo machine consensus header
Expand Down Expand Up @@ -148,4 +149,4 @@ message PacketAcknowledgementAbsenseData {
message NextSequenceRecvData {
bytes path = 1;
uint64 next_seq_recv = 2 [(gogoproto.moretags) = "yaml:\"next_seq_recv\""];
}
}
1 change: 1 addition & 0 deletions x/auth/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/bank/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/distribution/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/evidence/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/gov/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/ibc-transfer/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/ibc/02-client/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/ibc/03-connection/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/ibc/04-channel/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions x/ibc/light-clients/solomachine/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"io/ioutil"
"strconv"

"github.com/pkg/errors"

Expand All @@ -24,11 +25,11 @@ const (
// NewCreateClientCmd defines the command to create a new solo machine client.
func NewCreateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [client-id] [path/to/consensus_state.json]",
Use: "create [client-id] [sequence] [path/to/consensus_state.json]",
Short: "create new solo machine client",
Long: "create a new solo machine client with the specified identifier and consensus state",
Example: fmt.Sprintf("%s tx ibc %s create [client-id] [path/to/consensus_state.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
Example: fmt.Sprintf("%s tx ibc %s create [client-id] [sequence] [path/to/consensus_state.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
Expand All @@ -38,10 +39,15 @@ func NewCreateClientCmd() *cobra.Command {

clientID := args[0]

sequence, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

var consensusState *types.ConsensusState
if err := cdc.UnmarshalJSON([]byte(args[1]), consensusState); err != nil {
if err := cdc.UnmarshalJSON([]byte(args[2]), consensusState); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
Expand All @@ -54,7 +60,7 @@ func NewCreateClientCmd() *cobra.Command {

allowUpdateAfterProposal, _ := cmd.Flags().GetBool(flagAllowUpdateAfterProposal)

clientState := types.NewClientState(consensusState, allowUpdateAfterProposal)
clientState := types.NewClientState(sequence, consensusState, allowUpdateAfterProposal)
msg, err := clienttypes.NewMsgCreateClient(clientID, clientState, consensusState, clientCtx.GetFromAddress())
if err != nil {
return err
Expand Down
28 changes: 16 additions & 12 deletions x/ibc/light-clients/solomachine/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
var _ exported.ClientState = (*ClientState)(nil)

// NewClientState creates a new ClientState instance.
func NewClientState(consensusState *ConsensusState, allowUpdateAfterProposal bool) *ClientState {
func NewClientState(latestSequence uint64, consensusState *ConsensusState, allowUpdateAfterProposal bool) *ClientState {
return &ClientState{
Sequence: latestSequence,
FrozenSequence: 0,
ConsensusState: consensusState,
AllowUpdateAfterProposal: allowUpdateAfterProposal,
Expand All @@ -29,10 +30,10 @@ func (cs ClientState) ClientType() exported.ClientType {
}

// GetLatestHeight returns the latest sequence number.
// Return exported.Height to satisfy interface
// Epoch number is always 0 for a solo-machine
// Return exported.Height to satisfy ClientState interface
// Epoch number is always 0 for a solo-machine.
func (cs ClientState) GetLatestHeight() exported.Height {
return clienttypes.NewHeight(0, cs.ConsensusState.Sequence)
return clienttypes.NewHeight(0, cs.Sequence)
}

// IsFrozen returns true if the client is frozen.
Expand All @@ -54,6 +55,9 @@ func (cs ClientState) GetProofSpecs() []*ics23.ProofSpec {

// Validate performs basic validation of the client state fields.
func (cs ClientState) Validate() error {
if cs.Sequence == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "sequence cannot be 0")
}
if cs.ConsensusState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be nil")
}
Expand Down Expand Up @@ -92,7 +96,7 @@ func (cs ClientState) VerifyClientState(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -131,7 +135,7 @@ func (cs ClientState) VerifyClientConsensusState(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -167,7 +171,7 @@ func (cs ClientState) VerifyConnectionState(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -204,7 +208,7 @@ func (cs ClientState) VerifyChannelState(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -242,7 +246,7 @@ func (cs ClientState) VerifyPacketCommitment(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -280,7 +284,7 @@ func (cs ClientState) VerifyPacketAcknowledgement(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -318,7 +322,7 @@ func (cs ClientState) VerifyPacketAcknowledgementAbsence(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down Expand Up @@ -355,7 +359,7 @@ func (cs ClientState) VerifyNextSequenceRecv(
return err
}

cs.ConsensusState.Sequence++
cs.Sequence++
cs.ConsensusState.Timestamp = signature.Timestamp
setClientState(store, cdc, &cs)
return nil
Expand Down
Loading

0 comments on commit ec74416

Please sign in to comment.