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 022fb04077
commit 7bd4488ff9
4 changed files with 10 additions and 9 deletions

View file

@ -1086,21 +1086,23 @@ func (bc *Blockchain) ForEachTransfer(acc util.Uint160, tr *state.Transfer, f fu
return nil
}
// GetNEP5TransferLog returns NEP5 transfer log for the acc.
func (bc *Blockchain) GetNEP5TransferLog(acc util.Uint160) *state.TransferLog {
// ForEachNEP5Transfer executes f for each nep5 transfer in log.
func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, tr *state.NEP5Transfer, f func() error) error {
balances, err := bc.dao.GetNEP5Balances(acc)
if err != nil {
return nil
}
result := new(state.TransferLog)
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(state.NEP5TransferSize, tr, f)
if err != nil {
return err
}
}
return result
return nil
}
// GetNEP5Balances returns NEP5 balances for the acc.

View file

@ -26,6 +26,7 @@ type Blockchainer interface {
GetBlock(hash util.Uint256) (*block.Block, error)
GetContractState(hash util.Uint160) *state.Contract
GetEnrollments() ([]*state.Validator, error)
ForEachNEP5Transfer(util.Uint160, *state.NEP5Transfer, func() error) error
ForEachTransfer(util.Uint160, *state.Transfer, func() error) error
GetHeaderHash(int) util.Uint256
GetHeader(hash util.Uint256) (*block.Header, error)
@ -37,7 +38,6 @@ type Blockchainer interface {
GetAccountState(util.Uint160) *state.Account
GetAppExecResult(util.Uint256) (*state.AppExecResult, error)
GetNEP5Metadata(util.Uint160) (*state.NEP5Metadata, error)
GetNEP5TransferLog(util.Uint160) *state.TransferLog
GetNEP5Balances(util.Uint160) *state.NEP5Balances
GetValidators(txes ...*transaction.Transaction) ([]*keys.PublicKey, error)
GetScriptHashesForVerifying(*transaction.Transaction) ([]util.Uint160, error)

View file

@ -96,7 +96,7 @@ func (chain testChain) GetAccountState(util.Uint160) *state.Account {
func (chain testChain) GetNEP5Metadata(util.Uint160) (*state.NEP5Metadata, error) {
panic("TODO")
}
func (chain testChain) GetNEP5TransferLog(util.Uint160) *state.TransferLog {
func (chain testChain) ForEachNEP5Transfer(util.Uint160, *state.NEP5Transfer, func() error) error {
panic("TODO")
}
func (chain testChain) GetNEP5Balances(util.Uint160) *state.NEP5Balances {

View file

@ -726,9 +726,8 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
Received: []result.NEP5Transfer{},
Sent: []result.NEP5Transfer{},
}
lg := s.chain.GetNEP5TransferLog(u)
tr := new(state.NEP5Transfer)
err = lg.ForEach(state.NEP5TransferSize, tr, func() error {
err = s.chain.ForEachNEP5Transfer(u, tr, func() error {
transfer := result.NEP5Transfer{
Timestamp: tr.Timestamp,
Asset: tr.Asset,