pkg/network: convert to using binaryReader/Writer
This commit is contained in:
parent
07096a551b
commit
459542a978
7 changed files with 98 additions and 142 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -147,17 +148,13 @@ func (m *Message) CommandType() CommandType {
|
||||||
|
|
||||||
// Decode a Message from the given reader.
|
// Decode a Message from the given reader.
|
||||||
func (m *Message) Decode(r io.Reader) error {
|
func (m *Message) Decode(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &m.Magic); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&m.Magic)
|
||||||
}
|
br.ReadLE(&m.Command)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &m.Command); err != nil {
|
br.ReadLE(&m.Length)
|
||||||
return err
|
br.ReadLE(&m.Checksum)
|
||||||
}
|
if br.Err != nil {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &m.Length); err != nil {
|
return br.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &m.Checksum); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
// return if their is no payload.
|
// return if their is no payload.
|
||||||
if m.Length == 0 {
|
if m.Length == 0 {
|
||||||
|
@ -233,17 +230,13 @@ func (m *Message) decodePayload(r io.Reader) error {
|
||||||
|
|
||||||
// Encode a Message to any given io.Writer.
|
// Encode a Message to any given io.Writer.
|
||||||
func (m *Message) Encode(w io.Writer) error {
|
func (m *Message) Encode(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, m.Magic); err != nil {
|
br := util.BinWriter{W: w}
|
||||||
return err
|
br.WriteLE(m.Magic)
|
||||||
}
|
br.WriteLE(m.Command)
|
||||||
if err := binary.Write(w, binary.LittleEndian, m.Command); err != nil {
|
br.WriteLE(m.Length)
|
||||||
return err
|
br.WriteLE(m.Checksum)
|
||||||
}
|
if br.Err != nil {
|
||||||
if err := binary.Write(w, binary.LittleEndian, m.Length); err != nil {
|
return br.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, m.Checksum); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if m.Payload != nil {
|
if m.Payload != nil {
|
||||||
return m.Payload.EncodeBinary(w)
|
return m.Payload.EncodeBinary(w)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package payload
|
package payload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -26,30 +25,22 @@ func NewAddressAndTime(e util.Endpoint, t time.Time) *AddressAndTime {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (p *AddressAndTime) DecodeBinary(r io.Reader) error {
|
func (p *AddressAndTime) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Timestamp); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&p.Timestamp)
|
||||||
}
|
br.ReadLE(&p.Services)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Services); err != nil {
|
br.ReadBE(&p.Endpoint.IP)
|
||||||
return err
|
br.ReadBE(&p.Endpoint.Port)
|
||||||
}
|
return br.Err
|
||||||
if err := binary.Read(r, binary.BigEndian, &p.Endpoint.IP); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Read(r, binary.BigEndian, &p.Endpoint.Port)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (p *AddressAndTime) EncodeBinary(w io.Writer) error {
|
func (p *AddressAndTime) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Timestamp); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(p.Timestamp)
|
||||||
}
|
bw.WriteLE(p.Services)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Services); err != nil {
|
bw.WriteBE(p.Endpoint.IP)
|
||||||
return err
|
bw.WriteBE(p.Endpoint.Port)
|
||||||
}
|
return bw.Err
|
||||||
if err := binary.Write(w, binary.BigEndian, p.Endpoint.IP); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.BigEndian, p.Endpoint.Port)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressList is a list with AddrAndTime.
|
// AddressList is a list with AddrAndTime.
|
||||||
|
@ -59,7 +50,11 @@ type AddressList struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (p *AddressList) DecodeBinary(r io.Reader) error {
|
func (p *AddressList) DecodeBinary(r io.Reader) error {
|
||||||
listLen := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
listLen := br.ReadVarUint()
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
|
|
||||||
p.Addrs = make([]*AddressAndTime, listLen)
|
p.Addrs = make([]*AddressAndTime, listLen)
|
||||||
for i := 0; i < int(listLen); i++ {
|
for i := 0; i < int(listLen); i++ {
|
||||||
|
@ -73,8 +68,10 @@ func (p *AddressList) DecodeBinary(r io.Reader) error {
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (p *AddressList) EncodeBinary(w io.Writer) error {
|
func (p *AddressList) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(p.Addrs))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(p.Addrs)))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, addr := range p.Addrs {
|
for _, addr := range p.Addrs {
|
||||||
if err := addr.EncodeBinary(w); err != nil {
|
if err := addr.EncodeBinary(w); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package payload
|
package payload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -25,24 +24,22 @@ func NewGetBlocks(start []util.Uint256, stop util.Uint256) *GetBlocks {
|
||||||
|
|
||||||
// DecodeBinary implements the payload interface.
|
// DecodeBinary implements the payload interface.
|
||||||
func (p *GetBlocks) DecodeBinary(r io.Reader) error {
|
func (p *GetBlocks) DecodeBinary(r io.Reader) error {
|
||||||
lenStart := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lenStart := br.ReadVarUint()
|
||||||
p.HashStart = make([]util.Uint256, lenStart)
|
p.HashStart = make([]util.Uint256, lenStart)
|
||||||
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.HashStart); err != nil {
|
br.ReadLE(&p.HashStart)
|
||||||
return err
|
br.ReadLE(&p.HashStop)
|
||||||
}
|
return br.Err
|
||||||
return binary.Read(r, binary.LittleEndian, &p.HashStop)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the payload interface.
|
// EncodeBinary implements the payload interface.
|
||||||
func (p *GetBlocks) EncodeBinary(w io.Writer) error {
|
func (p *GetBlocks) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(p.HashStart))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(p.HashStart)))
|
||||||
}
|
bw.WriteLE(p.HashStart)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.HashStart); err != nil {
|
bw.WriteLE(p.HashStop)
|
||||||
return err
|
return bw.Err
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, p.HashStop)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size implements the payload interface.
|
// Size implements the payload interface.
|
||||||
|
|
|
@ -14,7 +14,11 @@ type Headers struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (p *Headers) DecodeBinary(r io.Reader) error {
|
func (p *Headers) DecodeBinary(r io.Reader) error {
|
||||||
lenHeaders := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lenHeaders := br.ReadVarUint()
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
|
|
||||||
p.Hdrs = make([]*core.Header, lenHeaders)
|
p.Hdrs = make([]*core.Header, lenHeaders)
|
||||||
|
|
||||||
|
@ -31,9 +35,12 @@ func (p *Headers) DecodeBinary(r io.Reader) error {
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (p *Headers) EncodeBinary(w io.Writer) error {
|
func (p *Headers) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(p.Hdrs))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(p.Hdrs)))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, header := range p.Hdrs {
|
for _, header := range p.Hdrs {
|
||||||
if err := header.EncodeBinary(w); err != nil {
|
if err := header.EncodeBinary(w); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package payload
|
package payload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -58,36 +57,28 @@ func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (p *Inventory) DecodeBinary(r io.Reader) error {
|
func (p *Inventory) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Type); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&p.Type)
|
||||||
}
|
|
||||||
|
|
||||||
listLen := util.ReadVarUint(r)
|
listLen := br.ReadVarUint()
|
||||||
p.Hashes = make([]util.Uint256, listLen)
|
p.Hashes = make([]util.Uint256, listLen)
|
||||||
for i := 0; i < int(listLen); i++ {
|
for i := 0; i < int(listLen); i++ {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Hashes[i]); err != nil {
|
br.ReadLE(&p.Hashes[i])
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (p *Inventory) EncodeBinary(w io.Writer) error {
|
func (p *Inventory) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Type); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(p.Type)
|
||||||
}
|
|
||||||
|
|
||||||
listLen := len(p.Hashes)
|
listLen := len(p.Hashes)
|
||||||
if err := util.WriteVarUint(w, uint64(listLen)); err != nil {
|
bw.WriteVarUint(uint64(listLen))
|
||||||
return err
|
for i := 0; i < listLen; i++ {
|
||||||
}
|
bw.WriteLE(p.Hashes[i])
|
||||||
for i := 0; i < len(p.Hashes); i++ {
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Hashes[i]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package payload
|
package payload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/core"
|
"github.com/CityOfZion/neo-go/pkg/core"
|
||||||
|
@ -20,18 +19,16 @@ func (m *MerkleBlock) DecodeBinary(r io.Reader) error {
|
||||||
if err := m.BlockBase.DecodeBinary(r); err != nil {
|
if err := m.BlockBase.DecodeBinary(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
br := util.BinReader{R: r}
|
||||||
|
|
||||||
m.TxCount = int(util.ReadVarUint(r))
|
m.TxCount = int(br.ReadVarUint())
|
||||||
n := util.ReadVarUint(r)
|
n := br.ReadVarUint()
|
||||||
m.Hashes = make([]util.Uint256, n)
|
m.Hashes = make([]util.Uint256, n)
|
||||||
for i := 0; i < len(m.Hashes); i++ {
|
for i := 0; i < len(m.Hashes); i++ {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &m.Hashes[i]); err != nil {
|
br.ReadLE(&m.Hashes[i])
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var err error
|
m.Flags = br.ReadBytes()
|
||||||
m.Flags, err = util.ReadVarBytes(r)
|
return br.Err
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MerkleBlock) EncodeBinary(w io.Writer) error {
|
func (m *MerkleBlock) EncodeBinary(w io.Writer) error {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package payload
|
package payload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const minVersionSize = 27
|
const minVersionSize = 27
|
||||||
|
@ -53,63 +54,36 @@ func NewVersion(id uint32, p uint16, ua string, h uint32, r bool) *Version {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (p *Version) DecodeBinary(r io.Reader) error {
|
func (p *Version) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Version); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&p.Version)
|
||||||
}
|
br.ReadLE(&p.Services)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Services); err != nil {
|
br.ReadLE(&p.Timestamp)
|
||||||
return err
|
br.ReadLE(&p.Port)
|
||||||
}
|
br.ReadLE(&p.Nonce)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Timestamp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Port); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Nonce); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var lenUA uint8
|
var lenUA uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &lenUA); err != nil {
|
br.ReadLE(&lenUA)
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.UserAgent = make([]byte, lenUA)
|
p.UserAgent = make([]byte, lenUA)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.UserAgent); err != nil {
|
br.ReadLE(&p.UserAgent)
|
||||||
return err
|
br.ReadLE(&p.StartHeight)
|
||||||
}
|
br.ReadLE(&p.Relay)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.StartHeight); err != nil {
|
return br.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Read(r, binary.LittleEndian, &p.Relay)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (p *Version) EncodeBinary(w io.Writer) error {
|
func (p *Version) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Version); err != nil {
|
br := util.BinWriter{W: w}
|
||||||
return err
|
br.WriteLE(p.Version)
|
||||||
}
|
br.WriteLE(p.Services)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Services); err != nil {
|
br.WriteLE(p.Timestamp)
|
||||||
return err
|
br.WriteLE(p.Port)
|
||||||
}
|
br.WriteLE(p.Nonce)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Timestamp); err != nil {
|
|
||||||
return err
|
br.WriteLE(uint8(len(p.UserAgent)))
|
||||||
}
|
br.WriteLE(p.UserAgent)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Port); err != nil {
|
br.WriteLE(p.StartHeight)
|
||||||
return err
|
br.WriteLE(&p.Relay)
|
||||||
}
|
return br.Err
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Nonce); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, uint8(len(p.UserAgent))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.UserAgent); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.StartHeight); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, p.Relay)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size implements the payloader interface.
|
// Size implements the payloader interface.
|
||||||
|
|
Loading…
Reference in a new issue