io: refactoring for using WriteVarBytes instead of WriteLE

goal is to be consistent with C# implementation.
For writing []byte WriteBytes used and for byte - WriteVarByte.
This commit is contained in:
Vsevolod Brekelov 2019-11-22 13:34:06 +03:00
parent d02673c112
commit 03ff2976ed
20 changed files with 33 additions and 34 deletions

View file

@ -177,8 +177,7 @@ func dumpDB(ctx *cli.Context) error {
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter) b.EncodeBinary(buf.BinWriter)
bytes := buf.Bytes() bytes := buf.Bytes()
writer.WriteLE(uint32(len(bytes))) writer.WriteVarBytes(bytes)
writer.WriteLE(bytes)
if writer.Err != nil { if writer.Err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }

View file

@ -169,7 +169,7 @@ func (p Payload) EncodeBinaryUnsigned(w *io.BinWriter) {
ww := io.NewBufBinWriter() ww := io.NewBufBinWriter()
p.message.EncodeBinary(ww.BinWriter) p.message.EncodeBinary(ww.BinWriter)
w.WriteBytes(ww.Bytes()) w.WriteVarBytes(ww.Bytes())
} }
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.

View file

@ -100,7 +100,7 @@ func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) {
w.WriteLE(p.ValidatorIndex) w.WriteLE(p.ValidatorIndex)
w.WriteLE(p.OriginalViewNumber) w.WriteLE(p.OriginalViewNumber)
w.WriteLE(p.Timestamp) w.WriteLE(p.Timestamp)
w.WriteBytes(p.InvocationScript) w.WriteVarBytes(p.InvocationScript)
} }
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
@ -116,7 +116,7 @@ func (p *commitCompact) EncodeBinary(w *io.BinWriter) {
w.WriteLE(p.ViewNumber) w.WriteLE(p.ViewNumber)
w.WriteLE(p.ValidatorIndex) w.WriteLE(p.ValidatorIndex)
w.WriteBE(p.Signature) w.WriteBE(p.Signature)
w.WriteBytes(p.InvocationScript) w.WriteVarBytes(p.InvocationScript)
} }
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
@ -128,7 +128,7 @@ func (p *preparationCompact) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (p *preparationCompact) EncodeBinary(w *io.BinWriter) { func (p *preparationCompact) EncodeBinary(w *io.BinWriter) {
w.WriteLE(p.ValidatorIndex) w.WriteLE(p.ValidatorIndex)
w.WriteBytes(p.InvocationScript) w.WriteVarBytes(p.InvocationScript)
} }
// AddPayload implements payload.RecoveryMessage interface. // AddPayload implements payload.RecoveryMessage interface.

View file

@ -1508,7 +1508,7 @@ func (bc *Blockchain) verifyBlockWitnesses(block *Block, prevHeader *Header) err
func hashAndIndexToBytes(h util.Uint256, index uint32) []byte { func hashAndIndexToBytes(h util.Uint256, index uint32) []byte {
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
buf.WriteLE(h.BytesReverse()) buf.WriteBytes(h.BytesReverse())
buf.WriteLE(index) buf.WriteLE(index)
return buf.Bytes() return buf.Bytes()
} }

View file

@ -69,7 +69,7 @@ func (state *BlockChainState) storeAsBlock(block *Block, sysFee uint32) error {
if err != nil { if err != nil {
return err return err
} }
buf.WriteLE(b) buf.WriteBytes(b)
if buf.Err != nil { if buf.Err != nil {
return buf.Err return buf.Err
} }
@ -79,7 +79,7 @@ func (state *BlockChainState) storeAsBlock(block *Block, sysFee uint32) error {
// storeAsCurrentBlock stores the given block witch prefix SYSCurrentBlock. // storeAsCurrentBlock stores the given block witch prefix SYSCurrentBlock.
func (state *BlockChainState) storeAsCurrentBlock(block *Block) error { func (state *BlockChainState) storeAsCurrentBlock(block *Block) error {
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
buf.WriteLE(block.Hash().BytesReverse()) buf.WriteBytes(block.Hash().BytesReverse())
buf.WriteLE(block.Index) buf.WriteLE(block.Index)
return state.store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes()) return state.store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes())
} }

View file

