2019-11-25 17:39:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"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/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-11-25 17:39:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// dao is a data access object.
|
2020-04-03 06:49:01 +00:00
|
|
|
type dao interface {
|
|
|
|
AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error)
|
|
|
|
DeleteContractState(hash util.Uint160) error
|
|
|
|
DeleteStorageItem(scripthash util.Uint160, key []byte) error
|
|
|
|
DeleteValidatorState(vs *state.Validator) error
|
|
|
|
GetAccountState(hash util.Uint160) (*state.Account, error)
|
|
|
|
GetAccountStateOrNew(hash util.Uint160) (*state.Account, error)
|
|
|
|
GetAndDecode(entity io.Serializable, key []byte) error
|
|
|
|
GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error)
|
|
|
|
GetAssetState(assetID util.Uint256) (*state.Asset, error)
|
|
|
|
GetBatch() *storage.MemBatch
|
|
|
|
GetBlock(hash util.Uint256) (*block.Block, uint32, error)
|
|
|
|
GetContractState(hash util.Uint160) (*state.Contract, error)
|
|
|
|
GetCurrentBlockHeight() (uint32, error)
|
|
|
|
GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error)
|
|
|
|
GetHeaderHashes() ([]util.Uint256, error)
|
|
|
|
GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error)
|
|
|
|
GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error)
|
|
|
|
GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem
|
|
|
|
GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error)
|
|
|
|
GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error)
|
|
|
|
GetUnspentCoinState(hash util.Uint256) (*state.UnspentCoin, error)
|
|
|
|
GetValidatorState(publicKey *keys.PublicKey) (*state.Validator, error)
|
|
|
|
GetValidatorStateOrNew(publicKey *keys.PublicKey) (*state.Validator, error)
|
|
|
|
GetValidators() []*state.Validator
|
|
|
|
GetValidatorsCount() (*state.ValidatorsCount, error)
|
|
|
|
GetVersion() (string, error)
|
|
|
|
GetWrapped() dao
|
|
|
|
HasTransaction(hash util.Uint256) bool
|
|
|
|
IsDoubleClaim(claim *transaction.ClaimTX) bool
|
|
|
|
IsDoubleSpend(tx *transaction.Transaction) bool
|
|
|
|
Persist() (int, error)
|
|
|
|
PutAccountState(as *state.Account) error
|
|
|
|
PutAppExecResult(aer *state.AppExecResult) error
|
|
|
|
PutAssetState(as *state.Asset) error
|
|
|
|
PutContractState(cs *state.Contract) error
|
|
|
|
PutCurrentHeader(hashAndIndex []byte) error
|
|
|
|
PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error
|
|
|
|
PutNEP5TransferLog(acc util.Uint160, index uint32, lg *state.NEP5TransferLog) error
|
|
|
|
PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error
|
|
|
|
PutUnspentCoinState(hash util.Uint256, ucs *state.UnspentCoin) error
|
|
|
|
PutValidatorState(vs *state.Validator) error
|
|
|
|
PutValidatorsCount(vc *state.ValidatorsCount) error
|
|
|
|
PutVersion(v string) error
|
|
|
|
StoreAsBlock(block *block.Block, sysFee uint32) error
|
|
|
|
StoreAsCurrentBlock(block *block.Block) error
|
|
|
|
StoreAsTransaction(tx *transaction.Transaction, index uint32) error
|
|
|
|
putAccountState(as *state.Account, buf *io.BufBinWriter) error
|
|
|
|
putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error
|
|
|
|
putUnspentCoinState(hash util.Uint256, ucs *state.UnspentCoin, buf *io.BufBinWriter) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// simpleDao is memCached wrapper around DB, simple dao implementation.
|
|
|
|
type simpleDao struct {
|
2019-11-25 17:39:11 +00:00
|
|
|
store *storage.MemCachedStore
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func newSimpleDao(backend storage.Store) *simpleDao {
|
|
|
|
return &simpleDao{store: storage.NewMemCachedStore(backend)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBatch returns currently accumulated DB changeset.
|
|
|
|
func (dao *simpleDao) GetBatch() *storage.MemBatch {
|
|
|
|
return dao.store.GetBatch()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWrapped returns new dao instance with another layer of wrapped
|
|
|
|
// MemCachedStore around the current dao store.
|
|
|
|
func (dao *simpleDao) GetWrapped() dao {
|
|
|
|
return newSimpleDao(dao.store)
|
2019-12-12 18:04:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// GetAndDecode performs get operation and decoding with serializable structures.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetAndDecode(entity io.Serializable, key []byte) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
entityBytes, err := dao.store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reader := io.NewBinReaderFromBuf(entityBytes)
|
|
|
|
entity.DecodeBinary(reader)
|
|
|
|
return reader.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put performs put operation with serializable structures.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) 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-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) 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
|
|
|
|
}
|
|
|
|
return dao.store.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- start accounts.
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// GetAccountStateOrNew retrieves Account from temporary or persistent Store
|
2019-11-25 17:39:11 +00:00
|
|
|
// or creates a new one if it doesn't exist and persists it.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
account, err := dao.GetAccountState(hash)
|
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
account = state.NewAccount(hash)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// GetAccountState returns Account from the given Store if it's
|
2019-11-25 17:39:11 +00:00
|
|
|
// present there. Returns nil otherwise.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetAccountState(hash util.Uint160) (*state.Account, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
account := &state.Account{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAccount, hash.BytesBE())
|
|
|
|
err := dao.GetAndDecode(account, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutAccountState(as *state.Account) error {
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putAccountState(as, io.NewBufBinWriter())
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) putAccountState(as *state.Account, buf *io.BufBinWriter) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE())
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putWithBuffer(as, key, buf)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end accounts.
|
|
|
|
|
|
|
|
// -- start assets.
|
|
|
|
|
|
|
|
// GetAssetState returns given asset state as recorded in the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetAssetState(assetID util.Uint256) (*state.Asset, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
asset := &state.Asset{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAsset, assetID.BytesBE())
|
|
|
|
err := dao.GetAndDecode(asset, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if asset.ID != assetID {
|
|
|
|
return nil, fmt.Errorf("found asset id is not equal to expected")
|
|
|
|
}
|
|
|
|
return asset, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutAssetState puts given asset state into the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutAssetState(as *state.Asset) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STAsset, as.ID.BytesBE())
|
|
|
|
return dao.Put(as, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- end assets.
|
|
|
|
|
|
|
|
// -- start contracts.
|
|
|
|
|
|
|
|
// GetContractState returns contract state as recorded in the given
|
|
|
|
// store by the given script hash.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetContractState(hash util.Uint160) (*state.Contract, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
contract := &state.Contract{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
|
|
|
|
err := dao.GetAndDecode(contract, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if contract.ScriptHash() != hash {
|
|
|
|
return nil, fmt.Errorf("found script hash is not equal to expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
return contract, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutContractState puts given contract state into the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutContractState(cs *state.Contract) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().BytesBE())
|
|
|
|
return dao.Put(cs, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteContractState deletes given contract state in the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) DeleteContractState(hash util.Uint160) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
|
|
|
|
return dao.store.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- end contracts.
|
|
|
|
|
2020-03-11 15:22:46 +00:00
|
|
|
// -- start nep5 balances.
|
|
|
|
|
|
|
|
// GetNEP5Balances retrieves nep5 balances from the cache.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error) {
|
2020-03-11 15:22:46 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNEP5Balances, acc.BytesBE())
|
|
|
|
bs := state.NewNEP5Balances()
|
|
|
|
err := dao.GetAndDecode(bs, key)
|
|
|
|
if err != nil && err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:06:46 +00:00
|
|
|
// PutNEP5Balances saves nep5 balances from the cache.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error {
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putNEP5Balances(acc, bs, io.NewBufBinWriter())
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error {
|
2020-03-11 15:22:46 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNEP5Balances, acc.BytesBE())
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putWithBuffer(bs, key, buf)
|
2020-03-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end nep5 balances.
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// -- start transfer log.
|
|
|
|
|
2020-03-12 09:43:21 +00:00
|
|
|
const nep5TransferBatchSize = 128
|
|
|
|
|
|
|
|
func getNEP5TransferLogKey(acc util.Uint160, index uint32) []byte {
|
|
|
|
key := make([]byte, 1+util.Uint160Size+4)
|
|
|
|
key[0] = byte(storage.STNEP5Transfers)
|
|
|
|
copy(key[1:], acc.BytesBE())
|
|
|
|
binary.LittleEndian.PutUint32(key[util.Uint160Size:], index)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:11:58 +00:00
|
|
|
// GetNEP5TransferLog retrieves transfer log from the cache.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error) {
|
2020-03-12 09:43:21 +00:00
|
|
|
key := getNEP5TransferLogKey(acc, index)
|
2020-03-05 14:11:58 +00:00
|
|
|
value, err := dao.store.Get(key)
|
|
|
|
if err != nil {
|
2020-03-05 12:16:03 +00:00
|
|
|
if err == storage.ErrKeyNotFound {
|
|
|
|
return new(state.NEP5TransferLog), nil
|
|
|
|
}
|
2020-03-05 14:11:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &state.NEP5TransferLog{Raw: value}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutNEP5TransferLog saves given transfer log in the cache.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutNEP5TransferLog(acc util.Uint160, index uint32, lg *state.NEP5TransferLog) error {
|
2020-03-12 09:43:21 +00:00
|
|
|
key := getNEP5TransferLogKey(acc, index)
|
2020-03-05 14:11:58 +00:00
|
|
|
return dao.store.Put(key, lg.Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendNEP5Transfer appends a single NEP5 transfer to a log.
|
2020-03-12 09:43:21 +00:00
|
|
|
// First return value signalizes that log size has exceeded batch size.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) {
|
2020-03-12 09:43:21 +00:00
|
|
|
lg, err := dao.GetNEP5TransferLog(acc, index)
|
2020-03-05 14:11:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrKeyNotFound {
|
2020-03-12 09:43:21 +00:00
|
|
|
return false, err
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
lg = new(state.NEP5TransferLog)
|
|
|
|
}
|
|
|
|
if err := lg.Append(tr); err != nil {
|
2020-03-12 09:43:21 +00:00
|
|
|
return false, err
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
2020-03-12 09:43:21 +00:00
|
|
|
return lg.Size() >= nep5TransferBatchSize, dao.PutNEP5TransferLog(acc, index, lg)
|
2020-03-05 14:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end transfer log.
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// -- start unspent coins.
|
|
|
|
|
|
|
|
// GetUnspentCoinState retrieves UnspentCoinState from the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetUnspentCoinState(hash util.Uint256) (*state.UnspentCoin, error) {
|
2020-03-09 13:56:37 +00:00
|
|
|
unspent := &state.UnspentCoin{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesLE())
|
|
|
|
err := dao.GetAndDecode(unspent, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return unspent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutUnspentCoinState puts given UnspentCoinState into the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutUnspentCoinState(hash util.Uint256, ucs *state.UnspentCoin) error {
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putUnspentCoinState(hash, ucs, io.NewBufBinWriter())
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) putUnspentCoinState(hash util.Uint256, ucs *state.UnspentCoin, buf *io.BufBinWriter) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesLE())
|
2020-03-17 09:06:46 +00:00
|
|
|
return dao.putWithBuffer(ucs, key, buf)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- end unspent coins.
|
|
|
|
|
|
|
|
// -- start validator.
|
|
|
|
|
|
|
|
// GetValidatorStateOrNew gets validator from store or created new one in case of error.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetValidatorStateOrNew(publicKey *keys.PublicKey) (*state.Validator, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
validatorState, err := dao.GetValidatorState(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
validatorState = &state.Validator{PublicKey: publicKey}
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
return validatorState, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidators returns all validators from store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetValidators() []*state.Validator {
|
2019-11-28 16:06:09 +00:00
|
|
|
var validators []*state.Validator
|
2019-11-25 17:39:11 +00:00
|
|
|
dao.store.Seek(storage.STValidator.Bytes(), func(k, v []byte) {
|
|
|
|
r := io.NewBinReaderFromBuf(v)
|
2019-11-28 16:06:09 +00:00
|
|
|
validator := &state.Validator{}
|
2019-11-25 17:39:11 +00:00
|
|
|
validator.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
validators = append(validators, validator)
|
|
|
|
})
|
|
|
|
return validators
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidatorState returns validator by publicKey.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetValidatorState(publicKey *keys.PublicKey) (*state.Validator, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
validatorState := &state.Validator{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STValidator, publicKey.Bytes())
|
|
|
|
err := dao.GetAndDecode(validatorState, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return validatorState, nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// PutValidatorState puts given Validator into the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutValidatorState(vs *state.Validator) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STValidator, vs.PublicKey.Bytes())
|
|
|
|
return dao.Put(vs, key)
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// DeleteValidatorState deletes given Validator into the given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) DeleteValidatorState(vs *state.Validator) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STValidator, vs.PublicKey.Bytes())
|
|
|
|
return dao.store.Delete(key)
|
|
|
|
}
|
|
|
|
|
2020-02-12 17:54:48 +00:00
|
|
|
// GetValidatorsCount returns current ValidatorsCount or new one if there is none
|
|
|
|
// in the DB.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetValidatorsCount() (*state.ValidatorsCount, error) {
|
2020-02-12 17:54:48 +00:00
|
|
|
vc := &state.ValidatorsCount{}
|
|
|
|
key := []byte{byte(storage.IXValidatorsCount)}
|
|
|
|
err := dao.GetAndDecode(vc, key)
|
|
|
|
if err != nil && err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutValidatorsCount put given ValidatorsCount in the store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutValidatorsCount(vc *state.ValidatorsCount) error {
|
2020-02-12 17:54:48 +00:00
|
|
|
key := []byte{byte(storage.IXValidatorsCount)}
|
|
|
|
return dao.Put(vc, key)
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
// -- end validator.
|
|
|
|
|
|
|
|
// -- start notification event.
|
|
|
|
|
|
|
|
// GetAppExecResult gets application execution result from the
|
|
|
|
// given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
aer := &state.AppExecResult{}
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNotification, hash.BytesBE())
|
|
|
|
err := dao.GetAndDecode(aer, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return aer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutAppExecResult puts given application execution result into the
|
|
|
|
// given store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutAppExecResult(aer *state.AppExecResult) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNotification, aer.TxHash.BytesBE())
|
|
|
|
return dao.Put(aer, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- end notification event.
|
|
|
|
|
|
|
|
// -- start storage item.
|
|
|
|
|
|
|
|
// GetStorageItem returns StorageItem if it exists in the given Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
|
2019-11-25 17:39:11 +00:00
|
|
|
b, err := dao.store.Get(makeStorageItemKey(scripthash, key))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
si := &state.StorageItem{}
|
2019-11-25 17:39:11 +00:00
|
|
|
si.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return si
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutStorageItem puts given StorageItem for given script with given
|
|
|
|
// key into the given Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
return dao.Put(si, makeStorageItemKey(scripthash, key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStorageItem drops storage item for the given script with the
|
|
|
|
// given key from the Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) DeleteStorageItem(scripthash util.Uint160, key []byte) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
return dao.store.Delete(makeStorageItemKey(scripthash, key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageItems returns all storage items for a given scripthash.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
var siMap = make(map[string]*state.StorageItem)
|
2019-11-25 17:39:11 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
saveToMap := func(k, v []byte) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(v)
|
2019-11-28 16:06:09 +00:00
|
|
|
si := &state.StorageItem{}
|
2019-11-25 17:39:11 +00:00
|
|
|
si.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
err = r.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cut prefix and hash.
|
|
|
|
siMap[string(k[21:])] = si
|
|
|
|
}
|
|
|
|
dao.store.Seek(storage.AppendPrefix(storage.STStorage, hash.BytesLE()), saveToMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return siMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeStorageItemKey returns a key used to store StorageItem in the DB.
|
|
|
|
func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte {
|
|
|
|
return storage.AppendPrefix(storage.STStorage, append(scripthash.BytesLE(), key...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- end storage item.
|
|
|
|
|
|
|
|
// -- other.
|
|
|
|
|
|
|
|
// GetBlock returns Block by the given hash if it exists in the store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetBlock(hash util.Uint256) (*block.Block, uint32, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE())
|
|
|
|
b, err := dao.store.Get(key)
|
|
|
|
if err != nil {
|
2020-02-27 13:31:28 +00:00
|
|
|
return nil, 0, err
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2020-02-27 13:31:28 +00:00
|
|
|
|
|
|
|
block, err := block.NewBlockFromTrimmedBytes(b[4:])
|
2019-11-25 17:39:11 +00:00
|
|
|
if err != nil {
|
2020-02-27 13:31:28 +00:00
|
|
|
return nil, 0, err
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2020-02-27 13:31:28 +00:00
|
|
|
return block, binary.LittleEndian.Uint32(b[:4]), nil
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion attempts to get the current version stored in the
|
|
|
|
// underlying Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetVersion() (string, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
version, err := dao.store.Get(storage.SYSVersion.Bytes())
|
|
|
|
return string(version), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentBlockHeight returns the current block height found in the
|
|
|
|
// underlying Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetCurrentBlockHeight() (uint32, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
b, err := dao.store.Get(storage.SYSCurrentBlock.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(b[32:36]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentHeaderHeight returns the current header height and hash from
|
|
|
|
// the underlying Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
var b []byte
|
|
|
|
b, err = dao.store.Get(storage.SYSCurrentHeader.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i = binary.LittleEndian.Uint32(b[32:36])
|
|
|
|
h, err = util.Uint256DecodeBytesLE(b[:32])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderHashes returns a sorted list of header hashes retrieved from
|
|
|
|
// the given underlying Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetHeaderHashes() ([]util.Uint256, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
hashMap := make(map[uint32][]util.Uint256)
|
|
|
|
dao.store.Seek(storage.IXHeaderHashList.Bytes(), func(k, v []byte) {
|
|
|
|
storedCount := binary.LittleEndian.Uint32(k[1:])
|
|
|
|
hashes, err := read2000Uint256Hashes(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
hashMap[storedCount] = hashes
|
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
hashes = make([]util.Uint256, 0, len(hashMap))
|
|
|
|
sortedKeys = make([]uint32, 0, len(hashMap))
|
|
|
|
)
|
|
|
|
|
|
|
|
for k := range hashMap {
|
|
|
|
sortedKeys = append(sortedKeys, k)
|
|
|
|
}
|
|
|
|
sort.Sort(slice(sortedKeys))
|
|
|
|
|
|
|
|
for _, key := range sortedKeys {
|
|
|
|
hashes = append(hashes[:key], hashMap[key]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransaction returns Transaction and its height by the given hash
|
|
|
|
// if it exists in the store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataTransaction, hash.BytesLE())
|
|
|
|
b, err := dao.store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
|
2019-12-12 15:52:23 +00:00
|
|
|
var height = r.ReadU32LE()
|
2019-11-25 17:39:11 +00:00
|
|
|
|
|
|
|
tx := &transaction.Transaction{}
|
|
|
|
tx.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, 0, r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, height, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutVersion stores the given version in the underlying Store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutVersion(v string) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
return dao.store.Put(storage.SYSVersion.Bytes(), []byte(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutCurrentHeader stores current header.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) PutCurrentHeader(hashAndIndex []byte) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
return dao.store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasTransaction returns true if the given store contains the given
|
|
|
|
// Transaction hash.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) HasTransaction(hash util.Uint256) bool {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataTransaction, hash.BytesLE())
|
|
|
|
if _, err := dao.store.Get(key); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreAsBlock stores the given block as DataBlock.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) StoreAsBlock(block *block.Block, sysFee uint32) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
var (
|
|
|
|
key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
|
|
|
|
buf = io.NewBufBinWriter()
|
|
|
|
)
|
2020-02-27 13:31:28 +00:00
|
|
|
buf.WriteU32LE(sysFee)
|
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)
|
2019-11-25 17:39:11 +00:00
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
|
|
|
return dao.store.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreAsCurrentBlock stores the given block witch prefix SYSCurrentBlock.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) StoreAsCurrentBlock(block *block.Block) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-12-12 15:52:23 +00:00
|
|
|
h := block.Hash()
|
|
|
|
h.EncodeBinary(buf.BinWriter)
|
|
|
|
buf.WriteU32LE(block.Index)
|
2019-11-25 17:39:11 +00:00
|
|
|
return dao.store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreAsTransaction stores the given TX as DataTransaction.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) StoreAsTransaction(tx *transaction.Transaction, index uint32) error {
|
2019-11-25 17:39:11 +00:00
|
|
|
key := storage.AppendPrefix(storage.DataTransaction, tx.Hash().BytesLE())
|
|
|
|
buf := io.NewBufBinWriter()
|
2019-12-12 15:52:23 +00:00
|
|
|
buf.WriteU32LE(index)
|
2019-11-25 17:39:11 +00:00
|
|
|
tx.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
|
|
|
return dao.store.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDoubleSpend verifies that the input transactions are not double spent.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) IsDoubleSpend(tx *transaction.Transaction) bool {
|
2020-03-12 17:24:54 +00:00
|
|
|
return dao.checkUsedInputs(tx.Inputs, state.CoinSpent)
|
2019-11-25 17:39:11 +00:00
|
|
|
}
|
2019-12-12 18:17:13 +00:00
|
|
|
|
2020-02-26 09:48:38 +00:00
|
|
|
// IsDoubleClaim verifies that given claim inputs are not already claimed by another tx.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) IsDoubleClaim(claim *transaction.ClaimTX) bool {
|
2020-03-12 17:24:54 +00:00
|
|
|
return dao.checkUsedInputs(claim.Claims, state.CoinClaimed)
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) checkUsedInputs(inputs []transaction.Input, coin state.Coin) bool {
|
2020-03-12 17:24:54 +00:00
|
|
|
if len(inputs) == 0 {
|
2020-02-26 09:48:38 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-03-12 17:24:54 +00:00
|
|
|
for _, inputs := range transaction.GroupInputsByPrevHash(inputs) {
|
2020-02-26 09:48:38 +00:00
|
|
|
prevHash := inputs[0].PrevHash
|
2020-03-09 15:56:24 +00:00
|
|
|
unspent, err := dao.GetUnspentCoinState(prevHash)
|
2020-02-26 09:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
2020-03-12 17:24:54 +00:00
|
|
|
if int(input.PrevIndex) >= len(unspent.States) || (unspent.States[input.PrevIndex].State&coin) != 0 {
|
2020-02-26 09:48:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:17:13 +00:00
|
|
|
// Persist flushes all the changes made into the (supposedly) persistent
|
|
|
|
// underlying store.
|
2020-04-03 06:49:01 +00:00
|
|
|
func (dao *simpleDao) Persist() (int, error) {
|
2019-12-12 18:17:13 +00:00
|
|
|
return dao.store.Persist()
|
|
|
|
}
|