Skip to content

Commit 8cd3208

Browse files
authored
Merge pull request #132 from hyperledger-labs/prove-host-consensus-state
Add `ProveHostConsensusState` to StateProver interface Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents c226f1b + a548019 commit 8cd3208

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

chains/tendermint/prover.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) (
5454
}
5555
}
5656

57+
// ProveHostConsensusState returns the existence proof of the consensus state at `height`
58+
// ibc-go doesn't use this proof, so it returns nil
59+
func (pr *Prover) ProveHostConsensusState(ctx core.QueryContext, height ibcexported.Height, consensusState ibcexported.ConsensusState) ([]byte, error) {
60+
return nil, nil
61+
}
62+
5763
/* LightClient implementation */
5864

5965
// CreateInitialLightClientState creates a pair of ClientState and ConsensusState submitted to the counterparty chain as MsgCreateClient

core/connection.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
9494
srcUpdateHeaders, dstUpdateHeaders []Header
9595
srcCsRes, dstCsRes *clienttypes.QueryClientStateResponse
9696
srcCS, dstCS ibcexported.ClientState
97-
srcCons, dstCons *clienttypes.QueryConsensusStateResponse
97+
srcConsRes, dstConsRes *clienttypes.QueryConsensusStateResponse
98+
srcCons, dstCons ibcexported.ConsensusState
9899
srcConsH, dstConsH ibcexported.Height
100+
srcHostConsProof, dstHostConsProof []byte
99101
)
100102
err = retry.Do(func() error {
101103
srcUpdateHeaders, dstUpdateHeaders, err = sh.SetupBothHeadersForUpdate(src, dst)
@@ -136,12 +138,26 @@ func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
136138

137139
// Store the heights
138140
srcConsH, dstConsH = srcCS.GetLatestHeight(), dstCS.GetLatestHeight()
139-
srcCons, dstCons, err = QueryClientConsensusStatePair(
141+
srcConsRes, dstConsRes, err = QueryClientConsensusStatePair(
140142
sh.GetQueryContext(src.ChainID()), sh.GetQueryContext(dst.ChainID()),
141143
src, dst, srcConsH, dstConsH, true)
142144
if err != nil {
143145
return nil, err
144146
}
147+
if err := src.Codec().UnpackAny(srcConsRes.ConsensusState, &srcCons); err != nil {
148+
return nil, err
149+
}
150+
if err := dst.Codec().UnpackAny(dstConsRes.ConsensusState, &dstCons); err != nil {
151+
return nil, err
152+
}
153+
srcHostConsProof, err = src.ProveHostConsensusState(sh.GetQueryContext(src.ChainID()), dstCS.GetLatestHeight(), dstCons)
154+
if err != nil {
155+
return nil, err
156+
}
157+
dstHostConsProof, err = dst.ProveHostConsensusState(sh.GetQueryContext(dst.ChainID()), srcCS.GetLatestHeight(), srcCons)
158+
if err != nil {
159+
return nil, err
160+
}
145161
}
146162

147163
switch {
@@ -160,15 +176,15 @@ func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
160176
if len(dstUpdateHeaders) > 0 {
161177
out.Src = append(out.Src, src.Path().UpdateClients(dstUpdateHeaders, addr)...)
162178
}
163-
out.Src = append(out.Src, src.Path().ConnTry(dst.Path(), dstCsRes, dstConn, dstCons, addr))
179+
out.Src = append(out.Src, src.Path().ConnTry(dst.Path(), dstCsRes, dstConn, dstConsRes, srcHostConsProof, addr))
164180
// Handshake has started on src (1 step done), relay `connOpenTry` and `updateClient` on dst
165181
case srcConn.Connection.State == conntypes.INIT && dstConn.Connection.State == conntypes.UNINITIALIZED:
166182
logConnectionStates(dst, src, dstConn, srcConn)
167183
addr := mustGetAddress(dst)
168184
if len(srcUpdateHeaders) > 0 {
169185
out.Dst = append(out.Dst, dst.Path().UpdateClients(srcUpdateHeaders, addr)...)
170186
}
171-
out.Dst = append(out.Dst, dst.Path().ConnTry(src.Path(), srcCsRes, srcConn, srcCons, addr))
187+
out.Dst = append(out.Dst, dst.Path().ConnTry(src.Path(), srcCsRes, srcConn, srcConsRes, dstHostConsProof, addr))
172188

173189
// Handshake has started on src end (2 steps done), relay `connOpenAck` and `updateClient` to dst end
174190
case srcConn.Connection.State == conntypes.TRYOPEN && dstConn.Connection.State == conntypes.INIT:
@@ -177,7 +193,7 @@ func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
177193
if len(srcUpdateHeaders) > 0 {
178194
out.Dst = append(out.Dst, dst.Path().UpdateClients(srcUpdateHeaders, addr)...)
179195
}
180-
out.Dst = append(out.Dst, dst.Path().ConnAck(src.Path(), srcCsRes, srcConn, srcCons, addr))
196+
out.Dst = append(out.Dst, dst.Path().ConnAck(src.Path(), srcCsRes, srcConn, srcConsRes, addr))
181197

182198
// Handshake has started on dst end (2 steps done), relay `connOpenAck` and `updateClient` to src end
183199
case srcConn.Connection.State == conntypes.INIT && dstConn.Connection.State == conntypes.TRYOPEN:
@@ -186,7 +202,7 @@ func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
186202
if len(dstUpdateHeaders) > 0 {
187203
out.Src = append(out.Src, src.Path().UpdateClients(dstUpdateHeaders, addr)...)
188204
}
189-
out.Src = append(out.Src, src.Path().ConnAck(dst.Path(), dstCsRes, dstConn, dstCons, addr))
205+
out.Src = append(out.Src, src.Path().ConnAck(dst.Path(), dstCsRes, dstConn, dstConsRes, addr))
190206

191207
// Handshake has confirmed on dst (3 steps done), relay `connOpenConfirm` and `updateClient` to src end
192208
case srcConn.Connection.State == conntypes.TRYOPEN && dstConn.Connection.State == conntypes.OPEN:

core/pathEnd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (pe *PathEnd) ConnTry(
9292
dstClientState *clienttypes.QueryClientStateResponse,
9393
dstConnState *conntypes.QueryConnectionResponse,
9494
dstConsState *clienttypes.QueryConsensusStateResponse,
95+
hostConsensusStateProof []byte,
9596
signer sdk.AccAddress,
9697
) sdk.Msg {
9798
cs, err := clienttypes.UnpackClientState(dstClientState.ClientState)
@@ -113,6 +114,7 @@ func (pe *PathEnd) ConnTry(
113114
cs.GetLatestHeight().(clienttypes.Height),
114115
signer.String(),
115116
)
117+
msg.HostConsensusStateProof = hostConsensusStateProof
116118
if err = msg.ValidateBasic(); err != nil {
117119
panic(err)
118120
}

core/provers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ type Prover interface {
2626

2727
// StateProver provides a generic way to generate existence proof of IBC states (e.g. ClientState, Connection, PacketCommitment, etc.)
2828
type StateProver interface {
29+
// ProveState returns a proof of an IBC state specified by `path` and `value`
2930
ProveState(ctx QueryContext, path string, value []byte) (proof []byte, proofHeight clienttypes.Height, err error)
31+
32+
// ProveHostConsensusState returns an existence proof of the consensus state at `height`
33+
// This proof would be ignored in ibc-go, but it is required to `getSelfConsensusState` of ibc-solidity.
34+
ProveHostConsensusState(ctx QueryContext, height exported.Height, consensusState exported.ConsensusState) (proof []byte, err error)
3035
}
3136

3237
// LightClient provides functions for creating and updating on-chain light clients on the counterparty chain

provers/mock/prover.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) (
120120
return makeProof(value), ctx.Height().(clienttypes.Height), nil
121121
}
122122

123+
// ProveHostConsensusState returns the proof of the consensus state at `height`
124+
func (pr *Prover) ProveHostConsensusState(ctx core.QueryContext, height exported.Height, consensusState exported.ConsensusState) ([]byte, error) {
125+
return clienttypes.MarshalConsensusState(pr.chain.Codec(), consensusState)
126+
}
127+
123128
func makeProof(bz []byte) []byte {
124129
h := sha256.Sum256(bz)
125130
return h[:]

0 commit comments

Comments
 (0)