transaction: implement proper Size() everywhere

Will be needed for the block test from `dev`.
This commit is contained in:
Roman Khimov 2019-08-30 11:28:30 +03:00
parent 200cce9f02
commit 683424cce8
12 changed files with 63 additions and 2 deletions

View file

@ -42,3 +42,11 @@ func (tx *ClaimTX) EncodeBinary(w io.Writer) error {
} }
return nil return nil
} }
func (tx *ClaimTX) Size() int {
sz := util.GetVarSize(uint64(len(tx.Claims)))
for _, claim := range tx.Claims {
sz += claim.Size()
}
return sz
}

View file

@ -23,3 +23,7 @@ func (tx *ContractTX) DecodeBinary(r io.Reader) error {
func (tx *ContractTX) EncodeBinary(w io.Writer) error { func (tx *ContractTX) EncodeBinary(w io.Writer) error {
return nil return nil
} }
func (tx *ContractTX) Size() int {
return 0
}

View file

@ -26,3 +26,7 @@ func (tx *EnrollmentTX) DecodeBinary(r io.Reader) error {
func (tx *EnrollmentTX) EncodeBinary(w io.Writer) error { func (tx *EnrollmentTX) EncodeBinary(w io.Writer) error {
return tx.PublicKey.EncodeBinary(w) return tx.PublicKey.EncodeBinary(w)
} }
func (tx *EnrollmentTX) Size() int {
return len(tx.PublicKey.Bytes())
}

View file

@ -53,3 +53,11 @@ func (tx *InvocationTX) EncodeBinary(w io.Writer) error {
} }
return bw.Err return bw.Err
} }
func (tx *InvocationTX) Size() int {
sz := util.GetVarSize(tx.Script)
if (tx.Version >= 1) {
sz += tx.Gas.Size()
}
return sz
}

View file

@ -17,3 +17,7 @@ func (tx *IssueTX) DecodeBinary(r io.Reader) error {
func (tx *IssueTX) EncodeBinary(w io.Writer) error { func (tx *IssueTX) EncodeBinary(w io.Writer) error {
return nil return nil
} }
func (tx *IssueTX) Size() int {
return 0
}

View file

@ -20,3 +20,7 @@ func (tx *MinerTX) DecodeBinary(r io.Reader) error {
func (tx *MinerTX) EncodeBinary(w io.Writer) error { func (tx *MinerTX) EncodeBinary(w io.Writer) error {
return binary.Write(w, binary.LittleEndian, tx.Nonce) return binary.Write(w, binary.LittleEndian, tx.Nonce)
} }
func (tx *MinerTX) Size() int {
return 4 // Nonce
}

View file

@ -58,3 +58,16 @@ func (tx *PublishTX) DecodeBinary(r io.Reader) error {
func (tx *PublishTX) EncodeBinary(w io.Writer) error { func (tx *PublishTX) EncodeBinary(w io.Writer) error {
return nil 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
}

View file

@ -62,3 +62,7 @@ func (tx *RegisterTX) EncodeBinary(w io.Writer) error {
bw.WriteLE(tx.Admin) bw.WriteLE(tx.Admin)
return bw.Err 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()
}

View file

@ -31,3 +31,11 @@ func (tx *StateTX) DecodeBinary(r io.Reader) error {
func (tx *StateTX) EncodeBinary(w io.Writer) error { func (tx *StateTX) EncodeBinary(w io.Writer) error {
return nil return nil
} }
func (tx *StateTX) Size() int {
sz := util.GetVarSize(uint64(len(tx.Descriptors)))
for _, desc := range tx.Descriptors {
sz += desc.Size()
}
return sz
}

View file

@ -39,3 +39,7 @@ func (s *StateDescriptor) DecodeBinary(r io.Reader) error {
func (s *StateDescriptor) EncodeBinary(w io.Writer) error { func (s *StateDescriptor) EncodeBinary(w io.Writer) error {
return nil return nil
} }
func (s *StateDescriptor) Size() int {
return 1 + util.GetVarSize(s.Key) + util.GetVarSize(s.Value) + util.GetVarSize(s.Field)
}

View file

@ -276,8 +276,7 @@ func (t *Transaction) Size() int {
outputSize := util.GetVarSize(t.Outputs) outputSize := util.GetVarSize(t.Outputs)
witnesSize := util.GetVarSize(t.Scripts) witnesSize := util.GetVarSize(t.Scripts)
// uint8 + uint8 + attrSize + inputSize + outputSize + witnesSize // 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 // Bytes convert the transaction to []byte

View file

@ -7,4 +7,5 @@ import "io"
type TXer interface { type TXer interface {
DecodeBinary(io.Reader) error DecodeBinary(io.Reader) error
EncodeBinary(io.Writer) error EncodeBinary(io.Writer) error
Size() int
} }