util: implement Serializable interface over Uint160

This commit is contained in:
Anna Shaleva 2020-05-04 10:35:56 +03:00
parent 08f5708edb
commit d1ec01c45e
6 changed files with 26 additions and 14 deletions

View file

@ -49,7 +49,7 @@ func NewAccount(scriptHash util.Uint160) *Account {
// DecodeBinary decodes Account from the given BinReader.
func (s *Account) DecodeBinary(br *io.BinReader) {
s.Version = uint8(br.ReadB())
br.ReadBytes(s.ScriptHash[:])
s.ScriptHash.DecodeBinary(br)
s.IsFrozen = br.ReadBool()
s.Balances = make(map[util.Uint256][]UnspentBalance)
@ -73,7 +73,7 @@ func (s *Account) DecodeBinary(br *io.BinReader) {
// EncodeBinary encodes Account to the given BinWriter.
func (s *Account) EncodeBinary(bw *io.BinWriter) {
bw.WriteB(byte(s.Version))
bw.WriteBytes(s.ScriptHash[:])
s.ScriptHash.EncodeBinary(bw)
bw.WriteBool(s.IsFrozen)
bw.WriteVarUint(uint64(len(s.Balances)))

View file

@ -37,11 +37,11 @@ func (a *Asset) DecodeBinary(br *io.BinReader) {
a.Available.DecodeBinary(br)
a.Precision = uint8(br.ReadB())
a.FeeMode = uint8(br.ReadB())
br.ReadBytes(a.FeeAddress[:])
a.FeeAddress.DecodeBinary(br)
a.Owner.DecodeBinary(br)
br.ReadBytes(a.Admin[:])
br.ReadBytes(a.Issuer[:])
a.Admin.DecodeBinary(br)
a.Issuer.DecodeBinary(br)
a.Expiration = br.ReadU32LE()
a.IsFrozen = br.ReadBool()
}
@ -55,12 +55,12 @@ func (a *Asset) EncodeBinary(bw *io.BinWriter) {
a.Available.EncodeBinary(bw)
bw.WriteB(byte(a.Precision))
bw.WriteB(byte(a.FeeMode))
bw.WriteBytes(a.FeeAddress[:])
a.FeeAddress.EncodeBinary(bw)
a.Owner.EncodeBinary(bw)
bw.WriteBytes(a.Admin[:])
bw.WriteBytes(a.Issuer[:])
a.Admin.EncodeBinary(bw)
a.Issuer.EncodeBinary(bw)
bw.WriteU32LE(a.Expiration)
bw.WriteBool(a.IsFrozen)
}

View file

@ -28,13 +28,13 @@ type AppExecResult struct {
// EncodeBinary implements the Serializable interface.
func (ne *NotificationEvent) EncodeBinary(w *io.BinWriter) {
w.WriteBytes(ne.ScriptHash[:])
ne.ScriptHash.EncodeBinary(w)
vm.EncodeBinaryStackItem(ne.Item, w)
}
// DecodeBinary implements the Serializable interface.
func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) {
r.ReadBytes(ne.ScriptHash[:])
ne.ScriptHash.DecodeBinary(r)
ne.Item = vm.DecodeBinaryStackItem(r)
}

View file

@ -57,7 +57,7 @@ func (tx *RegisterTX) DecodeBinary(br *io.BinReader) {
tx.Owner.DecodeBinary(br)
br.ReadBytes(tx.Admin[:])
tx.Admin.DecodeBinary(br)
}
// EncodeBinary implements Serializable interface.
@ -67,7 +67,7 @@ func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) {
tx.Amount.EncodeBinary(bw)
bw.WriteB(byte(tx.Precision))
bw.WriteBytes(tx.Owner.Bytes())
bw.WriteBytes(tx.Admin[:])
tx.Admin.EncodeBinary(bw)
}
// registeredAsset is a wrapper for RegisterTransaction

View file

@ -112,7 +112,7 @@ func (t *Transaction) DecodeBinary(br *io.BinReader) {
t.Type = TXType(br.ReadB())
t.Version = uint8(br.ReadB())
t.Nonce = br.ReadU32LE()
br.ReadBytes(t.Sender[:])
t.Sender.DecodeBinary(br)
t.ValidUntilBlock = br.ReadU32LE()
t.decodeData(br)
@ -174,7 +174,7 @@ func (t *Transaction) encodeHashableFields(bw *io.BinWriter) {
bw.WriteB(byte(t.Type))
bw.WriteB(byte(t.Version))
bw.WriteU32LE(t.Nonce)
bw.WriteBytes(t.Sender[:])
t.Sender.EncodeBinary(bw)
bw.WriteU32LE(t.ValidUntilBlock)
// Underlying TXer.

View file

@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"strings"
"github.com/nspcc-dev/neo-go/pkg/io"
)
// Uint160Size is the size of Uint160 in bytes.
@ -131,3 +133,13 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
func (u Uint160) MarshalJSON() ([]byte, error) {
return []byte(`"0x` + u.StringLE() + `"`), nil
}
// EncodeBinary implements Serializable interface.
func (u *Uint160) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(u[:])
}
// DecodeBinary implements Serializable interface.
func (u *Uint160) DecodeBinary(br *io.BinReader) {
br.ReadBytes(u[:])
}