From 43b28ffa06205406cc5f416f94a0fe653215b248 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 9 Jul 2020 10:41:53 +0300 Subject: [PATCH] core: get rid of NEP5TransferSize Part of #1133 It will help us to use big.Int to store amount of NEP5 tokens. As far as big.Int doesn't have constant size, we shouldn't use `NEP5TransferSize` constant anymore. --- pkg/core/state/nep5.go | 22 ++++++++++++++-------- pkg/core/state/nep5_test.go | 8 +++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/core/state/nep5.go b/pkg/core/state/nep5.go index a0631dd09..ee552af7e 100644 --- a/pkg/core/state/nep5.go +++ b/pkg/core/state/nep5.go @@ -17,11 +17,10 @@ type NEP5Tracker struct { // NEP5TransferLog is a log of NEP5 token transfers for the specific command. type NEP5TransferLog struct { Raw []byte + // size is the number of NEP5Transfers written into Raw + size int } -// NEP5TransferSize is a size of a marshaled NEP5Transfer struct in bytes. -const NEP5TransferSize = util.Uint160Size*3 + 8 + 4 + 8 + util.Uint256Size - // NEP5Transfer represents a single NEP5 Transfer event. type NEP5Transfer struct { // Asset is a NEP5 contract hash. @@ -89,6 +88,7 @@ func (lg *NEP5TransferLog) Append(tr *NEP5Transfer) error { return w.Err } lg.Raw = append(lg.Raw, w.Bytes()...) + lg.size++ return nil } @@ -98,9 +98,10 @@ func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) error) error { return nil } tr := new(NEP5Transfer) - for i := 0; i < len(lg.Raw); i += NEP5TransferSize { - r := io.NewBinReaderFromBuf(lg.Raw[i : i+NEP5TransferSize]) - tr.DecodeBinary(r) + var bytesRead int + for i := 0; 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 { @@ -112,7 +113,7 @@ func (lg *NEP5TransferLog) ForEach(f func(*NEP5Transfer) error) error { // Size returns an amount of transfer written in log. func (lg *NEP5TransferLog) Size() int { - return len(lg.Raw) / NEP5TransferSize + return lg.size } // EncodeBinary implements io.Serializable interface. @@ -128,7 +129,6 @@ func (t *NEP5Tracker) DecodeBinary(r *io.BinReader) { } // EncodeBinary implements io.Serializable interface. -// Note: change NEP5TransferSize constant when changing this function. func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) { w.WriteBytes(t.Asset[:]) w.WriteBytes(t.Tx[:]) @@ -141,6 +141,11 @@ func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) { // DecodeBinary implements io.Serializable interface. func (t *NEP5Transfer) DecodeBinary(r *io.BinReader) { + _ = t.DecodeBinaryReturnCount(r) +} + +// DecodeBinaryReturnCount decodes NEP5Transfer and returns the number of bytes read. +func (t *NEP5Transfer) DecodeBinaryReturnCount(r *io.BinReader) int { r.ReadBytes(t.Asset[:]) r.ReadBytes(t.Tx[:]) r.ReadBytes(t.From[:]) @@ -148,4 +153,5 @@ func (t *NEP5Transfer) DecodeBinary(r *io.BinReader) { t.Block = r.ReadU32LE() t.Timestamp = r.ReadU64LE() t.Amount = int64(r.ReadU64LE()) + return util.Uint160Size*3 + 8 + 4 + 8 + util.Uint256Size } diff --git a/pkg/core/state/nep5_test.go b/pkg/core/state/nep5_test.go index 543d05819..d95dbc3ff 100644 --- a/pkg/core/state/nep5_test.go +++ b/pkg/core/state/nep5_test.go @@ -64,7 +64,13 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) { func TestNEP5TransferSize(t *testing.T) { tr := randomTransfer(rand.New(rand.NewSource(0))) size := io.GetVarSize(tr) - require.EqualValues(t, NEP5TransferSize, size) + w := io.NewBufBinWriter() + tr.EncodeBinary(w.BinWriter) + require.NoError(t, w.Err) + r := io.NewBinReaderFromBuf(w.Bytes()) + actualTr := &NEP5Transfer{} + actual := actualTr.DecodeBinaryReturnCount(r) + require.EqualValues(t, actual, size) } func randomTransfer(r *rand.Rand) *NEP5Transfer {