2020-04-07 09:41:12 +00:00
|
|
|
package dao
|
2019-12-13 15:43:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-06 11:34:17 +00:00
|
|
|
"bytes"
|
2020-04-03 06:49:01 +00:00
|
|
|
"errors"
|
2020-05-06 11:34:17 +00:00
|
|
|
"sort"
|
2020-04-03 06:49:01 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-03-17 09:06:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-12-13 15:43:46 +00:00
|
|
|
)
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// Cached is a data access object that mimics DAO, but has a write cache
|
2019-12-13 15:43:46 +00:00
|
|
|
// for accounts and read cache for contracts. These are the most frequently used
|
|
|
|
// objects in the storeBlock().
|
2020-04-07 09:41:12 +00:00
|
|
|
type Cached struct {
|
|
|
|
DAO
|
2019-12-13 15:43:46 +00:00
|
|
|
accounts map[util.Uint160]*state.Account
|
|
|
|
contracts map[util.Uint160]*state.Contract
|
2020-03-11 09:13:02 +00:00
|
|
|
unspents map[util.Uint256]*state.UnspentCoin
|
2020-03-12 11:31:45 +00:00
|
|
|
balances map[util.Uint160]*state.NEP5Balances
|
2020-03-12 11:49:59 +00:00
|
|
|
transfers map[util.Uint160]map[uint32]*state.NEP5TransferLog
|
2020-05-06 11:34:17 +00:00
|
|
|
storage *itemCache
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// NewCached returns new Cached wrapping around given backing store.
|
|
|
|
func NewCached(d DAO) *Cached {
|
2019-12-13 15:43:46 +00:00
|
|
|
accs := make(map[util.Uint160]*state.Account)
|
|
|
|
ctrs := make(map[util.Uint160]*state.Contract)
|
2020-03-11 09:13:02 +00:00
|
|
|
unspents := make(map[util.Uint256]*state.UnspentCoin)
|
2020-03-12 11:31:45 +00:00
|
|
|
balances := make(map[util.Uint160]*state.NEP5Balances)
|
2020-03-12 11:49:59 +00:00
|
|
|
transfers := make(map[util.Uint160]map[uint32]*state.NEP5TransferLog)
|
2020-05-06 11:34:17 +00:00
|
|
|
st := newItemCache()
|
|
|
|
dao := d.GetWrapped()
|
|
|
|
if cd, ok := dao.(*Cached); ok {
|
|
|
|
for h, m := range cd.storage.st {
|
|
|
|
for _, k := range cd.storage.keys[h] {
|
|
|
|
st.put(h, []byte(k), m[k].State, copyItem(&m[k].StorageItem))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Cached{dao, accs, ctrs, unspents, balances, transfers, st}
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetAccountStateOrNew retrieves Account from cache or underlying store
|
2019-12-13 15:43:46 +00:00
|
|
|
// or creates a new one if it doesn't exist.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) {
|
2019-12-13 15:43:46 +00:00
|
|
|
if cd.accounts[hash] != nil {
|
|
|
|
return cd.accounts[hash], nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.GetAccountStateOrNew(hash)
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetAccountState retrieves Account from cache or underlying store.
|
|
|
|
func (cd *Cached) GetAccountState(hash util.Uint160) (*state.Account, error) {
|
2019-12-13 15:43:46 +00:00
|
|
|
if cd.accounts[hash] != nil {
|
|
|
|
return cd.accounts[hash], nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.GetAccountState(hash)
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutAccountState saves given Account in the cache.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) PutAccountState(as *state.Account) error {
|
2019-12-13 15:43:46 +00:00
|
|
|
cd.accounts[as.ScriptHash] = as
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetContractState returns contract state from cache or underlying store.
|
|
|
|
func (cd *Cached) GetContractState(hash util.Uint160) (*state.Contract, error) {
|
2019-12-13 15:43:46 +00:00
|
|
|
if cd.contracts[hash] != nil {
|
|
|
|
return cd.contracts[hash], nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
cs, err := cd.DAO.GetContractState(hash)
|
2019-12-13 15:43:46 +00:00
|
|
|
if err == nil {
|
|
|
|
cd.contracts[hash] = cs
|
|
|
|
}
|
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutContractState puts given contract state into the given store.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) PutContractState(cs *state.Contract) error {
|
2019-12-13 15:43:46 +00:00
|
|
|
cd.contracts[cs.ScriptHash()] = cs
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.PutContractState(cs)
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// DeleteContractState deletes given contract state in cache and backing store.
|
|
|
|
func (cd *Cached) DeleteContractState(hash util.Uint160) error {
|
2019-12-13 15:43:46 +00:00
|
|
|
cd.contracts[hash] = nil
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.DeleteContractState(hash)
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetUnspentCoinState retrieves UnspentCoin from cache or underlying store.
|
|
|
|
func (cd *Cached) GetUnspentCoinState(hash util.Uint256) (*state.UnspentCoin, error) {
|
2020-03-11 09:13:02 +00:00
|
|
|
if cd.unspents[hash] != nil {
|
|
|
|
return cd.unspents[hash], nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.GetUnspentCoinState(hash)
|
2020-03-11 09:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutUnspentCoinState saves given UnspentCoin in the cache.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) PutUnspentCoinState(hash util.Uint256, ucs *state.UnspentCoin) error {
|
2020-03-11 09:13:02 +00:00
|
|
|
cd.unspents[hash] = ucs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-12 11:31:45 +00:00
|
|
|
// GetNEP5Balances retrieves NEP5Balances for the acc.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) GetNEP5Balances(acc util.Uint160) (*state.NEP5Balances, error) {
|
2020-03-12 11:31:45 +00:00
|
|
|
if bs := cd.balances[acc]; bs != nil {
|
|
|
|
return bs, nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.GetNEP5Balances(acc)
|
2020-03-12 11:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutNEP5Balances saves NEP5Balances for the acc.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) PutNEP5Balances(acc util.Uint160, bs *state.NEP5Balances) error {
|
2020-03-12 11:31:45 +00:00
|
|
|
cd.balances[acc] = bs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-12 11:49:59 +00:00
|
|
|
// GetNEP5TransferLog retrieves NEP5TransferLog for the acc.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) GetNEP5TransferLog(acc util.Uint160, index uint32) (*state.NEP5TransferLog, error) {
|
2020-03-12 11:49:59 +00:00
|
|
|
ts := cd.transfers[acc]
|
|
|
|
if ts != nil && ts[index] != nil {
|
|
|
|
return ts[index], nil
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.GetNEP5TransferLog(acc, index)
|
2020-03-12 11:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutNEP5TransferLog saves NEP5TransferLog for the acc.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) PutNEP5TransferLog(acc util.Uint160, index uint32, bs *state.NEP5TransferLog) error {
|
2020-03-12 11:49:59 +00:00
|
|
|
ts := cd.transfers[acc]
|
|
|
|
if ts == nil {
|
|
|
|
ts = make(map[uint32]*state.NEP5TransferLog, 2)
|
|
|
|
cd.transfers[acc] = ts
|
|
|
|
}
|
|
|
|
ts[index] = bs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendNEP5Transfer appends new transfer to a transfer event log.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) {
|
2020-03-12 11:49:59 +00:00
|
|
|
lg, err := cd.GetNEP5TransferLog(acc, index)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := lg.Append(tr); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return lg.Size() >= nep5TransferBatchSize, cd.PutNEP5TransferLog(acc, index, lg)
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:43:46 +00:00
|
|
|
// Persist flushes all the changes made into the (supposedly) persistent
|
|
|
|
// underlying store.
|
2020-04-07 09:41:12 +00:00
|
|
|
func (cd *Cached) Persist() (int, error) {
|
2020-05-06 11:34:17 +00:00
|
|
|
if err := cd.FlushStorage(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
lowerCache, ok := cd.DAO.(*Cached)
|
|
|
|
// If the lower DAO is Cached, we only need to flush the MemCached DB.
|
|
|
|
// This actually breaks DAO interface incapsulation, but for our current
|
2020-04-03 06:49:01 +00:00
|
|
|
// usage scenario it should be good enough if cd doesn't modify object
|
|
|
|
// caches (accounts/contracts/etc) in any way.
|
|
|
|
if ok {
|
2020-04-07 09:41:12 +00:00
|
|
|
var simpleCache *Simple
|
2020-04-03 06:49:01 +00:00
|
|
|
for simpleCache == nil {
|
2020-05-06 11:34:17 +00:00
|
|
|
if err := lowerCache.FlushStorage(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
simpleCache, ok = lowerCache.DAO.(*Simple)
|
2020-04-03 06:49:01 +00:00
|
|
|
if !ok {
|
2020-04-07 09:41:12 +00:00
|
|
|
lowerCache, ok = cd.DAO.(*Cached)
|
2020-04-03 06:49:01 +00:00
|
|
|
if !ok {
|
2020-04-07 09:41:12 +00:00
|
|
|
return 0, errors.New("unsupported lower DAO")
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return simpleCache.Persist()
|
|
|
|
}
|
2020-03-17 09:06:46 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
|
2019-12-13 15:43:46 +00:00
|
|
|
for sc := range cd.accounts {
|
2020-04-07 09:41:12 +00:00
|
|
|
err := cd.DAO.putAccountState(cd.accounts[sc], buf)
|
2019-12-13 15:43:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-17 09:06:46 +00:00
|
|
|
buf.Reset()
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
2020-03-11 09:13:02 +00:00
|
|
|
for hash := range cd.unspents {
|
2020-04-07 09:41:12 +00:00
|
|
|
err := cd.DAO.putUnspentCoinState(hash, cd.unspents[hash], buf)
|
2020-03-11 09:13:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-17 09:06:46 +00:00
|
|
|
buf.Reset()
|
2020-03-11 09:13:02 +00:00
|
|
|
}
|
2020-03-12 11:31:45 +00:00
|
|
|
for acc, bs := range cd.balances {
|
2020-04-07 09:41:12 +00:00
|
|
|
err := cd.DAO.putNEP5Balances(acc, bs, buf)
|
2020-03-12 11:31:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-17 09:06:46 +00:00
|
|
|
buf.Reset()
|
2020-03-12 11:31:45 +00:00
|
|
|
}
|
2020-03-12 11:49:59 +00:00
|
|
|
for acc, ts := range cd.transfers {
|
|
|
|
for ind, lg := range ts {
|
2020-04-07 09:41:12 +00:00
|
|
|
err := cd.DAO.PutNEP5TransferLog(acc, ind, lg)
|
2020-03-12 11:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 09:41:12 +00:00
|
|
|
return cd.DAO.Persist()
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
2020-04-03 06:49:01 +00:00
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
// GetWrapped implements DAO interface.
|
|
|
|
func (cd *Cached) GetWrapped() DAO {
|
|
|
|
return &Cached{cd.DAO.GetWrapped(),
|
2020-04-03 06:49:01 +00:00
|
|
|
cd.accounts,
|
|
|
|
cd.contracts,
|
|
|
|
cd.unspents,
|
|
|
|
cd.balances,
|
|
|
|
cd.transfers,
|
2020-05-06 11:34:17 +00:00
|
|
|
cd.storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushStorage flushes storage changes to the underlying DAO.
|
|
|
|
func (cd *Cached) FlushStorage() error {
|
|
|
|
if d, ok := cd.DAO.(*Cached); ok {
|
|
|
|
d.storage.st = cd.storage.st
|
|
|
|
d.storage.keys = cd.storage.keys
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for h, items := range cd.storage.st {
|
|
|
|
for _, k := range cd.storage.keys[h] {
|
|
|
|
ti := items[k]
|
|
|
|
switch ti.State {
|
|
|
|
case putOp, addOp:
|
|
|
|
err := cd.DAO.PutStorageItem(h, []byte(k), &ti.StorageItem)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case delOp:
|
|
|
|
err := cd.DAO.DeleteStorageItem(h, []byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyItem(si *state.StorageItem) *state.StorageItem {
|
|
|
|
val := make([]byte, len(si.Value))
|
|
|
|
copy(val, si.Value)
|
|
|
|
return &state.StorageItem{
|
|
|
|
Value: val,
|
|
|
|
IsConst: si.IsConst,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageItem returns StorageItem if it exists in the given store.
|
|
|
|
func (cd *Cached) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
|
|
|
|
ti := cd.storage.getItem(scripthash, key)
|
|
|
|
if ti != nil {
|
|
|
|
if ti.State == delOp {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return copyItem(&ti.StorageItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
si := cd.DAO.GetStorageItem(scripthash, key)
|
|
|
|
if si != nil {
|
|
|
|
cd.storage.put(scripthash, key, getOp, si)
|
|
|
|
return copyItem(si)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutStorageItem puts given StorageItem for given script with given
|
|
|
|
// key into the given store.
|
|
|
|
func (cd *Cached) PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error {
|
|
|
|
item := copyItem(si)
|
|
|
|
ti := cd.storage.getItem(scripthash, key)
|
|
|
|
if ti != nil {
|
|
|
|
if ti.State == delOp || ti.State == getOp {
|
|
|
|
ti.State = putOp
|
|
|
|
}
|
|
|
|
ti.StorageItem = *item
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
op := addOp
|
|
|
|
if it := cd.DAO.GetStorageItem(scripthash, key); it != nil {
|
|
|
|
op = putOp
|
|
|
|
}
|
|
|
|
cd.storage.put(scripthash, key, op, item)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStorageItem drops storage item for the given script with the
|
|
|
|
// given key from the store.
|
|
|
|
func (cd *Cached) DeleteStorageItem(scripthash util.Uint160, key []byte) error {
|
|
|
|
ti := cd.storage.getItem(scripthash, key)
|
|
|
|
if ti != nil {
|
|
|
|
ti.State = delOp
|
|
|
|
ti.Value = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
it := cd.DAO.GetStorageItem(scripthash, key)
|
|
|
|
if it != nil {
|
|
|
|
cd.storage.put(scripthash, key, delOp, it)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageItems returns all storage items for a given scripthash.
|
|
|
|
func (cd *Cached) GetStorageItems(hash util.Uint160) ([]StorageItemWithKey, error) {
|
|
|
|
items, err := cd.DAO.GetStorageItems(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|
2020-05-06 11:34:17 +00:00
|
|
|
|
|
|
|
cache := cd.storage.getItems(hash)
|
|
|
|
if len(cache) == 0 {
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]StorageItemWithKey, 0, len(items))
|
|
|
|
for i := range items {
|
|
|
|
_, ok := cache[string(items[i].Key)]
|
|
|
|
if !ok {
|
|
|
|
result = append(result, items[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(result, func(i, j int) bool { return bytes.Compare(result[i].Key, result[j].Key) == -1 })
|
|
|
|
|
|
|
|
for _, k := range cd.storage.keys[hash] {
|
|
|
|
v := cache[k]
|
|
|
|
if v.State != delOp {
|
|
|
|
val := make([]byte, len(v.StorageItem.Value))
|
|
|
|
copy(val, v.StorageItem.Value)
|
|
|
|
result = append(result, StorageItemWithKey{
|
|
|
|
StorageItem: state.StorageItem{
|
|
|
|
Value: val,
|
|
|
|
IsConst: v.StorageItem.IsConst,
|
|
|
|
},
|
|
|
|
Key: []byte(k),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2020-04-03 06:49:01 +00:00
|
|
|
}
|