From 0da9fe69461d46e6591cad3c142f33221ca0477f Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 16 Sep 2019 12:33:53 +0300 Subject: [PATCH] io: move size calculator there It's mostly used for Serializable and in other cases where one needs to estimate binary-encoded size of the stucture. This also simplifies future removal of the Size() from Serializable. --- pkg/core/blockchain_test.go | 10 +++++----- pkg/core/transaction/attribute.go | 3 +-- pkg/core/transaction/claim.go | 3 +-- pkg/core/transaction/invocation.go | 2 +- pkg/core/transaction/publish.go | 9 ++++----- pkg/core/transaction/register.go | 2 +- pkg/core/transaction/state.go | 3 +-- pkg/core/transaction/state_descriptor.go | 3 +-- pkg/core/transaction/transaction.go | 8 ++++---- pkg/core/transaction/witness.go | 2 +- pkg/{util => io}/size.go | 8 +++----- pkg/{util => io}/size_test.go | 12 ++++++------ pkg/network/payload/version.go | 3 +-- 13 files changed, 30 insertions(+), 38 deletions(-) rename pkg/{util => io}/size.go (94%) rename pkg/{util => io}/size_test.go (91%) diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 9a1c38562..a2fcc188e 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -6,7 +6,7 @@ import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core/storage" - "github.com/CityOfZion/neo-go/pkg/util" + "github.com/CityOfZion/neo-go/pkg/io" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -142,10 +142,10 @@ func TestGetTransaction(t *testing.T) { assert.Equal(t, block.Index, height) assert.Equal(t, block.Transactions[0], tx) assert.Equal(t, 10, tx.Size()) - assert.Equal(t, 1, util.GetVarSize(tx.Attributes)) - assert.Equal(t, 1, util.GetVarSize(tx.Inputs)) - assert.Equal(t, 1, util.GetVarSize(tx.Outputs)) - assert.Equal(t, 1, util.GetVarSize(tx.Scripts)) + assert.Equal(t, 1, io.GetVarSize(tx.Attributes)) + assert.Equal(t, 1, io.GetVarSize(tx.Inputs)) + assert.Equal(t, 1, io.GetVarSize(tx.Outputs)) + assert.Equal(t, 1, io.GetVarSize(tx.Scripts)) } func newTestChain(t *testing.T) *Blockchain { diff --git a/pkg/core/transaction/attribute.go b/pkg/core/transaction/attribute.go index b4e07aee4..3c60d3d5f 100644 --- a/pkg/core/transaction/attribute.go +++ b/pkg/core/transaction/attribute.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) // Attribute represents a Transaction attribute. @@ -87,7 +86,7 @@ func (attr *Attribute) Size() int { case DescriptionURL: sz += 1 + len(attr.Data) default: - sz += util.GetVarSize(attr.Data) + sz += io.GetVarSize(attr.Data) } return sz } diff --git a/pkg/core/transaction/claim.go b/pkg/core/transaction/claim.go index bb864548c..6c1a69eda 100644 --- a/pkg/core/transaction/claim.go +++ b/pkg/core/transaction/claim.go @@ -2,7 +2,6 @@ package transaction import ( "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) // ClaimTX represents a claim transaction. @@ -42,7 +41,7 @@ func (tx *ClaimTX) EncodeBinary(bw *io.BinWriter) error { // Size returns serialized binary size for this transaction. func (tx *ClaimTX) Size() int { - sz := util.GetVarSize(uint64(len(tx.Claims))) + sz := io.GetVarSize(uint64(len(tx.Claims))) for _, claim := range tx.Claims { sz += claim.Size() } diff --git a/pkg/core/transaction/invocation.go b/pkg/core/transaction/invocation.go index 52a105bcb..bfdf3df66 100644 --- a/pkg/core/transaction/invocation.go +++ b/pkg/core/transaction/invocation.go @@ -53,7 +53,7 @@ func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) error { // Size returns serialized binary size for this transaction. func (tx *InvocationTX) Size() int { - sz := util.GetVarSize(tx.Script) + sz := io.GetVarSize(tx.Script) if tx.Version >= 1 { sz += tx.Gas.Size() } diff --git a/pkg/core/transaction/publish.go b/pkg/core/transaction/publish.go index c5149bc97..fbf3d11d6 100644 --- a/pkg/core/transaction/publish.go +++ b/pkg/core/transaction/publish.go @@ -3,7 +3,6 @@ package transaction import ( "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/smartcontract" - "github.com/CityOfZion/neo-go/pkg/util" ) // PublishTX represents a publish transaction. @@ -73,14 +72,14 @@ func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) error { // Size returns serialized binary size for this transaction. func (tx *PublishTX) Size() int { - sz := util.GetVarSize(tx.Script) + util.GetVarSize(uint64(len(tx.ParamList))) + sz := io.GetVarSize(tx.Script) + io.GetVarSize(uint64(len(tx.ParamList))) sz += 1 * len(tx.ParamList) sz++ if tx.Version >= 1 { sz++ } - sz += util.GetVarSize(tx.Name) + util.GetVarSize(tx.CodeVersion) - sz += util.GetVarSize(tx.Author) + util.GetVarSize(tx.Email) - sz += util.GetVarSize(tx.Description) + sz += io.GetVarSize(tx.Name) + io.GetVarSize(tx.CodeVersion) + sz += io.GetVarSize(tx.Author) + io.GetVarSize(tx.Email) + sz += io.GetVarSize(tx.Description) return sz } diff --git a/pkg/core/transaction/register.go b/pkg/core/transaction/register.go index 711cb7465..1d45aa23e 100644 --- a/pkg/core/transaction/register.go +++ b/pkg/core/transaction/register.go @@ -62,5 +62,5 @@ func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) error { // Size returns serialized binary size for this transaction. func (tx *RegisterTX) Size() int { - return 1 + util.GetVarSize(tx.Name) + tx.Amount.Size() + 1 + len(tx.Owner.Bytes()) + tx.Admin.Size() + return 1 + io.GetVarSize(tx.Name) + tx.Amount.Size() + 1 + len(tx.Owner.Bytes()) + tx.Admin.Size() } diff --git a/pkg/core/transaction/state.go b/pkg/core/transaction/state.go index 490c8f6a2..be52ef734 100644 --- a/pkg/core/transaction/state.go +++ b/pkg/core/transaction/state.go @@ -2,7 +2,6 @@ package transaction import ( "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) // StateTX represents a state transaction. @@ -38,7 +37,7 @@ func (tx *StateTX) EncodeBinary(w *io.BinWriter) error { // Size returns serialized binary size for this transaction. func (tx *StateTX) Size() int { - sz := util.GetVarSize(uint64(len(tx.Descriptors))) + sz := io.GetVarSize(uint64(len(tx.Descriptors))) for _, desc := range tx.Descriptors { sz += desc.Size() } diff --git a/pkg/core/transaction/state_descriptor.go b/pkg/core/transaction/state_descriptor.go index c7e82822e..4c794fceb 100644 --- a/pkg/core/transaction/state_descriptor.go +++ b/pkg/core/transaction/state_descriptor.go @@ -2,7 +2,6 @@ package transaction import ( "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) // DescStateType represents the type of StateDescriptor. @@ -44,5 +43,5 @@ func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) error { // Size returns serialized binary size for state descriptor. func (s *StateDescriptor) Size() int { - return 1 + util.GetVarSize(s.Key) + util.GetVarSize(s.Value) + util.GetVarSize(s.Field) + return 1 + io.GetVarSize(s.Key) + io.GetVarSize(s.Value) + io.GetVarSize(s.Field) } diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index 3bf651317..cd9e3c352 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -265,10 +265,10 @@ func (t Transaction) GroupOutputByAssetID() map[util.Uint256][]*Output { // Size returns the size of the transaction in term of bytes func (t *Transaction) Size() int { - attrSize := util.GetVarSize(t.Attributes) - inputSize := util.GetVarSize(t.Inputs) - outputSize := util.GetVarSize(t.Outputs) - witnesSize := util.GetVarSize(t.Scripts) + attrSize := io.GetVarSize(t.Attributes) + inputSize := io.GetVarSize(t.Inputs) + outputSize := io.GetVarSize(t.Outputs) + witnesSize := io.GetVarSize(t.Scripts) // uint8 + uint8 + attrSize + inputSize + outputSize + witnesSize return 2 + attrSize + inputSize + outputSize + witnesSize + t.Data.Size() } diff --git a/pkg/core/transaction/witness.go b/pkg/core/transaction/witness.go index 20c1d8d91..d4da3836a 100644 --- a/pkg/core/transaction/witness.go +++ b/pkg/core/transaction/witness.go @@ -42,7 +42,7 @@ func (w *Witness) MarshalJSON() ([]byte, error) { // Size returns the size in bytes of the Witness. func (w *Witness) Size() int { - return util.GetVarSize(w.InvocationScript) + util.GetVarSize(w.VerificationScript) + return io.GetVarSize(w.InvocationScript) + io.GetVarSize(w.VerificationScript) } // ScriptHash returns the hash of the VerificationScript. diff --git a/pkg/util/size.go b/pkg/io/size.go similarity index 94% rename from pkg/util/size.go rename to pkg/io/size.go index 33b7b60fc..bde30be19 100644 --- a/pkg/util/size.go +++ b/pkg/io/size.go @@ -1,10 +1,8 @@ -package util +package io import ( "fmt" "reflect" - - "github.com/CityOfZion/neo-go/pkg/io" ) var ( @@ -67,9 +65,9 @@ func GetVarSize(value interface{}) int { if valueLength != 0 { switch reflect.ValueOf(value).Index(0).Interface().(type) { - case io.Serializable: + case Serializable: for i := 0; i < valueLength; i++ { - elem := v.Index(i).Interface().(io.Serializable) + elem := v.Index(i).Interface().(Serializable) valueSize += elem.Size() } case uint8, int8: diff --git a/pkg/util/size_test.go b/pkg/io/size_test.go similarity index 91% rename from pkg/util/size_test.go rename to pkg/io/size_test.go index d77ceaebc..e3028accd 100644 --- a/pkg/util/size_test.go +++ b/pkg/io/size_test.go @@ -1,10 +1,10 @@ -package util +package io import ( "fmt" "testing" - "github.com/CityOfZion/neo-go/pkg/io" + "github.com/CityOfZion/neo-go/pkg/util" "github.com/stretchr/testify/assert" ) @@ -12,11 +12,11 @@ import ( type smthSerializable struct { } -func (*smthSerializable) DecodeBinary(*io.BinReader) error { +func (*smthSerializable) DecodeBinary(*BinReader) error { return nil } -func (*smthSerializable) EncodeBinary(*io.BinWriter) error { +func (*smthSerializable) EncodeBinary(*BinWriter) error { return nil } @@ -87,7 +87,7 @@ func TestVarSize(t *testing.T) { }, { // The neo C# implementation doe not allowed this! - Uint160{1, 2, 4, 5, 6}, + util.Uint160{1, 2, 4, 5, 6}, "test_Uint160_1", 21, }, @@ -153,7 +153,7 @@ func TestVarSize(t *testing.T) { 241, }, // The neo C# implementation doe not allowed this! - {Uint256{1, 2, 3, 4, 5, 6}, + {util.Uint256{1, 2, 3, 4, 5, 6}, "test_Uint256_1", 33, }, diff --git a/pkg/network/payload/version.go b/pkg/network/payload/version.go index b9d347d45..de78b1b3b 100644 --- a/pkg/network/payload/version.go +++ b/pkg/network/payload/version.go @@ -4,7 +4,6 @@ import ( "time" "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) // Size of the payload not counting UserAgent encoding (which is at least 1 byte @@ -83,5 +82,5 @@ func (p *Version) EncodeBinary(br *io.BinWriter) error { // Size implements the payloader interface. func (p *Version) Size() uint32 { - return uint32(minVersionSize + util.GetVarSize(p.UserAgent)) + return uint32(minVersionSize + io.GetVarSize(p.UserAgent)) }