core: do not store NEP5 transfer log in memory

Traversing transfer log instead of accumulating and returning it
is faster and takes less memory.
This commit is contained in:
Evgenii Stratonikov 2020-08-06 14:20:36 +03:00
parent c3c88a57cd
commit 807338f97e
4 changed files with 10 additions and 9 deletions

View file

@ -805,21 +805,23 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, h util.Uint256, b *
}
}
// GetNEP5TransferLog returns NEP5 transfer log for the acc.
func (bc *Blockchain) GetNEP5TransferLog(acc util.Uint160) *state.NEP5TransferLog {
// ForEachNEP5Transfer executes f for each nep5 transfer in log.
func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, f func(*state.NEP5Transfer) error) error {
balances, err := bc.dao.GetNEP5Balances(acc)
if err != nil {
return nil
}
result := new(state.NEP5TransferLog)
for i := uint32(0); i <= balances.NextTransferBatch; i++ {
lg, err := bc.dao.GetNEP5TransferLog(acc, i)
if err != nil {
return nil
}
result.Raw = append(result.Raw, lg.Raw...)
err = lg.ForEach(f)
if err != nil {
return err
}
}
return result
return nil
}
// GetNEP5Balances returns NEP5 balances for the acc.

View file

@ -30,6 +30,7 @@ type Blockchainer interface {
GetContractScriptHash(id int32) (util.Uint160, error)
GetEnrollments() ([]state.Validator, error)
GetGoverningTokenBalance(acc util.Uint160) (*big.Int, uint32)
ForEachNEP5Transfer(util.Uint160, func(*state.NEP5Transfer) error) error
GetHeaderHash(int) util.Uint256
GetHeader(hash util.Uint256) (*block.Header, error)
CurrentHeaderHash() util.Uint256
@ -39,7 +40,6 @@ type Blockchainer interface {
GetAccountState(util.Uint160) *state.Account
GetAppExecResult(util.Uint256) (*state.AppExecResult, error)
GetNextBlockValidators() ([]*keys.PublicKey, error)
GetNEP5TransferLog(util.Uint160) *state.NEP5TransferLog
GetNEP5Balances(util.Uint160) *state.NEP5Balances
GetValidators() ([]*keys.PublicKey, error)
GetStandByCommittee() keys.PublicKeys

View file

@ -94,7 +94,7 @@ func (chain testChain) GetAccountState(util.Uint160) *state.Account {
func (chain testChain) GetNextBlockValidators() ([]*keys.PublicKey, error) {
panic("TODO")
}
func (chain testChain) GetNEP5TransferLog(util.Uint160) *state.NEP5TransferLog {
func (chain testChain) ForEachNEP5Transfer(util.Uint160, func(*state.NEP5Transfer) error) error {
panic("TODO")
}
func (chain testChain) GetNEP5Balances(util.Uint160) *state.NEP5Balances {

View file

@ -541,9 +541,8 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
Received: []result.NEP5Transfer{},
Sent: []result.NEP5Transfer{},
}
lg := s.chain.GetNEP5TransferLog(u)
cache := make(map[int32]decimals)
err = lg.ForEach(func(tr *state.NEP5Transfer) error {
err = s.chain.ForEachNEP5Transfer(u, func(tr *state.NEP5Transfer) error {
d, err := s.getDecimals(tr.Asset, cache)
if err != nil {
return nil