@ -52,7 +52,7 @@ func (cs *ContractState) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (cs *ContractState) EncodeBinary(bw *io.BinWriter) { func (cs *ContractState) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(cs.Script) bw.WriteVarBytes(cs.Script)
bw.WriteArray(cs.ParamList) bw.WriteArray(cs.ParamList)
bw.WriteLE(cs.ReturnType) bw.WriteLE(cs.ReturnType)
bw.WriteLE(cs.Properties) bw.WriteLE(cs.Properties)

View file

@ -53,7 +53,7 @@ func deleteStorageItemInStore(s storage.Store, scripthash util.Uint160, key []by
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (si *StorageItem) EncodeBinary(w *io.BinWriter) { func (si *StorageItem) EncodeBinary(w *io.BinWriter) {
w.WriteBytes(si.Value) w.WriteVarBytes(si.Value)
w.WriteLE(si.IsConst) w.WriteLE(si.IsConst)
} }

View file

@ -55,18 +55,18 @@ func (attr *Attribute) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(&attr.Usage) bw.WriteLE(&attr.Usage)
switch attr.Usage { switch attr.Usage {
case ECDH02, ECDH03: case ECDH02, ECDH03:
bw.WriteLE(attr.Data[1:]) bw.WriteBytes(attr.Data[1:])
case Description, Remark, Remark1, Remark2, Remark3, Remark4, case Description, Remark, Remark1, Remark2, Remark3, Remark4,
Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11, Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11,
Remark12, Remark13, Remark14, Remark15: Remark12, Remark13, Remark14, Remark15:
bw.WriteBytes(attr.Data) bw.WriteVarBytes(attr.Data)
case DescriptionURL: case DescriptionURL:
var urllen = uint8(len(attr.Data)) var urllen = uint8(len(attr.Data))
bw.WriteLE(urllen) bw.WriteLE(urllen)
fallthrough fallthrough
case Script, ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5, Hash6, case Script, ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5, Hash6,
Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15: Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15:
bw.WriteLE(attr.Data) bw.WriteBytes(attr.Data)
default: default:
bw.Err = fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage) bw.Err = fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage)
} }

View file

@ -45,7 +45,7 @@ func (tx *InvocationTX) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) { func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(tx.Script) bw.WriteVarBytes(tx.Script)
if tx.Version >= 1 { if tx.Version >= 1 {
bw.WriteLE(tx.Gas) bw.WriteLE(tx.Gas)
} }

View file

@ -51,7 +51,7 @@ func (tx *PublishTX) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) { func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(tx.Script) bw.WriteVarBytes(tx.Script)
bw.WriteVarUint(uint64(len(tx.ParamList))) bw.WriteVarUint(uint64(len(tx.ParamList)))
for _, param := range tx.ParamList { for _, param := range tx.ParamList {
bw.WriteLE(uint8(param)) bw.WriteLE(uint8(param))

View file

@ -49,6 +49,6 @@ func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteString(tx.Name) bw.WriteString(tx.Name)
bw.WriteLE(tx.Amount) bw.WriteLE(tx.Amount)
bw.WriteLE(tx.Precision) bw.WriteLE(tx.Precision)
bw.WriteLE(tx.Owner.Bytes()) bw.WriteBytes(tx.Owner.Bytes())
bw.WriteLE(tx.Admin) bw.WriteLE(tx.Admin)
} }

View file

@ -33,7 +33,7 @@ func (s *StateDescriptor) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) { func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) {
w.WriteLE(s.Type) w.WriteLE(s.Type)
w.WriteBytes(s.Key) w.WriteVarBytes(s.Key)
w.WriteBytes(s.Value) w.WriteVarBytes(s.Value)
w.WriteString(s.Field) w.WriteString(s.Field)
} }

View file

@ -23,8 +23,8 @@ func (w *Witness) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (w *Witness) EncodeBinary(bw *io.BinWriter) { func (w *Witness) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(w.InvocationScript) bw.WriteVarBytes(w.InvocationScript)
bw.WriteBytes(w.VerificationScript) bw.WriteVarBytes(w.VerificationScript)
} }
// MarshalJSON implements the json marshaller interface. // MarshalJSON implements the json marshaller interface.

View file

