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:
Roman Khimov 2020-09-08 12:57:45 +03:00
parent 6761efff24
commit d3e415d3bd
4 changed files with 18 additions and 17 deletions

View file

@ -1031,8 +1031,8 @@ func (bc *Blockchain) ForEachTransfer(acc util.Uint160, tr *state.Transfer, f fu
if err != nil { if err != nil {
return nil return nil
} }
for i := uint32(0); i <= nb; i++ { for i := int(nb); i >= 0; i-- {
lg, err := bc.dao.GetTransferLog(acc, i) lg, err := bc.dao.GetTransferLog(acc, uint32(i))
if err != nil { if err != nil {
return nil return nil
} }
@ -1050,8 +1050,8 @@ func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, tr *state.NEP5Transf
if err != nil { if err != nil {
return nil return nil
} }
for i := uint32(0); i <= balances.NextTransferBatch; i++ { for i := int(balances.NextTransferBatch); i >= 0; i-- {
lg, err := bc.dao.GetNEP5TransferLog(acc, i) lg, err := bc.dao.GetNEP5TransferLog(acc, uint32(i))
if err != nil { if err != nil {
return nil return nil
} }

View file

@ -114,8 +114,8 @@ func (lg *TransferLog) ForEach(size int, tr io.Serializable, f func() error) err
if lg == nil { if lg == nil {
return nil return nil
} }
for i := 0; i < len(lg.Raw); i += size { for i := len(lg.Raw); i > 0; i -= size {
r := io.NewBinReaderFromBuf(lg.Raw[i : i+size]) r := io.NewBinReaderFromBuf(lg.Raw[i-size : i])
tr.DecodeBinary(r) tr.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return r.Err return r.Err

View file

@ -28,11 +28,11 @@ func TestNEP5TransferLog_Append(t *testing.T) {
require.Equal(t, len(expected), lg.Size()/NEP5TransferSize) require.Equal(t, len(expected), lg.Size()/NEP5TransferSize)
i := 0 i := len(expected) - 1
tr := new(NEP5Transfer) tr := new(NEP5Transfer)
err := lg.ForEach(NEP5TransferSize, tr, func() error { err := lg.ForEach(NEP5TransferSize, tr, func() error {
require.Equal(t, expected[i], tr) require.Equal(t, expected[i], tr)
i++ i--
return nil return nil
}) })
require.NoError(t, err) require.NoError(t, err)

View file

@ -1284,24 +1284,25 @@ func checkNep5TransfersAux(t *testing.T, e *executor, acc interface{}, onlyFirst
if onlyFirst { if onlyFirst {
require.Equal(t, 1, len(res.Received)) require.Equal(t, 1, len(res.Received))
require.Equal(t, "1000", res.Received[0].Amount)
require.Equal(t, assetHashOld, res.Received[0].Asset)
require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[0].Address)
} else { } else {
require.Equal(t, 3, len(res.Received)) require.Equal(t, 3, len(res.Received))
}
require.Equal(t, "1000", res.Received[0].Amount) require.Equal(t, "1000", res.Received[2].Amount)
require.Equal(t, assetHashOld, res.Received[0].Asset) require.Equal(t, assetHashOld, res.Received[2].Asset)
require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[0].Address) require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[2].Address)
if !onlyFirst {
require.Equal(t, "2", res.Received[1].Amount) require.Equal(t, "2", res.Received[1].Amount)
require.Equal(t, assetHash, res.Received[1].Asset) require.Equal(t, assetHash, res.Received[1].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[1].Address) require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[1].Address)
require.Equal(t, uint32(0), res.Received[1].NotifyIndex) require.Equal(t, uint32(0), res.Received[1].NotifyIndex)
require.Equal(t, "1", res.Received[2].Amount) require.Equal(t, "1", res.Received[0].Amount)
require.Equal(t, assetHash, res.Received[2].Asset) require.Equal(t, assetHash, res.Received[0].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[2].Address) require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[0].Address)
require.Equal(t, uint32(1), res.Received[2].NotifyIndex) require.Equal(t, uint32(1), res.Received[0].NotifyIndex)
} }
require.Equal(t, 1, len(res.Sent)) require.Equal(t, 1, len(res.Sent))