From 58275cabca75ff3a117868c4748c8a1adfb77339 Mon Sep 17 00:00:00 2001 From: mossid Date: Wed, 12 Jun 2019 00:08:04 +0200 Subject: [PATCH] add readme, reflect ICS02 revision --- x/ibc/02-client/README.md | 49 ++++++++++++++++++++++++++++++++++++++ x/ibc/02-client/manager.go | 31 ++++++++++-------------- x/ibc/02-client/types.go | 26 ++++++++------------ 3 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 x/ibc/02-client/README.md diff --git a/x/ibc/02-client/README.md b/x/ibc/02-client/README.md new file mode 100644 index 000000000000..a9bd6892771b --- /dev/null +++ b/x/ibc/02-client/README.md @@ -0,0 +1,49 @@ +# ICS 02: Client + +Package `client` defines types and method to store and update light clients which tracks on other chain's state. +The main type is `Client`, which provides `commitment.Root` to verify state proofs and `ConsensusState` to +verify header proofs. + +## Spec + +```typescript +interface ConsensusState { + height: uint64 + root: CommitmentRoot + validityPredicate: ValidityPredicate + eqivocationPredicate: EquivocationPredicate +} + +interface ClientState { + consensusState: ConsensusState + verifiedRoots: Map + frozen: bool +} + +interface Header { + height: uint64 + proof: HeaderProof + state: Maybe[ConsensusState] + root: CommitmentRoot +} + +type ValidityPredicate = (ConsensusState, Header) => Error | ConsensusState + +type EquivocationPredicate = (ConsensusState, Header, Header) => bool +``` + +## Impl + +### types.go + +`spec: interface ConsensusState` is implemented by `type ConsensusState`. `ConsensusState.{GetHeight(), GetRoot(), +Validate(), Equivocation()}` each corresponds to `spec: ConsensusState.{height, root, validityPredicate, +equivocationPredicate}`. `ConsensusState.Kind()` returns `Kind`, which is an enum indicating the type of the +consensus algorithm. + +`spec: interface Header` is implemented by `type Header`. `Header{GetHeight(), Proof(), State(), GetRoot()}` +each corresponds to `spec: Header.{height, proof, state, root}`. + +### manager.go + +`spec: interface ClientState` is implemented by `type Object`. // TODO diff --git a/x/ibc/02-client/manager.go b/x/ibc/02-client/manager.go index 502478104757..0134d8340d55 100644 --- a/x/ibc/02-client/manager.go +++ b/x/ibc/02-client/manager.go @@ -10,6 +10,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment" ) +// XXX: implement spec: ClientState.verifiedRoots + type IDGenerator func(sdk.Context /*Header,*/, mapping.Value) string func IntegerIDGenerator(ctx sdk.Context, v mapping.Value) string { @@ -59,13 +61,14 @@ func (man Manager) object(id string) Object { } } -func (man Manager) Create(ctx sdk.Context, cs Client) string { +func (man Manager) Create(ctx sdk.Context, cs ConsensusState) (Object, error) { id := man.idgen(ctx, man.idval) - err := man.object(id).create(ctx, cs) - if err != nil { - panic(err) + obj := man.object(id) + if obj.exists(ctx) { + return Object{}, errors.New("Create client on already existing id") } - return id + obj.client.Set(ctx, cs) + return obj, nil } func (man Manager) Query(ctx sdk.Context, id string) (Object, error) { @@ -89,7 +92,7 @@ func (man CounterpartyManager) Query(id string) CounterObject { type Object struct { id string - client mapping.Value + client mapping.Value // ConsensusState freeze mapping.Boolean } @@ -98,14 +101,6 @@ type CounterObject struct { client commitment.Value } -func (obj Object) create(ctx sdk.Context, st Client) error { - if obj.exists(ctx) { - return errors.New("Create client on already existing id") - } - obj.client.Set(ctx, st) - return nil -} - func (obj Object) exists(ctx sdk.Context) bool { return obj.client.Exists(ctx) } @@ -114,16 +109,16 @@ func (obj Object) ID() string { return obj.id } -func (obj Object) Value(ctx sdk.Context) (res Client) { +func (obj Object) Value(ctx sdk.Context) (res ConsensusState) { obj.client.Get(ctx, &res) return } -func (obj Object) Is(ctx sdk.Context, client Client) bool { +func (obj Object) Is(ctx sdk.Context, client ConsensusState) bool { return obj.client.Is(ctx, client) } -func (obj CounterObject) Is(ctx sdk.Context, client Client) bool { +func (obj CounterObject) Is(ctx sdk.Context, client ConsensusState) bool { return obj.client.Is(ctx, client) } @@ -136,7 +131,7 @@ func (obj Object) Update(ctx sdk.Context, header Header) error { return errors.New("client is frozen") } - var stored Client + var stored ConsensusState obj.client.GetIfExists(ctx, &stored) updated, err := stored.Validate(header) if err != nil { diff --git a/x/ibc/02-client/types.go b/x/ibc/02-client/types.go index 7b9f81ec7789..2ae484853354 100644 --- a/x/ibc/02-client/types.go +++ b/x/ibc/02-client/types.go @@ -5,35 +5,29 @@ import ( ) // TODO: types in this file should be (de/)serialized with proto in the future - -type AminoMarshaler interface { - MarshalAmino() (string, error) - UnmarshalAmino(string) error -} - -type ValidityPredicateBase interface { - Kind() Kind - GetHeight() int64 - Equal(ValidityPredicateBase) bool -} +// currently amkno codec handles it // ConsensusState -type Client interface { +type ConsensusState interface { Kind() Kind - GetBase() ValidityPredicateBase + GetHeight() uint64 GetRoot() commitment.Root - Validate(Header) (Client, error) // ValidityPredicate + Validate(Header) (ConsensusState, error) // ValidityPredicate + Equivocation(Header, Header) bool // EquivocationPredicate } -func Equal(client1, client2 Client) bool { +/* +func Equal(client1, client2 ConsensusState) bool { return client1.Kind() == client2.Kind() && client1.GetBase().Equal(client2.GetBase()) } +*/ type Header interface { Kind() Kind + GetHeight() uint64 // Proof() HeaderProof - GetBase() ValidityPredicateBase // can be nil + State() ConsensusState // can be nil GetRoot() commitment.Root }