cosmos-sdk/store/gaskv/store.go

177 lines
4.8 KiB
Go
Raw Normal View History

2019-02-01 17:03:09 -08:00
package gaskv
2018-05-11 08:46:50 -07:00
import (
"io"
2019-02-01 17:03:09 -08:00
"github.com/cosmos/cosmos-sdk/store/types"
2018-05-11 08:46:50 -07:00
)
2018-05-08 13:03:42 -07:00
2019-02-01 17:03:09 -08:00
var _ types.KVStore = &Store{}
2018-05-08 15:27:41 -07:00
2019-02-01 17:03:09 -08:00
// Store applies gas tracking to an underlying KVStore. It implements the
// KVStore interface.
2019-02-01 17:03:09 -08:00
type Store struct {
gasMeter types.GasMeter
gasConfig types.GasConfig
parent types.KVStore
2018-05-08 13:03:42 -07:00
}
2019-02-01 17:03:09 -08:00
// NewStore returns a reference to a new GasKVStore.
2018-05-08 13:03:42 -07:00
// nolint
2019-02-01 17:03:09 -08:00
func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig types.GasConfig) *Store {
kvs := &Store{
2018-07-26 18:24:18 -07:00
gasMeter: gasMeter,
gasConfig: gasConfig,
parent: parent,
2018-05-08 13:03:42 -07:00
}
return kvs
}
// Implements Store.
2019-02-01 17:03:09 -08:00
func (gs *Store) GetStoreType() types.StoreType {
return gs.parent.GetStoreType()
2018-05-08 13:03:42 -07:00
}
// Implements KVStore.
2019-02-01 17:03:09 -08:00
func (gs *Store) Get(key []byte) (value []byte) {
gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostFlat, types.GasReadCostFlatDesc)
value = gs.parent.Get(key)
2018-05-08 15:27:41 -07:00
// TODO overflow-safe math?
2019-02-01 17:03:09 -08:00
gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostPerByte*types.Gas(len(value)), types.GasReadPerByteDesc)
2018-07-26 18:24:18 -07:00
2018-05-08 15:27:41 -07:00
return value
2018-05-08 13:03:42 -07:00
}
// Implements KVStore.
2019-02-01 17:03:09 -08:00
func (gs *Store) Set(key []byte, value []byte) {
types.AssertValidValue(value)
gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostFlat, types.GasWriteCostFlatDesc)
2018-05-08 15:27:41 -07:00
// TODO overflow-safe math?
2019-02-01 17:03:09 -08:00
gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostPerByte*types.Gas(len(value)), types.GasWritePerByteDesc)
gs.parent.Set(key, value)
2018-05-08 13:03:42 -07:00
}
// Implements KVStore.
2019-02-01 17:03:09 -08:00
func (gs *Store) Has(key []byte) bool {
gs.gasMeter.ConsumeGas(gs.gasConfig.HasCost, types.GasHasDesc)
return gs.parent.Has(key)
2018-05-08 13:03:42 -07:00
}
// Implements KVStore.
2019-02-01 17:03:09 -08:00
func (gs *Store) Delete(key []byte) {
// charge gas to prevent certain attack vectors even though space is being freed
2019-02-01 17:03:09 -08:00
gs.gasMeter.ConsumeGas(gs.gasConfig.DeleteCost, types.GasDeleteDesc)
gs.parent.Delete(key)
2018-05-08 13:03:42 -07:00
}
// Iterator implements the KVStore interface. It returns an iterator which
// incurs a flat gas cost for seeking to the first key/value pair and a variable
// gas cost based on the current value's length if the iterator is valid.
2019-02-01 17:03:09 -08:00
func (gs *Store) Iterator(start, end []byte) types.Iterator {
return gs.iterator(start, end, true)
2018-05-08 13:03:42 -07:00
}
// ReverseIterator implements the KVStore interface. It returns a reverse
// iterator which incurs a flat gas cost for seeking to the first key/value pair
// and a variable gas cost based on the current value's length if the iterator
// is valid.
2019-02-01 17:03:09 -08:00
func (gs *Store) ReverseIterator(start, end []byte) types.Iterator {
return gs.iterator(start, end, false)
2018-05-08 13:03:42 -07:00
}
// Implements KVStore.
2019-02-01 17:03:09 -08:00
func (gs *Store) CacheWrap() types.CacheWrap {
panic("cannot CacheWrap a GasKVStore")
}
// CacheWrapWithTrace implements the KVStore interface.
2019-02-01 17:03:09 -08:00
func (gs *Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
panic("cannot CacheWrapWithTrace a GasKVStore")
2018-05-08 13:03:42 -07:00
}
2019-02-01 17:03:09 -08:00
func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator {
var parent types.Iterator
2018-05-08 13:03:42 -07:00
if ascending {
parent = gs.parent.Iterator(start, end)
2018-05-08 13:03:42 -07:00
} else {
parent = gs.parent.ReverseIterator(start, end)
2018-05-08 13:03:42 -07:00
}
gi := newGasIterator(gs.gasMeter, gs.gasConfig, parent)
if gi.Valid() {
gi.(*gasIterator).consumeSeekGas()
}
return gi
2018-05-08 15:27:41 -07:00
}
type gasIterator struct {
2019-02-01 17:03:09 -08:00
gasMeter types.GasMeter
gasConfig types.GasConfig
parent types.Iterator
2018-05-08 15:27:41 -07:00
}
2019-02-01 17:03:09 -08:00
func newGasIterator(gasMeter types.GasMeter, gasConfig types.GasConfig, parent types.Iterator) types.Iterator {
2018-05-08 15:27:41 -07:00
return &gasIterator{
2018-07-26 18:24:18 -07:00
gasMeter: gasMeter,
gasConfig: gasConfig,
parent: parent,
2018-05-08 15:27:41 -07:00
}
}
// Implements Iterator.
func (gi *gasIterator) Domain() (start []byte, end []byte) {
return gi.parent.Domain()
2018-05-08 15:27:41 -07:00
}
// Implements Iterator.
func (gi *gasIterator) Valid() bool {
return gi.parent.Valid()
2018-05-08 15:27:41 -07:00
}
// Next implements the Iterator interface. It seeks to the next key/value pair
// in the iterator. It incurs a flat gas cost for seeking and a variable gas
// cost based on the current value's length if the iterator is valid.
func (gi *gasIterator) Next() {
if gi.Valid() {
gi.consumeSeekGas()
}
gi.parent.Next()
2018-05-08 15:27:41 -07:00
}
// Key implements the Iterator interface. It returns the current key and it does
// not incur any gas cost.
func (gi *gasIterator) Key() (key []byte) {
key = gi.parent.Key()
2018-05-08 16:11:22 -07:00
return key
2018-05-08 15:27:41 -07:00
}
// Value implements the Iterator interface. It returns the current value and it
// does not incur any gas cost.
func (gi *gasIterator) Value() (value []byte) {
value = gi.parent.Value()
2018-05-08 15:27:41 -07:00
return value
}
// Implements Iterator.
func (gi *gasIterator) Close() {
gi.parent.Close()
}
// Error delegates the Error call to the parent iterator.
func (gi *gasIterator) Error() error {
return gi.parent.Error()
}
// consumeSeekGas consumes a flat gas cost for seeking and a variable gas cost
// based on the current value's length.
func (gi *gasIterator) consumeSeekGas() {
value := gi.Value()
2019-02-01 17:03:09 -08:00
gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*types.Gas(len(value)), types.GasValuePerByteDesc)
gi.gasMeter.ConsumeGas(gi.gasConfig.IterNextCostFlat, types.GasIterNextCostFlatDesc)
2018-05-08 13:03:42 -07:00
}