From d1ec01c45e0787bae9b544d190c155c589d6cb6e Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 4 May 2020 10:35:56 +0300 Subject: [PATCH] util: implement Serializable interface over Uint160 --- pkg/core/state/account.go | 4 ++-- pkg/core/state/asset.go | 12 ++++++------ pkg/core/state/notification_event.go | 4 ++-- pkg/core/transaction/register.go | 4 ++-- pkg/core/transaction/transaction.go | 4 ++-- pkg/util/uint160.go | 12 ++++++++++++ 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/pkg/core/state/account.go b/pkg/core/state/account.go index 2fc3a4f66..4e65ec27e 100644 --- a/pkg/core/state/account.go +++ b/pkg/core/state/account.go @@ -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))) diff --git a/pkg/core/state/asset.go b/pkg/core/state/asset.go index 0b624516b..d59deba68 100644 --- a/pkg/core/state/asset.go +++ b/pkg/core/state/asset.go @@ -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) } diff --git a/pkg/core/state/notification_event.go b/pkg/core/state/notification_event.go index 2599f781e..06e634464 100644 --- a/pkg/core/state/notification_event.go +++ b/pkg/core/state/notification_event.go @@ -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) } diff --git a/pkg/core/transaction/register.go b/pkg/core/transaction/register.go index 0351b040d..c17eccb26 100644 --- a/pkg/core/transaction/register.go +++ b/pkg/core/transaction/register.go @@ -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 diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index cc83cb086..185ee1e57 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -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. diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 1e457d323..736d1fe78 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -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[:]) +}