util: implement io.Serializable for Uint256

This commit is contained in:
Evgenii Stratonikov 2019-11-14 11:07:23 +03:00
parent b16e56a47b
commit 2be18f91df
9 changed files with 48 additions and 54 deletions

View file

@ -20,10 +20,7 @@ func (p *prepareRequest) EncodeBinary(w *io.BinWriter) {
w.WriteLE(p.Timestamp) w.WriteLE(p.Timestamp)
w.WriteLE(p.Nonce) w.WriteLE(p.Nonce)
w.WriteBE(p.NextConsensus[:]) w.WriteBE(p.NextConsensus[:])
w.WriteVarUint(uint64(len(p.TransactionHashes))) w.WriteArray(p.TransactionHashes)
for i := range p.TransactionHashes {
w.WriteBE(p.TransactionHashes[i][:])
}
p.MinerTransaction.EncodeBinary(w) p.MinerTransaction.EncodeBinary(w)
} }
@ -32,12 +29,6 @@ func (p *prepareRequest) DecodeBinary(r *io.BinReader) {
r.ReadLE(&p.Timestamp) r.ReadLE(&p.Timestamp)
r.ReadLE(&p.Nonce) r.ReadLE(&p.Nonce)
r.ReadBE(p.NextConsensus[:]) r.ReadBE(p.NextConsensus[:])
r.ReadArray(&p.TransactionHashes)
lenHashes := r.ReadVarUint()
p.TransactionHashes = make([]util.Uint256, lenHashes)
for i := range p.TransactionHashes {
r.ReadBE(p.TransactionHashes[i][:])
}
p.MinerTransaction.DecodeBinary(r) p.MinerTransaction.DecodeBinary(r)
} }

View file

@ -135,10 +135,7 @@ func (b *Block) DecodeBinary(br *io.BinReader) {
// Serializable interface. // Serializable interface.
func (b *Block) EncodeBinary(bw *io.BinWriter) { func (b *Block) EncodeBinary(bw *io.BinWriter) {
b.BlockBase.EncodeBinary(bw) b.BlockBase.EncodeBinary(bw)
bw.WriteVarUint(uint64(len(b.Transactions))) bw.WriteArray(b.Transactions)
for _, tx := range b.Transactions {
tx.EncodeBinary(bw)
}
} }
// Compare implements the queue Item interface. // Compare implements the queue Item interface.

View file

@ -58,10 +58,6 @@ func (l *HeaderHashList) Slice(start, end int) []util.Uint256 {
// WriteTo writes n underlying hashes to the given BinWriter // WriteTo writes n underlying hashes to the given BinWriter
// starting from start. // starting from start.
func (l *HeaderHashList) Write(bw *io.BinWriter, start, n int) error { func (l *HeaderHashList) Write(bw *io.BinWriter, start, n int) error {
bw.WriteVarUint(uint64(n)) bw.WriteArray(l.Slice(start, start+n))
hashes := l.Slice(start, start+n)
for _, hash := range hashes {
bw.WriteLE(hash)
}
return bw.Err return bw.Err
} }

View file

@ -1,9 +1,10 @@
package io package io_test
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -13,18 +14,18 @@ type smthSerializable struct {
some [42]byte some [42]byte
} }
func (*smthSerializable) DecodeBinary(*BinReader) {} func (*smthSerializable) DecodeBinary(*io.BinReader) {}
func (ss *smthSerializable) EncodeBinary(bw *BinWriter) { func (ss *smthSerializable) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(ss.some) bw.WriteLE(ss.some)
} }
// Mock structure that gives error in EncodeBinary(). // Mock structure that gives error in EncodeBinary().
type smthNotReallySerializable struct{} type smthNotReallySerializable struct{}
func (*smthNotReallySerializable) DecodeBinary(*BinReader) {} func (*smthNotReallySerializable) DecodeBinary(*io.BinReader) {}
func (*smthNotReallySerializable) EncodeBinary(bw *BinWriter) { func (*smthNotReallySerializable) EncodeBinary(bw *io.BinWriter) {
bw.Err = fmt.Errorf("smth bad happened in smthNotReallySerializable") bw.Err = fmt.Errorf("smth bad happened in smthNotReallySerializable")
} }
@ -182,7 +183,7 @@ func TestVarSize(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(fmt.Sprintf("run: %s", tc.name), func(t *testing.T) { t.Run(fmt.Sprintf("run: %s", tc.name), func(t *testing.T) {
result := GetVarSize(tc.variable) result := io.GetVarSize(tc.variable)
assert.Equal(t, tc.expected, result) assert.Equal(t, tc.expected, result)
}) })
} }
@ -194,7 +195,7 @@ func panicVarSize(t *testing.T, v interface{}) {
assert.NotNil(t, r) assert.NotNil(t, r)
}() }()
_ = GetVarSize(v) _ = io.GetVarSize(v)
// this should never execute // this should never execute
assert.Nil(t, t) assert.Nil(t, t)
} }

View file

@ -23,16 +23,12 @@ func NewGetBlocks(start []util.Uint256, stop util.Uint256) *GetBlocks {
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (p *GetBlocks) DecodeBinary(br *io.BinReader) { func (p *GetBlocks) DecodeBinary(br *io.BinReader) {
lenStart := br.ReadVarUint() br.ReadArray(&p.HashStart)
p.HashStart = make([]util.Uint256, lenStart)
br.ReadLE(&p.HashStart)
br.ReadLE(&p.HashStop) br.ReadLE(&p.HashStop)
} }
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (p *GetBlocks) EncodeBinary(bw *io.BinWriter) { func (p *GetBlocks) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarUint(uint64(len(p.HashStart))) bw.WriteArray(p.HashStart)
bw.WriteLE(p.HashStart)
bw.WriteLE(p.HashStop) bw.WriteLE(p.HashStop)
} }

View file

@ -57,21 +57,11 @@ func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (p *Inventory) DecodeBinary(br *io.BinReader) { func (p *Inventory) DecodeBinary(br *io.BinReader) {
br.ReadLE(&p.Type) br.ReadLE(&p.Type)
br.ReadArray(&p.Hashes)
listLen := br.ReadVarUint()
p.Hashes = make([]util.Uint256, listLen)
for i := 0; i < int(listLen); i++ {
br.ReadLE(&p.Hashes[i])
}
} }
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (p *Inventory) EncodeBinary(bw *io.BinWriter) { func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(p.Type) bw.WriteLE(p.Type)
bw.WriteArray(p.Hashes)
listLen := len(p.Hashes)
bw.WriteVarUint(uint64(listLen))
for i := 0; i < listLen; i++ {
bw.WriteLE(p.Hashes[i])
}
} }

View file

@ -20,11 +20,7 @@ func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
m.BlockBase.DecodeBinary(br) m.BlockBase.DecodeBinary(br)
m.TxCount = int(br.ReadVarUint()) m.TxCount = int(br.ReadVarUint())
n := br.ReadVarUint() br.ReadArray(&m.Hashes)
m.Hashes = make([]util.Uint256, n)
for i := 0; i < len(m.Hashes); i++ {
br.ReadLE(&m.Hashes[i])
}
m.Flags = br.ReadBytes() m.Flags = br.ReadBytes()
} }
@ -34,9 +30,6 @@ func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
m.BlockBase.EncodeBinary(bw) m.BlockBase.EncodeBinary(bw)
bw.WriteVarUint(uint64(m.TxCount)) bw.WriteVarUint(uint64(m.TxCount))
bw.WriteVarUint(uint64(len(m.Hashes))) bw.WriteArray(m.Hashes)
for i := 0; i < len(m.Hashes); i++ {
bw.WriteLE(m.Hashes[i])
}
bw.WriteBytes(m.Flags) bw.WriteBytes(m.Flags)
} }

View file

@ -6,6 +6,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings" "strings"
"github.com/CityOfZion/neo-go/pkg/io"
) )
// Uint256Size is the size of Uint256 in bytes. // Uint256Size is the size of Uint256 in bytes.
@ -93,3 +95,13 @@ func (u Uint256) MarshalJSON() ([]byte, error) {
// -1 implies u < other. // -1 implies u < other.
// 0 implies u = other. // 0 implies u = other.
func (u Uint256) CompareTo(other Uint256) int { return bytes.Compare(u[:], other[:]) } func (u Uint256) CompareTo(other Uint256) int { return bytes.Compare(u[:], other[:]) }
// EncodeBinary implements io.Serializable interface.
func (u Uint256) EncodeBinary(w *io.BinWriter) {
w.WriteBE(u)
}
// DecodeBinary implements io.Serializable interface.
func (u *Uint256) DecodeBinary(r *io.BinReader) {
r.ReadBE(u[:])
}

View file

@ -4,7 +4,9 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestUint256UnmarshalJSON(t *testing.T) { func TestUint256UnmarshalJSON(t *testing.T) {
@ -75,3 +77,19 @@ func TestUInt256Equals(t *testing.T) {
t.Fatalf("%s and %s must be equal", ua, ua) t.Fatalf("%s and %s must be equal", ua, ua)
} }
} }
func TestUint256_Serializable(t *testing.T) {
a := Uint256{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
}
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
var b Uint256
r := io.NewBinReaderFromBuf(w.Bytes())
b.DecodeBinary(r)
require.Equal(t, a, b)
}