493 lines
12 KiB
Go
493 lines
12 KiB
Go
package iavl
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
"time"
|
|
|
|
ics23iavl "github.com/confio/ics23-iavl"
|
|
ics23 "github.com/confio/ics23/go"
|
|
"github.com/tendermint/iavl"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"github.com/tendermint/tendermint/crypto/merkle"
|
|
tmkv "github.com/tendermint/tendermint/libs/kv"
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/cachekv"
|
|
"github.com/cosmos/cosmos-sdk/store/tracekv"
|
|
"github.com/cosmos/cosmos-sdk/store/types"
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
const (
|
|
defaultIAVLCacheSize = 10000
|
|
)
|
|
|
|
var (
|
|
_ types.KVStore = (*Store)(nil)
|
|
_ types.CommitStore = (*Store)(nil)
|
|
_ types.CommitKVStore = (*Store)(nil)
|
|
_ types.Queryable = (*Store)(nil)
|
|
)
|
|
|
|
// Store Implements types.KVStore and CommitKVStore.
|
|
type Store struct {
|
|
tree Tree
|
|
}
|
|
|
|
// LoadStore returns an IAVL Store as a CommitKVStore. Internally, it will load the
|
|
// store's version (id) from the provided DB. An error is returned if the version
|
|
// fails to load.
|
|
func LoadStore(db dbm.DB, id types.CommitID, lazyLoading bool) (types.CommitKVStore, error) {
|
|
tree, err := iavl.NewMutableTree(db, defaultIAVLCacheSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if lazyLoading {
|
|
_, err = tree.LazyLoadVersion(id.Version)
|
|
} else {
|
|
_, err = tree.LoadVersion(id.Version)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Store{
|
|
tree: tree,
|
|
}, nil
|
|
}
|
|
|
|
// UnsafeNewStore returns a reference to a new IAVL Store with a given mutable
|
|
// IAVL tree reference. It should only be used for testing purposes.
|
|
//
|
|
// CONTRACT: The IAVL tree should be fully loaded.
|
|
// CONTRACT: PruningOptions passed in as argument must be the same as pruning options
|
|
// passed into iavl.MutableTree
|
|
func UnsafeNewStore(tree *iavl.MutableTree) *Store {
|
|
return &Store{
|
|
tree: tree,
|
|
}
|
|
}
|
|
|
|
// GetImmutable returns a reference to a new store backed by an immutable IAVL
|
|
// tree at a specific version (height) without any pruning options. This should
|
|
// be used for querying and iteration only. If the version does not exist or has
|
|
// been pruned, an error will be returned. Any mutable operations executed will
|
|
// result in a panic.
|
|
func (st *Store) GetImmutable(version int64) (*Store, error) {
|
|
if !st.VersionExists(version) {
|
|
return nil, iavl.ErrVersionDoesNotExist
|
|
}
|
|
|
|
iTree, err := st.tree.GetImmutable(version)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Store{
|
|
tree: &immutableTree{iTree},
|
|
}, nil
|
|
}
|
|
|
|
// Commit commits the current store state and returns a CommitID with the new
|
|
// version and hash.
|
|
func (st *Store) Commit() types.CommitID {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "commit")
|
|
|
|
hash, version, err := st.tree.SaveVersion()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return types.CommitID{
|
|
Version: version,
|
|
Hash: hash,
|
|
}
|
|
}
|
|
|
|
// Implements Committer.
|
|
func (st *Store) LastCommitID() types.CommitID {
|
|
return types.CommitID{
|
|
Version: st.tree.Version(),
|
|
Hash: st.tree.Hash(),
|
|
}
|
|
}
|
|
|
|
// SetPruning panics as pruning options should be provided at initialization
|
|
// since IAVl accepts pruning options directly.
|
|
func (st *Store) SetPruning(_ types.PruningOptions) {
|
|
panic("cannot set pruning options on an initialized IAVL store")
|
|
}
|
|
|
|
// VersionExists returns whether or not a given version is stored.
|
|
func (st *Store) VersionExists(version int64) bool {
|
|
return st.tree.VersionExists(version)
|
|
}
|
|
|
|
// Implements Store.
|
|
func (st *Store) GetStoreType() types.StoreType {
|
|
return types.StoreTypeIAVL
|
|
}
|
|
|
|
// Implements Store.
|
|
func (st *Store) CacheWrap() types.CacheWrap {
|
|
return cachekv.NewStore(st)
|
|
}
|
|
|
|
// CacheWrapWithTrace implements the Store interface.
|
|
func (st *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
|
|
return cachekv.NewStore(tracekv.NewStore(st, w, tc))
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) Set(key, value []byte) {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "set")
|
|
types.AssertValidKey(key)
|
|
types.AssertValidValue(value)
|
|
st.tree.Set(key, value)
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) Get(key []byte) []byte {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "get")
|
|
_, value := st.tree.Get(key)
|
|
return value
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) Has(key []byte) (exists bool) {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "has")
|
|
return st.tree.Has(key)
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) Delete(key []byte) {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "delete")
|
|
st.tree.Remove(key)
|
|
}
|
|
|
|
// DeleteVersions deletes a series of versions from the MutableTree. An error
|
|
// is returned if any single version is invalid or the delete fails. All writes
|
|
// happen in a single batch with a single commit.
|
|
func (st *Store) DeleteVersions(versions ...int64) error {
|
|
return st.tree.DeleteVersions(versions...)
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) Iterator(start, end []byte) types.Iterator {
|
|
var iTree *iavl.ImmutableTree
|
|
|
|
switch tree := st.tree.(type) {
|
|
case *immutableTree:
|
|
iTree = tree.ImmutableTree
|
|
case *iavl.MutableTree:
|
|
iTree = tree.ImmutableTree
|
|
}
|
|
|
|
return newIAVLIterator(iTree, start, end, true)
|
|
}
|
|
|
|
// Implements types.KVStore.
|
|
func (st *Store) ReverseIterator(start, end []byte) types.Iterator {
|
|
var iTree *iavl.ImmutableTree
|
|
|
|
switch tree := st.tree.(type) {
|
|
case *immutableTree:
|
|
iTree = tree.ImmutableTree
|
|
case *iavl.MutableTree:
|
|
iTree = tree.ImmutableTree
|
|
}
|
|
|
|
return newIAVLIterator(iTree, start, end, false)
|
|
}
|
|
|
|
// Handle gatest the latest height, if height is 0
|
|
func getHeight(tree Tree, req abci.RequestQuery) int64 {
|
|
height := req.Height
|
|
if height == 0 {
|
|
latest := tree.Version()
|
|
if tree.VersionExists(latest - 1) {
|
|
height = latest - 1
|
|
} else {
|
|
height = latest
|
|
}
|
|
}
|
|
return height
|
|
}
|
|
|
|
// Query implements ABCI interface, allows queries
|
|
//
|
|
// by default we will return from (latest height -1),
|
|
// as we will have merkle proofs immediately (header height = data height + 1)
|
|
// If latest-1 is not present, use latest (which must be present)
|
|
// if you care to have the latest data to see a tx results, you must
|
|
// explicitly set the height you want to see
|
|
func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
|
|
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "query")
|
|
|
|
if len(req.Data) == 0 {
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrTxDecode, "query cannot be zero length"))
|
|
}
|
|
|
|
tree := st.tree
|
|
|
|
// store the height we chose in the response, with 0 being changed to the
|
|
// latest height
|
|
res.Height = getHeight(tree, req)
|
|
|
|
switch req.Path {
|
|
case "/key": // get by key
|
|
key := req.Data // data holds the key bytes
|
|
|
|
res.Key = key
|
|
if !st.VersionExists(res.Height) {
|
|
res.Log = iavl.ErrVersionDoesNotExist.Error()
|
|
break
|
|
}
|
|
|
|
_, res.Value = tree.GetVersioned(key, res.Height)
|
|
if !req.Prove {
|
|
break
|
|
}
|
|
|
|
// Continue to prove existence/absence of value
|
|
// Must convert store.Tree to iavl.MutableTree with given version to use in CreateProof
|
|
iTree, err := tree.GetImmutable(res.Height)
|
|
if err != nil {
|
|
// sanity check: If value for given version was retrieved, immutable tree must also be retrievable
|
|
panic(fmt.Sprintf("version exists in store but could not retrieve corresponding versioned tree in store, %s", err.Error()))
|
|
}
|
|
mtree := &iavl.MutableTree{
|
|
ImmutableTree: iTree,
|
|
}
|
|
|
|
// get proof from tree and convert to merkle.Proof before adding to result
|
|
res.Proof = getProofFromTree(mtree, req.Data, res.Value != nil)
|
|
|
|
case "/subspace":
|
|
var KVs []types.KVPair
|
|
|
|
subspace := req.Data
|
|
res.Key = subspace
|
|
|
|
iterator := types.KVStorePrefixIterator(st, subspace)
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
KVs = append(KVs, types.KVPair{Key: iterator.Key(), Value: iterator.Value()})
|
|
}
|
|
|
|
iterator.Close()
|
|
res.Value = cdc.MustMarshalBinaryBare(KVs)
|
|
|
|
default:
|
|
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unexpected query path: %v", req.Path))
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// Takes a MutableTree, a key, and a flag for creating existence or absence proof and returns the
|
|
// appropriate merkle.Proof. Since this must be called after querying for the value, this function should never error
|
|
// Thus, it will panic on error rather than returning it
|
|
func getProofFromTree(tree *iavl.MutableTree, key []byte, exists bool) *merkle.Proof {
|
|
var (
|
|
commitmentProof *ics23.CommitmentProof
|
|
err error
|
|
)
|
|
|
|
if exists {
|
|
// value was found
|
|
commitmentProof, err = ics23iavl.CreateMembershipProof(tree, key)
|
|
if err != nil {
|
|
// sanity check: If value was found, membership proof must be creatable
|
|
panic(fmt.Sprintf("unexpected value for empty proof: %s", err.Error()))
|
|
}
|
|
} else {
|
|
// value wasn't found
|
|
commitmentProof, err = ics23iavl.CreateNonMembershipProof(tree, key)
|
|
if err != nil {
|
|
// sanity check: If value wasn't found, nonmembership proof must be creatable
|
|
panic(fmt.Sprintf("unexpected error for nonexistence proof: %s", err.Error()))
|
|
}
|
|
}
|
|
|
|
op := types.NewIavlCommitmentOp(key, commitmentProof)
|
|
return &merkle.Proof{Ops: []merkle.ProofOp{op.ProofOp()}}
|
|
}
|
|
|
|
//----------------------------------------
|
|
|
|
// Implements types.Iterator.
|
|
type iavlIterator struct {
|
|
// Domain
|
|
start, end []byte
|
|
|
|
key []byte // The current key (mutable)
|
|
value []byte // The current value (mutable)
|
|
|
|
// Underlying store
|
|
tree *iavl.ImmutableTree
|
|
|
|
// Channel to push iteration values.
|
|
iterCh chan tmkv.Pair
|
|
|
|
// Close this to release goroutine.
|
|
quitCh chan struct{}
|
|
|
|
// Close this to signal that state is initialized.
|
|
initCh chan struct{}
|
|
|
|
mtx sync.Mutex
|
|
|
|
ascending bool // Iteration order
|
|
|
|
invalid bool // True once, true forever (mutable)
|
|
}
|
|
|
|
var _ types.Iterator = (*iavlIterator)(nil)
|
|
|
|
// newIAVLIterator will create a new iavlIterator.
|
|
// CONTRACT: Caller must release the iavlIterator, as each one creates a new
|
|
// goroutine.
|
|
func newIAVLIterator(tree *iavl.ImmutableTree, start, end []byte, ascending bool) *iavlIterator {
|
|
iter := &iavlIterator{
|
|
tree: tree,
|
|
start: sdk.CopyBytes(start),
|
|
end: sdk.CopyBytes(end),
|
|
ascending: ascending,
|
|
iterCh: make(chan tmkv.Pair), // Set capacity > 0?
|
|
quitCh: make(chan struct{}),
|
|
initCh: make(chan struct{}),
|
|
}
|
|
go iter.iterateRoutine()
|
|
go iter.initRoutine()
|
|
return iter
|
|
}
|
|
|
|
// Run this to funnel items from the tree to iterCh.
|
|
func (iter *iavlIterator) iterateRoutine() {
|
|
iter.tree.IterateRange(
|
|
iter.start, iter.end, iter.ascending,
|
|
func(key, value []byte) bool {
|
|
select {
|
|
case <-iter.quitCh:
|
|
return true // done with iteration.
|
|
case iter.iterCh <- tmkv.Pair{Key: key, Value: value}:
|
|
return false // yay.
|
|
}
|
|
},
|
|
)
|
|
close(iter.iterCh) // done.
|
|
}
|
|
|
|
// Run this to fetch the first item.
|
|
func (iter *iavlIterator) initRoutine() {
|
|
iter.receiveNext()
|
|
close(iter.initCh)
|
|
}
|
|
|
|
// Implements types.Iterator.
|
|
func (iter *iavlIterator) Domain() (start, end []byte) {
|
|
return iter.start, iter.end
|
|
}
|
|
|
|
// Implements types.Iterator.
|
|
func (iter *iavlIterator) Valid() bool {
|
|
iter.waitInit()
|
|
iter.mtx.Lock()
|
|
|
|
validity := !iter.invalid
|
|
iter.mtx.Unlock()
|
|
return validity
|
|
}
|
|
|
|
// Implements types.Iterator.
|
|
func (iter *iavlIterator) Next() {
|
|
iter.waitInit()
|
|
iter.mtx.Lock()
|
|
iter.assertIsValid(true)
|
|
|
|
iter.receiveNext()
|
|
iter.mtx.Unlock()
|
|
}
|
|
|
|
// Implements types.Iterator.
|
|
func (iter *iavlIterator) Key() []byte {
|
|
iter.waitInit()
|
|
iter.mtx.Lock()
|
|
iter.assertIsValid(true)
|
|
|
|
key := iter.key
|
|
iter.mtx.Unlock()
|
|
return key
|
|
}
|
|
|
|
// Implements types.Iterator.
|
|
func (iter *iavlIterator) Value() []byte {
|
|
iter.waitInit()
|
|
iter.mtx.Lock()
|
|
iter.assertIsValid(true)
|
|
|
|
val := iter.value
|
|
iter.mtx.Unlock()
|
|
return val
|
|
}
|
|
|
|
// Close closes the IAVL iterator by closing the quit channel and waiting for
|
|
// the iterCh to finish/close.
|
|
func (iter *iavlIterator) Close() {
|
|
close(iter.quitCh)
|
|
// wait iterCh to close
|
|
for range iter.iterCh {
|
|
}
|
|
}
|
|
|
|
// Error performs a no-op.
|
|
func (iter *iavlIterator) Error() error {
|
|
return nil
|
|
}
|
|
|
|
//----------------------------------------
|
|
|
|
func (iter *iavlIterator) setNext(key, value []byte) {
|
|
iter.assertIsValid(false)
|
|
|
|
iter.key = key
|
|
iter.value = value
|
|
}
|
|
|
|
func (iter *iavlIterator) setInvalid() {
|
|
iter.assertIsValid(false)
|
|
|
|
iter.invalid = true
|
|
}
|
|
|
|
func (iter *iavlIterator) waitInit() {
|
|
<-iter.initCh
|
|
}
|
|
|
|
func (iter *iavlIterator) receiveNext() {
|
|
kvPair, ok := <-iter.iterCh
|
|
if ok {
|
|
iter.setNext(kvPair.Key, kvPair.Value)
|
|
} else {
|
|
iter.setInvalid()
|
|
}
|
|
}
|
|
|
|
// assertIsValid panics if the iterator is invalid. If unlockMutex is true,
|
|
// it also unlocks the mutex before panicing, to prevent deadlocks in code that
|
|
// recovers from panics
|
|
func (iter *iavlIterator) assertIsValid(unlockMutex bool) {
|
|
if iter.invalid {
|
|
if unlockMutex {
|
|
iter.mtx.Unlock()
|
|
}
|
|
panic("invalid iterator")
|
|
}
|
|
}
|