io: drop Size() method from Serializable and associated

It's no longer needed after the io.GetVarSize() improvement. It's duplicating
a lot of EncodeBinary() logic also.
This commit is contained in:
Roman Khimov 2019-09-16 16:08:00 +03:00
parent 56c72b5c67
commit e299a44983
28 changed files with 8 additions and 149 deletions

View file

@ -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))

View file

@ -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")

View file

@ -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))

View file

@ -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{

View file

@ -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
}

View file

@ -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
}

View file

@ -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())
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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{}{

View file

@ -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
}

View file

@ -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()
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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()

View file

@ -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
}

View file

@ -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)

View file

@ -2,7 +2,6 @@ package io
// Serializable defines the binary encoding/decoding interface.
type Serializable interface {
Size() int
DecodeBinary(*BinReader) error
EncodeBinary(*BinWriter) error
}

View file

@ -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{}

View file

@ -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 }

View file

@ -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))
}

View file

@ -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{}

View file

@ -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(),

View file

@ -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

View file

@ -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

View file

@ -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