neoneo-go/pkg/core/dao/dao.go

520 lines
16 KiB
Go
Raw Normal View History

2020-04-07 09:41:12 +00:00
package dao
import (
"bytes"
"encoding/binary"
"fmt"
"sort"
"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/io"
"github.com/nspcc-dev/neo-go/pkg/util"
)
2020-04-07 09:41:12 +00:00
// DAO is a data access object.
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
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)
GetBatch() *storage.MemBatch
GetBlock(hash util.Uint256) (*block.Block, 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)
GetStorageItemsWithPrefix(hash util.Uint160, prefix []byte) (map[string]*state.StorageItem, error)
GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error)
GetVersion() (string, error)
2020-04-07 09:41:12 +00:00
GetWrapped() DAO
HasTransaction(hash util.Uint256) bool
Persist() (int, error)
PutAccountState(as *state.Account) error
PutAppExecResult(aer *state.AppExecResult) 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
PutVersion(v string) error
StoreAsBlock(block *block.Block) 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
}
2020-04-07 09:41:12 +00:00
// Simple is memCached wrapper around DB, simple DAO implementation.
type Simple struct {
Store *storage.MemCachedStore
}
2020-04-07 09:41:12 +00:00
// NewSimple creates new simple dao using provided backend store.
func NewSimple(backend storage.Store) *Simple {
return &Simple{Store: storage.NewMemCachedStore(backend)}
}
// 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-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 {
return NewSimple(dao.Store)
}
// 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)
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 {
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 {
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())
}
// -- start accounts.
// GetAccountStateOrNew retrieves Account from temporary or persistent Store
// or creates a new one if it doesn't exist and persists it.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) {
account, err := dao.GetAccountState(hash)
if err != nil {
if err != storage.ErrKeyNotFound {
return nil, err
}
account = state.NewAccount(hash)
}
return account, nil
}
// GetAccountState returns Account from the given Store if it's
// present there. Returns nil otherwise.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetAccountState(hash util.Uint160) (*state.Account, error) {
account := &state.Account{}
key := storage.AppendPrefix(storage.STAccount, hash.BytesBE())
err := dao.GetAndDecode(account, key)
if err != nil {
return nil, err
}
return account, err
}
2020-04-07 09:41:12 +00:00
// PutAccountState saves given Account in given store.
func (dao *Simple) PutAccountState(as *state.Account) error {
return dao.putAccountState(as, io.NewBufBinWriter())
}
2020-04-07 09:41:12 +00:00
func (dao *Simple) putAccountState(as *state.Account, buf *io.BufBinWriter) error {
key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE())
return dao.putWithBuffer(as, key, buf)
}
// -- end accounts.
// -- start contracts.
// GetContractState returns contract state as recorded in the given
// store by the given script hash.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetContractState(hash util.Uint160) (*state.Contract, error) {
contract := &state.Contract{}
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-07 09:41:12 +00:00
func (dao *Simple) PutContractState(cs *state.Contract) error {
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().BytesBE())
return dao.Put(cs, key)
}
// DeleteContractState deletes given contract state in the given store.
2020-04-07 09:41:12 +00:00
func (dao *Simple) DeleteContractState(hash util.Uint160) error {
key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
2020-04-07 09:41:12 +00:00
return dao.Store.Delete(key)
}
// -- end contracts.
// -- start nep5 balances.
// GetNEP5Balances retrieves nep5 balances from the cache.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error) {
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
}
// PutNEP5Balances saves nep5 balances from the cache.
2020-04-07 09:41:12 +00:00
func (dao *Simple) PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error {
return dao.putNEP5Balances(acc, bs, io.NewBufBinWriter())
}
2020-04-07 09:41:12 +00:00
func (dao *Simple) putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error {
key := storage.AppendPrefix(storage.STNEP5Balances, acc.BytesBE())
return dao.putWithBuffer(bs, key, buf)
}
// -- end nep5 balances.
2020-03-05 14:11:58 +00:00
// -- start transfer log.
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-07 09:41:12 +00:00
func (dao *Simple) GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error) {
key := getNEP5TransferLogKey(acc, index)
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 {
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-07 09:41:12 +00:00
func (dao *Simple) PutNEP5TransferLog(acc util.Uint160, index uint32, lg *state.NEP5TransferLog) error {
key := getNEP5TransferLogKey(acc, index)
2020-04-07 09:41:12 +00:00
return dao.Store.Put(key, lg.Raw)
2020-03-05 14:11:58 +00:00
}
// AppendNEP5Transfer appends a single NEP5 transfer to a log.
// First return value signalizes that log size has exceeded batch size.
2020-04-07 09:41:12 +00:00
func (dao *Simple) AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) {
lg, err := dao.GetNEP5TransferLog(acc, index)
2020-03-05 14:11:58 +00:00
if err != nil {
if err != storage.ErrKeyNotFound {
return false, err
2020-03-05 14:11:58 +00:00
}
lg = new(state.NEP5TransferLog)
}
if err := lg.Append(tr); err != nil {
return false, err
2020-03-05 14:11:58 +00:00
}
return lg.Size() >= nep5TransferBatchSize, dao.PutNEP5TransferLog(acc, index, lg)
2020-03-05 14:11:58 +00:00
}
// -- end transfer log.
// -- start notification event.
// GetAppExecResult gets application execution result from the
// given store.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) {
aer := &state.AppExecResult{}
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-07 09:41:12 +00:00
func (dao *Simple) PutAppExecResult(aer *state.AppExecResult) error {
key := storage.AppendPrefix(storage.STNotification, aer.TxHash.BytesBE())
return dao.Put(aer, key)
}
// -- end notification event.
// -- start storage item.
2020-04-07 09:41:12 +00:00
// GetStorageItem returns StorageItem if it exists in the given store.
func (dao *Simple) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
b, err := dao.Store.Get(makeStorageItemKey(scripthash, key))
if err != nil {
return nil
}
r := io.NewBinReaderFromBuf(b)
si := &state.StorageItem{}
si.DecodeBinary(r)
if r.Err != nil {
return nil
}
return si
}
// PutStorageItem puts given StorageItem for given script with given
2020-04-07 09:41:12 +00:00
// key into the given store.
func (dao *Simple) PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error {
return dao.Put(si, makeStorageItemKey(scripthash, key))
}
// DeleteStorageItem drops storage item for the given script with the
2020-04-07 09:41:12 +00:00
// given key from the store.
func (dao *Simple) DeleteStorageItem(scripthash util.Uint160, key []byte) error {
return dao.Store.Delete(makeStorageItemKey(scripthash, key))
}
// GetStorageItems returns all storage items for a given scripthash.
2020-04-07 09:41:12 +00:00
func (dao *Simple) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
return dao.GetStorageItemsWithPrefix(hash, nil)
}
// GetStorageItemsWithPrefix returns all storage items with given prefix for a
// given scripthash.
func (dao *Simple) GetStorageItemsWithPrefix(hash util.Uint160, prefix []byte) (map[string]*state.StorageItem, error) {
var siMap = make(map[string]*state.StorageItem)
var err error
lookupKey := storage.AppendPrefix(storage.STStorage, hash.BytesLE())
if prefix != nil {
lookupKey = append(lookupKey, prefix...)
}
saveToMap := func(k, v []byte) {
if err != nil {
return
}
r := io.NewBinReaderFromBuf(v)
si := &state.StorageItem{}
si.DecodeBinary(r)
if r.Err != nil {
err = r.Err
return
}
// Cut prefix and hash.
siMap[string(k[len(lookupKey):])] = si
}
dao.Store.Seek(lookupKey, 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.
func (dao *Simple) GetBlock(hash util.Uint256) (*block.Block, error) {
key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE())
2020-04-07 09:41:12 +00:00
b, err := dao.Store.Get(key)
if err != nil {
return nil, err
}
block, err := block.NewBlockFromTrimmedBytes(b)
if err != nil {
return nil, err
}
return block, nil
}
// GetVersion attempts to get the current version stored in the
2020-04-07 09:41:12 +00:00
// underlying store.
func (dao *Simple) GetVersion() (string, error) {
version, err := dao.Store.Get(storage.SYSVersion.Bytes())
return string(version), err
}
// 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())
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) {
var b []byte
2020-04-07 09:41:12 +00:00
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
2020-04-07 09:41:12 +00:00
// the given underlying store.
func (dao *Simple) GetHeaderHashes() ([]util.Uint256, error) {
hashMap := make(map[uint32][]util.Uint256)
2020-04-07 09:41:12 +00:00
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.Slice(sortedKeys, func(i, j int) bool { return sortedKeys[i] < sortedKeys[j] })
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-07 09:41:12 +00:00
func (dao *Simple) GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error) {
key := storage.AppendPrefix(storage.DataTransaction, hash.BytesLE())
2020-04-07 09:41:12 +00:00
b, err := dao.Store.Get(key)
if err != nil {
return nil, 0, err
}
r := io.NewBinReaderFromBuf(b)
var height = r.ReadU32LE()
tx := &transaction.Transaction{}
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.
func (dao *Simple) PutVersion(v string) error {
return dao.Store.Put(storage.SYSVersion.Bytes(), []byte(v))
}
// 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)
}
// 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)
hashes := make([]util.Uint256, 0)
br.ReadArray(&hashes)
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-07 09:41:12 +00:00
func (dao *Simple) HasTransaction(hash util.Uint256) bool {
key := storage.AppendPrefix(storage.DataTransaction, hash.BytesLE())
2020-04-07 09:41:12 +00:00
if _, err := dao.Store.Get(key); err == nil {
return true
}
return false
}
// StoreAsBlock stores the given block as DataBlock.
func (dao *Simple) StoreAsBlock(block *block.Block) error {
var (
key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
buf = io.NewBufBinWriter()
)
b, err := block.Trim()
if err != nil {
return err
}
buf.WriteBytes(b)
if buf.Err != nil {
return buf.Err
}
2020-04-07 09:41:12 +00:00
return dao.Store.Put(key, buf.Bytes())
}
// StoreAsCurrentBlock stores the given block witch prefix SYSCurrentBlock.
2020-04-07 09:41:12 +00:00
func (dao *Simple) StoreAsCurrentBlock(block *block.Block) error {
buf := io.NewBufBinWriter()
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())
}
// StoreAsTransaction stores the given TX as DataTransaction.
2020-04-07 09:41:12 +00:00
func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32) error {
key := storage.AppendPrefix(storage.DataTransaction, tx.Hash().BytesLE())
buf := io.NewBufBinWriter()
buf.WriteU32LE(index)
tx.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())
}
// Persist flushes all the changes made into the (supposedly) persistent
// underlying store.
2020-04-07 09:41:12 +00:00
func (dao *Simple) Persist() (int, error) {
return dao.Store.Persist()
}