2017-07-13 10:22:05 -07:00
|
|
|
package ibc
|
|
|
|
|
|
|
|
import (
|
2017-07-13 13:11:15 -07:00
|
|
|
wire "github.com/tendermint/go-wire"
|
2017-07-13 10:22:05 -07:00
|
|
|
"github.com/tendermint/light-client/certifiers"
|
|
|
|
|
|
|
|
"github.com/tendermint/basecoin/stack"
|
|
|
|
"github.com/tendermint/basecoin/state"
|
|
|
|
)
|
|
|
|
|
2017-07-13 13:11:15 -07:00
|
|
|
const (
|
|
|
|
prefixHash = "v"
|
|
|
|
prefixHeight = "h"
|
|
|
|
prefixPacket = "p"
|
|
|
|
)
|
|
|
|
|
2017-07-14 07:29:29 -07:00
|
|
|
// newCertifier loads up the current state of this chain to make a proper certifier
|
|
|
|
// it will load the most recent height before block h if h is positive
|
|
|
|
// if h < 0, it will load the latest height
|
|
|
|
func newCertifier(store state.KVStore, chainID string, h int) (*certifiers.InquiringCertifier, error) {
|
2017-07-13 10:22:05 -07:00
|
|
|
// each chain has their own prefixed subspace
|
2017-07-14 07:29:29 -07:00
|
|
|
p := newDBProvider(store)
|
2017-07-13 10:22:05 -07:00
|
|
|
|
2017-07-14 07:29:29 -07:00
|
|
|
var seed certifiers.Seed
|
|
|
|
var err error
|
|
|
|
if h > 0 {
|
|
|
|
// this gets the most recent verified seed below the specified height
|
|
|
|
seed, err = p.GetByHeight(h)
|
|
|
|
} else {
|
|
|
|
// 0 or negative means start at latest seed
|
|
|
|
seed, err = certifiers.LatestSeed(p)
|
|
|
|
}
|
2017-07-13 10:22:05 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have no source for untrusted keys, but use the db to load trusted history
|
|
|
|
cert := certifiers.NewInquiring(chainID, seed.Validators, p,
|
|
|
|
certifiers.MissingProvider{})
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbProvider wraps our kv store so it integrates with light-client verification
|
|
|
|
type dbProvider struct {
|
2017-07-13 13:11:15 -07:00
|
|
|
byHash state.KVStore
|
|
|
|
byHeight *state.Span
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDBProvider(store state.KVStore) *dbProvider {
|
|
|
|
return &dbProvider{
|
|
|
|
byHash: stack.PrefixedStore(prefixHash, store),
|
|
|
|
byHeight: state.NewSpan(stack.PrefixedStore(prefixHeight, store)),
|
|
|
|
}
|
2017-07-13 10:22:05 -07:00
|
|
|
}
|
|
|
|
|
2017-07-13 13:11:15 -07:00
|
|
|
var _ certifiers.Provider = &dbProvider{}
|
2017-07-13 10:22:05 -07:00
|
|
|
|
2017-07-13 13:11:15 -07:00
|
|
|
func (d *dbProvider) StoreSeed(seed certifiers.Seed) error {
|
|
|
|
// TODO: don't duplicate data....
|
|
|
|
b := wire.BinaryBytes(seed)
|
|
|
|
d.byHash.Set(seed.Hash(), b)
|
|
|
|
d.byHeight.Set(uint64(seed.Height()), b)
|
2017-07-13 10:22:05 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-13 13:11:15 -07:00
|
|
|
func (d *dbProvider) GetByHeight(h int) (seed certifiers.Seed, err error) {
|
|
|
|
b, _ := d.byHeight.LTE(uint64(h))
|
|
|
|
if b == nil {
|
|
|
|
return seed, certifiers.ErrSeedNotFound()
|
|
|
|
}
|
|
|
|
err = wire.ReadBinaryBytes(b, &seed)
|
|
|
|
return
|
2017-07-13 10:22:05 -07:00
|
|
|
}
|
2017-07-13 13:11:15 -07:00
|
|
|
|
|
|
|
func (d *dbProvider) GetByHash(hash []byte) (seed certifiers.Seed, err error) {
|
|
|
|
b := d.byHash.Get(hash)
|
|
|
|
if b == nil {
|
|
|
|
return seed, certifiers.ErrSeedNotFound()
|
|
|
|
}
|
|
|
|
err = wire.ReadBinaryBytes(b, &seed)
|
|
|
|
return
|
2017-07-13 10:22:05 -07:00
|
|
|
}
|