2020-04-07 09:41:12 +00:00
|
|
|
package dao
|
2019-12-13 15:43:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-03 06:49:01 +00:00
|
|
|
"errors"
|
|
|
|
|
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
|
2020-12-13 15:26:35 +00:00
|
|
|
// for accounts and NEP17 transfer data. These are the most frequently used
|
2019-12-13 15:43:46 +00:00
|
|
|
// objects in the storeBlock().
|
2020-04-07 09:41:12 +00:00
|
|
|
type Cached struct {
|
|
|
|
DAO
|
2021-07-25 10:48:50 +00:00
|
|
|
balances map[util.Uint160]*state.NEP17TransferInfo
|
2020-11-24 08:14:25 +00:00
|
|
|
transfers map[util.Uint160]map[uint32]*state.NEP17TransferLog
|
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 {
|
2021-07-25 10:48:50 +00:00
|
|
|
balances := make(map[util.Uint160]*state.NEP17TransferInfo)
|
2020-11-24 08:14:25 +00:00
|
|
|
transfers := make(map[util.Uint160]map[uint32]*state.NEP17TransferLog)
|
2021-07-29 19:47:33 +00:00
|
|
|
return &Cached{d.GetWrapped(), balances, transfers}
|
2019-12-13 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 10:48:50 +00:00
|
|
|
// GetNEP17TransferInfo retrieves NEP17TransferInfo for the acc.
|
|
|
|
func (cd *Cached) GetNEP17TransferInfo(acc util.Uint160) (*state.NEP17TransferInfo, error) {
|
2020-03-12 11:31:45 +00:00
|
|
|
if bs := cd.balances[acc]; bs != nil {
|
|
|
|
return bs, nil
|
|
|
|
}
|
2021-07-25 10:48:50 +00:00
|
|
|
return cd.DAO.GetNEP17TransferInfo(acc)
|
2020-03-12 11:31:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 10:48:50 +00:00
|
|
|
// PutNEP17TransferInfo saves NEP17TransferInfo for the acc.
|
|
|
|
func (cd *Cached) PutNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo) error {
|
2020-03-12 11:31:45 +00:00
|
|
|
cd.balances[acc] = bs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// GetNEP17TransferLog retrieves NEP17TransferLog for the acc.
|
|
|
|
func (cd *Cached) GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error) {
|
2020-03-12 11:49:59 +00:00
|
|
|
ts := cd.transfers[acc]
|
|
|
|
if ts != nil && ts[index] != nil {
|
|
|
|
return ts[index], nil
|
|
|
|
}
|
2020-11-24 08:14:25 +00:00
|
|
|
return cd.DAO.GetNEP17TransferLog(acc, index)
|
2020-03-12 11:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// PutNEP17TransferLog saves NEP17TransferLog for the acc.
|
|
|
|
func (cd *Cached) PutNEP17TransferLog(acc util.Uint160, index uint32, bs *state.NEP17TransferLog) error {
|
2020-03-12 11:49:59 +00:00
|
|
|
ts := cd.transfers[acc]
|
|
|
|
if ts == nil {
|
2020-11-24 08:14:25 +00:00
|
|
|
ts = make(map[uint32]*state.NEP17TransferLog, 2)
|
2020-03-12 11:49:59 +00:00
|
|
|
cd.transfers[acc] = ts
|
|
|
|
}
|
|
|
|
ts[index] = bs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// AppendNEP17Transfer appends new transfer to a transfer event log.
|
2021-02-26 11:08:48 +00:00
|
|
|
func (cd *Cached) AppendNEP17Transfer(acc util.Uint160, index uint32, isNew bool, tr *state.NEP17Transfer) (bool, error) {
|
|
|
|
var lg *state.NEP17TransferLog
|
|
|
|
if isNew {
|
|
|
|
lg = new(state.NEP17TransferLog)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
lg, err = cd.GetNEP17TransferLog(acc, index)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-03-12 11:49:59 +00:00
|
|
|
}
|
|
|
|
if err := lg.Append(tr); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-11-24 08:14:25 +00:00
|
|
|
return lg.Size() >= state.NEP17TransferBatchSize, cd.PutNEP17TransferLog(acc, index, lg)
|
2020-03-12 11:49:59 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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
|
2020-12-13 15:26:35 +00:00
|
|
|
// caches (accounts/transfer data) in any way.
|
2020-04-03 06:49:01 +00:00
|
|
|
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-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()
|
|
|
|
|
2020-03-12 11:31:45 +00:00
|
|
|
for acc, bs := range cd.balances {
|
2021-07-25 10:48:50 +00:00
|
|
|
err := cd.DAO.putNEP17TransferInfo(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-11-24 08:14:25 +00:00
|
|
|
err := cd.DAO.PutNEP17TransferLog(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.balances,
|
|
|
|
cd.transfers,
|
|
|
|
}
|
|
|
|
}
|