From d3e415d3bd0ffe7ecd51b76db30ca5fad8158844 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 8 Sep 2020 12:57:45 +0300 Subject: [PATCH] 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. --- pkg/core/blockchain.go | 8 ++++---- pkg/core/state/nep5.go | 4 ++-- pkg/core/state/nep5_test.go | 4 ++-- pkg/rpc/server/server_test.go | 19 ++++++++++--------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 2119f22a3..bfe617767 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1031,8 +1031,8 @@ func (bc *Blockchain) ForEachTransfer(acc util.Uint160, tr *state.Transfer, f fu if err != nil { return nil } - for i := uint32(0); i <= nb; i++ { - lg, err := bc.dao.GetTransferLog(acc, i) + for i := int(nb); i >= 0; i-- { + lg, err := bc.dao.GetTransferLog(acc, uint32(i)) if err != nil { return nil } @@ -1050,8 +1050,8 @@ func (bc *Blockchain) ForEachNEP5Transfer(acc util.Uint160, tr *state.NEP5Transf 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 0ef8543f5..877b87049 100644 --- a/pkg/core/state/nep5.go +++ b/pkg/core/state/nep5.go @@ -114,8 +114,8 @@ func (lg *TransferLog) ForEach(size int, tr io.Serializable, f func() error) err if lg == nil { return nil } - for i := 0; i < len(lg.Raw); i += size { - r := io.NewBinReaderFromBuf(lg.Raw[i : i+size]) + for i := len(lg.Raw); i > 0; i -= size { + r := io.NewBinReaderFromBuf(lg.Raw[i-size : i]) tr.DecodeBinary(r) if r.Err != nil { return r.Err diff --git a/pkg/core/state/nep5_test.go b/pkg/core/state/nep5_test.go index 90fb01e16..3776821a2 100644 --- a/pkg/core/state/nep5_test.go +++ b/pkg/core/state/nep5_test.go @@ -28,11 +28,11 @@ func TestNEP5TransferLog_Append(t *testing.T) { require.Equal(t, len(expected), lg.Size()/NEP5TransferSize) - i := 0 + i := len(expected) - 1 tr := new(NEP5Transfer) err := lg.ForEach(NEP5TransferSize, tr, func() error { require.Equal(t, expected[i], tr) - i++ + i-- return nil }) require.NoError(t, err) diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index e21e27a71..ce08abaeb 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -1284,24 +1284,25 @@ func checkNep5TransfersAux(t *testing.T, e *executor, acc interface{}, onlyFirst if onlyFirst { 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 { require.Equal(t, 3, 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) + require.Equal(t, "1000", res.Received[2].Amount) + require.Equal(t, assetHashOld, res.Received[2].Asset) + require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[2].Address) - if !onlyFirst { require.Equal(t, "2", res.Received[1].Amount) require.Equal(t, assetHash, res.Received[1].Asset) require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[1].Address) require.Equal(t, uint32(0), res.Received[1].NotifyIndex) - require.Equal(t, "1", res.Received[2].Amount) - require.Equal(t, assetHash, res.Received[2].Asset) - require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[2].Address) - require.Equal(t, uint32(1), res.Received[2].NotifyIndex) + require.Equal(t, "1", res.Received[0].Amount) + require.Equal(t, assetHash, res.Received[0].Asset) + require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[0].Address) + require.Equal(t, uint32(1), res.Received[0].NotifyIndex) } require.Equal(t, 1, len(res.Sent))