github.com/Finschia/finschia-sdk@v0.48.1/store/iavl/tree.go (about)

     1  package iavl
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cosmos/iavl"
     7  
     8  	"github.com/Finschia/finschia-sdk/store/types"
     9  )
    10  
    11  var (
    12  	_ Tree = (*immutableTree)(nil)
    13  	_ Tree = (*iavl.MutableTree)(nil)
    14  )
    15  
    16  type (
    17  	// Tree defines an interface that both mutable and immutable IAVL trees
    18  	// must implement. For mutable IAVL trees, the interface is directly
    19  	// implemented by an iavl.MutableTree. For an immutable IAVL tree, a wrapper
    20  	// must be made.
    21  	Tree interface {
    22  		Has(key []byte) (bool, error)
    23  		Get(key []byte) ([]byte, error)
    24  		Set(key, value []byte) (bool, error)
    25  		Remove(key []byte) ([]byte, bool, error)
    26  		SaveVersion() ([]byte, int64, error)
    27  		DeleteVersion(version int64) error
    28  		DeleteVersions(versions ...int64) error
    29  		Version() int64
    30  		Hash() ([]byte, error)
    31  		VersionExists(version int64) bool
    32  		GetVersioned(key []byte, version int64) ([]byte, error)
    33  		GetVersionedWithProof(key []byte, version int64) ([]byte, *iavl.RangeProof, error)
    34  		GetImmutable(version int64) (*iavl.ImmutableTree, error)
    35  		SetInitialVersion(version uint64)
    36  		Iterator(start, end []byte, ascending bool) (types.Iterator, error)
    37  		LoadVersionForOverwriting(targetVersion int64) (int64, error)
    38  	}
    39  
    40  	// immutableTree is a simple wrapper around a reference to an iavl.ImmutableTree
    41  	// that implements the Tree interface. It should only be used for querying
    42  	// and iteration, specifically at previous heights.
    43  	immutableTree struct {
    44  		*iavl.ImmutableTree
    45  	}
    46  )
    47  
    48  func (it *immutableTree) Set(_, _ []byte) (bool, error) {
    49  	panic("cannot call 'Set' on an immutable IAVL tree")
    50  }
    51  
    52  func (it *immutableTree) Remove(_ []byte) ([]byte, bool, error) {
    53  	panic("cannot call 'Remove' on an immutable IAVL tree")
    54  }
    55  
    56  func (it *immutableTree) SaveVersion() ([]byte, int64, error) {
    57  	panic("cannot call 'SaveVersion' on an immutable IAVL tree")
    58  }
    59  
    60  func (it *immutableTree) DeleteVersion(_ int64) error {
    61  	panic("cannot call 'DeleteVersion' on an immutable IAVL tree")
    62  }
    63  
    64  func (it *immutableTree) DeleteVersions(_ ...int64) error {
    65  	panic("cannot call 'DeleteVersions' on an immutable IAVL tree")
    66  }
    67  
    68  func (it *immutableTree) SetInitialVersion(_ uint64) {
    69  	panic("cannot call 'SetInitialVersion' on an immutable IAVL tree")
    70  }
    71  
    72  func (it *immutableTree) VersionExists(version int64) bool {
    73  	return it.Version() == version
    74  }
    75  
    76  func (it *immutableTree) GetVersioned(key []byte, version int64) ([]byte, error) {
    77  	if it.Version() != version {
    78  		return nil, fmt.Errorf("version mismatch on immutable IAVL tree; got: %d, expected: %d", version, it.Version())
    79  	}
    80  
    81  	return it.Get(key)
    82  }
    83  
    84  func (it *immutableTree) GetVersionedWithProof(key []byte, version int64) ([]byte, *iavl.RangeProof, error) {
    85  	if it.Version() != version {
    86  		return nil, nil, fmt.Errorf("version mismatch on immutable IAVL tree; got: %d, expected: %d", version, it.Version())
    87  	}
    88  
    89  	return it.GetWithProof(key)
    90  }
    91  
    92  func (it *immutableTree) GetImmutable(version int64) (*iavl.ImmutableTree, error) {
    93  	if it.Version() != version {
    94  		return nil, fmt.Errorf("version mismatch on immutable IAVL tree; got: %d, expected: %d", version, it.Version())
    95  	}
    96  
    97  	return it.ImmutableTree, nil
    98  }
    99  
   100  func (it *immutableTree) LoadVersionForOverwriting(targetVersion int64) (int64, error) {
   101  	panic("cannot call 'LoadVersionForOverwriting' on an immutable IAVL tree")
   102  }