Skip to content

Commit

Permalink
use typed string for status
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner committed May 3, 2021
1 parent 90c9f29 commit cca717d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion modules/core/02-client/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (q Keeper) ClientStatus(c context.Context, req *types.QueryClientStatusRequ
status := clientState.Status(ctx, clientStore, q.cdc)

return &types.QueryClientStatusResponse{
Status: status,
Status: status.String(),
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (suite *KeeperTestSuite) TestQueryClientStatus() {
ClientId: path.EndpointA.ClientID,
}
},
true, exported.Active,
true, exported.Active.String(),
},
{
"Unknown client status",
Expand All @@ -453,7 +453,7 @@ func (suite *KeeperTestSuite) TestQueryClientStatus() {
ClientId: path.EndpointA.ClientID,
}
},
true, exported.Unknown,
true, exported.Unknown.String(),
},
{
"Frozen client status",
Expand All @@ -469,7 +469,7 @@ func (suite *KeeperTestSuite) TestQueryClientStatus() {
ClientId: path.EndpointA.ClientID,
}
},
true, exported.Frozen,
true, exported.Frozen.String(),
},
}

Expand Down
21 changes: 16 additions & 5 deletions modules/core/exported/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Status represents the status of a client
type Status string

const (
// TypeClientMisbehaviour is the shared evidence misbehaviour type
TypeClientMisbehaviour string = "client_misbehaviour"
Expand All @@ -23,13 +26,16 @@ const (
Localhost string = "09-localhost"

// Active is a status type of a client. An active client is allowed to be used.
Active string = "Active"
Active Status = "Active"

// Frozen is a status type of a client. A frozen client is not allowed to be used.
Frozen string = "Frozen"
Frozen Status = "Frozen"

// Expired is a status type of a client. An expired client is not allowed to be used.
Expired Status = "Expired"

// Unknown indicates there was an error in determining the status of a client
Unknown string = "Unknown"
// Unknown indicates there was an error in determining the status of a client.
Unknown Status = "Unknown"
)

// ClientState defines the required common functions for light clients.
Expand All @@ -49,7 +55,7 @@ type ClientState interface {

// Status function
// Clients must return their status. Only Active clients are allowed to process packets.
Status(ctx sdk.Context, clientStore sdk.KVStore, cdc codec.BinaryMarshaler) string
Status(ctx sdk.Context, clientStore sdk.KVStore, cdc codec.BinaryMarshaler) Status

// Genesis function
ExportMetadata(sdk.KVStore) []GenesisMetadata
Expand Down Expand Up @@ -233,3 +239,8 @@ type GenesisMetadata interface {
// returns metadata value
GetValue() []byte
}

// String returns the string representation of a client status.
func (s Status) String() string {
return string(s)
}
2 changes: 1 addition & 1 deletion modules/light-clients/06-solomachine/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cs ClientState) GetLatestHeight() exported.Height {
// The client may be:
// - Active: if frozen sequence is 0
// - Frozen: otherwise solo machine is frozen
func (cs ClientState) Status(_ sdk.Context, _ sdk.KVStore, _ codec.BinaryMarshaler) string {
func (cs ClientState) Status(_ sdk.Context, _ sdk.KVStore, _ codec.BinaryMarshaler) exported.Status {
if cs.FrozenSequence != 0 {
return exported.Frozen
}
Expand Down
9 changes: 2 additions & 7 deletions modules/light-clients/07-tendermint/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import (
"github.com/cosmos/ibc-go/modules/core/exported"
)

// Expired is a potential status of a client. A client is expired
// if its latest consensus state timestamp added to its trusting period
// is before or equal to the current time.
const Expired = "Expired"

var _ exported.ClientState = (*ClientState)(nil)

// NewClientState creates a new ClientState instance
Expand Down Expand Up @@ -75,7 +70,7 @@ func (cs ClientState) Status(
ctx sdk.Context,
clientStore sdk.KVStore,
cdc codec.BinaryMarshaler,
) string {
) exported.Status {
if !cs.FrozenHeight.IsZero() {
return exported.Frozen
}
Expand All @@ -87,7 +82,7 @@ func (cs ClientState) Status(
}

if cs.IsExpired(consState.Timestamp, ctx.BlockTime()) {
return Expired
return exported.Expired
}

return exported.Active
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (suite *TendermintTestSuite) TestStatus() {
testCases := []struct {
name string
malleate func()
expStatus string
expStatus exported.Status
}{
{"client is active", func() {}, exported.Active},
{"client is frozen", func() {
Expand All @@ -50,7 +50,7 @@ func (suite *TendermintTestSuite) TestStatus() {
}, exported.Unknown},
{"client status is expired", func() {
suite.coordinator.IncrementTimeBy(clientState.TrustingPeriod)
}, types.Expired},
}, exported.Expired},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/09-localhost/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (cs ClientState) GetLatestHeight() exported.Height {

// Status always returns Active. The localhost status cannot be changed.
func (cs ClientState) Status(_ sdk.Context, _ sdk.KVStore, _ codec.BinaryMarshaler,
) string {
) exported.Status {
return exported.Active
}

Expand Down

0 comments on commit cca717d

Please sign in to comment.