Merge pull request #1782 from nspcc-dev/optimize/nep17

dao: do not look up new NEP17 batch
This commit is contained in:
Roman Khimov 2021-03-02 15:28:42 +03:00 committed by GitHub
commit e6d3a60431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 14 deletions

View file

@ -953,11 +953,12 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
bs.LastUpdatedBlock = b.Index bs.LastUpdatedBlock = b.Index
balances.Trackers[id] = bs balances.Trackers[id] = bs
transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount) transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount)
isBig, err := cache.AppendNEP17Transfer(fromAddr, balances.NextTransferBatch, transfer) balances.NewBatch, err = cache.AppendNEP17Transfer(fromAddr,
balances.NextTransferBatch, balances.NewBatch, transfer)
if err != nil { if err != nil {
return return
} }
if isBig { if balances.NewBatch {
balances.NextTransferBatch++ balances.NextTransferBatch++
} }
if err := cache.PutNEP17Balances(fromAddr, balances); err != nil { if err := cache.PutNEP17Balances(fromAddr, balances); err != nil {
@ -975,11 +976,12 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
balances.Trackers[id] = bs balances.Trackers[id] = bs
transfer.Amount = *amount transfer.Amount = *amount
isBig, err := cache.AppendNEP17Transfer(toAddr, balances.NextTransferBatch, transfer) balances.NewBatch, err = cache.AppendNEP17Transfer(toAddr,
balances.NextTransferBatch, balances.NewBatch, transfer)
if err != nil { if err != nil {
return return
} }
if isBig { if balances.NewBatch {
balances.NextTransferBatch++ balances.NextTransferBatch++
} }
if err := cache.PutNEP17Balances(toAddr, balances); err != nil { if err := cache.PutNEP17Balances(toAddr, balances); err != nil {

View file

@ -61,11 +61,17 @@ func (cd *Cached) PutNEP17TransferLog(acc util.Uint160, index uint32, bs *state.
} }
// AppendNEP17Transfer appends new transfer to a transfer event log. // AppendNEP17Transfer appends new transfer to a transfer event log.
func (cd *Cached) AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error) { func (cd *Cached) AppendNEP17Transfer(acc util.Uint160, index uint32, isNew bool, tr *state.NEP17Transfer) (bool, error) {
lg, err := cd.GetNEP17TransferLog(acc, index) var lg *state.NEP17TransferLog
if isNew {
lg = new(state.NEP17TransferLog)
} else {
var err error
lg, err = cd.GetNEP17TransferLog(acc, index)
if err != nil { if err != nil {
return false, err return false, err
} }
}
if err := lg.Append(tr); err != nil { if err := lg.Append(tr); err != nil {
return false, err return false, err
} }

View file

@ -31,7 +31,7 @@ var (
// DAO is a data access object. // DAO is a data access object.
type DAO interface { type DAO interface {
AppendAppExecResult(aer *state.AppExecResult, buf *io.BufBinWriter) error AppendAppExecResult(aer *state.AppExecResult, buf *io.BufBinWriter) error
AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error) AppendNEP17Transfer(acc util.Uint160, index uint32, isNew bool, tr *state.NEP17Transfer) (bool, error)
DeleteBlock(h util.Uint256, buf *io.BufBinWriter) error DeleteBlock(h util.Uint256, buf *io.BufBinWriter) error
DeleteContractID(id int32) error DeleteContractID(id int32) error
DeleteStorageItem(id int32, key []byte) error DeleteStorageItem(id int32, key []byte) error
@ -205,13 +205,16 @@ func (dao *Simple) PutNEP17TransferLog(acc util.Uint160, index uint32, lg *state
// AppendNEP17Transfer appends a single NEP17 transfer to a log. // AppendNEP17Transfer appends a single NEP17 transfer to a log.
// First return value signalizes that log size has exceeded batch size. // First return value signalizes that log size has exceeded batch size.
func (dao *Simple) AppendNEP17Transfer(acc util.Uint160, index uint32, tr *state.NEP17Transfer) (bool, error) { func (dao *Simple) AppendNEP17Transfer(acc util.Uint160, index uint32, isNew bool, tr *state.NEP17Transfer) (bool, error) {
lg, err := dao.GetNEP17TransferLog(acc, index) var lg *state.NEP17TransferLog
if isNew {
lg = new(state.NEP17TransferLog)
} else {
var err error
lg, err = dao.GetNEP17TransferLog(acc, index)
if err != nil { if err != nil {
if err != storage.ErrKeyNotFound {
return false, err return false, err
} }
lg = new(state.NEP17TransferLog)
} }
if err := lg.Append(tr); err != nil { if err := lg.Append(tr); err != nil {
return false, err return false, err

View file

@ -50,6 +50,8 @@ type NEP17Balances struct {
Trackers map[int32]NEP17Tracker Trackers map[int32]NEP17Tracker
// NextTransferBatch stores an index of the next transfer batch. // NextTransferBatch stores an index of the next transfer batch.
NextTransferBatch uint32 NextTransferBatch uint32
// NewBatch is true if batch with the `NextTransferBatch` index should be created.
NewBatch bool
} }
// NewNEP17Balances returns new NEP17Balances. // NewNEP17Balances returns new NEP17Balances.
@ -62,6 +64,7 @@ func NewNEP17Balances() *NEP17Balances {
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) { func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
bs.NextTransferBatch = r.ReadU32LE() bs.NextTransferBatch = r.ReadU32LE()
bs.NewBatch = r.ReadBool()
lenBalances := r.ReadVarUint() lenBalances := r.ReadVarUint()
m := make(map[int32]NEP17Tracker, lenBalances) m := make(map[int32]NEP17Tracker, lenBalances)
for i := 0; i < int(lenBalances); i++ { for i := 0; i < int(lenBalances); i++ {
@ -76,6 +79,7 @@ func (bs *NEP17Balances) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (bs *NEP17Balances) EncodeBinary(w *io.BinWriter) { func (bs *NEP17Balances) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(bs.NextTransferBatch) w.WriteU32LE(bs.NextTransferBatch)
w.WriteBool(bs.NewBatch)
w.WriteVarUint(uint64(len(bs.Trackers))) w.WriteVarUint(uint64(len(bs.Trackers)))
for k, v := range bs.Trackers { for k, v := range bs.Trackers {
w.WriteU32LE(uint32(k)) w.WriteU32LE(uint32(k))