Skip to content

Commit

Permalink
add readme, reflect ICS02 revision
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Jun 11, 2019
1 parent 29ed6b6 commit 58275ca
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 34 deletions.
49 changes: 49 additions & 0 deletions x/ibc/02-client/README.md
Original file line number Diff line number Diff line change
@@ -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<uint64, CommitmentRoot>
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
31 changes: 13 additions & 18 deletions x/ibc/02-client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down
26 changes: 10 additions & 16 deletions x/ibc/02-client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 58275ca

Please sign in to comment.