-
Notifications
You must be signed in to change notification settings - Fork 26
/
prover.go
163 lines (144 loc) · 5.24 KB
/
prover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package mock
import (
"context"
"crypto/sha256"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
mocktypes "github.com/datachainlab/ibc-mock-client/modules/light-clients/xx-mock/types"
"github.com/hyperledger-labs/yui-relayer/core"
)
type Prover struct {
chain core.Chain
}
var _ core.Prover = (*Prover)(nil)
func NewProver(chain core.Chain) *Prover {
return &Prover{chain: chain}
}
func (pr *Prover) Init(homePath string, timeout time.Duration, codec codec.ProtoCodecMarshaler, debug bool) error {
return nil
}
// SetRelayInfo sets source's path and counterparty's info to the chain
func (pr *Prover) SetRelayInfo(_ *core.PathEnd, _ *core.ProvableChain, _ *core.PathEnd) error {
return nil // prover uses chain's path instead
}
func (pr *Prover) SetupForRelay(ctx context.Context) error {
return nil
}
// CreateMsgCreateClient creates a CreateClientMsg to this chain
func (pr *Prover) CreateMsgCreateClient(clientID string, dstHeader core.Header, signer sdk.AccAddress) (*clienttypes.MsgCreateClient, error) {
h := dstHeader.(*mocktypes.Header)
clientState := &mocktypes.ClientState{
LatestHeight: h.Height,
}
consensusState := &mocktypes.ConsensusState{
Timestamp: h.Timestamp,
}
return clienttypes.NewMsgCreateClient(
clientState,
consensusState,
signer.String(),
)
}
// SetupHeadersForUpdate returns the finalized header and any intermediate headers needed to apply it to the client on the counterpaty chain
func (pr *Prover) SetupHeadersForUpdate(dstChain core.ChainInfoICS02Querier, latestFinalizedHeader core.Header) ([]core.Header, error) {
return []core.Header{latestFinalizedHeader.(*mocktypes.Header)}, nil
}
// GetLatestFinalizedHeader returns the latest finalized header
func (pr *Prover) GetLatestFinalizedHeader() (latestFinalizedHeader core.Header, err error) {
chainLatestHeight, err := pr.chain.LatestHeight()
if err != nil {
return nil, err
}
return &mocktypes.Header{
Height: clienttypes.Height{
RevisionNumber: chainLatestHeight.GetRevisionNumber(),
RevisionHeight: chainLatestHeight.GetRevisionHeight(),
},
Timestamp: uint64(time.Now().UnixNano()),
}, nil
}
// QueryClientConsensusState returns the ClientConsensusState and its proof
func (pr *Prover) QueryClientConsensusStateWithProof(ctx core.QueryContext, dstClientConsHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
res, err := pr.chain.QueryClientConsensusState(ctx, dstClientConsHeight)
if err != nil {
return nil, err
}
bz, err := pr.chain.Codec().Marshal(res.ConsensusState)
if err != nil {
return nil, err
}
res.Proof = makeProof(bz)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
// QueryClientStateWithProof returns the ClientState and its proof
func (pr *Prover) QueryClientStateWithProof(ctx core.QueryContext) (*clienttypes.QueryClientStateResponse, error) {
res, err := pr.chain.QueryClientState(ctx)
if err != nil {
return nil, err
}
bz, err := pr.chain.Codec().Marshal(res.ClientState)
if err != nil {
return nil, err
}
res.Proof = makeProof(bz)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
// QueryConnectionWithProof returns the Connection and its proof
func (pr *Prover) QueryConnectionWithProof(ctx core.QueryContext) (*conntypes.QueryConnectionResponse, error) {
res, err := pr.chain.QueryConnection(ctx)
if err != nil {
return nil, err
}
bz, err := pr.chain.Codec().Marshal(res.Connection)
if err != nil {
return nil, err
}
res.Proof = makeProof(bz)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
// QueryChannelWithProof returns the Channel and its proof
func (pr *Prover) QueryChannelWithProof(ctx core.QueryContext) (chanRes *chantypes.QueryChannelResponse, err error) {
res, err := pr.chain.QueryChannel(ctx)
if err != nil {
return nil, err
}
bz, err := pr.chain.Codec().Marshal(res.Channel)
if err != nil {
return nil, err
}
res.Proof = makeProof(bz)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
// QueryPacketCommitmentWithProof returns the packet commitment and its proof
func (pr *Prover) QueryPacketCommitmentWithProof(ctx core.QueryContext, seq uint64) (comRes *chantypes.QueryPacketCommitmentResponse, err error) {
res, err := pr.chain.QueryPacketCommitment(ctx, seq)
if err != nil {
return nil, err
}
res.Proof = makeProof(res.Commitment)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
// QueryPacketAcknowledgementCommitmentWithProof returns the packet acknowledgement commitment and its proof
func (pr *Prover) QueryPacketAcknowledgementCommitmentWithProof(ctx core.QueryContext, seq uint64) (ackRes *chantypes.QueryPacketAcknowledgementResponse, err error) {
res, err := pr.chain.QueryPacketAcknowledgementCommitment(ctx, seq)
if err != nil {
return nil, err
}
res.Proof = makeProof(res.Acknowledgement)
res.ProofHeight = ctx.Height().(clienttypes.Height)
return res, nil
}
func makeProof(bz []byte) []byte {
h := sha256.Sum256(bz)
return h[:]
}