diff --git a/pkg/core/block_test.go b/pkg/core/block_test.go index ffa159e40..546d1de47 100644 --- a/pkg/core/block_test.go +++ b/pkg/core/block_test.go @@ -225,7 +225,7 @@ func TestBlockSizeCalculation(t *testing.T) { txID := tx.Hash() assert.Equal(t, expected[i].ID, txID.ReverseString()) - assert.Equal(t, expected[i].Size, tx.Size()) + assert.Equal(t, expected[i].Size, io.GetVarSize(tx)) assert.Equal(t, expected[i].Type, tx.Type.String()) assert.Equal(t, expected[i].Version, int(tx.Version)) assert.Equal(t, expected[i].InputsLen, len(tx.Inputs)) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index a31b0b037..08e5b298a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -628,7 +628,7 @@ func (bc *Blockchain) References(t *transaction.Transaction) map[util.Uint256]*t // FeePerByte returns network fee divided by the size of the transaction func (bc *Blockchain) FeePerByte(t *transaction.Transaction) util.Fixed8 { - return bc.NetworkFee(t).Div(int64(t.Size())) + return bc.NetworkFee(t).Div(int64(io.GetVarSize(t))) } // NetworkFee returns network fee @@ -669,8 +669,8 @@ func (bc *Blockchain) GetMemPool() MemPool { // Verify verifies whether a transaction is bonafide or not. // Golang implementation of Verify method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L270). func (bc *Blockchain) Verify(t *transaction.Transaction) error { - if t.Size() > transaction.MaxTransactionSize { - return errors.Errorf("invalid transaction size = %d. It shoud be less then MaxTransactionSize = %d", t.Size(), transaction.MaxTransactionSize) + if io.GetVarSize(t) > transaction.MaxTransactionSize { + return errors.Errorf("invalid transaction size = %d. It shoud be less then MaxTransactionSize = %d", io.GetVarSize(t), transaction.MaxTransactionSize) } if ok := bc.verifyInputs(t); !ok { return errors.New("invalid transaction's inputs") diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index a2fcc188e..8f35861c2 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -141,7 +141,7 @@ 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, 10, io.GetVarSize(tx)) assert.Equal(t, 1, io.GetVarSize(tx.Attributes)) assert.Equal(t, 1, io.GetVarSize(tx.Inputs)) assert.Equal(t, 1, io.GetVarSize(tx.Outputs)) diff --git a/pkg/core/transaction/attribute.go b/pkg/core/transaction/attribute.go index 3c60d3d5f..425dd72e6 100644 --- a/pkg/core/transaction/attribute.go +++ b/pkg/core/transaction/attribute.go @@ -74,23 +74,6 @@ func (attr *Attribute) EncodeBinary(bw *io.BinWriter) error { return bw.Err } -// Size returns the size in number bytes of the Attribute -func (attr *Attribute) Size() int { - sz := 1 // usage - switch attr.Usage { - case ContractHash, ECDH02, ECDH03, Vote, - Hash1, Hash2, Hash3, Hash4, Hash5, Hash6, Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15: - sz += 32 // uint8 + 32 = size(attrUsage) + 32 - case Script: - sz += 20 // uint8 + 20 = size(attrUsage) + 20 - case DescriptionURL: - sz += 1 + len(attr.Data) - default: - sz += io.GetVarSize(attr.Data) - } - return sz -} - // MarshalJSON implements the json Marschaller interface func (attr *Attribute) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]string{ diff --git a/pkg/core/transaction/claim.go b/pkg/core/transaction/claim.go index 6c1a69eda..61d44614c 100644 --- a/pkg/core/transaction/claim.go +++ b/pkg/core/transaction/claim.go @@ -38,12 +38,3 @@ func (tx *ClaimTX) EncodeBinary(bw *io.BinWriter) error { } return nil } - -// Size returns serialized binary size for this transaction. -func (tx *ClaimTX) Size() int { - sz := io.GetVarSize(uint64(len(tx.Claims))) - for _, claim := range tx.Claims { - sz += claim.Size() - } - return sz -} diff --git a/pkg/core/transaction/contract.go b/pkg/core/transaction/contract.go index c7d91fa12..4c445da21 100644 --- a/pkg/core/transaction/contract.go +++ b/pkg/core/transaction/contract.go @@ -24,8 +24,3 @@ func (tx *ContractTX) DecodeBinary(r *io.BinReader) error { func (tx *ContractTX) EncodeBinary(w *io.BinWriter) error { return nil } - -// Size returns serialized binary size for this transaction. -func (tx *ContractTX) Size() int { - return 0 -} diff --git a/pkg/core/transaction/enrollment.go b/pkg/core/transaction/enrollment.go index 9ef1b4f68..d01b3eedb 100644 --- a/pkg/core/transaction/enrollment.go +++ b/pkg/core/transaction/enrollment.go @@ -25,8 +25,3 @@ func (tx *EnrollmentTX) DecodeBinary(r *io.BinReader) error { func (tx *EnrollmentTX) EncodeBinary(w *io.BinWriter) error { return tx.PublicKey.EncodeBinary(w) } - -// Size returns serialized binary size for this transaction. -func (tx *EnrollmentTX) Size() int { - return len(tx.PublicKey.Bytes()) -} diff --git a/pkg/core/transaction/input.go b/pkg/core/transaction/input.go index cce3db6c3..a5a13411d 100644 --- a/pkg/core/transaction/input.go +++ b/pkg/core/transaction/input.go @@ -27,8 +27,3 @@ func (in *Input) EncodeBinary(bw *io.BinWriter) error { bw.WriteLE(in.PrevIndex) return bw.Err } - -// Size returns the size in bytes of the Input -func (in Input) Size() int { - return in.PrevHash.Size() + 2 // 2 = sizeOf uint16 -} diff --git a/pkg/core/transaction/invocation.go b/pkg/core/transaction/invocation.go index bfdf3df66..43a2409ed 100644 --- a/pkg/core/transaction/invocation.go +++ b/pkg/core/transaction/invocation.go @@ -50,12 +50,3 @@ func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) error { } return bw.Err } - -// Size returns serialized binary size for this transaction. -func (tx *InvocationTX) Size() int { - sz := io.GetVarSize(tx.Script) - if tx.Version >= 1 { - sz += tx.Gas.Size() - } - return sz -} diff --git a/pkg/core/transaction/issue.go b/pkg/core/transaction/issue.go index 7555d267b..9f412e97a 100644 --- a/pkg/core/transaction/issue.go +++ b/pkg/core/transaction/issue.go @@ -17,8 +17,3 @@ func (tx *IssueTX) DecodeBinary(r *io.BinReader) error { func (tx *IssueTX) EncodeBinary(w *io.BinWriter) error { return nil } - -// Size returns serialized binary size for this transaction. -func (tx *IssueTX) Size() int { - return 0 -} diff --git a/pkg/core/transaction/miner.go b/pkg/core/transaction/miner.go index b64fbee9b..923253926 100644 --- a/pkg/core/transaction/miner.go +++ b/pkg/core/transaction/miner.go @@ -21,8 +21,3 @@ func (tx *MinerTX) EncodeBinary(w *io.BinWriter) error { w.WriteLE(tx.Nonce) return w.Err } - -// Size returns serialized binary size for this transaction. -func (tx *MinerTX) Size() int { - return 4 // Nonce -} diff --git a/pkg/core/transaction/output.go b/pkg/core/transaction/output.go index 887bcb18f..a6aacbdca 100644 --- a/pkg/core/transaction/output.go +++ b/pkg/core/transaction/output.go @@ -49,11 +49,6 @@ func (out *Output) EncodeBinary(bw *io.BinWriter) error { return bw.Err } -// Size returns the size in bytes of the Output -func (out *Output) Size() int { - return out.AssetID.Size() + out.Amount.Size() + out.ScriptHash.Size() -} - // MarshalJSON implements the Marshaler interface func (out *Output) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]interface{}{ diff --git a/pkg/core/transaction/publish.go b/pkg/core/transaction/publish.go index fbf3d11d6..6a07fdfce 100644 --- a/pkg/core/transaction/publish.go +++ b/pkg/core/transaction/publish.go @@ -69,17 +69,3 @@ func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) error { bw.WriteString(tx.Description) return bw.Err } - -// Size returns serialized binary size for this transaction. -func (tx *PublishTX) Size() int { - sz := io.GetVarSize(tx.Script) + io.GetVarSize(uint64(len(tx.ParamList))) - sz += 1 * len(tx.ParamList) - sz++ - if tx.Version >= 1 { - sz++ - } - 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 1d45aa23e..c3c227165 100644 --- a/pkg/core/transaction/register.go +++ b/pkg/core/transaction/register.go @@ -59,8 +59,3 @@ func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) error { bw.WriteLE(tx.Admin) return bw.Err } - -// Size returns serialized binary size for this transaction. -func (tx *RegisterTX) Size() int { - 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 be52ef734..8723b972d 100644 --- a/pkg/core/transaction/state.go +++ b/pkg/core/transaction/state.go @@ -34,12 +34,3 @@ func (tx *StateTX) EncodeBinary(w *io.BinWriter) error { } return w.Err } - -// Size returns serialized binary size for this transaction. -func (tx *StateTX) Size() int { - sz := io.GetVarSize(uint64(len(tx.Descriptors))) - for _, desc := range tx.Descriptors { - sz += desc.Size() - } - return sz -} diff --git a/pkg/core/transaction/state_descriptor.go b/pkg/core/transaction/state_descriptor.go index 4c794fceb..40dbbcf14 100644 --- a/pkg/core/transaction/state_descriptor.go +++ b/pkg/core/transaction/state_descriptor.go @@ -40,8 +40,3 @@ func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) error { w.WriteString(s.Field) return w.Err } - -// Size returns serialized binary size for state descriptor. -func (s *StateDescriptor) Size() int { - 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 cd9e3c352..628677f21 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -263,16 +263,6 @@ func (t Transaction) GroupOutputByAssetID() map[util.Uint256][]*Output { return m } -// Size returns the size of the transaction in term of bytes -func (t *Transaction) Size() int { - 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() -} - // Bytes convert the transaction to []byte func (t *Transaction) Bytes() []byte { buf := io.NewBufBinWriter() diff --git a/pkg/core/transaction/txer.go b/pkg/core/transaction/txer.go index c26c6364c..62558143b 100644 --- a/pkg/core/transaction/txer.go +++ b/pkg/core/transaction/txer.go @@ -7,5 +7,4 @@ import "github.com/CityOfZion/neo-go/pkg/io" type TXer interface { DecodeBinary(*io.BinReader) error EncodeBinary(*io.BinWriter) error - Size() int } diff --git a/pkg/core/transaction/witness.go b/pkg/core/transaction/witness.go index d4da3836a..9256ecc16 100644 --- a/pkg/core/transaction/witness.go +++ b/pkg/core/transaction/witness.go @@ -40,11 +40,6 @@ func (w *Witness) MarshalJSON() ([]byte, error) { return json.Marshal(data) } -// Size returns the size in bytes of the Witness. -func (w *Witness) Size() int { - return io.GetVarSize(w.InvocationScript) + io.GetVarSize(w.VerificationScript) -} - // ScriptHash returns the hash of the VerificationScript. func (w Witness) ScriptHash() util.Uint160 { return hash.Hash160(w.VerificationScript) diff --git a/pkg/io/serializable.go b/pkg/io/serializable.go index 0f49c61d8..2063d2c6d 100644 --- a/pkg/io/serializable.go +++ b/pkg/io/serializable.go @@ -2,7 +2,6 @@ package io // Serializable defines the binary encoding/decoding interface. type Serializable interface { - Size() int DecodeBinary(*BinReader) error EncodeBinary(*BinWriter) error } diff --git a/pkg/io/size_test.go b/pkg/io/size_test.go index 9a5d2de66..85275fe94 100644 --- a/pkg/io/size_test.go +++ b/pkg/io/size_test.go @@ -22,10 +22,6 @@ func (ss *smthSerializable) EncodeBinary(bw *BinWriter) error { return nil } -func (*smthSerializable) Size() int { - return 42 -} - func TestVarSize(t *testing.T) { testCases := []struct { variable interface{} diff --git a/pkg/network/payload/getblocks.go b/pkg/network/payload/getblocks.go index f1e3e1fc1..3298c5b5a 100644 --- a/pkg/network/payload/getblocks.go +++ b/pkg/network/payload/getblocks.go @@ -38,6 +38,3 @@ func (p *GetBlocks) EncodeBinary(bw *io.BinWriter) error { bw.WriteLE(p.HashStop) return bw.Err } - -// Size implements the payload interface. -func (p *GetBlocks) Size() uint32 { return 0 } diff --git a/pkg/network/payload/version.go b/pkg/network/payload/version.go index de78b1b3b..2cb79fbe2 100644 --- a/pkg/network/payload/version.go +++ b/pkg/network/payload/version.go @@ -79,8 +79,3 @@ func (p *Version) EncodeBinary(br *io.BinWriter) error { br.WriteLE(&p.Relay) return br.Err } - -// Size implements the payloader interface. -func (p *Version) Size() uint32 { - return uint32(minVersionSize + io.GetVarSize(p.UserAgent)) -} diff --git a/pkg/network/payload/version_test.go b/pkg/network/payload/version_test.go index 559fe94ed..5c4b3b73e 100644 --- a/pkg/network/payload/version_test.go +++ b/pkg/network/payload/version_test.go @@ -20,7 +20,7 @@ func TestVersionEncodeDecode(t *testing.T) { err := version.EncodeBinary(buf.BinWriter) assert.Nil(t, err) b := buf.Bytes() - assert.Equal(t, int(version.Size()), len(b)) + assert.Equal(t, io.GetVarSize(version), len(b)) r := io.NewBinReaderFromBuf(b) versionDecoded := &Version{} diff --git a/pkg/rpc/wrappers/tx_raw_output.go b/pkg/rpc/wrappers/tx_raw_output.go index d3ebbd33a..7a8a6fcf7 100644 --- a/pkg/rpc/wrappers/tx_raw_output.go +++ b/pkg/rpc/wrappers/tx_raw_output.go @@ -3,6 +3,7 @@ package wrappers import ( "github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/transaction" + "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/util" ) @@ -30,7 +31,7 @@ func NewTransactionOutputRaw(tx *transaction.Transaction, header *core.Header, c return TransactionOutputRaw{ Transaction: tx, TxHash: tx.Hash(), - Size: tx.Size(), + Size: io.GetVarSize(tx), SysFee: chain.SystemFee(tx), NetFee: chain.NetworkFee(tx), Blockhash: header.Hash(), diff --git a/pkg/util/fixed8.go b/pkg/util/fixed8.go index ec7b96d61..1364a0601 100644 --- a/pkg/util/fixed8.go +++ b/pkg/util/fixed8.go @@ -104,11 +104,6 @@ func (f *Fixed8) UnmarshalJSON(data []byte) error { return nil } -// Size returns the size in number of bytes of Fixed8. -func (f *Fixed8) Size() int { - return 8 -} - // MarshalJSON implements the json marshaller interface. func (f Fixed8) MarshalJSON() ([]byte, error) { return []byte(`"` + f.String() + `"`), nil diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 996bd9334..8c9202935 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -70,11 +70,6 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) { return err } -// Size returns the length of the bytes representation of Uint160 -func (u Uint160) Size() int { - return uint160Size -} - // MarshalJSON implements the json marshaller interface. func (u Uint160) MarshalJSON() ([]byte, error) { return []byte(`"0x` + u.String() + `"`), nil diff --git a/pkg/util/uint256.go b/pkg/util/uint256.go index 0dbad3f89..4de10b135 100644 --- a/pkg/util/uint256.go +++ b/pkg/util/uint256.go @@ -82,11 +82,6 @@ func (u *Uint256) UnmarshalJSON(data []byte) (err error) { return err } -// Size returns the length of the bytes representation of Uint256 -func (u Uint256) Size() int { - return uint256Size -} - // MarshalJSON implements the json marshaller interface. func (u Uint256) MarshalJSON() ([]byte, error) { return []byte(`"0x` + u.ReverseString() + `"`), nil