@ -219,7 +219,7 @@ func (p *PublicKey) DecodeBinary(r *io.BinReader) {
// EncodeBinary encodes a PublicKey to the given BinWriter. // EncodeBinary encodes a PublicKey to the given BinWriter.
func (p *PublicKey) EncodeBinary(w *io.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. // Signature returns a NEO-specific hash of the key.

View file

@ -93,18 +93,18 @@ func (w *BinWriter) WriteVarUint(val uint64) {
} }
// WriteVarBytes writes a variable byte into the underlying io.Writer without prefix. // WriteBytes writes a variable byte into the underlying io.Writer without prefix.
func (w *BinWriter) WriteVarBytes(b []byte) { func (w *BinWriter) WriteBytes(b []byte) {
w.WriteLE(b) w.WriteLE(b)
} }
// WriteBytes writes a variable length byte array into the underlying io.Writer. // WriteVarBytes writes a variable length byte array into the underlying io.Writer.
func (w *BinWriter) WriteBytes(b []byte) { func (w *BinWriter) WriteVarBytes(b []byte) {
w.WriteVarUint(uint64(len(b))) w.WriteVarUint(uint64(len(b)))
w.WriteLE(b) w.WriteLE(b)
} }
// WriteString writes a variable length string into the underlying io.Writer. // WriteString writes a variable length string into the underlying io.Writer.
func (w *BinWriter) WriteString(s string) { func (w *BinWriter) WriteString(s string) {
w.WriteBytes([]byte(s)) w.WriteVarBytes([]byte(s))
} }

View file

@ -69,7 +69,7 @@ func TestWriterErrHandling(t *testing.T) {
bw.WriteLE(uint32(0)) bw.WriteLE(uint32(0))
bw.WriteBE(uint32(0)) bw.WriteBE(uint32(0))
bw.WriteVarUint(0) bw.WriteVarUint(0)
bw.WriteBytes([]byte{0x55, 0xaa}) bw.WriteVarBytes([]byte{0x55, 0xaa})
bw.WriteString("neo") bw.WriteString("neo")
assert.NotNil(t, bw.Err) assert.NotNil(t, bw.Err)
} }

View file

@ -31,5 +31,5 @@ func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarUint(uint64(m.TxCount)) bw.WriteVarUint(uint64(m.TxCount))
bw.WriteArray(m.Hashes) bw.WriteArray(m.Hashes)
bw.WriteBytes(m.Flags) bw.WriteVarBytes(m.Flags)
} }

View file

@ -73,7 +73,7 @@ func (p *Version) EncodeBinary(br *io.BinWriter) {
br.WriteLE(p.Port) br.WriteLE(p.Port)
br.WriteLE(p.Nonce) br.WriteLE(p.Nonce)
br.WriteBytes(p.UserAgent) br.WriteVarBytes(p.UserAgent)
br.WriteLE(p.StartHeight) br.WriteLE(p.StartHeight)
br.WriteLE(&p.Relay) br.WriteLE(&p.Relay)
} }

View file

@ -13,8 +13,8 @@ import (
// emit a VM Instruction with data to the given buffer. // emit a VM Instruction with data to the given buffer.
func emit(w *io.BufBinWriter, instr vm.Instruction, b []byte) { func emit(w *io.BufBinWriter, instr vm.Instruction, b []byte) {
w.WriteLE(byte(instr)) emitOpcode(w, instr)
w.WriteVarBytes(b) w.WriteBytes(b)
} }
// emitOpcode emits a single VM Instruction the given buffer. // emitOpcode emits a single VM Instruction the given buffer.

View file

@ -44,13 +44,13 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
switch t := item.(type) { switch t := item.(type) {
case *ByteArrayItem: case *ByteArrayItem:
w.WriteLE(byte(byteArrayT)) w.WriteLE(byte(byteArrayT))
w.WriteBytes(t.value) w.WriteVarBytes(t.value)
case *BoolItem: case *BoolItem:
w.WriteLE(byte(booleanT)) w.WriteLE(byte(booleanT))
w.WriteLE(t.value) w.WriteLE(t.value)
case *BigIntegerItem: case *BigIntegerItem:
w.WriteLE(byte(integerT)) w.WriteLE(byte(integerT))
w.WriteBytes(t.Bytes()) w.WriteVarBytes(t.Bytes())
case *InteropItem: case *InteropItem:
w.Err = errors.New("not supported") w.Err = errors.New("not supported")
case *ArrayItem, *StructItem: case *ArrayItem, *StructItem: