forked from TrueCloudLab/neoneo-go
0da9fe6946
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.
45 lines
1 KiB
Go
45 lines
1 KiB
Go
package transaction
|
|
|
|
import (
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
)
|
|
|
|
// StateTX represents a state transaction.
|
|
type StateTX struct {
|
|
Descriptors []*StateDescriptor
|
|
}
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
func (tx *StateTX) DecodeBinary(r *io.BinReader) error {
|
|
lenDesc := r.ReadVarUint()
|
|
tx.Descriptors = make([]*StateDescriptor, lenDesc)
|
|
for i := 0; i < int(lenDesc); i++ {
|
|
tx.Descriptors[i] = &StateDescriptor{}
|
|
err := tx.Descriptors[i].DecodeBinary(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return r.Err
|
|
}
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
func (tx *StateTX) EncodeBinary(w *io.BinWriter) error {
|
|
w.WriteVarUint(uint64(len(tx.Descriptors)))
|
|
for _, desc := range tx.Descriptors {
|
|
err := desc.EncodeBinary(w)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
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
|
|
}
|