-
Notifications
You must be signed in to change notification settings - Fork 26
/
prover.go
254 lines (211 loc) · 7.53 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package tendermint
import (
"context"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/modules/core/exported"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
tmclient "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types"
"github.com/tendermint/tendermint/light"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/hyperledger-labs/yui-relayer/core"
)
type Prover struct {
chain *Chain
config ProverConfig
}
var _ core.ProverI = (*Prover)(nil)
func NewProver(chain *Chain, config ProverConfig) *Prover {
return &Prover{chain: chain, config: config}
}
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
}
// GetChainID returns the chain ID
func (pr *Prover) GetChainID() string {
return pr.chain.ChainID()
}
// QueryClientConsensusState returns the ClientConsensusState and its proof
func (pr *Prover) QueryClientConsensusStateWithProof(height int64, dstClientConsHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
return pr.chain.queryClientConsensusState(height, dstClientConsHeight, true)
}
// QueryClientStateWithProof returns the ClientState and its proof
func (pr *Prover) QueryClientStateWithProof(height int64) (*clienttypes.QueryClientStateResponse, error) {
return pr.chain.queryClientState(height, true)
}
// QueryConnectionWithProof returns the Connection and its proof
func (pr *Prover) QueryConnectionWithProof(height int64) (*conntypes.QueryConnectionResponse, error) {
return pr.chain.queryConnection(height, true)
}
// QueryChannelWithProof returns the Channel and its proof
func (pr *Prover) QueryChannelWithProof(height int64) (chanRes *chantypes.QueryChannelResponse, err error) {
return pr.chain.queryChannel(height, true)
}
// QueryPacketCommitmentWithProof returns the packet commitment and its proof
func (pr *Prover) QueryPacketCommitmentWithProof(height int64, seq uint64) (comRes *chantypes.QueryPacketCommitmentResponse, err error) {
return pr.chain.queryPacketCommitment(height, seq, true)
}
// QueryPacketAcknowledgementCommitmentWithProof returns the packet acknowledgement commitment and its proof
func (pr *Prover) QueryPacketAcknowledgementCommitmentWithProof(height int64, seq uint64) (ackRes *chantypes.QueryPacketAcknowledgementResponse, err error) {
return pr.chain.queryPacketAcknowledgementCommitment(height, seq, true)
}
// QueryLatestHeader returns the latest header from the chain
func (pr *Prover) QueryLatestHeader() (out core.HeaderI, err error) {
var h int64
if h, err = pr.chain.GetLatestHeight(); err != nil {
return nil, err
}
return pr.queryHeaderAtHeight(h)
}
// GetLatestLightHeight uses the CLI utilities to pull the latest height from a given chain
func (pr *Prover) GetLatestLightHeight() (int64, error) {
db, df, err := pr.NewLightDB()
if err != nil {
return -1, err
}
defer df()
client, err := pr.LightClient(db)
if err != nil {
return -1, err
}
return client.LastTrustedHeight()
}
// CreateMsgCreateClient creates a CreateClientMsg to this chain
func (pr *Prover) CreateMsgCreateClient(clientID string, dstHeader core.HeaderI, signer sdk.AccAddress) (*clienttypes.MsgCreateClient, error) {
ubdPeriod, err := pr.chain.QueryUnbondingPeriod()
if err != nil {
return nil, err
}
consensusParams, err := pr.chain.QueryConsensusParams()
if err != nil {
return nil, err
}
return createClient(
dstHeader.(*tmclient.Header),
pr.getTrustingPeriod(),
ubdPeriod,
consensusParams,
signer,
), nil
}
// SetupHeader creates a new header based on a given header
func (pr *Prover) SetupHeader(dstChain core.LightClientIBCQueryierI, srcHeader core.HeaderI) (core.HeaderI, error) {
srcChain := pr.chain
// make copy of header stored in mop
tmp := srcHeader.(*tmclient.Header)
h := *tmp
dsth, err := dstChain.GetLatestLightHeight()
if err != nil {
return nil, err
}
// retrieve counterparty client from dst chain
counterpartyClientRes, err := dstChain.QueryClientState(dsth)
if err != nil {
return nil, err
}
var cs exported.ClientState
if err := srcChain.codec.UnpackAny(counterpartyClientRes.ClientState, &cs); err != nil {
return nil, err
}
// inject TrustedHeight as latest height stored on counterparty client
h.TrustedHeight = cs.GetLatestHeight().(clienttypes.Height)
// query TrustedValidators at Trusted Height from srcChain
valSet, err := srcChain.QueryValsetAtHeight(h.TrustedHeight)
if err != nil {
return nil, err
}
// inject TrustedValidators into header
h.TrustedValidators = valSet
return &h, nil
}
func lightError(err error) error { return fmt.Errorf("light client: %w", err) }
// UpdateLightWithHeader calls client.Update and then .
func (pr *Prover) UpdateLightWithHeader() (header core.HeaderI, provableHeight int64, queryableHeight int64, err error) {
// create database connection
db, df, err := pr.NewLightDB()
if err != nil {
return nil, 0, 0, lightError(err)
}
defer df()
client, err := pr.LightClient(db)
if err != nil {
return nil, 0, 0, lightError(err)
}
sh, err := client.Update(context.Background(), time.Now())
if err != nil {
return nil, 0, 0, lightError(err)
}
if sh == nil {
sh, err = client.TrustedLightBlock(0)
if err != nil {
return nil, 0, 0, lightError(err)
}
}
protoVal, err := tmtypes.NewValidatorSet(sh.ValidatorSet.Validators).ToProto()
if err != nil {
return nil, 0, 0, err
}
h := &tmclient.Header{
SignedHeader: sh.SignedHeader.ToProto(),
ValidatorSet: protoVal,
}
queryableHeight = int64(h.GetHeight().GetRevisionHeight())
// NOTE: We query connection at height - 1 because of the way tendermint returns
// proofs the commit for height n is contained in the header of height n + 1
provableHeight = queryableHeight - 1
return h, provableHeight, queryableHeight, nil
}
// TrustOptions returns light.TrustOptions given a height and hash
func (pr *Prover) TrustOptions(height int64, hash []byte) light.TrustOptions {
return light.TrustOptions{
Period: pr.getTrustingPeriod(),
Height: height,
Hash: hash,
}
}
/// internal method ///
// getTrustingPeriod returns the trusting period for the chain
func (pr *Prover) getTrustingPeriod() time.Duration {
tp, _ := time.ParseDuration(pr.config.TrustingPeriod)
return tp
}
// queryHeaderAtHeight returns the header at a given height
func (c *Prover) queryHeaderAtHeight(height int64) (*tmclient.Header, error) {
var (
page int = 1
perPage int = 100000
)
if height <= 0 {
return nil, fmt.Errorf("must pass in valid height, %d not valid", height)
}
res, err := c.chain.Client.Commit(context.Background(), &height)
if err != nil {
return nil, err
}
val, err := c.chain.Client.Validators(context.Background(), &height, &page, &perPage)
if err != nil {
return nil, err
}
protoVal, err := tmtypes.NewValidatorSet(val.Validators).ToProto()
if err != nil {
return nil, err
}
return &tmclient.Header{
// NOTE: This is not a SignedHeader
// We are missing a light.Commit type here
SignedHeader: res.SignedHeader.ToProto(),
ValidatorSet: protoVal,
}, nil
}