diff --git a/cli/server/server.go b/cli/server/server.go index b7290c4b0..d11c5ce7b 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -177,8 +177,7 @@ func dumpDB(ctx *cli.Context) error { buf := io.NewBufBinWriter() b.EncodeBinary(buf.BinWriter) bytes := buf.Bytes() - writer.WriteLE(uint32(len(bytes))) - writer.WriteLE(bytes) + writer.WriteVarBytes(bytes) if writer.Err != nil { return cli.NewExitError(err, 1) } diff --git a/pkg/consensus/payload.go b/pkg/consensus/payload.go index 6740e9370..881d0bbf1 100644 --- a/pkg/consensus/payload.go +++ b/pkg/consensus/payload.go @@ -169,7 +169,7 @@ func (p Payload) EncodeBinaryUnsigned(w *io.BinWriter) { ww := io.NewBufBinWriter() p.message.EncodeBinary(ww.BinWriter) - w.WriteBytes(ww.Bytes()) + w.WriteVarBytes(ww.Bytes()) } // EncodeBinary implements io.Serializable interface. diff --git a/pkg/consensus/recovery_message.go b/pkg/consensus/recovery_message.go index 8e0d62533..de1ef594e 100644 --- a/pkg/consensus/recovery_message.go +++ b/pkg/consensus/recovery_message.go @@ -100,7 +100,7 @@ func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) { w.WriteLE(p.ValidatorIndex) w.WriteLE(p.OriginalViewNumber) w.WriteLE(p.Timestamp) - w.WriteBytes(p.InvocationScript) + w.WriteVarBytes(p.InvocationScript) } // DecodeBinary implements io.Serializable interface. @@ -116,7 +116,7 @@ func (p *commitCompact) EncodeBinary(w *io.BinWriter) { w.WriteLE(p.ViewNumber) w.WriteLE(p.ValidatorIndex) w.WriteBE(p.Signature) - w.WriteBytes(p.InvocationScript) + w.WriteVarBytes(p.InvocationScript) } // DecodeBinary implements io.Serializable interface. @@ -128,7 +128,7 @@ func (p *preparationCompact) DecodeBinary(r *io.BinReader) { // EncodeBinary implements io.Serializable interface. func (p *preparationCompact) EncodeBinary(w *io.BinWriter) { w.WriteLE(p.ValidatorIndex) - w.WriteBytes(p.InvocationScript) + w.WriteVarBytes(p.InvocationScript) } // AddPayload implements payload.RecoveryMessage interface. diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 2e589ca29..f47fb0060 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1508,7 +1508,7 @@ func (bc *Blockchain) verifyBlockWitnesses(block *Block, prevHeader *Header) err func hashAndIndexToBytes(h util.Uint256, index uint32) []byte { buf := io.NewBufBinWriter() - buf.WriteLE(h.BytesReverse()) + buf.WriteBytes(h.BytesReverse()) buf.WriteLE(index) return buf.Bytes() } diff --git a/pkg/core/blockchain_state.go b/pkg/core/blockchain_state.go index db13408f8..b2b00b030 100644 --- a/pkg/core/blockchain_state.go +++ b/pkg/core/blockchain_state.go @@ -69,7 +69,7 @@ func (state *BlockChainState) storeAsBlock(block *Block, sysFee uint32) error { if err != nil { return err } - buf.WriteLE(b) + buf.WriteBytes(b) if buf.Err != nil { return buf.Err } @@ -79,7 +79,7 @@ func (state *BlockChainState) storeAsBlock(block *Block, sysFee uint32) error { // storeAsCurrentBlock stores the given block witch prefix SYSCurrentBlock. func (state *BlockChainState) storeAsCurrentBlock(block *Block) error { buf := io.NewBufBinWriter() - buf.WriteLE(block.Hash().BytesReverse()) + buf.WriteBytes(block.Hash().BytesReverse()) buf.WriteLE(block.Index) return state.store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes()) } diff --git a/pkg/core/contract_state.go b/pkg/core/contract_state.go index e3efdce27..2b3a32138 100644 --- a/pkg/core/contract_state.go +++ b/pkg/core/contract_state.go @@ -52,7 +52,7 @@ func (cs *ContractState) DecodeBinary(br *io.BinReader) { // EncodeBinary implements Serializable interface. func (cs *ContractState) EncodeBinary(bw *io.BinWriter) { - bw.WriteBytes(cs.Script) + bw.WriteVarBytes(cs.Script) bw.WriteArray(cs.ParamList) bw.WriteLE(cs.ReturnType) bw.WriteLE(cs.Properties) diff --git a/pkg/core/storage_item.go b/pkg/core/storage_item.go index 2e6166a8b..a78de51dc 100644 --- a/pkg/core/storage_item.go +++ b/pkg/core/storage_item.go @@ -53,7 +53,7 @@ func deleteStorageItemInStore(s storage.Store, scripthash util.Uint160, key []by // EncodeBinary implements Serializable interface. func (si *StorageItem) EncodeBinary(w *io.BinWriter) { - w.WriteBytes(si.Value) + w.WriteVarBytes(si.Value) w.WriteLE(si.IsConst) } diff --git a/pkg/core/transaction/attribute.go b/pkg/core/transaction/attribute.go index 45883cb18..3cd92d333 100644 --- a/pkg/core/transaction/attribute.go +++ b/pkg/core/transaction/attribute.go @@ -55,18 +55,18 @@ func (attr *Attribute) EncodeBinary(bw *io.BinWriter) { bw.WriteLE(&attr.Usage) switch attr.Usage { case ECDH02, ECDH03: - bw.WriteLE(attr.Data[1:]) + bw.WriteBytes(attr.Data[1:]) case Description, Remark, Remark1, Remark2, Remark3, Remark4, Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11, Remark12, Remark13, Remark14, Remark15: - bw.WriteBytes(attr.Data) + bw.WriteVarBytes(attr.Data) case DescriptionURL: var urllen = uint8(len(attr.Data)) bw.WriteLE(urllen) fallthrough case Script, ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5, Hash6, Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15: - bw.WriteLE(attr.Data) + bw.WriteBytes(attr.Data) default: bw.Err = fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage) } diff --git a/pkg/core/transaction/invocation.go b/pkg/core/transaction/invocation.go index f303ddf16..6276a1a28 100644 --- a/pkg/core/transaction/invocation.go +++ b/pkg/core/transaction/invocation.go @@ -45,7 +45,7 @@ func (tx *InvocationTX) DecodeBinary(br *io.BinReader) { // EncodeBinary implements Serializable interface. func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) { - bw.WriteBytes(tx.Script) + bw.WriteVarBytes(tx.Script) if tx.Version >= 1 { bw.WriteLE(tx.Gas) } diff --git a/pkg/core/transaction/publish.go b/pkg/core/transaction/publish.go index e46ce6a56..131a4ac9a 100644 --- a/pkg/core/transaction/publish.go +++ b/pkg/core/transaction/publish.go @@ -51,7 +51,7 @@ func (tx *PublishTX) DecodeBinary(br *io.BinReader) { // EncodeBinary implements Serializable interface. func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) { - bw.WriteBytes(tx.Script) + bw.WriteVarBytes(tx.Script) bw.WriteVarUint(uint64(len(tx.ParamList))) for _, param := range tx.ParamList { bw.WriteLE(uint8(param)) diff --git a/pkg/core/transaction/register.go b/pkg/core/transaction/register.go index 5ca940ac3..e8d754eb3 100644 --- a/pkg/core/transaction/register.go +++ b/pkg/core/transaction/register.go @@ -49,6 +49,6 @@ func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) { bw.WriteString(tx.Name) bw.WriteLE(tx.Amount) bw.WriteLE(tx.Precision) - bw.WriteLE(tx.Owner.Bytes()) + bw.WriteBytes(tx.Owner.Bytes()) bw.WriteLE(tx.Admin) } diff --git a/pkg/core/transaction/state_descriptor.go b/pkg/core/transaction/state_descriptor.go index 087ea5baf..c5d3cc958 100644 --- a/pkg/core/transaction/state_descriptor.go +++ b/pkg/core/transaction/state_descriptor.go @@ -33,7 +33,7 @@ func (s *StateDescriptor) DecodeBinary(r *io.BinReader) { // EncodeBinary implements Serializable interface. func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) { w.WriteLE(s.Type) - w.WriteBytes(s.Key) - w.WriteBytes(s.Value) + w.WriteVarBytes(s.Key) + w.WriteVarBytes(s.Value) w.WriteString(s.Field) } diff --git a/pkg/core/transaction/witness.go b/pkg/core/transaction/witness.go index 8640dca74..bdbecb80d 100644 --- a/pkg/core/transaction/witness.go +++ b/pkg/core/transaction/witness.go @@ -23,8 +23,8 @@ func (w *Witness) DecodeBinary(br *io.BinReader) { // EncodeBinary implements Serializable interface. func (w *Witness) EncodeBinary(bw *io.BinWriter) { - bw.WriteBytes(w.InvocationScript) - bw.WriteBytes(w.VerificationScript) + bw.WriteVarBytes(w.InvocationScript) + bw.WriteVarBytes(w.VerificationScript) } // MarshalJSON implements the json marshaller interface. diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index 8bae4a652..dad925ffd 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -219,7 +219,7 @@ func (p *PublicKey) DecodeBinary(r *io.BinReader) { // EncodeBinary encodes a PublicKey to the given BinWriter. func (p *PublicKey) EncodeBinary(w *io.BinWriter) { - w.WriteLE(p.Bytes()) + w.WriteBytes(p.Bytes()) } // Signature returns a NEO-specific hash of the key. diff --git a/pkg/io/binaryWriter.go b/pkg/io/binaryWriter.go index d3d5503e1..b5aee0d0c 100644 --- a/pkg/io/binaryWriter.go +++ b/pkg/io/binaryWriter.go @@ -93,18 +93,18 @@ func (w *BinWriter) WriteVarUint(val uint64) { } -// WriteVarBytes writes a variable byte into the underlying io.Writer without prefix. -func (w *BinWriter) WriteVarBytes(b []byte) { +// WriteBytes writes a variable byte into the underlying io.Writer without prefix. +func (w *BinWriter) WriteBytes(b []byte) { w.WriteLE(b) } -// WriteBytes writes a variable length byte array into the underlying io.Writer. -func (w *BinWriter) WriteBytes(b []byte) { +// WriteVarBytes writes a variable length byte array into the underlying io.Writer. +func (w *BinWriter) WriteVarBytes(b []byte) { w.WriteVarUint(uint64(len(b))) w.WriteLE(b) } // WriteString writes a variable length string into the underlying io.Writer. func (w *BinWriter) WriteString(s string) { - w.WriteBytes([]byte(s)) + w.WriteVarBytes([]byte(s)) } diff --git a/pkg/io/binaryrw_test.go b/pkg/io/binaryrw_test.go index 4cdfbac2d..213b394e0 100644 --- a/pkg/io/binaryrw_test.go +++ b/pkg/io/binaryrw_test.go @@ -69,7 +69,7 @@ func TestWriterErrHandling(t *testing.T) { bw.WriteLE(uint32(0)) bw.WriteBE(uint32(0)) bw.WriteVarUint(0) - bw.WriteBytes([]byte{0x55, 0xaa}) + bw.WriteVarBytes([]byte{0x55, 0xaa}) bw.WriteString("neo") assert.NotNil(t, bw.Err) } diff --git a/pkg/network/payload/merkleblock.go b/pkg/network/payload/merkleblock.go index dad96f305..67142c2b4 100644 --- a/pkg/network/payload/merkleblock.go +++ b/pkg/network/payload/merkleblock.go @@ -31,5 +31,5 @@ func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) { bw.WriteVarUint(uint64(m.TxCount)) bw.WriteArray(m.Hashes) - bw.WriteBytes(m.Flags) + bw.WriteVarBytes(m.Flags) } diff --git a/pkg/network/payload/version.go b/pkg/network/payload/version.go index ead40df01..b221eef63 100644 --- a/pkg/network/payload/version.go +++ b/pkg/network/payload/version.go @@ -73,7 +73,7 @@ func (p *Version) EncodeBinary(br *io.BinWriter) { br.WriteLE(p.Port) br.WriteLE(p.Nonce) - br.WriteBytes(p.UserAgent) + br.WriteVarBytes(p.UserAgent) br.WriteLE(p.StartHeight) br.WriteLE(&p.Relay) } diff --git a/pkg/vm/compiler/emit.go b/pkg/vm/compiler/emit.go index 296adea22..6cf70206c 100644 --- a/pkg/vm/compiler/emit.go +++ b/pkg/vm/compiler/emit.go @@ -13,8 +13,8 @@ import ( // emit a VM Instruction with data to the given buffer. func emit(w *io.BufBinWriter, instr vm.Instruction, b []byte) { - w.WriteLE(byte(instr)) - w.WriteVarBytes(b) + emitOpcode(w, instr) + w.WriteBytes(b) } // emitOpcode emits a single VM Instruction the given buffer. diff --git a/pkg/vm/serialization.go b/pkg/vm/serialization.go index 81562df0d..5434e8b5a 100644 --- a/pkg/vm/serialization.go +++ b/pkg/vm/serialization.go @@ -44,13 +44,13 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) { switch t := item.(type) { case *ByteArrayItem: w.WriteLE(byte(byteArrayT)) - w.WriteBytes(t.value) + w.WriteVarBytes(t.value) case *BoolItem: w.WriteLE(byte(booleanT)) w.WriteLE(t.value) case *BigIntegerItem: w.WriteLE(byte(integerT)) - w.WriteBytes(t.Bytes()) + w.WriteVarBytes(t.Bytes()) case *InteropItem: w.Err = errors.New("not supported") case *ArrayItem, *StructItem: