2018-05-11 08:46:50 -07:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2018-07-12 16:58:51 -07:00
|
|
|
"io"
|
|
|
|
|
2018-05-11 08:46:50 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
2018-05-08 13:03:42 -07:00
|
|
|
|
2018-07-26 18:24:18 -07:00
|
|
|
var _ KVStore = &gasKVStore{}
|
2018-05-08 15:27:41 -07:00
|
|
|
|
2018-09-26 07:02:56 -07:00
|
|
|
// gasKVStore applies gas tracking to an underlying KVStore. It implements the
|
|
|
|
// KVStore interface.
|
2018-05-08 13:03:42 -07:00
|
|
|
type gasKVStore struct {
|
2018-07-26 18:24:18 -07:00
|
|
|
gasMeter sdk.GasMeter
|
|
|
|
gasConfig sdk.GasConfig
|
|
|
|
parent sdk.KVStore
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -07:00
|
|
|
// NewGasKVStore returns a reference to a new GasKVStore.
|
2018-05-08 13:03:42 -07:00
|
|
|
// nolint
|
2018-07-26 18:24:18 -07:00
|
|
|
func NewGasKVStore(gasMeter sdk.GasMeter, gasConfig sdk.GasConfig, parent sdk.KVStore) *gasKVStore {
|
2018-05-08 13:03:42 -07:00
|
|
|
kvs := &gasKVStore{
|
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.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) GetStoreType() sdk.StoreType {
|
|
|
|
return gs.parent.GetStoreType()
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Get(key []byte) (value []byte) {
|
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostFlat, sdk.GasReadCostFlatDesc)
|
|
|
|
value = gs.parent.Get(key)
|
|
|
|
|
2018-05-08 15:27:41 -07:00
|
|
|
// TODO overflow-safe math?
|
2018-09-26 07:02:56 -07:00
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostPerByte*sdk.Gas(len(value)), sdk.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.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Set(key []byte, value []byte) {
|
2019-01-29 12:28:41 -08:00
|
|
|
assertValidValue(value)
|
2018-09-26 07:02:56 -07:00
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostFlat, sdk.GasWriteCostFlatDesc)
|
2018-05-08 15:27:41 -07:00
|
|
|
// TODO overflow-safe math?
|
2018-09-26 07:02:56 -07:00
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostPerByte*sdk.Gas(len(value)), sdk.GasWritePerByteDesc)
|
|
|
|
gs.parent.Set(key, value)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Has(key []byte) bool {
|
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.HasCost, sdk.GasHasDesc)
|
|
|
|
return gs.parent.Has(key)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Delete(key []byte) {
|
|
|
|
// charge gas to prevent certain attack vectors even though space is being freed
|
|
|
|
gs.gasMeter.ConsumeGas(gs.gasConfig.DeleteCost, sdk.GasDeleteDesc)
|
|
|
|
gs.parent.Delete(key)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
2018-06-21 14:33:36 -07:00
|
|
|
// Implements KVStore
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Prefix(prefix []byte) KVStore {
|
2018-07-26 18:24:18 -07:00
|
|
|
// Keep gasstore layer at the top
|
|
|
|
return &gasKVStore{
|
2018-09-26 07:02:56 -07:00
|
|
|
gasMeter: gs.gasMeter,
|
|
|
|
gasConfig: gs.gasConfig,
|
|
|
|
parent: prefixStore{gs.parent, prefix},
|
2018-07-26 18:24:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) Gas(meter GasMeter, config GasConfig) KVStore {
|
|
|
|
return NewGasKVStore(meter, config, gs)
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -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.
|
|
|
|
func (gs *gasKVStore) Iterator(start, end []byte) sdk.Iterator {
|
|
|
|
return gs.iterator(start, end, true)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -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.
|
|
|
|
func (gs *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator {
|
|
|
|
return gs.iterator(start, end, false)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) CacheWrap() sdk.CacheWrap {
|
2018-07-12 16:58:51 -07:00
|
|
|
panic("cannot CacheWrap a GasKVStore")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheWrapWithTrace implements the KVStore interface.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap {
|
2018-07-12 16:58:51 -07:00
|
|
|
panic("cannot CacheWrapWithTrace a GasKVStore")
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator {
|
2018-05-11 08:46:50 -07:00
|
|
|
var parent sdk.Iterator
|
2018-05-08 13:03:42 -07:00
|
|
|
if ascending {
|
2018-09-26 07:02:56 -07:00
|
|
|
parent = gs.parent.Iterator(start, end)
|
2018-05-08 13:03:42 -07:00
|
|
|
} else {
|
2018-09-26 07:02:56 -07:00
|
|
|
parent = gs.parent.ReverseIterator(start, end)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|
2018-09-26 07:02:56 -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 {
|
2018-07-26 18:24:18 -07:00
|
|
|
gasMeter sdk.GasMeter
|
|
|
|
gasConfig sdk.GasConfig
|
|
|
|
parent sdk.Iterator
|
2018-05-08 15:27:41 -07:00
|
|
|
}
|
|
|
|
|
2018-07-26 18:24:18 -07:00
|
|
|
func newGasIterator(gasMeter sdk.GasMeter, gasConfig sdk.GasConfig, parent sdk.Iterator) sdk.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.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gi *gasIterator) Domain() (start []byte, end []byte) {
|
|
|
|
return gi.parent.Domain()
|
2018-05-08 15:27:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gi *gasIterator) Valid() bool {
|
|
|
|
return gi.parent.Valid()
|
2018-05-08 15:27:41 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -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
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -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
|
|
|
}
|
|
|
|
|
2018-09-26 07:02:56 -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.
|
2018-09-26 07:02:56 -07:00
|
|
|
func (gi *gasIterator) Close() {
|
|
|
|
gi.parent.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2018-12-10 02:48:19 -08:00
|
|
|
gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*sdk.Gas(len(value)), sdk.GasValuePerByteDesc)
|
2018-09-26 07:02:56 -07:00
|
|
|
gi.gasMeter.ConsumeGas(gi.gasConfig.IterNextCostFlat, sdk.GasIterNextCostFlatDesc)
|
2018-05-08 13:03:42 -07:00
|
|
|
}
|