-
Notifications
You must be signed in to change notification settings - Fork 28
/
prover.go
95 lines (81 loc) · 2.86 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
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"
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
config ProverConfig
}
var _ core.Prover = (*Prover)(nil)
func NewProver(chain core.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
}
// 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
}
for i := uint64(0); i < pr.config.FinalityDelay; i++ {
if prevHeight, success := chainLatestHeight.Decrement(); success {
chainLatestHeight = prevHeight
} else {
break
}
}
timestamp, err := pr.chain.Timestamp(chainLatestHeight)
if err != nil {
return nil, err
}
return &mocktypes.Header{
Height: clienttypes.Height{
RevisionNumber: chainLatestHeight.GetRevisionNumber(),
RevisionHeight: chainLatestHeight.GetRevisionHeight(),
},
Timestamp: uint64(timestamp.UnixNano()),
}, nil
}
// ProveState returns the proof of an IBC state specified by `path` and `value`
func (pr *Prover) ProveState(ctx core.QueryContext, path string, value []byte) ([]byte, clienttypes.Height, error) {
return makeProof(value), ctx.Height().(clienttypes.Height), nil
}
func makeProof(bz []byte) []byte {
h := sha256.Sum256(bz)
return h[:]
}