diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index b6582c6a4..a8c9f0e6d 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -789,8 +789,8 @@ func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, f func(*state.NEP5Tr if err != nil { return nil } - for i := uint32(0); i <= balances.NextTransferBatch; i++ { - lg, err := bc.dao.GetNEP5TransferLog(acc, i) + for i := int(balances.NextTransferBatch); i >= 0; i-- { + lg, err := bc.dao.GetNEP5TransferLog(acc, uint32(i)) if err != nil { return nil } diff --git a/pkg/core/state/nep5.go b/pkg/core/state/nep5.go index a19f571ad..dce048e0b 100644 --- a/pkg/core/state/nep5.go +++ b/pkg/core/state/nep5.go @@ -103,18 +103,20 @@ func (lg *NEP5TransferLog) Append(tr *NEP5Transfer) error { // ForEach iterates over transfer log returning on first error. func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) error) error { - if lg == nil { + if lg == nil || len(lg.Raw) == 0 { return nil } - tr := new(NEP5Transfer) - var bytesRead int - for i := 1; i < len(lg.Raw); i += bytesRead { - r := io.NewBinReaderFromBuf(lg.Raw[i:]) - bytesRead = tr.DecodeBinaryReturnCount(r) - if r.Err != nil { - return r.Err - } else if err := f(tr); err != nil { - return nil + transfers := make([]NEP5Transfer, lg.Size()) + r := io.NewBinReaderFromBuf(lg.Raw[1:]) + for i := 0; i < lg.Size(); i++ { + transfers[i].DecodeBinary(r) + } + if r.Err != nil { + return r.Err + } + for i := len(transfers) - 1; i >= 0; i-- { + if err := f(&transfers[i]); err != nil { + return err } } return nil diff --git a/pkg/core/state/nep5_test.go b/pkg/core/state/nep5_test.go index 507e848c0..06012c31d 100644 --- a/pkg/core/state/nep5_test.go +++ b/pkg/core/state/nep5_test.go @@ -29,10 +29,10 @@ func TestNEP5TransferLog_Append(t *testing.T) { require.Equal(t, len(expected), lg.Size()) - i := 0 + i := len(expected) - 1 err := lg.ForEach(func(tr *NEP5Transfer) error { require.Equal(t, expected[i], tr) - i++ + i-- return nil }) require.NoError(t, err)