forked from TrueCloudLab/neoneo-go
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.
This commit is contained in:
parent
5bf00db2c9
commit
0da9fe6946
13 changed files with 30 additions and 38 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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:
|
|
@ -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,
|
||||
},
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue