2020-04-07 09:41:12 +00:00
|
|
|
package dao
|
2019-11-25 17:39:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-06 12:54:44 +00:00
|
|
|
"context"
|
2019-11-25 17:39:11 +00:00
|
|
|
"encoding/binary"
|
2020-10-15 11:45:29 +00:00
|
|
|
"errors"
|
2021-08-02 11:20:33 +00:00
|
|
|
"fmt"
|
2020-11-11 15:43:28 +00:00
|
|
|
iocore "io"
|
2019-11-25 17:39:11 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-05-29 14:20:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mpt"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-11-11 15:43:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-11-25 17:39:11 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// HasTransaction errors.
|
2020-10-15 11:45:29 +00:00
|
|
|
var (
|
|
|
|
// ErrAlreadyExists is returned when transaction exists in dao.
|
|
|
|
ErrAlreadyExists = errors.New("transaction already exists")
|
|
|
|
// ErrHasConflicts is returned when transaction is in the list of conflicting
|
|
|
|
// transactions which are already in dao.
|
|
|
|
ErrHasConflicts = errors.New("transaction has conflicts")
|
|
|
|
)
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// DAO is a data access object.
|
|
|
|
type DAO interface {
|
2020-11-24 09:07:58 +00:00
|
|
|
DeleteBlock(h util.Uint256, buf *io.BufBinWriter) error
|
2020-12-13 15:26:35 +00:00
|
|
|
DeleteContractID(id int32) error
|
2020-06-18 10:50:30 +00:00
|
|
|
DeleteStorageItem(id int32, key []byte) error
|
2020-04-03 06:49:01 +00:00
|
|
|
GetAndDecode(entity io.Serializable, key []byte) error
|
2020-11-11 15:43:28 +00:00
|
|
|
GetAppExecResults(hash util.Uint256, trig trigger.Type) ([]state.AppExecResult, error)
|
2020-04-03 06:49:01 +00:00
|
|
|
GetBatch() *storage.MemBatch
|
2020-06-04 19:59:34 +00:00
|
|
|
GetBlock(hash util.Uint256) (*block.Block, error)
|
2020-07-28 13:36:47 +00:00
|
|
|
GetContractScriptHash(id int32) (util.Uint160, error)
|
2020-04-03 06:49:01 +00:00
|
|
|
GetCurrentBlockHeight() (uint32, error)
|
|
|
|
GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error)
|
|
|
|
GetHeaderHashes() ([]util.Uint256, error)
|
2021-11-16 16:18:06 +00:00
|
|
|
GetTokenTransferInfo(acc util.Uint160) (*state.TokenTransferInfo, error)
|
2022-01-18 15:28:24 +00:00
|
|
|
GetTokenTransferLog(acc util.Uint160, start uint64, index uint32, isNEP11 bool) (*state.TokenTransferLog, error)
|
2021-08-03 06:56:39 +00:00
|
|
|
GetStateSyncPoint() (uint32, error)
|
|
|
|
GetStateSyncCurrentBlockHeight() (uint32, error)
|
2021-03-05 14:06:54 +00:00
|
|
|
GetStorageItem(id int32, key []byte) state.StorageItem
|
2021-09-24 14:22:45 +00:00
|
|
|
GetStorageItems(id int32) ([]state.StorageItemWithKey, error)
|
|
|
|
GetStorageItemsWithPrefix(id int32, prefix []byte) ([]state.StorageItemWithKey, error)
|
2020-04-03 06:49:01 +00:00
|
|
|
GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error)
|
2021-10-20 14:19:16 +00:00
|
|
|
GetVersion() (Version, error)
|
2022-02-03 08:46:09 +00:00
|
|
|
GetCloned() DAO
|
2020-04-07 09:41:12 +00:00
|
|
|
GetWrapped() DAO
|
2020-10-15 11:45:29 +00:00
|
|
|
HasTransaction(hash util.Uint256) error
|
2020-04-03 06:49:01 +00:00
|
|
|
Persist() (int, error)
|
2021-11-22 07:41:40 +00:00
|
|
|
PersistSync() (int, error)
|
2020-12-13 15:26:35 +00:00
|
|
|
PutContractID(id int32, hash util.Uint160) error
|
2020-04-03 06:49:01 +00:00
|
|
|
PutCurrentHeader(hashAndIndex []byte) error
|
2021-11-16 16:18:06 +00:00
|
|
|
PutTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo) error
|
2022-01-18 15:28:24 +00:00
|
|
|
PutTokenTransferLog(acc util.Uint160, start uint64, index uint32, isNEP11 bool, lg *state.TokenTransferLog) error
|
2021-08-03 06:56:39 +00:00
|
|
|
PutStateSyncPoint(p uint32) error
|
|
|
|
PutStateSyncCurrentBlockHeight(h uint32) error
|
2021-03-05 14:06:54 +00:00
|
|
|
PutStorageItem(id int32, key []byte, si state.StorageItem) error
|
2021-10-20 14:19:16 +00:00
|
|
|
PutVersion(v Version) error
|
2022-01-17 17:41:51 +00:00
|
|
|
Seek(id int32, rng storage.SeekRange, f func(k, v []byte) bool)
|
2021-12-16 13:55:50 +00:00
|
|
|
SeekAsync(ctx context.Context, id int32, rng storage.SeekRange) chan storage.KeyValue
|
2021-12-07 20:05:28 +00:00
|
|
|
StoreAsBlock(block *block.Block, aer1 *state.AppExecResult, aer2 *state.AppExecResult, buf *io.BufBinWriter) error
|
2020-08-31 19:11:49 +00:00
|
|
|
StoreAsCurrentBlock(block *block.Block, buf *io.BufBinWriter) error
|
2021-12-07 20:05:28 +00:00
|
|
|
StoreAsTransaction(tx *transaction.Transaction, index uint32, aer *state.AppExecResult, buf *io.BufBinWriter) error
|
2021-11-16 16:18:06 +00:00
|
|
|
putTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo, buf *io.BufBinWriter) error
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// Simple is memCached wrapper around DB, simple DAO implementation.
|
|
|
|
type Simple struct {
|
2021-10-22 07:58:53 +00:00
|
|
|
Version Version
|
|
|
|
Store *storage.MemCachedStore
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// NewSimple creates new simple dao using provided backend store.
|
2021-08-17 15:40:11 +00:00
|
|
|
func NewSimple(backend storage.Store, stateRootInHeader bool, p2pSigExtensions bool) *Simple {
|
2020-05-29 14:20:00 +00:00
|
|
|
st := storage.NewMemCachedStore(backend)
|
2021-09-27 13:35:25 +00:00
|
|
|
return &Simple{
|
2021-10-22 07:58:53 +00:00
|
|
|
Version: Version{
|
|
|
|
StoragePrefix: storage.STStorage,
|
|
|
|
StateRootInHeader: stateRootInHeader,
|
|
|
|
P2PSigExtensions: p2pSigExtensions,
|
|
|
|
},
|
|
|
|
Store: st,
|
2021-09-27 13:35:25 +00:00
|
|
|
}
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBatch returns currently accumulated DB changeset.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) GetBatch() *storage.MemBatch {
|
|
|
|
return dao.Store.GetBatch()
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetWrapped returns new DAO instance with another layer of wrapped
|
|
|
|
// MemCachedStore around the current DAO Store.
|
|
|
|
func (dao *Simple) GetWrapped() DAO {
|
2021-10-22 07:58:53 +00:00
|
|
|
d := NewSimple(dao.Store, dao.Version.StateRootInHeader, dao.Version.P2PSigExtensions)
|
|
|
|
d.Version = dao.Version
|
2020-05-29 14:20:00 +00:00
|
|
|
return d
|
2019-12-12 18:04:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:46:09 +00:00
|
|
|
// GetCloned returns new DAO instance with shared trie of MemCachedStore, use it for
|
|
|
|
// the latest layer that either doesn't need to Persist, or Persists to another well-known
|
|
|
|
// non-shared (!) layer.
|
|
|
|
func (dao *Simple) GetCloned() DAO {
|
|
|
|
d := *dao
|
|
|
|
d.Store = dao.Store.Clone()
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// GetAndDecode performs get operation and decoding with serializable structures.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) GetAndDecode(entity io.Serializable, key []byte) error {
|
|
|
|
entityBytes, err := dao.Store.Get(key)
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reader := io.NewBinReaderFromBuf(entityBytes)
|
|
|
|
entity.DecodeBinary(reader)
|
|
|
|
return reader.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put performs put operation with serializable structures.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) Put(entity io.Serializable, key []byte) error {
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putWithBuffer(entity, key, io.NewBufBinWriter())
|
|
|
|
}
|
|
|
|
|
|
|
|
// putWithBuffer performs put operation using buf as a pre-allocated buffer for serialization.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) putWithBuffer(entity io.Serializable, key []byte, buf *io.BufBinWriter) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
entity.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return dao.Store.Put(key, buf.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
func makeContractIDKey(id int32) []byte {
|
|
|
|
key := make([]byte, 5)
|
2020-11-18 20:10:48 +00:00
|
|
|
key[0] = byte(storage.STContractID)
|
2020-12-13 15:26:35 +00:00
|
|
|
binary.LittleEndian.PutUint32(key[1:], uint32(id))
|
|
|
|
return key
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// DeleteContractID deletes contract's id to hash mapping.
|
|
|
|
func (dao *Simple) DeleteContractID(id int32) error {
|
|
|
|
return dao.Store.Delete(makeContractIDKey(id))
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// PutContractID adds a mapping from contract's ID to its hash.
|
|
|
|
func (dao *Simple) PutContractID(id int32, hash util.Uint160) error {
|
|
|
|
return dao.Store.Put(makeContractIDKey(id), hash.BytesBE())
|
2020-06-09 12:12:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// GetContractScriptHash retrieves contract's hash given its ID.
|
2020-07-28 13:36:47 +00:00
|
|
|
func (dao *Simple) GetContractScriptHash(id int32) (util.Uint160, error) {
|
2020-12-13 15:26:35 +00:00
|
|
|
var data = new(util.Uint160)
|
|
|
|
if err := dao.GetAndDecode(data, makeContractIDKey(id)); err != nil {
|
2020-07-28 13:36:47 +00:00
|
|
|
return *data, err
|
|
|
|
}
|
|
|
|
return *data, nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// -- start NEP-17 transfer info.
|
2020-03-11 15:22:46 +00:00
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// GetTokenTransferInfo retrieves NEP-17 transfer info from the cache.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (dao *Simple) GetTokenTransferInfo(acc util.Uint160) (*state.TokenTransferInfo, error) {
|
|
|
|
key := storage.AppendPrefix(storage.STTokenTransferInfo, acc.BytesBE())
|
|
|
|
bs := state.NewTokenTransferInfo()
|
2020-03-11 15:22:46 +00:00
|
|
|
err := dao.GetAndDecode(bs, key)
|
|
|
|
if err != nil && err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// PutTokenTransferInfo saves NEP-17 transfer info in the cache.
|
2021-11-16 16:18:06 +00:00
|
|
|
func (dao *Simple) PutTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo) error {
|
|
|
|
return dao.putTokenTransferInfo(acc, bs, io.NewBufBinWriter())
|
2020-03-17 09:06:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
func (dao *Simple) putTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo, buf *io.BufBinWriter) error {
|
|
|
|
key := storage.AppendPrefix(storage.STTokenTransferInfo, acc.BytesBE())
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putWithBuffer(bs, key, buf)
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// -- end NEP-17 transfer info.
|
2020-03-11 15:22:46 +00:00
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// -- start transfer log.
|
|
|
|
|
2022-01-18 15:28:24 +00:00
|
|
|
func getTokenTransferLogKey(acc util.Uint160, newestTimestamp uint64, index uint32, isNEP11 bool) []byte {
|
|
|
|
key := make([]byte, 1+util.Uint160Size+8+4)
|
2021-11-16 20:09:04 +00:00
|
|
|
if isNEP11 {
|
|
|
|
key[0] = byte(storage.STNEP11Transfers)
|
|
|
|
} else {
|
|
|
|
key[0] = byte(storage.STNEP17Transfers)
|
|
|
|
}
|
2020-03-12 09:43:21 +00:00
|
|
|
copy(key[1:], acc.BytesBE())
|
2022-01-18 15:28:24 +00:00
|
|
|
binary.BigEndian.PutUint64(key[1+util.Uint160Size:], newestTimestamp)
|
|
|
|
binary.BigEndian.PutUint32(key[1+util.Uint160Size+8:], index)
|
2020-03-12 09:43:21 +00:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2022-01-18 15:28:24 +00:00
|
|
|
// SeekNEP17TransferLog executes f for each NEP-17 transfer in log starting from
|
|
|
|
// the transfer with the newest timestamp up to the oldest transfer. It continues
|
|
|
|
// iteration until false is returned from f. The last non-nil error is returned.
|
|
|
|
func (dao *Simple) SeekNEP17TransferLog(acc util.Uint160, newestTimestamp uint64, f func(*state.NEP17Transfer) (bool, error)) error {
|
|
|
|
key := getTokenTransferLogKey(acc, newestTimestamp, 0, false)
|
|
|
|
prefixLen := 1 + util.Uint160Size
|
|
|
|
var seekErr error
|
|
|
|
dao.Store.Seek(storage.SeekRange{
|
|
|
|
Prefix: key[:prefixLen],
|
|
|
|
Start: key[prefixLen : prefixLen+8],
|
|
|
|
Backwards: true,
|
|
|
|
}, func(k, v []byte) bool {
|
|
|
|
lg := &state.TokenTransferLog{Raw: v}
|
|
|
|
cont, err := lg.ForEachNEP17(f)
|
|
|
|
if err != nil {
|
|
|
|
seekErr = err
|
|
|
|
}
|
|
|
|
return cont
|
|
|
|
})
|
|
|
|
return seekErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeekNEP11TransferLog executes f for each NEP-11 transfer in log starting from
|
|
|
|
// the transfer with the newest timestamp up to the oldest transfer. It continues
|
|
|
|
// iteration until false is returned from f. The last non-nil error is returned.
|
|
|
|
func (dao *Simple) SeekNEP11TransferLog(acc util.Uint160, newestTimestamp uint64, f func(*state.NEP11Transfer) (bool, error)) error {
|
|
|
|
key := getTokenTransferLogKey(acc, newestTimestamp, 0, true)
|
|
|
|
prefixLen := 1 + util.Uint160Size
|
|
|
|
var seekErr error
|
|
|
|
dao.Store.Seek(storage.SeekRange{
|
|
|
|
Prefix: key[:prefixLen],
|
|
|
|
Start: key[prefixLen : prefixLen+8],
|
|
|
|
Backwards: true,
|
|
|
|
}, func(k, v []byte) bool {
|
|
|
|
lg := &state.TokenTransferLog{Raw: v}
|
|
|
|
cont, err := lg.ForEachNEP11(f)
|
|
|
|
if err != nil {
|
|
|
|
seekErr = err
|
|
|
|
}
|
|
|
|
return cont
|
|
|
|
})
|
|
|
|
return seekErr
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// GetTokenTransferLog retrieves transfer log from the cache.
|
2022-01-18 15:28:24 +00:00
|
|
|
func (dao *Simple) GetTokenTransferLog(acc util.Uint160, newestTimestamp uint64, index uint32, isNEP11 bool) (*state.TokenTransferLog, error) {
|
|
|
|
key := getTokenTransferLogKey(acc, newestTimestamp, index, isNEP11)
|
2020-04-07 09:41:12 +00:00
|
|
|
value, err := dao.Store.Get(key)
|
2020-03-05 14:11:58 +00:00
|
|
|
if err != nil {
|
2020-03-05 12:16:03 +00:00
|
|
|
if err == storage.ErrKeyNotFound {
|
2021-11-16 16:18:06 +00:00
|
|
|
return new(state.TokenTransferLog), nil
|
2020-03-05 12:16:03 +00:00
|
|
|
}
|
2020-03-05 14:11:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-16 16:18:06 +00:00
|
|
|
return &state.TokenTransferLog{Raw: value}, nil
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:18:06 +00:00
|
|
|
// PutTokenTransferLog saves given transfer log in the cache.
|
2022-01-18 15:28:24 +00:00
|
|
|
func (dao *Simple) PutTokenTransferLog(acc util.Uint160, start uint64, index uint32, isNEP11 bool, lg *state.TokenTransferLog) error {
|
|
|
|
key := getTokenTransferLogKey(acc, start, index, isNEP11)
|
2020-04-07 09:41:12 +00:00
|
|
|
return dao.Store.Put(key, lg.Raw)
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end transfer log.
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// -- start notification event.
|
|
|
|
|
2020-11-11 15:43:28 +00:00
|
|
|
// GetAppExecResults gets application execution results with the specified trigger from the
|
2019-11-25 17:39:11 +00:00
|
|
|
// given store.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (dao *Simple) GetAppExecResults(hash util.Uint256, trig trigger.Type) ([]state.AppExecResult, error) {
|
2021-12-07 20:05:28 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataExecutable, hash.BytesBE())
|
|
|
|
bs, err := dao.Store.Get(key)
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
r := io.NewBinReaderFromBuf(bs)
|
|
|
|
switch r.ReadB() {
|
|
|
|
case storage.ExecBlock:
|
|
|
|
_, err = block.NewTrimmedFromReader(dao.Version.StateRootInHeader, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case storage.ExecTransaction:
|
|
|
|
_ = r.ReadU32LE()
|
|
|
|
tx := &transaction.Transaction{}
|
|
|
|
tx.DecodeBinary(r)
|
|
|
|
}
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
result := make([]state.AppExecResult, 0, 2)
|
|
|
|
for {
|
|
|
|
aer := new(state.AppExecResult)
|
|
|
|
aer.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
if r.Err == iocore.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
if aer.Trigger&trig != 0 {
|
|
|
|
result = append(result, *aer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// -- end notification event.
|
|
|
|
|
|
|
|
// -- start storage item.
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetStorageItem returns StorageItem if it exists in the given store.
|
2021-03-05 14:06:54 +00:00
|
|
|
func (dao *Simple) GetStorageItem(id int32, key []byte) state.StorageItem {
|
2021-10-22 07:58:53 +00:00
|
|
|
b, err := dao.Store.Get(makeStorageItemKey(dao.Version.StoragePrefix, id, key))
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-09 09:09:44 +00:00
|
|
|
return b
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
// PutStorageItem puts given StorageItem for given id with given
|
2020-04-07 09:41:12 +00:00
|
|
|
// key into the given store.
|
2021-03-05 14:06:54 +00:00
|
|
|
func (dao *Simple) PutStorageItem(id int32, key []byte, si state.StorageItem) error {
|
2021-10-22 07:58:53 +00:00
|
|
|
stKey := makeStorageItemKey(dao.Version.StoragePrefix, id, key)
|
2021-03-09 09:09:44 +00:00
|
|
|
return dao.Store.Put(stKey, si)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
// DeleteStorageItem drops storage item for the given id with the
|
2020-04-07 09:41:12 +00:00
|
|
|
// given key from the store.
|
2020-06-18 10:50:30 +00:00
|
|
|
func (dao *Simple) DeleteStorageItem(id int32, key []byte) error {
|
2021-10-22 07:58:53 +00:00
|
|
|
stKey := makeStorageItemKey(dao.Version.StoragePrefix, id, key)
|
2020-05-29 14:20:00 +00:00
|
|
|
return dao.Store.Delete(stKey)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
// GetStorageItems returns all storage items for a given id.
|
2021-09-24 14:22:45 +00:00
|
|
|
func (dao *Simple) GetStorageItems(id int32) ([]state.StorageItemWithKey, error) {
|
2020-06-18 10:50:30 +00:00
|
|
|
return dao.GetStorageItemsWithPrefix(id, nil)
|
2020-04-25 21:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
// GetStorageItemsWithPrefix returns all storage items with given id for a
|
2020-04-25 21:23:30 +00:00
|
|
|
// given scripthash.
|
2021-09-24 14:22:45 +00:00
|
|
|
func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) ([]state.StorageItemWithKey, error) {
|
|
|
|
var siArr []state.StorageItemWithKey
|
2019-11-25 17:39:11 +00:00
|
|
|
|
2022-01-17 17:41:51 +00:00
|
|
|
saveToArr := func(k, v []byte) bool {
|
2019-11-25 17:39:11 +00:00
|
|
|
// Cut prefix and hash.
|
2021-09-24 15:44:42 +00:00
|
|
|
// #1468, but don't need to copy here, because it is done by Store.
|
2021-09-24 14:22:45 +00:00
|
|
|
siArr = append(siArr, state.StorageItemWithKey{
|
2021-09-24 15:44:42 +00:00
|
|
|
Key: k,
|
|
|
|
Item: state.StorageItem(v),
|
2021-09-24 14:22:45 +00:00
|
|
|
})
|
2022-01-17 17:41:51 +00:00
|
|
|
return true
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2021-12-16 13:55:50 +00:00
|
|
|
dao.Seek(id, storage.SeekRange{Prefix: prefix}, saveToArr)
|
2021-09-24 14:22:45 +00:00
|
|
|
return siArr, nil
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:55:50 +00:00
|
|
|
// Seek executes f for all storage items matching a given `rng` (matching given prefix and
|
|
|
|
// starting from the point specified). If key or value is to be used outside of f, they
|
2022-01-17 17:41:51 +00:00
|
|
|
// may not be copied. Seek continues iterating until false is returned from f.
|
|
|
|
func (dao *Simple) Seek(id int32, rng storage.SeekRange, f func(k, v []byte) bool) {
|
2021-12-16 13:55:50 +00:00
|
|
|
rng.Prefix = makeStorageItemKey(dao.Version.StoragePrefix, id, rng.Prefix)
|
2022-01-17 17:41:51 +00:00
|
|
|
dao.Store.Seek(rng, func(k, v []byte) bool {
|
|
|
|
return f(k[len(rng.Prefix):], v)
|
2021-10-19 15:03:47 +00:00
|
|
|
})
|
2021-10-04 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:55:50 +00:00
|
|
|
// SeekAsync sends all storage items matching a given `rng` (matching given prefix and
|
|
|
|
// starting from the point specified) to a channel and returns the channel.
|
|
|
|
// Resulting keys and values may not be copied.
|
|
|
|
func (dao *Simple) SeekAsync(ctx context.Context, id int32, rng storage.SeekRange) chan storage.KeyValue {
|
|
|
|
rng.Prefix = makeStorageItemKey(dao.Version.StoragePrefix, id, rng.Prefix)
|
|
|
|
return dao.Store.SeekAsync(ctx, rng, true)
|
2020-11-03 15:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// makeStorageItemKey returns a key used to store StorageItem in the DB.
|
2021-09-27 13:35:25 +00:00
|
|
|
func makeStorageItemKey(prefix storage.KeyPrefix, id int32, key []byte) []byte {
|
2020-06-18 10:50:30 +00:00
|
|
|
// 1 for prefix + 4 for Uint32 + len(key) for key
|
|
|
|
buf := make([]byte, 5+len(key))
|
2021-09-27 13:35:25 +00:00
|
|
|
buf[0] = byte(prefix)
|
2020-06-18 10:50:30 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf[1:], uint32(id))
|
|
|
|
copy(buf[5:], key)
|
|
|
|
return buf
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end storage item.
|
|
|
|
|
|
|
|
// -- other.
|
|
|
|
|
|
|
|
// GetBlock returns Block by the given hash if it exists in the store.
|
2020-06-04 19:59:34 +00:00
|
|
|
func (dao *Simple) GetBlock(hash util.Uint256) (*block.Block, error) {
|
2021-12-07 20:05:28 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataExecutable, hash.BytesBE())
|
2020-04-07 09:41:12 +00:00
|
|
|
b, err := dao.Store.Get(key)
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
2020-06-04 19:59:34 +00:00
|
|
|
return nil, err
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2020-02-27 13:31:28 +00:00
|
|
|
|
2021-12-07 20:05:28 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
if r.ReadB() != storage.ExecBlock {
|
|
|
|
return nil, errors.New("internal DB inconsistency")
|
|
|
|
}
|
|
|
|
block, err := block.NewTrimmedFromReader(dao.Version.StateRootInHeader, r)
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
2020-06-04 19:59:34 +00:00
|
|
|
return nil, err
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2020-06-04 19:59:34 +00:00
|
|
|
return block, nil
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 14:19:16 +00:00
|
|
|
// Version represents current dao version.
|
|
|
|
type Version struct {
|
2021-11-03 09:55:33 +00:00
|
|
|
StoragePrefix storage.KeyPrefix
|
|
|
|
StateRootInHeader bool
|
|
|
|
P2PSigExtensions bool
|
|
|
|
P2PStateExchangeExtensions bool
|
|
|
|
KeepOnlyLatestState bool
|
|
|
|
Value string
|
2021-10-20 14:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 07:58:53 +00:00
|
|
|
const (
|
|
|
|
stateRootInHeaderBit = 1 << iota
|
|
|
|
p2pSigExtensionsBit
|
2021-11-03 09:55:33 +00:00
|
|
|
p2pStateExchangeExtensionsBit
|
2021-10-22 08:09:47 +00:00
|
|
|
keepOnlyLatestStateBit
|
2021-10-22 07:58:53 +00:00
|
|
|
)
|
|
|
|
|
2021-10-20 14:19:16 +00:00
|
|
|
// FromBytes decodes v from a byte-slice.
|
|
|
|
func (v *Version) FromBytes(data []byte) error {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return errors.New("missing version")
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for ; i < len(data) && data[i] != '\x00'; i++ {
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(data) {
|
|
|
|
v.Value = string(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 07:58:53 +00:00
|
|
|
if len(data) != i+3 {
|
|
|
|
return errors.New("version is invalid")
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:19:16 +00:00
|
|
|
v.Value = string(data[:i])
|
2021-10-22 07:58:53 +00:00
|
|
|
v.StoragePrefix = storage.KeyPrefix(data[i+1])
|
|
|
|
v.StateRootInHeader = data[i+2]&stateRootInHeaderBit != 0
|
|
|
|
v.P2PSigExtensions = data[i+2]&p2pSigExtensionsBit != 0
|
2021-11-03 09:55:33 +00:00
|
|
|
v.P2PStateExchangeExtensions = data[i+2]&p2pStateExchangeExtensionsBit != 0
|
2021-10-22 08:09:47 +00:00
|
|
|
v.KeepOnlyLatestState = data[i+2]&keepOnlyLatestStateBit != 0
|
2021-10-20 14:19:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes encodes v to a byte-slice.
|
|
|
|
func (v *Version) Bytes() []byte {
|
2021-10-22 07:58:53 +00:00
|
|
|
var mask byte
|
|
|
|
if v.StateRootInHeader {
|
|
|
|
mask |= stateRootInHeaderBit
|
|
|
|
}
|
|
|
|
if v.P2PSigExtensions {
|
|
|
|
mask |= p2pSigExtensionsBit
|
|
|
|
}
|
2021-11-03 09:55:33 +00:00
|
|
|
if v.P2PStateExchangeExtensions {
|
|
|
|
mask |= p2pStateExchangeExtensionsBit
|
|
|
|
}
|
2021-10-22 08:09:47 +00:00
|
|
|
if v.KeepOnlyLatestState {
|
|
|
|
mask |= keepOnlyLatestStateBit
|
|
|
|
}
|
2021-10-22 07:58:53 +00:00
|
|
|
return append([]byte(v.Value), '\x00', byte(v.StoragePrefix), mask)
|
2021-10-20 14:19:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// GetVersion attempts to get the current version stored in the
|
2020-04-07 09:41:12 +00:00
|
|
|
// underlying store.
|
2021-10-20 14:19:16 +00:00
|
|
|
func (dao *Simple) GetVersion() (Version, error) {
|
|
|
|
var version Version
|
|
|
|
|
|
|
|
data, err := dao.Store.Get(storage.SYSVersion.Bytes())
|
|
|
|
if err == nil {
|
|
|
|
err = version.FromBytes(data)
|
|
|
|
}
|
|
|
|
return version, err
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentBlockHeight returns the current block height found in the
|
2020-04-07 09:41:12 +00:00
|
|
|
// underlying store.
|
|
|
|
func (dao *Simple) GetCurrentBlockHeight() (uint32, error) {
|
|
|
|
b, err := dao.Store.Get(storage.SYSCurrentBlock.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(b[32:36]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentHeaderHeight returns the current header height and hash from
|
2020-04-07 09:41:12 +00:00
|
|
|
// the underlying store.
|
|
|
|
func (dao *Simple) GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
var b []byte
|
2020-04-07 09:41:12 +00:00
|
|
|
b, err = dao.Store.Get(storage.SYSCurrentHeader.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i = binary.LittleEndian.Uint32(b[32:36])
|
|
|
|
h, err = util.Uint256DecodeBytesLE(b[:32])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-03 06:56:39 +00:00
|
|
|
// GetStateSyncPoint returns current state synchronisation point P.
|
|
|
|
func (dao *Simple) GetStateSyncPoint() (uint32, error) {
|
|
|
|
b, err := dao.Store.Get(storage.SYSStateSyncPoint.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateSyncCurrentBlockHeight returns current block height stored during state
|
|
|
|
// synchronisation process.
|
|
|
|
func (dao *Simple) GetStateSyncCurrentBlockHeight() (uint32, error) {
|
|
|
|
b, err := dao.Store.Get(storage.SYSStateSyncCurrentBlockHeight.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(b), nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// GetHeaderHashes returns a sorted list of header hashes retrieved from
|
2020-04-07 09:41:12 +00:00
|
|
|
// the given underlying store.
|
|
|
|
func (dao *Simple) GetHeaderHashes() ([]util.Uint256, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
hashMap := make(map[uint32][]util.Uint256)
|
2021-12-16 13:55:50 +00:00
|
|
|
dao.Store.Seek(storage.SeekRange{
|
|
|
|
Prefix: storage.IXHeaderHashList.Bytes(),
|
2022-01-17 17:41:51 +00:00
|
|
|
}, func(k, v []byte) bool {
|
2019-11-25 17:39:11 +00:00
|
|
|
storedCount := binary.LittleEndian.Uint32(k[1:])
|
|
|
|
hashes, err := read2000Uint256Hashes(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
hashMap[storedCount] = hashes
|
2022-01-17 17:41:51 +00:00
|
|
|
return true
|
2019-11-25 17:39:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
hashes = make([]util.Uint256, 0, len(hashMap))
|
|
|
|
sortedKeys = make([]uint32, 0, len(hashMap))
|
|
|
|
)
|
|
|
|
|
|
|
|
for k := range hashMap {
|
|
|
|
sortedKeys = append(sortedKeys, k)
|
|
|
|
}
|
2020-04-07 09:49:23 +00:00
|
|
|
sort.Slice(sortedKeys, func(i, j int) bool { return sortedKeys[i] < sortedKeys[j] })
|
2019-11-25 17:39:11 +00:00
|
|
|
|
|
|
|
for _, key := range sortedKeys {
|
|
|
|
hashes = append(hashes[:key], hashMap[key]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransaction returns Transaction and its height by the given hash
|
2020-10-15 11:45:29 +00:00
|
|
|
// if it exists in the store. It does not return dummy transactions.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error) {
|
2021-12-07 20:05:28 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataExecutable, hash.BytesBE())
|
2020-04-07 09:41:12 +00:00
|
|
|
b, err := dao.Store.Get(key)
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
if len(b) < 6 {
|
2020-10-15 11:45:29 +00:00
|
|
|
return nil, 0, errors.New("bad transaction bytes")
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
if b[0] != storage.ExecTransaction {
|
|
|
|
return nil, 0, errors.New("internal DB inconsistency")
|
|
|
|
}
|
|
|
|
if b[5] == transaction.DummyVersion {
|
2020-10-15 11:45:29 +00:00
|
|
|
return nil, 0, storage.ErrKeyNotFound
|
|
|
|
}
|
2019-11-25 17:39:11 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
2021-12-07 20:05:28 +00:00
|
|
|
_ = r.ReadB()
|
2019-11-25 17:39:11 +00:00
|
|
|
|
2019-12-12 15:52:23 +00:00
|
|
|
var height = r.ReadU32LE()
|
2019-11-25 17:39:11 +00:00
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := &transaction.Transaction{}
|
2019-11-25 17:39:11 +00:00
|
|
|
tx.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, 0, r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, height, nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// PutVersion stores the given version in the underlying store.
|
2021-10-20 14:19:16 +00:00
|
|
|
func (dao *Simple) PutVersion(v Version) error {
|
2021-10-22 07:58:53 +00:00
|
|
|
dao.Version = v
|
2021-10-20 14:19:16 +00:00
|
|
|
return dao.Store.Put(storage.SYSVersion.Bytes(), v.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutCurrentHeader stores current header.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) PutCurrentHeader(hashAndIndex []byte) error {
|
|
|
|
return dao.Store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndex)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 06:56:39 +00:00
|
|
|
// PutStateSyncPoint stores current state synchronisation point P.
|
|
|
|
func (dao *Simple) PutStateSyncPoint(p uint32) error {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, p)
|
|
|
|
return dao.Store.Put(storage.SYSStateSyncPoint.Bytes(), buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutStateSyncCurrentBlockHeight stores current block height during state synchronisation process.
|
|
|
|
func (dao *Simple) PutStateSyncCurrentBlockHeight(h uint32) error {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, h)
|
|
|
|
return dao.Store.Put(storage.SYSStateSyncCurrentBlockHeight.Bytes(), buf)
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// read2000Uint256Hashes attempts to read 2000 Uint256 hashes from
|
|
|
|
// the given byte array.
|
|
|
|
func read2000Uint256Hashes(b []byte) ([]util.Uint256, error) {
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
br := io.NewBinReaderFromIO(r)
|
2019-12-12 15:52:23 +00:00
|
|
|
hashes := make([]util.Uint256, 0)
|
|
|
|
br.ReadArray(&hashes)
|
2019-11-25 17:39:11 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return nil, br.Err
|
|
|
|
}
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 11:45:29 +00:00
|
|
|
// HasTransaction returns nil if the given store does not contain the given
|
|
|
|
// Transaction hash. It returns an error in case if transaction is in chain
|
|
|
|
// or in the list of conflicting transactions.
|
|
|
|
func (dao *Simple) HasTransaction(hash util.Uint256) error {
|
2021-12-07 20:05:28 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataExecutable, hash.BytesBE())
|
2020-10-15 11:45:29 +00:00
|
|
|
bytes, err := dao.Store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2020-10-15 11:45:29 +00:00
|
|
|
|
2021-12-07 20:05:28 +00:00
|
|
|
if len(bytes) < 6 {
|
2020-10-15 11:45:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
if bytes[5] == transaction.DummyVersion {
|
2020-10-15 11:45:29 +00:00
|
|
|
return ErrHasConflicts
|
|
|
|
}
|
|
|
|
return ErrAlreadyExists
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:11:49 +00:00
|
|
|
// StoreAsBlock stores given block as DataBlock. It can reuse given buffer for
|
|
|
|
// the purpose of value serialization.
|
2021-12-07 20:05:28 +00:00
|
|
|
func (dao *Simple) StoreAsBlock(block *block.Block, aer1 *state.AppExecResult, aer2 *state.AppExecResult, buf *io.BufBinWriter) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
var (
|
2021-12-07 20:05:28 +00:00
|
|
|
key = storage.AppendPrefix(storage.DataExecutable, block.Hash().BytesBE())
|
2019-11-25 17:39:11 +00:00
|
|
|
)
|
2020-08-31 19:11:49 +00:00
|
|
|
if buf == nil {
|
|
|
|
buf = io.NewBufBinWriter()
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
buf.WriteB(storage.ExecBlock)
|
2019-11-25 17:39:11 +00:00
|
|
|
b, err := block.Trim()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-12 15:52:23 +00:00
|
|
|
buf.WriteBytes(b)
|
2021-12-07 20:05:28 +00:00
|
|
|
if aer1 != nil {
|
|
|
|
aer1.EncodeBinary(buf.BinWriter)
|
|
|
|
}
|
|
|
|
if aer2 != nil {
|
|
|
|
aer2.EncodeBinary(buf.BinWriter)
|
|
|
|
}
|
2019-11-25 17:39:11 +00:00
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return dao.Store.Put(key, buf.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 09:07:58 +00:00
|
|
|
// DeleteBlock removes block from dao.
|
|
|
|
func (dao *Simple) DeleteBlock(h util.Uint256, w *io.BufBinWriter) error {
|
|
|
|
batch := dao.Store.Batch()
|
|
|
|
key := make([]byte, util.Uint256Size+1)
|
2021-12-07 20:05:28 +00:00
|
|
|
key[0] = byte(storage.DataExecutable)
|
2020-11-24 09:07:58 +00:00
|
|
|
copy(key[1:], h.BytesBE())
|
|
|
|
bs, err := dao.Store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 20:05:28 +00:00
|
|
|
r := io.NewBinReaderFromBuf(bs)
|
|
|
|
if r.ReadB() != storage.ExecBlock {
|
|
|
|
return errors.New("internal DB inconsistency")
|
|
|
|
}
|
|
|
|
b, err := block.NewTrimmedFromReader(dao.Version.StateRootInHeader, r)
|
2020-11-24 09:07:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if w == nil {
|
|
|
|
w = io.NewBufBinWriter()
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
w.WriteB(storage.ExecBlock)
|
2021-03-01 13:44:47 +00:00
|
|
|
b.Header.EncodeBinary(w.BinWriter)
|
|
|
|
w.BinWriter.WriteB(0)
|
2020-11-24 09:07:58 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
|
|
|
batch.Put(key, w.Bytes())
|
|
|
|
|
|
|
|
for _, tx := range b.Transactions {
|
|
|
|
copy(key[1:], tx.Hash().BytesBE())
|
|
|
|
batch.Delete(key)
|
2021-10-22 07:58:53 +00:00
|
|
|
if dao.Version.P2PSigExtensions {
|
2021-08-17 15:40:11 +00:00
|
|
|
for _, attr := range tx.GetAttributes(transaction.ConflictsT) {
|
|
|
|
hash := attr.Value.(*transaction.Conflicts).Hash
|
|
|
|
copy(key[1:], hash.BytesBE())
|
|
|
|
batch.Delete(key)
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 09:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dao.Store.PutBatch(batch)
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:11:49 +00:00
|
|
|
// StoreAsCurrentBlock stores a hash of the given block with prefix
|
|
|
|
// SYSCurrentBlock. It can reuse given buffer for the purpose of value
|
|
|
|
// serialization.
|
|
|
|
func (dao *Simple) StoreAsCurrentBlock(block *block.Block, buf *io.BufBinWriter) error {
|
|
|
|
if buf == nil {
|
|
|
|
buf = io.NewBufBinWriter()
|
|
|
|
}
|
2019-12-12 15:52:23 +00:00
|
|
|
h := block.Hash()
|
|
|
|
h.EncodeBinary(buf.BinWriter)
|
|
|
|
buf.WriteU32LE(block.Index)
|
2020-04-07 09:41:12 +00:00
|
|
|
return dao.Store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes())
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 15:33:09 +00:00
|
|
|
// StoreAsTransaction stores given TX as DataTransaction. It also stores transactions
|
|
|
|
// given tx has conflicts with as DataTransaction with dummy version. It can reuse given
|
2020-08-31 19:11:49 +00:00
|
|
|
// buffer for the purpose of value serialization.
|
2021-12-07 20:05:28 +00:00
|
|
|
func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32, aer *state.AppExecResult, buf *io.BufBinWriter) error {
|
|
|
|
key := storage.AppendPrefix(storage.DataExecutable, tx.Hash().BytesBE())
|
2020-08-31 19:11:49 +00:00
|
|
|
if buf == nil {
|
|
|
|
buf = io.NewBufBinWriter()
|
|
|
|
}
|
2021-12-07 20:05:28 +00:00
|
|
|
buf.WriteB(storage.ExecTransaction)
|
2019-12-12 15:52:23 +00:00
|
|
|
buf.WriteU32LE(index)
|
2021-08-17 15:33:09 +00:00
|
|
|
tx.EncodeBinary(buf.BinWriter)
|
2021-12-07 20:05:28 +00:00
|
|
|
if aer != nil {
|
|
|
|
aer.EncodeBinary(buf.BinWriter)
|
|
|
|
}
|
2019-11-25 17:39:11 +00:00
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2021-08-17 15:33:09 +00:00
|
|
|
err := dao.Store.Put(key, buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-02 11:20:33 +00:00
|
|
|
}
|
2021-10-22 07:58:53 +00:00
|
|
|
if dao.Version.P2PSigExtensions {
|
2021-08-17 15:33:09 +00:00
|
|
|
var value []byte
|
|
|
|
for _, attr := range tx.GetAttributes(transaction.ConflictsT) {
|
|
|
|
hash := attr.Value.(*transaction.Conflicts).Hash
|
|
|
|
copy(key[1:], hash.BytesBE())
|
|
|
|
if value == nil {
|
|
|
|
buf.Reset()
|
2021-12-07 20:05:28 +00:00
|
|
|
buf.WriteB(storage.ExecTransaction)
|
2021-08-17 15:33:09 +00:00
|
|
|
buf.WriteU32LE(index)
|
|
|
|
buf.BinWriter.WriteB(transaction.DummyVersion)
|
|
|
|
value = buf.Bytes()
|
|
|
|
}
|
|
|
|
err = dao.Store.Put(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to store conflicting transaction %s for transaction %s: %w", hash.StringLE(), tx.Hash().StringLE(), err)
|
|
|
|
}
|
2021-08-02 11:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:17:13 +00:00
|
|
|
// Persist flushes all the changes made into the (supposedly) persistent
|
2021-11-22 07:41:40 +00:00
|
|
|
// underlying store. It doesn't block accesses to DAO from other threads.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (dao *Simple) Persist() (int, error) {
|
|
|
|
return dao.Store.Persist()
|
2019-12-12 18:17:13 +00:00
|
|
|
}
|
2020-12-24 16:32:27 +00:00
|
|
|
|
2021-11-22 07:41:40 +00:00
|
|
|
// PersistSync flushes all the changes made into the (supposedly) persistent
|
|
|
|
// underlying store. It's a synchronous version of Persist that doesn't allow
|
|
|
|
// other threads to work with DAO while flushing the Store.
|
|
|
|
func (dao *Simple) PersistSync() (int, error) {
|
|
|
|
return dao.Store.PersistSync()
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
// GetMPTBatch storage changes to be applied to MPT.
|
|
|
|
func (dao *Simple) GetMPTBatch() mpt.Batch {
|
2020-12-26 10:27:59 +00:00
|
|
|
var b mpt.Batch
|
2021-10-22 07:58:53 +00:00
|
|
|
dao.Store.MemoryStore.SeekAll([]byte{byte(dao.Version.StoragePrefix)}, func(k, v []byte) {
|
2020-12-26 10:27:59 +00:00
|
|
|
b.Add(k[1:], v)
|
2020-12-24 16:32:27 +00:00
|
|
|
})
|
2021-01-29 14:33:24 +00:00
|
|
|
return b
|
2020-12-24 16:32:27 +00:00
|
|
|
}
|