From 683424cce89e2bb7676d6bd79e94235e1f4434a0 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 30 Aug 2019 11:28:30 +0300 Subject: [PATCH] transaction: implement proper Size() everywhere Will be needed for the block test from `dev`. --- pkg/core/transaction/claim.go | 8 ++++++++ pkg/core/transaction/contract.go | 4 ++++ pkg/core/transaction/enrollment.go | 4 ++++ pkg/core/transaction/invocation.go | 8 ++++++++ pkg/core/transaction/issue.go | 4 ++++ pkg/core/transaction/miner.go | 4 ++++ pkg/core/transaction/publish.go | 13 +++++++++++++ pkg/core/transaction/register.go | 4 ++++ pkg/core/transaction/state.go | 8 ++++++++ pkg/core/transaction/state_descriptor.go | 4 ++++ pkg/core/transaction/transaction.go | 3 +-- pkg/core/transaction/txer.go | 1 + 12 files changed, 63 insertions(+), 2 deletions(-) diff --git a/pkg/core/transaction/claim.go b/pkg/core/transaction/claim.go index ecb7f0720..0b5418506 100644 --- a/pkg/core/transaction/claim.go +++ b/pkg/core/transaction/claim.go @@ -42,3 +42,11 @@ func (tx *ClaimTX) EncodeBinary(w io.Writer) error { } return nil } + +func (tx *ClaimTX) Size() int { + sz := util.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 7e4cee6ac..3bcde6d37 100644 --- a/pkg/core/transaction/contract.go +++ b/pkg/core/transaction/contract.go @@ -23,3 +23,7 @@ func (tx *ContractTX) DecodeBinary(r io.Reader) error { func (tx *ContractTX) EncodeBinary(w io.Writer) error { return nil } + +func (tx *ContractTX) Size() int { + return 0 +} diff --git a/pkg/core/transaction/enrollment.go b/pkg/core/transaction/enrollment.go index e43b92481..884525afe 100644 --- a/pkg/core/transaction/enrollment.go +++ b/pkg/core/transaction/enrollment.go @@ -26,3 +26,7 @@ func (tx *EnrollmentTX) DecodeBinary(r io.Reader) error { func (tx *EnrollmentTX) EncodeBinary(w io.Writer) error { return tx.PublicKey.EncodeBinary(w) } + +func (tx *EnrollmentTX) Size() int { + return len(tx.PublicKey.Bytes()) +} diff --git a/pkg/core/transaction/invocation.go b/pkg/core/transaction/invocation.go index 2a0a73fcd..54de3fb00 100644 --- a/pkg/core/transaction/invocation.go +++ b/pkg/core/transaction/invocation.go @@ -53,3 +53,11 @@ func (tx *InvocationTX) EncodeBinary(w io.Writer) error { } return bw.Err } + +func (tx *InvocationTX) Size() int { + sz := util.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 4ad8bdec5..3a2af4e9d 100644 --- a/pkg/core/transaction/issue.go +++ b/pkg/core/transaction/issue.go @@ -17,3 +17,7 @@ func (tx *IssueTX) DecodeBinary(r io.Reader) error { func (tx *IssueTX) EncodeBinary(w io.Writer) error { return nil } + +func (tx *IssueTX) Size() int { + return 0 +} diff --git a/pkg/core/transaction/miner.go b/pkg/core/transaction/miner.go index 3fa8075e9..32c41fa48 100644 --- a/pkg/core/transaction/miner.go +++ b/pkg/core/transaction/miner.go @@ -20,3 +20,7 @@ func (tx *MinerTX) DecodeBinary(r io.Reader) error { func (tx *MinerTX) EncodeBinary(w io.Writer) error { return binary.Write(w, binary.LittleEndian, tx.Nonce) } + +func (tx *MinerTX) Size() int { + return 4 // Nonce +} diff --git a/pkg/core/transaction/publish.go b/pkg/core/transaction/publish.go index f5e0a6eec..c62d91661 100644 --- a/pkg/core/transaction/publish.go +++ b/pkg/core/transaction/publish.go @@ -58,3 +58,16 @@ func (tx *PublishTX) DecodeBinary(r io.Reader) error { func (tx *PublishTX) EncodeBinary(w io.Writer) error { return nil } + +func (tx *PublishTX) Size() int { + sz := util.GetVarSize(tx.Script) + util.GetVarSize(uint64(len(tx.ParamList))) + sz += 1 * len(tx.ParamList) + sz += 1 + if tx.Version >= 1 { + sz += 1 + } + sz += util.GetVarSize(tx.Name) + util.GetVarSize(tx.CodeVersion) + sz += util.GetVarSize(tx.Author) + util.GetVarSize(tx.Email) + sz += util.GetVarSize(tx.Description) + return sz +} diff --git a/pkg/core/transaction/register.go b/pkg/core/transaction/register.go index 2d47fd442..1403e7433 100644 --- a/pkg/core/transaction/register.go +++ b/pkg/core/transaction/register.go @@ -62,3 +62,7 @@ func (tx *RegisterTX) EncodeBinary(w io.Writer) error { bw.WriteLE(tx.Admin) return bw.Err } + +func (tx *RegisterTX) Size() int { + return 1 + util.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 6bcbf8e2a..8a68c92e5 100644 --- a/pkg/core/transaction/state.go +++ b/pkg/core/transaction/state.go @@ -31,3 +31,11 @@ func (tx *StateTX) DecodeBinary(r io.Reader) error { func (tx *StateTX) EncodeBinary(w io.Writer) error { return nil } + +func (tx *StateTX) Size() int { + sz := util.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 95f41be52..770bb6c53 100644 --- a/pkg/core/transaction/state_descriptor.go +++ b/pkg/core/transaction/state_descriptor.go @@ -39,3 +39,7 @@ func (s *StateDescriptor) DecodeBinary(r io.Reader) error { func (s *StateDescriptor) EncodeBinary(w io.Writer) error { return nil } + +func (s *StateDescriptor) Size() int { + return 1 + util.GetVarSize(s.Key) + util.GetVarSize(s.Value) + util.GetVarSize(s.Field) +} diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index 3c2332373..32583196f 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -276,8 +276,7 @@ func (t *Transaction) Size() int { outputSize := util.GetVarSize(t.Outputs) witnesSize := util.GetVarSize(t.Scripts) // uint8 + uint8 + attrSize + inputSize + outputSize + witnesSize - return 2 + attrSize + inputSize + outputSize + witnesSize - + return 2 + attrSize + inputSize + outputSize + witnesSize + t.Data.Size() } // Bytes convert the transaction to []byte diff --git a/pkg/core/transaction/txer.go b/pkg/core/transaction/txer.go index 0134f15fb..e87187bfd 100644 --- a/pkg/core/transaction/txer.go +++ b/pkg/core/transaction/txer.go @@ -7,4 +7,5 @@ import "io" type TXer interface { DecodeBinary(io.Reader) error EncodeBinary(io.Writer) error + Size() int }