neoneo-go/pkg/core/transaction/state.go
Roman Khimov 5bf00db2c9 io: move BinReader/BinWriter there, redo Serializable with it
The logic here is that we'll have all binary encoding/decoding done via our io
package, which simplifies error handling. This functionality doesn't belong to
util, so it's moved.

This also expands BufBinWriter with Reset() method to fit the needs of core
package.
2019-09-16 23:39:51 +03:00

46 lines
1 KiB
Go

package transaction
import (
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
)
// 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 := util.GetVarSize(uint64(len(tx.Descriptors)))
for _, desc := range tx.Descriptors {
sz += desc.Size()
}
return sz
}