core/state: reverse the order of ForEachTransfer
When using limits we're usually concerned about the most recent transfers. Returning 3 transfers from the middle of the chain isn't very helpful.
This commit is contained in:
parent
ff11a5f990
commit
373c669c6a
3 changed files with 16 additions and 14 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
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
|
||||
} else if err := f(tr); err != nil {
|
||||
return nil
|
||||
}
|
||||
for i := len(transfers) - 1; i >= 0; i-- {
|
||||
if err := f(&transfers[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue