forked from TrueCloudLab/neoneo-go
Merge pull request #322 from nspcc-dev/drop-redundant-dev-code-part-3
Drop redundant dev code part 3, refs. #307, #315 and #318.
This commit is contained in:
commit
c6d7ef2de4
35 changed files with 515 additions and 922 deletions
|
@ -1,29 +0,0 @@
|
||||||
package command
|
|
||||||
|
|
||||||
// Size of command field in bytes
|
|
||||||
const (
|
|
||||||
Size = 12
|
|
||||||
)
|
|
||||||
|
|
||||||
// Type represents the type of a message command.
|
|
||||||
type Type string
|
|
||||||
|
|
||||||
// Valid protocol commands used to send between nodes.
|
|
||||||
const (
|
|
||||||
Version Type = "version"
|
|
||||||
Mempool Type = "mempool"
|
|
||||||
Ping Type = "ping"
|
|
||||||
Pong Type = "pong"
|
|
||||||
Verack Type = "verack"
|
|
||||||
GetAddr Type = "getaddr"
|
|
||||||
Addr Type = "addr"
|
|
||||||
GetHeaders Type = "getheaders"
|
|
||||||
Headers Type = "headers"
|
|
||||||
GetBlocks Type = "getblocks"
|
|
||||||
Inv Type = "inv"
|
|
||||||
GetData Type = "getdata"
|
|
||||||
Block Type = "block"
|
|
||||||
TX Type = "tx"
|
|
||||||
Consensus Type = "consensus"
|
|
||||||
Unknown Type = "unknown"
|
|
||||||
)
|
|
|
@ -47,7 +47,7 @@ func WriteMessage(w io.Writer, magic protocol.Magic, message Messager) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
payloadLen := util.BufferLength(buf)
|
payloadLen := uint32(buf.Len())
|
||||||
checksum := checksum.FromBytes(buf.Bytes())
|
checksum := checksum.FromBytes(buf.Bytes())
|
||||||
|
|
||||||
bw.Write(payloadLen)
|
bw.Write(payloadLen)
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
package protocol
|
|
||||||
|
|
||||||
//Version represents the latest protocol version for the neo node
|
|
||||||
type Version uint32
|
|
||||||
|
|
||||||
const (
|
|
||||||
// DefaultVersion is the nodes default protocol version
|
|
||||||
DefaultVersion Version = 0
|
|
||||||
// UserAgent is the nodes user agent or human-readable name
|
|
||||||
UserAgent = "/NEO-GO/"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ServiceFlag indicates the services provided by the node. 1 = P2P Full Node
|
|
||||||
type ServiceFlag uint64
|
|
||||||
|
|
||||||
// List of Services offered by the node
|
|
||||||
const (
|
|
||||||
NodePeerService ServiceFlag = 1
|
|
||||||
// BloomFilerService ServiceFlag = 2 // Not implemented
|
|
||||||
// PrunedNode ServiceFlag = 3 // Not implemented
|
|
||||||
// LightNode ServiceFlag = 4 // Not implemented
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
// Magic is the network that NEO is running on
|
|
||||||
type Magic uint32
|
|
||||||
|
|
||||||
// List of possible networks
|
|
||||||
const (
|
|
||||||
MainNet Magic = 7630401
|
|
||||||
TestNet Magic = 0x74746e41
|
|
||||||
)
|
|
||||||
|
|
||||||
// String implements the stringer interface
|
|
||||||
func (m Magic) String() string {
|
|
||||||
switch m {
|
|
||||||
case MainNet:
|
|
||||||
return "Mainnet"
|
|
||||||
case TestNet:
|
|
||||||
return "Testnet"
|
|
||||||
default:
|
|
||||||
return "UnknownNet"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,49 +0,0 @@
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"crypto/sha256"
|
|
||||||
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Convenience function
|
|
||||||
|
|
||||||
// BufferLength returns the length of a buffer as uint32
|
|
||||||
func BufferLength(buf *bytes.Buffer) uint32 {
|
|
||||||
|
|
||||||
return uint32(buf.Len())
|
|
||||||
}
|
|
||||||
|
|
||||||
// SumSHA256 returns the sha256 sum of the data
|
|
||||||
func SumSHA256(b []byte) []byte {
|
|
||||||
h := sha256.New()
|
|
||||||
h.Write(b)
|
|
||||||
return h.Sum(nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CalculateHash takes a function with a binary writer and returns
|
|
||||||
// the double hash of the io.Writer
|
|
||||||
func CalculateHash(f func(bw *BinWriter)) (Uint256, error) {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
bw := &BinWriter{W: buf}
|
|
||||||
|
|
||||||
f(bw)
|
|
||||||
|
|
||||||
var hash Uint256
|
|
||||||
hash = sha256.Sum256(buf.Bytes())
|
|
||||||
hash = sha256.Sum256(hash.Bytes())
|
|
||||||
return hash, bw.Err
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//ReaderToBuffer converts a io.Reader into a bytes.Buffer
|
|
||||||
func ReaderToBuffer(r io.Reader) (*bytes.Buffer, error) {
|
|
||||||
byt, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
buf := bytes.NewBuffer(byt)
|
|
||||||
return buf, nil
|
|
||||||
}
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
@ -69,17 +68,11 @@ func NewAccountState(scriptHash util.Uint160) *AccountState {
|
||||||
|
|
||||||
// DecodeBinary decodes AccountState from the given io.Reader.
|
// DecodeBinary decodes AccountState from the given io.Reader.
|
||||||
func (s *AccountState) DecodeBinary(r io.Reader) error {
|
func (s *AccountState) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.Version); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&s.Version)
|
||||||
}
|
br.ReadLE(&s.ScriptHash)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.ScriptHash); err != nil {
|
br.ReadLE(&s.IsFrozen)
|
||||||
return err
|
lenVotes := br.ReadVarUint()
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.IsFrozen); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
lenVotes := util.ReadVarUint(r)
|
|
||||||
s.Votes = make([]*keys.PublicKey, lenVotes)
|
s.Votes = make([]*keys.PublicKey, lenVotes)
|
||||||
for i := 0; i < int(lenVotes); i++ {
|
for i := 0; i < int(lenVotes); i++ {
|
||||||
s.Votes[i] = &keys.PublicKey{}
|
s.Votes[i] = &keys.PublicKey{}
|
||||||
|
@ -89,37 +82,25 @@ func (s *AccountState) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Balances = make(map[util.Uint256]util.Fixed8)
|
s.Balances = make(map[util.Uint256]util.Fixed8)
|
||||||
lenBalances := util.ReadVarUint(r)
|
lenBalances := br.ReadVarUint()
|
||||||
for i := 0; i < int(lenBalances); i++ {
|
for i := 0; i < int(lenBalances); i++ {
|
||||||
key := util.Uint256{}
|
key := util.Uint256{}
|
||||||
if err := binary.Read(r, binary.LittleEndian, &key); err != nil {
|
br.ReadLE(&key)
|
||||||
return err
|
|
||||||
}
|
|
||||||
var val util.Fixed8
|
var val util.Fixed8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &val); err != nil {
|
br.ReadLE(&val)
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.Balances[key] = val
|
s.Balances[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary encode AccountState to the given io.Writer.
|
// EncodeBinary encode AccountState to the given io.Writer.
|
||||||
func (s *AccountState) EncodeBinary(w io.Writer) error {
|
func (s *AccountState) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, s.Version); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(s.Version)
|
||||||
}
|
bw.WriteLE(s.ScriptHash)
|
||||||
if err := binary.Write(w, binary.LittleEndian, s.ScriptHash); err != nil {
|
bw.WriteLE(s.IsFrozen)
|
||||||
return err
|
bw.WriteVarUint(uint64(len(s.Votes)))
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, s.IsFrozen); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := util.WriteVarUint(w, uint64(len(s.Votes))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, point := range s.Votes {
|
for _, point := range s.Votes {
|
||||||
if err := point.EncodeBinary(w); err != nil {
|
if err := point.EncodeBinary(w); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -127,19 +108,13 @@ func (s *AccountState) EncodeBinary(w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
balances := s.nonZeroBalances()
|
balances := s.nonZeroBalances()
|
||||||
if err := util.WriteVarUint(w, uint64(len(balances))); err != nil {
|
bw.WriteVarUint(uint64(len(balances)))
|
||||||
return err
|
|
||||||
}
|
|
||||||
for k, v := range balances {
|
for k, v := range balances {
|
||||||
if err := binary.Write(w, binary.LittleEndian, k); err != nil {
|
bw.WriteLE(k)
|
||||||
return err
|
bw.WriteLE(v)
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, v); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the non-zero balances for the account.
|
// Returns only the non-zero balances for the account.
|
||||||
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
||||||
|
@ -48,95 +47,56 @@ type AssetState struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (a *AssetState) DecodeBinary(r io.Reader) error {
|
func (a *AssetState) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.ID); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&a.ID)
|
||||||
}
|
br.ReadLE(&a.AssetType)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.AssetType); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
a.Name = br.ReadString()
|
||||||
a.Name, err = util.ReadVarString(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Amount); err != nil {
|
br.ReadLE(&a.Amount)
|
||||||
return err
|
br.ReadLE(&a.Available)
|
||||||
}
|
br.ReadLE(&a.Precision)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Available); err != nil {
|
br.ReadLE(&a.FeeMode)
|
||||||
return err
|
br.ReadLE(&a.FeeAddress)
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Precision); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.FeeMode); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.FeeAddress); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
a.Owner = &keys.PublicKey{}
|
a.Owner = &keys.PublicKey{}
|
||||||
if err := a.Owner.DecodeBinary(r); err != nil {
|
if err := a.Owner.DecodeBinary(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Admin); err != nil {
|
br.ReadLE(&a.Admin)
|
||||||
return err
|
br.ReadLE(&a.Issuer)
|
||||||
}
|
br.ReadLE(&a.Expiration)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Issuer); err != nil {
|
br.ReadLE(&a.IsFrozen)
|
||||||
return err
|
|
||||||
}
|
return br.Err
|
||||||
if err := binary.Read(r, binary.LittleEndian, &a.Expiration); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Read(r, binary.LittleEndian, &a.IsFrozen)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (a *AssetState) EncodeBinary(w io.Writer) error {
|
func (a *AssetState) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.ID); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(a.ID)
|
||||||
}
|
bw.WriteLE(a.AssetType)
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.AssetType); err != nil {
|
bw.WriteString(a.Name)
|
||||||
return err
|
bw.WriteLE(a.Amount)
|
||||||
}
|
bw.WriteLE(a.Available)
|
||||||
if err := util.WriteVarUint(w, uint64(len(a.Name))); err != nil {
|
bw.WriteLE(a.Precision)
|
||||||
return err
|
bw.WriteLE(a.FeeMode)
|
||||||
}
|
bw.WriteLE(a.FeeAddress)
|
||||||
if err := binary.Write(w, binary.LittleEndian, []byte(a.Name)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Amount); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Available); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Precision); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.FeeMode); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.FeeAddress); err != nil {
|
if bw.Err != nil {
|
||||||
return err
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := a.Owner.EncodeBinary(w); err != nil {
|
if err := a.Owner.EncodeBinary(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Admin); err != nil {
|
bw.WriteLE(a.Admin)
|
||||||
return err
|
bw.WriteLE(a.Issuer)
|
||||||
}
|
bw.WriteLE(a.Expiration)
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Issuer); err != nil {
|
bw.WriteLE(a.IsFrozen)
|
||||||
return err
|
return bw.Err
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, a.Expiration); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, a.IsFrozen)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the asset name based on its type.
|
// GetName returns the asset name based on its type.
|
||||||
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
||||||
|
@ -79,9 +78,11 @@ func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
|
||||||
return block, err
|
return block, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
br := util.BinReader{R: r}
|
||||||
var padding uint8
|
var padding uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &padding); err != nil {
|
br.ReadLE(&padding)
|
||||||
return block, err
|
if br.Err != nil {
|
||||||
|
return block, br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
block.Script = &transaction.Witness{}
|
block.Script = &transaction.Witness{}
|
||||||
|
@ -89,17 +90,15 @@ func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
|
||||||
return block, err
|
return block, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lenTX := util.ReadVarUint(r)
|
lenTX := br.ReadVarUint()
|
||||||
block.Transactions = make([]*transaction.Transaction, lenTX)
|
block.Transactions = make([]*transaction.Transaction, lenTX)
|
||||||
for i := 0; i < int(lenTX); i++ {
|
for i := 0; i < int(lenTX); i++ {
|
||||||
var hash util.Uint256
|
var hash util.Uint256
|
||||||
if err := binary.Read(r, binary.LittleEndian, &hash); err != nil {
|
br.ReadLE(&hash)
|
||||||
return block, err
|
|
||||||
}
|
|
||||||
block.Transactions[i] = transaction.NewTrimmedTX(hash)
|
block.Transactions[i] = transaction.NewTrimmedTX(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
return block, nil
|
return block, br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim returns a subset of the block data to save up space
|
// Trim returns a subset of the block data to save up space
|
||||||
|
@ -110,23 +109,22 @@ func (b *Block) Trim() ([]byte, error) {
|
||||||
if err := b.encodeHashableFields(buf); err != nil {
|
if err := b.encodeHashableFields(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := binary.Write(buf, binary.LittleEndian, uint8(1)); err != nil {
|
bw := util.BinWriter{W: buf}
|
||||||
return nil, err
|
bw.WriteLE(uint8(1))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return nil, bw.Err
|
||||||
}
|
}
|
||||||
if err := b.Script.EncodeBinary(buf); err != nil {
|
if err := b.Script.EncodeBinary(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lenTX := uint64(len(b.Transactions))
|
bw.WriteVarUint(uint64(len(b.Transactions)))
|
||||||
if err := util.WriteVarUint(buf, lenTX); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, tx := range b.Transactions {
|
for _, tx := range b.Transactions {
|
||||||
if err := binary.Write(buf, binary.LittleEndian, tx.Hash()); err != nil {
|
bw.WriteLE(tx.Hash())
|
||||||
return nil, err
|
}
|
||||||
}
|
if bw.Err != nil {
|
||||||
|
return nil, bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +134,11 @@ func (b *Block) DecodeBinary(r io.Reader) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
lentx := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lentx := br.ReadVarUint()
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
b.Transactions = make([]*transaction.Transaction, lentx)
|
b.Transactions = make([]*transaction.Transaction, lentx)
|
||||||
for i := 0; i < int(lentx); i++ {
|
for i := 0; i < int(lentx); i++ {
|
||||||
b.Transactions[i] = &transaction.Transaction{}
|
b.Transactions[i] = &transaction.Transaction{}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
@ -67,8 +66,10 @@ func (b *BlockBase) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var padding uint8
|
var padding uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &padding); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&padding)
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
}
|
}
|
||||||
if padding != 1 {
|
if padding != 1 {
|
||||||
return fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
return fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
||||||
|
@ -83,8 +84,10 @@ func (b *BlockBase) EncodeBinary(w io.Writer) error {
|
||||||
if err := b.encodeHashableFields(w); err != nil {
|
if err := b.encodeHashableFields(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := binary.Write(w, binary.LittleEndian, uint8(1)); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(uint8(1))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
return b.Script.EncodeBinary(w)
|
return b.Script.EncodeBinary(w)
|
||||||
}
|
}
|
||||||
|
@ -108,50 +111,31 @@ func (b *BlockBase) createHash() error {
|
||||||
// encodeHashableFields will only encode the fields used for hashing.
|
// encodeHashableFields will only encode the fields used for hashing.
|
||||||
// see Hash() for more information about the fields.
|
// see Hash() for more information about the fields.
|
||||||
func (b *BlockBase) encodeHashableFields(w io.Writer) error {
|
func (b *BlockBase) encodeHashableFields(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.Version); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(b.Version)
|
||||||
}
|
bw.WriteLE(b.PrevHash)
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.PrevHash); err != nil {
|
bw.WriteLE(b.MerkleRoot)
|
||||||
return err
|
bw.WriteLE(b.Timestamp)
|
||||||
}
|
bw.WriteLE(b.Index)
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.MerkleRoot); err != nil {
|
bw.WriteLE(b.ConsensusData)
|
||||||
return err
|
bw.WriteLE(b.NextConsensus)
|
||||||
}
|
return bw.Err
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.Timestamp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.Index); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, b.ConsensusData); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, b.NextConsensus)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// decodeHashableFields will only decode the fields used for hashing.
|
// decodeHashableFields will only decode the fields used for hashing.
|
||||||
// see Hash() for more information about the fields.
|
// see Hash() for more information about the fields.
|
||||||
func (b *BlockBase) decodeHashableFields(r io.Reader) error {
|
func (b *BlockBase) decodeHashableFields(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.Version); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&b.Version)
|
||||||
}
|
br.ReadLE(&b.PrevHash)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.PrevHash); err != nil {
|
br.ReadLE(&b.MerkleRoot)
|
||||||
return err
|
br.ReadLE(&b.Timestamp)
|
||||||
}
|
br.ReadLE(&b.Index)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.MerkleRoot); err != nil {
|
br.ReadLE(&b.ConsensusData)
|
||||||
return err
|
br.ReadLE(&b.NextConsensus)
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.Timestamp); err != nil {
|
if br.Err != nil {
|
||||||
return err
|
return br.Err
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.Index); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.ConsensusData); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &b.NextConsensus); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the hash of the block here so we dont need to do this
|
// Make the hash of the block here so we dont need to do this
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -60,14 +59,11 @@ func (l *HeaderHashList) Slice(start, end int) []util.Uint256 {
|
||||||
// WriteTo will write n underlying hashes to the given io.Writer
|
// WriteTo will write n underlying hashes to the given io.Writer
|
||||||
// starting from start.
|
// starting from start.
|
||||||
func (l *HeaderHashList) Write(w io.Writer, start, n int) error {
|
func (l *HeaderHashList) Write(w io.Writer, start, n int) error {
|
||||||
if err := util.WriteVarUint(w, uint64(n)); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(n))
|
||||||
}
|
|
||||||
hashes := l.Slice(start, start+n)
|
hashes := l.Slice(start, start+n)
|
||||||
for _, hash := range hashes {
|
for _, hash := range hashes {
|
||||||
if err := binary.Write(w, binary.LittleEndian, hash); err != nil {
|
bw.WriteLE(hash)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
@ -68,49 +67,33 @@ func NewSpentCoinState(hash util.Uint256, height uint32) *SpentCoinState {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (s *SpentCoinState) DecodeBinary(r io.Reader) error {
|
func (s *SpentCoinState) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.txHash); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&s.txHash)
|
||||||
}
|
br.ReadLE(&s.txHeight)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.txHeight); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.items = make(map[uint16]uint32)
|
s.items = make(map[uint16]uint32)
|
||||||
lenItems := util.ReadVarUint(r)
|
lenItems := br.ReadVarUint()
|
||||||
for i := 0; i < int(lenItems); i++ {
|
for i := 0; i < int(lenItems); i++ {
|
||||||
var (
|
var (
|
||||||
key uint16
|
key uint16
|
||||||
value uint32
|
value uint32
|
||||||
)
|
)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &key); err != nil {
|
br.ReadLE(&key)
|
||||||
return err
|
br.ReadLE(&value)
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &value); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.items[key] = value
|
s.items[key] = value
|
||||||
}
|
}
|
||||||
return nil
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (s *SpentCoinState) EncodeBinary(w io.Writer) error {
|
func (s *SpentCoinState) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, s.txHash); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(s.txHash)
|
||||||
}
|
bw.WriteLE(s.txHeight)
|
||||||
if err := binary.Write(w, binary.LittleEndian, s.txHeight); err != nil {
|
bw.WriteVarUint(uint64(len(s.items)))
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := util.WriteVarUint(w, uint64(len(s.items))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for k, v := range s.items {
|
for k, v := range s.items {
|
||||||
if err := binary.Write(w, binary.LittleEndian, k); err != nil {
|
bw.WriteLE(k)
|
||||||
return err
|
bw.WriteLE(v)
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, v); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
@ -55,7 +56,7 @@ func HeaderHashes(s Store) ([]util.Uint256, error) {
|
||||||
hashMap := make(map[uint32][]util.Uint256)
|
hashMap := make(map[uint32][]util.Uint256)
|
||||||
s.Seek(IXHeaderHashList.Bytes(), func(k, v []byte) {
|
s.Seek(IXHeaderHashList.Bytes(), func(k, v []byte) {
|
||||||
storedCount := binary.LittleEndian.Uint32(k[1:])
|
storedCount := binary.LittleEndian.Uint32(k[1:])
|
||||||
hashes, err := util.Read2000Uint256Hashes(v)
|
hashes, err := read2000Uint256Hashes(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -78,3 +79,17 @@ func HeaderHashes(s Store) ([]util.Uint256, error) {
|
||||||
|
|
||||||
return hashes, nil
|
return hashes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read2000Uint256Hashes attempts to read 2000 Uint256 hashes from
|
||||||
|
// the given byte array.
|
||||||
|
func read2000Uint256Hashes(b []byte) ([]util.Uint256, error) {
|
||||||
|
r := bytes.NewReader(b)
|
||||||
|
br := util.BinReader{R: r}
|
||||||
|
lenHashes := br.ReadVarUint()
|
||||||
|
hashes := make([]util.Uint256, lenHashes)
|
||||||
|
br.ReadLE(hashes)
|
||||||
|
if br.Err != nil {
|
||||||
|
return nil, br.Err
|
||||||
|
}
|
||||||
|
return hashes, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -18,66 +17,58 @@ type Attribute struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (attr *Attribute) DecodeBinary(r io.Reader) error {
|
func (attr *Attribute) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &attr.Usage); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&attr.Usage)
|
||||||
}
|
|
||||||
if attr.Usage == ContractHash ||
|
// very special case
|
||||||
attr.Usage == Vote ||
|
|
||||||
(attr.Usage >= Hash1 && attr.Usage <= Hash15) {
|
|
||||||
attr.Data = make([]byte, 32)
|
|
||||||
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
|
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
|
||||||
attr.Data = make([]byte, 33)
|
attr.Data = make([]byte, 33)
|
||||||
attr.Data[0] = byte(attr.Usage)
|
attr.Data[0] = byte(attr.Usage)
|
||||||
return binary.Read(r, binary.LittleEndian, attr.Data[1:])
|
br.ReadLE(attr.Data[1:])
|
||||||
|
return br.Err
|
||||||
}
|
}
|
||||||
if attr.Usage == Script {
|
var datasize uint64
|
||||||
attr.Data = make([]byte, 20)
|
switch attr.Usage {
|
||||||
return binary.Read(r, binary.LittleEndian, attr.Data)
|
case ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5,
|
||||||
|
Hash6, Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13,
|
||||||
|
Hash14, Hash15:
|
||||||
|
datasize = 32
|
||||||
|
case Script:
|
||||||
|
datasize = 20
|
||||||
|
case DescriptionURL:
|
||||||
|
datasize = 1
|
||||||
|
case Description, Remark, Remark1, Remark2, Remark3, Remark4,
|
||||||
|
Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11,
|
||||||
|
Remark12, Remark13, Remark14, Remark15:
|
||||||
|
datasize = br.ReadVarUint()
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("failed decoding TX attribute usage: 0x%2x", attr.Usage)
|
||||||
}
|
}
|
||||||
if attr.Usage == DescriptionURL {
|
attr.Data = make([]byte, datasize)
|
||||||
attr.Data = make([]byte, 1)
|
br.ReadLE(attr.Data)
|
||||||
return binary.Read(r, binary.LittleEndian, attr.Data)
|
return br.Err
|
||||||
}
|
|
||||||
if attr.Usage == Description || attr.Usage >= Remark {
|
|
||||||
lenData := util.ReadVarUint(r)
|
|
||||||
attr.Data = make([]byte, lenData)
|
|
||||||
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed decoding TX attribute usage: 0x%2x", attr.Usage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (attr *Attribute) EncodeBinary(w io.Writer) error {
|
func (attr *Attribute) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, &attr.Usage); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(&attr.Usage)
|
||||||
|
switch attr.Usage {
|
||||||
|
case ECDH02, ECDH03:
|
||||||
|
bw.WriteLE(attr.Data[1:])
|
||||||
|
case DescriptionURL, Description, Remark, Remark1, Remark2, Remark3, Remark4,
|
||||||
|
Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11,
|
||||||
|
Remark12, Remark13, Remark14, Remark15:
|
||||||
|
bw.WriteVarUint(uint64(len(attr.Data)))
|
||||||
|
fallthrough
|
||||||
|
case Script, ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5, Hash6,
|
||||||
|
Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15:
|
||||||
|
bw.WriteLE(attr.Data)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage)
|
||||||
}
|
}
|
||||||
if attr.Usage == ContractHash ||
|
|
||||||
attr.Usage == Vote ||
|
return bw.Err
|
||||||
(attr.Usage >= Hash1 && attr.Usage <= Hash15) {
|
|
||||||
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
|
|
||||||
attr.Data[0] = byte(attr.Usage)
|
|
||||||
return binary.Write(w, binary.LittleEndian, attr.Data[1:33])
|
|
||||||
}
|
|
||||||
if attr.Usage == Script {
|
|
||||||
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
if attr.Usage == DescriptionURL {
|
|
||||||
if err := util.WriteVarUint(w, uint64(len(attr.Data))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
if attr.Usage == Description || attr.Usage >= Remark {
|
|
||||||
if err := util.WriteVarUint(w, uint64(len(attr.Data))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the size in number bytes of the Attribute
|
// Size returns the size in number bytes of the Attribute
|
||||||
|
|
|
@ -13,7 +13,11 @@ type ClaimTX struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (tx *ClaimTX) DecodeBinary(r io.Reader) error {
|
func (tx *ClaimTX) DecodeBinary(r io.Reader) error {
|
||||||
lenClaims := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lenClaims := br.ReadVarUint()
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
tx.Claims = make([]*Input, lenClaims)
|
tx.Claims = make([]*Input, lenClaims)
|
||||||
for i := 0; i < int(lenClaims); i++ {
|
for i := 0; i < int(lenClaims); i++ {
|
||||||
tx.Claims[i] = &Input{}
|
tx.Claims[i] = &Input{}
|
||||||
|
@ -26,8 +30,10 @@ func (tx *ClaimTX) DecodeBinary(r io.Reader) error {
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (tx *ClaimTX) EncodeBinary(w io.Writer) error {
|
func (tx *ClaimTX) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(tx.Claims))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(tx.Claims)))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, claim := range tx.Claims {
|
for _, claim := range tx.Claims {
|
||||||
if err := claim.EncodeBinary(w); err != nil {
|
if err := claim.EncodeBinary(w); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -18,21 +17,18 @@ type Input struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (in *Input) DecodeBinary(r io.Reader) error {
|
func (in *Input) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &in.PrevHash); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&in.PrevHash)
|
||||||
}
|
br.ReadLE(&in.PrevIndex)
|
||||||
return binary.Read(r, binary.LittleEndian, &in.PrevIndex)
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (in *Input) EncodeBinary(w io.Writer) error {
|
func (in *Input) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, in.PrevHash); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(in.PrevHash)
|
||||||
}
|
bw.WriteLE(in.PrevIndex)
|
||||||
if err := binary.Write(w, binary.LittleEndian, in.PrevIndex); err != nil {
|
return bw.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the size in bytes of the Input
|
// Size returns the size in bytes of the Input
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -34,21 +33,16 @@ func NewInvocationTX(script []byte) *Transaction {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (tx *InvocationTX) DecodeBinary(r io.Reader) error {
|
func (tx *InvocationTX) DecodeBinary(r io.Reader) error {
|
||||||
lenScript := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
tx.Script = make([]byte, lenScript)
|
tx.Script = br.ReadBytes()
|
||||||
if err := binary.Read(r, binary.LittleEndian, tx.Script); err != nil {
|
br.ReadLE(&tx.Gas)
|
||||||
return err
|
return br.Err
|
||||||
}
|
|
||||||
return binary.Read(r, binary.LittleEndian, &tx.Gas)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (tx *InvocationTX) EncodeBinary(w io.Writer) error {
|
func (tx *InvocationTX) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(tx.Script))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteBytes(tx.Script)
|
||||||
}
|
bw.WriteLE(tx.Gas)
|
||||||
if err := binary.Write(w, binary.LittleEndian, tx.Script); err != nil {
|
return bw.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, tx.Gas)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
@ -36,24 +35,20 @@ func NewOutput(assetID util.Uint256, amount util.Fixed8, scriptHash util.Uint160
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (out *Output) DecodeBinary(r io.Reader) error {
|
func (out *Output) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &out.AssetID); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&out.AssetID)
|
||||||
}
|
br.ReadLE(&out.Amount)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &out.Amount); err != nil {
|
br.ReadLE(&out.ScriptHash)
|
||||||
return err
|
return br.Err
|
||||||
}
|
|
||||||
return binary.Read(r, binary.LittleEndian, &out.ScriptHash)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (out *Output) EncodeBinary(w io.Writer) error {
|
func (out *Output) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, out.AssetID); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(out.AssetID)
|
||||||
}
|
bw.WriteLE(out.Amount)
|
||||||
if err := binary.Write(w, binary.LittleEndian, out.Amount); err != nil {
|
bw.WriteLE(out.ScriptHash)
|
||||||
return err
|
return bw.Err
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, out.ScriptHash)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the size in bytes of the Output
|
// Size returns the size in bytes of the Output
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
||||||
|
@ -24,55 +23,30 @@ type PublishTX struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (tx *PublishTX) DecodeBinary(r io.Reader) error {
|
func (tx *PublishTX) DecodeBinary(r io.Reader) error {
|
||||||
var err error
|
br := util.BinReader{R: r}
|
||||||
|
tx.Script = br.ReadBytes()
|
||||||
|
|
||||||
tx.Script, err = util.ReadVarBytes(r)
|
lenParams := br.ReadVarUint()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
lenParams := util.ReadVarUint(r)
|
|
||||||
tx.ParamList = make([]smartcontract.ParamType, lenParams)
|
tx.ParamList = make([]smartcontract.ParamType, lenParams)
|
||||||
for i := 0; i < int(lenParams); i++ {
|
for i := 0; i < int(lenParams); i++ {
|
||||||
var ptype uint8
|
var ptype uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &ptype); err != nil {
|
br.ReadLE(&ptype)
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx.ParamList[i] = smartcontract.ParamType(ptype)
|
tx.ParamList[i] = smartcontract.ParamType(ptype)
|
||||||
}
|
}
|
||||||
|
|
||||||
var rtype uint8
|
var rtype uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &rtype); err != nil {
|
br.ReadLE(&rtype)
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx.ReturnType = smartcontract.ParamType(rtype)
|
tx.ReturnType = smartcontract.ParamType(rtype)
|
||||||
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &tx.NeedStorage); err != nil {
|
br.ReadLE(&tx.NeedStorage)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tx.Name, err = util.ReadVarString(r)
|
tx.Name = br.ReadString()
|
||||||
if err != nil {
|
tx.CodeVersion = br.ReadString()
|
||||||
return err
|
tx.Author = br.ReadString()
|
||||||
}
|
tx.Email = br.ReadString()
|
||||||
tx.CodeVersion, err = util.ReadVarString(r)
|
tx.Description = br.ReadString()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx.Author, err = util.ReadVarString(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx.Email, err = util.ReadVarString(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
tx.Description, err = util.ReadVarString(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||||
|
@ -32,22 +31,15 @@ type RegisterTX struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (tx *RegisterTX) DecodeBinary(r io.Reader) error {
|
func (tx *RegisterTX) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &tx.AssetType); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&tx.AssetType)
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
tx.Name = br.ReadString()
|
||||||
tx.Name, err = util.ReadVarString(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &tx.Amount); err != nil {
|
br.ReadLE(&tx.Amount)
|
||||||
return err
|
br.ReadLE(&tx.Precision)
|
||||||
}
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
if err := binary.Read(r, binary.LittleEndian, &tx.Precision); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Owner = &keys.PublicKey{}
|
tx.Owner = &keys.PublicKey{}
|
||||||
|
@ -55,25 +47,18 @@ func (tx *RegisterTX) DecodeBinary(r io.Reader) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return binary.Read(r, binary.LittleEndian, &tx.Admin)
|
br.ReadLE(&tx.Admin)
|
||||||
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
func (tx *RegisterTX) EncodeBinary(w io.Writer) error {
|
func (tx *RegisterTX) EncodeBinary(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, tx.AssetType); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteLE(tx.AssetType)
|
||||||
}
|
bw.WriteString(tx.Name)
|
||||||
if err := util.WriteVarString(w, tx.Name); err != nil {
|
bw.WriteLE(tx.Amount)
|
||||||
return err
|
bw.WriteLE(tx.Precision)
|
||||||
}
|
bw.WriteLE(tx.Owner.Bytes())
|
||||||
if err := binary.Write(w, binary.LittleEndian, tx.Amount); err != nil {
|
bw.WriteLE(tx.Admin)
|
||||||
return err
|
return bw.Err
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, tx.Precision); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, tx.Owner.Bytes()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, tx.Admin)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,11 @@ type StateTX struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (tx *StateTX) DecodeBinary(r io.Reader) error {
|
func (tx *StateTX) DecodeBinary(r io.Reader) error {
|
||||||
lenDesc := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lenDesc := br.ReadVarUint()
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
for i := 0; i < int(lenDesc); i++ {
|
for i := 0; i < int(lenDesc); i++ {
|
||||||
tx.Descriptors[i] = &StateDescriptor{}
|
tx.Descriptors[i] = &StateDescriptor{}
|
||||||
if err := tx.Descriptors[i].DecodeBinary(r); err != nil {
|
if err := tx.Descriptors[i].DecodeBinary(r); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -26,30 +25,14 @@ type StateDescriptor struct {
|
||||||
|
|
||||||
// DecodeBinary implements the Payload interface.
|
// DecodeBinary implements the Payload interface.
|
||||||
func (s *StateDescriptor) DecodeBinary(r io.Reader) error {
|
func (s *StateDescriptor) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &s.Type); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&s.Type)
|
||||||
}
|
|
||||||
|
|
||||||
keyLen := util.ReadVarUint(r)
|
s.Key = br.ReadBytes()
|
||||||
s.Key = make([]byte, keyLen)
|
s.Value = br.ReadBytes()
|
||||||
if err := binary.Read(r, binary.LittleEndian, s.Key); err != nil {
|
s.Field = br.ReadString()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
valLen := util.ReadVarUint(r)
|
return br.Err
|
||||||
s.Value = make([]byte, valLen)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, s.Value); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldLen := util.ReadVarUint(r)
|
|
||||||
field := make([]byte, fieldLen)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, field); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.Field = string(field)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the Payload interface.
|
// EncodeBinary implements the Payload interface.
|
||||||
|
|
|
@ -2,7 +2,6 @@ package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
|
@ -79,17 +78,17 @@ func (t *Transaction) AddInput(in *Input) {
|
||||||
|
|
||||||
// DecodeBinary implements the payload interface.
|
// DecodeBinary implements the payload interface.
|
||||||
func (t *Transaction) DecodeBinary(r io.Reader) error {
|
func (t *Transaction) DecodeBinary(r io.Reader) error {
|
||||||
if err := binary.Read(r, binary.LittleEndian, &t.Type); err != nil {
|
br := util.BinReader{R: r}
|
||||||
return err
|
br.ReadLE(&t.Type)
|
||||||
}
|
br.ReadLE(&t.Version)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &t.Version); err != nil {
|
if br.Err != nil {
|
||||||
return err
|
return br.Err
|
||||||
}
|
}
|
||||||
if err := t.decodeData(r); err != nil {
|
if err := t.decodeData(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
lenAttrs := util.ReadVarUint(r)
|
lenAttrs := br.ReadVarUint()
|
||||||
t.Attributes = make([]*Attribute, lenAttrs)
|
t.Attributes = make([]*Attribute, lenAttrs)
|
||||||
for i := 0; i < int(lenAttrs); i++ {
|
for i := 0; i < int(lenAttrs); i++ {
|
||||||
t.Attributes[i] = &Attribute{}
|
t.Attributes[i] = &Attribute{}
|
||||||
|
@ -100,7 +99,7 @@ func (t *Transaction) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lenInputs := util.ReadVarUint(r)
|
lenInputs := br.ReadVarUint()
|
||||||
t.Inputs = make([]*Input, lenInputs)
|
t.Inputs = make([]*Input, lenInputs)
|
||||||
for i := 0; i < int(lenInputs); i++ {
|
for i := 0; i < int(lenInputs); i++ {
|
||||||
t.Inputs[i] = &Input{}
|
t.Inputs[i] = &Input{}
|
||||||
|
@ -109,7 +108,7 @@ func (t *Transaction) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lenOutputs := util.ReadVarUint(r)
|
lenOutputs := br.ReadVarUint()
|
||||||
t.Outputs = make([]*Output, lenOutputs)
|
t.Outputs = make([]*Output, lenOutputs)
|
||||||
for i := 0; i < int(lenOutputs); i++ {
|
for i := 0; i < int(lenOutputs); i++ {
|
||||||
t.Outputs[i] = &Output{}
|
t.Outputs[i] = &Output{}
|
||||||
|
@ -118,7 +117,7 @@ func (t *Transaction) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lenScripts := util.ReadVarUint(r)
|
lenScripts := br.ReadVarUint()
|
||||||
t.Scripts = make([]*Witness, lenScripts)
|
t.Scripts = make([]*Witness, lenScripts)
|
||||||
for i := 0; i < int(lenScripts); i++ {
|
for i := 0; i < int(lenScripts); i++ {
|
||||||
t.Scripts[i] = &Witness{}
|
t.Scripts[i] = &Witness{}
|
||||||
|
@ -127,6 +126,9 @@ func (t *Transaction) DecodeBinary(r io.Reader) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if br.Err != nil {
|
||||||
|
return br.Err
|
||||||
|
}
|
||||||
// Create the hash of the transaction at decode, so we dont need
|
// Create the hash of the transaction at decode, so we dont need
|
||||||
// to do it anymore.
|
// to do it anymore.
|
||||||
return t.createHash()
|
return t.createHash()
|
||||||
|
@ -172,8 +174,10 @@ func (t *Transaction) EncodeBinary(w io.Writer) error {
|
||||||
if err := t.encodeHashableFields(w); err != nil {
|
if err := t.encodeHashableFields(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := util.WriteVarUint(w, uint64(len(t.Scripts))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(t.Scripts)))
|
||||||
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, s := range t.Scripts {
|
for _, s := range t.Scripts {
|
||||||
if err := s.EncodeBinary(w); err != nil {
|
if err := s.EncodeBinary(w); err != nil {
|
||||||
|
@ -186,11 +190,12 @@ func (t *Transaction) EncodeBinary(w io.Writer) error {
|
||||||
// encodeHashableFields will only encode the fields that are not used for
|
// encodeHashableFields will only encode the fields that are not used for
|
||||||
// signing the transaction, which are all fields except the scripts.
|
// signing the transaction, which are all fields except the scripts.
|
||||||
func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
||||||
if err := binary.Write(w, binary.LittleEndian, t.Type); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
|
||||||
}
|
bw.WriteLE(t.Type)
|
||||||
if err := binary.Write(w, binary.LittleEndian, t.Version); err != nil {
|
bw.WriteLE(t.Version)
|
||||||
return err
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Underlying TXer.
|
// Underlying TXer.
|
||||||
|
@ -201,9 +206,9 @@ func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attributes
|
// Attributes
|
||||||
lenAttrs := uint64(len(t.Attributes))
|
bw.WriteVarUint(uint64(len(t.Attributes)))
|
||||||
if err := util.WriteVarUint(w, lenAttrs); err != nil {
|
if bw.Err != nil {
|
||||||
return err
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, attr := range t.Attributes {
|
for _, attr := range t.Attributes {
|
||||||
if err := attr.EncodeBinary(w); err != nil {
|
if err := attr.EncodeBinary(w); err != nil {
|
||||||
|
@ -212,8 +217,9 @@ func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
if err := util.WriteVarUint(w, uint64(len(t.Inputs))); err != nil {
|
bw.WriteVarUint(uint64(len(t.Inputs)))
|
||||||
return err
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, in := range t.Inputs {
|
for _, in := range t.Inputs {
|
||||||
if err := in.EncodeBinary(w); err != nil {
|
if err := in.EncodeBinary(w); err != nil {
|
||||||
|
@ -222,8 +228,9 @@ func (t *Transaction) encodeHashableFields(w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Outputs
|
// Outputs
|
||||||
if err := util.WriteVarUint(w, uint64(len(t.Outputs))); err != nil {
|
bw.WriteVarUint(uint64(len(t.Outputs)))
|
||||||
return err
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
for _, out := range t.Outputs {
|
for _, out := range t.Outputs {
|
||||||
if err := out.EncodeBinary(w); err != nil {
|
if err := out.EncodeBinary(w); err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
@ -18,28 +17,21 @@ type Witness struct {
|
||||||
|
|
||||||
// DecodeBinary implements the payload interface.
|
// DecodeBinary implements the payload interface.
|
||||||
func (w *Witness) DecodeBinary(r io.Reader) error {
|
func (w *Witness) DecodeBinary(r io.Reader) error {
|
||||||
lenb := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
w.InvocationScript = make([]byte, lenb)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, w.InvocationScript); err != nil {
|
w.InvocationScript = br.ReadBytes()
|
||||||
return err
|
w.VerificationScript = br.ReadBytes()
|
||||||
}
|
return br.Err
|
||||||
lenb = util.ReadVarUint(r)
|
|
||||||
w.VerificationScript = make([]byte, lenb)
|
|
||||||
return binary.Read(r, binary.LittleEndian, w.VerificationScript)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements the payload interface.
|
// EncodeBinary implements the payload interface.
|
||||||
func (w *Witness) EncodeBinary(writer io.Writer) error {
|
func (w *Witness) EncodeBinary(writer io.Writer) error {
|
||||||
if err := util.WriteVarUint(writer, uint64(len(w.InvocationScript))); err != nil {
|
bw := util.BinWriter{W: writer}
|
||||||
return err
|
|
||||||
}
|
bw.WriteBytes(w.InvocationScript)
|
||||||
if err := binary.Write(writer, binary.LittleEndian, w.InvocationScript); err != nil {
|
bw.WriteBytes(w.VerificationScript)
|
||||||
return err
|
|
||||||
}
|
return bw.Err
|
||||||
if err := util.WriteVarUint(writer, uint64(len(w.VerificationScript))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(writer, binary.LittleEndian, w.VerificationScript)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json marshaller interface.
|
// MarshalJSON implements the json marshaller interface.
|
||||||
|
|
|
@ -2,7 +2,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
@ -68,29 +67,25 @@ func (u UnspentCoins) commit(b storage.Batch) error {
|
||||||
|
|
||||||
// EncodeBinary encodes UnspentCoinState to the given io.Writer.
|
// EncodeBinary encodes UnspentCoinState to the given io.Writer.
|
||||||
func (s *UnspentCoinState) EncodeBinary(w io.Writer) error {
|
func (s *UnspentCoinState) EncodeBinary(w io.Writer) error {
|
||||||
if err := util.WriteVarUint(w, uint64(len(s.states))); err != nil {
|
bw := util.BinWriter{W: w}
|
||||||
return err
|
bw.WriteVarUint(uint64(len(s.states)))
|
||||||
}
|
|
||||||
for _, state := range s.states {
|
for _, state := range s.states {
|
||||||
if err := binary.Write(w, binary.LittleEndian, byte(state)); err != nil {
|
bw.WriteLE(byte(state))
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary decodes UnspentCoinState from the given io.Reader.
|
// DecodeBinary decodes UnspentCoinState from the given io.Reader.
|
||||||
func (s *UnspentCoinState) DecodeBinary(r io.Reader) error {
|
func (s *UnspentCoinState) DecodeBinary(r io.Reader) error {
|
||||||
lenStates := util.ReadVarUint(r)
|
br := util.BinReader{R: r}
|
||||||
|
lenStates := br.ReadVarUint()
|
||||||
s.states = make([]CoinState, lenStates)
|
s.states = make([]CoinState, lenStates)
|
||||||
for i := 0; i < int(lenStates); i++ {
|
for i := 0; i < int(lenStates); i++ {
|
||||||
var state uint8
|
var state uint8
|
||||||
if err := binary.Read(r, binary.LittleEndian, &state); err != nil {
|
br.ReadLE(&state)
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.states[i] = CoinState(state)
|
s.states[i] = CoinState(state)
|
||||||
}
|
}
|
||||||
return nil
|
return br.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsDoubleSpend verifies that the input transactions are not double spent.
|
// IsDoubleSpend verifies that the input transactions are not double spent.
|
||||||
|
|
|
@ -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 (
|
||||||
|
@ -49,23 +50,26 @@ type CommandType string
|
||||||
|
|
||||||
// Valid protocol commands used to send between nodes.
|
// Valid protocol commands used to send between nodes.
|
||||||
const (
|
const (
|
||||||
CMDVersion CommandType = "version"
|
|
||||||
CMDVerack CommandType = "verack"
|
|
||||||
CMDGetAddr CommandType = "getaddr"
|
|
||||||
CMDAddr CommandType = "addr"
|
CMDAddr CommandType = "addr"
|
||||||
CMDGetHeaders CommandType = "getheaders"
|
|
||||||
CMDHeaders CommandType = "headers"
|
|
||||||
CMDGetBlocks CommandType = "getblocks"
|
|
||||||
CMDInv CommandType = "inv"
|
|
||||||
CMDGetData CommandType = "getdata"
|
|
||||||
CMDBlock CommandType = "block"
|
CMDBlock CommandType = "block"
|
||||||
CMDTX CommandType = "tx"
|
|
||||||
CMDConsensus CommandType = "consensus"
|
CMDConsensus CommandType = "consensus"
|
||||||
CMDUnknown CommandType = "unknown"
|
|
||||||
CMDFilterAdd CommandType = "filteradd"
|
CMDFilterAdd CommandType = "filteradd"
|
||||||
CMDFilterClear CommandType = "filterclear"
|
CMDFilterClear CommandType = "filterclear"
|
||||||
CMDFilterLoad CommandType = "filterload"
|
CMDFilterLoad CommandType = "filterload"
|
||||||
|
CMDGetAddr CommandType = "getaddr"
|
||||||
|
CMDGetBlocks CommandType = "getblocks"
|
||||||
|
CMDGetData CommandType = "getdata"
|
||||||
|
CMDGetHeaders CommandType = "getheaders"
|
||||||
|
CMDHeaders CommandType = "headers"
|
||||||
|
CMDInv CommandType = "inv"
|
||||||
|
CMDMempool CommandType = "mempool"
|
||||||
CMDMerkleBlock CommandType = "merkleblock"
|
CMDMerkleBlock CommandType = "merkleblock"
|
||||||
|
CMDPing CommandType = "ping"
|
||||||
|
CMDPong CommandType = "pong"
|
||||||
|
CMDTX CommandType = "tx"
|
||||||
|
CMDUnknown CommandType = "unknown"
|
||||||
|
CMDVerack CommandType = "verack"
|
||||||
|
CMDVersion CommandType = "version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewMessage returns a new message with the given payload.
|
// NewMessage returns a new message with the given payload.
|
||||||
|
@ -99,38 +103,44 @@ func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Messa
|
||||||
func (m *Message) CommandType() CommandType {
|
func (m *Message) CommandType() CommandType {
|
||||||
cmd := cmdByteArrayToString(m.Command)
|
cmd := cmdByteArrayToString(m.Command)
|
||||||
switch cmd {
|
switch cmd {
|
||||||
case "version":
|
|
||||||
return CMDVersion
|
|
||||||
case "verack":
|
|
||||||
return CMDVerack
|
|
||||||
case "getaddr":
|
|
||||||
return CMDGetAddr
|
|
||||||
case "addr":
|
case "addr":
|
||||||
return CMDAddr
|
return CMDAddr
|
||||||
case "getheaders":
|
|
||||||
return CMDGetHeaders
|
|
||||||
case "headers":
|
|
||||||
return CMDHeaders
|
|
||||||
case "getblocks":
|
|
||||||
return CMDGetBlocks
|
|
||||||
case "inv":
|
|
||||||
return CMDInv
|
|
||||||
case "getdata":
|
|
||||||
return CMDGetData
|
|
||||||
case "block":
|
case "block":
|
||||||
return CMDBlock
|
return CMDBlock
|
||||||
case "tx":
|
|
||||||
return CMDTX
|
|
||||||
case "consensus":
|
case "consensus":
|
||||||
return CMDConsensus
|
return CMDConsensus
|
||||||
case "merkleblock":
|
|
||||||
return CMDMerkleBlock
|
|
||||||
case "filterload":
|
|
||||||
return CMDFilterLoad
|
|
||||||
case "filteradd":
|
case "filteradd":
|
||||||
return CMDFilterAdd
|
return CMDFilterAdd
|
||||||
case "filterclear":
|
case "filterclear":
|
||||||
return CMDFilterClear
|
return CMDFilterClear
|
||||||
|
case "filterload":
|
||||||
|
return CMDFilterLoad
|
||||||
|
case "getaddr":
|
||||||
|
return CMDGetAddr
|
||||||
|
case "getblocks":
|
||||||
|
return CMDGetBlocks
|
||||||
|
case "getdata":
|
||||||
|
return CMDGetData
|
||||||
|
case "getheaders":
|
||||||
|
return CMDGetHeaders
|
||||||
|
case "headers":
|
||||||
|
return CMDHeaders
|
||||||
|
case "inv":
|
||||||
|
return CMDInv
|
||||||
|
case "mempool":
|
||||||
|
return CMDMempool
|
||||||
|
case "merkleblock":
|
||||||
|
return CMDMerkleBlock
|
||||||
|
case "ping":
|
||||||
|
return CMDPing
|
||||||
|
case "pong":
|
||||||
|
return CMDPong
|
||||||
|
case "tx":
|
||||||
|
return CMDTX
|
||||||
|
case "verack":
|
||||||
|
return CMDVerack
|
||||||
|
case "version":
|
||||||
|
return CMDVersion
|
||||||
default:
|
default:
|
||||||
return CMDUnknown
|
return CMDUnknown
|
||||||
}
|
}
|
||||||
|
@ -138,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 {
|
||||||
|
@ -224,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,13 +1,23 @@
|
||||||
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
|
||||||
|
|
||||||
|
// List of Services offered by the node
|
||||||
|
const (
|
||||||
|
nodePeerService uint64 = 1
|
||||||
|
// BloomFilerService uint64 = 2 // Not implemented
|
||||||
|
// PrunedNode uint64 = 3 // Not implemented
|
||||||
|
// LightNode uint64 = 4 // Not implemented
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
// Version payload.
|
// Version payload.
|
||||||
type Version struct {
|
type Version struct {
|
||||||
// currently the version of the protocol is 0
|
// currently the version of the protocol is 0
|
||||||
|
@ -32,7 +42,7 @@ type Version struct {
|
||||||
func NewVersion(id uint32, p uint16, ua string, h uint32, r bool) *Version {
|
func NewVersion(id uint32, p uint16, ua string, h uint32, r bool) *Version {
|
||||||
return &Version{
|
return &Version{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
Services: 1,
|
Services: nodePeerService,
|
||||||
Timestamp: uint32(time.Now().UTC().Unix()),
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
||||||
Port: p,
|
Port: p,
|
||||||
Nonce: id,
|
Nonce: id,
|
||||||
|
@ -44,63 +54,31 @@ 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 {
|
p.UserAgent = br.ReadBytes()
|
||||||
return err
|
br.ReadLE(&p.StartHeight)
|
||||||
}
|
br.ReadLE(&p.Relay)
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Port); err != nil {
|
return br.Err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.Nonce); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var lenUA uint8
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &lenUA); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.UserAgent = make([]byte, lenUA)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.UserAgent); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, &p.StartHeight); err != nil {
|
|
||||||
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.WriteBytes(p.UserAgent)
|
||||||
}
|
br.WriteLE(p.StartHeight)
|
||||||
if err := binary.Write(w, binary.LittleEndian, p.Port); err != nil {
|
br.WriteLE(&p.Relay)
|
||||||
return err
|
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.
|
||||||
|
|
|
@ -23,19 +23,21 @@ func TestCreateMultiSigRedeemScript(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.NewBuffer(out)
|
buf := bytes.NewBuffer(out)
|
||||||
b, _ := buf.ReadByte()
|
br := util.BinReader{R: buf}
|
||||||
|
var b uint8
|
||||||
|
br.ReadLE(&b)
|
||||||
assert.Equal(t, vm.PUSH3, vm.Instruction(b))
|
assert.Equal(t, vm.PUSH3, vm.Instruction(b))
|
||||||
|
|
||||||
for i := 0; i < len(validators); i++ {
|
for i := 0; i < len(validators); i++ {
|
||||||
b, err := util.ReadVarBytes(buf)
|
bb := br.ReadBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, validators[i].Bytes(), b)
|
assert.Equal(t, validators[i].Bytes(), bb)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, _ = buf.ReadByte()
|
br.ReadLE(&b)
|
||||||
assert.Equal(t, vm.PUSH3, vm.Instruction(b))
|
assert.Equal(t, vm.PUSH3, vm.Instruction(b))
|
||||||
b, _ = buf.ReadByte()
|
br.ReadLE(&b)
|
||||||
assert.Equal(t, vm.CHECKMULTISIG, vm.Instruction(b))
|
assert.Equal(t, vm.CHECKMULTISIG, vm.Instruction(b))
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,27 +12,31 @@ type BinReader struct {
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads from the underlying io.Reader
|
// ReadLE reads from the underlying io.Reader
|
||||||
// into the interface v in LE
|
// into the interface v in little-endian format
|
||||||
func (r *BinReader) Read(v interface{}) {
|
func (r *BinReader) ReadLE(v interface{}) {
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Err = binary.Read(r.R, binary.LittleEndian, v)
|
r.Err = binary.Read(r.R, binary.LittleEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadBigEnd reads from the underlying io.Reader
|
// ReadBE reads from the underlying io.Reader
|
||||||
// into the interface v in BE
|
// into the interface v in big-endian format
|
||||||
func (r *BinReader) ReadBigEnd(v interface{}) {
|
func (r *BinReader) ReadBE(v interface{}) {
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Err = binary.Read(r.R, binary.BigEndian, v)
|
r.Err = binary.Read(r.R, binary.BigEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
//VarUint reads a variable integer from the
|
// ReadVarUint reads a variable-length-encoded integer from the
|
||||||
// underlying reader
|
// underlying reader
|
||||||
func (r *BinReader) VarUint() uint64 {
|
func (r *BinReader) ReadVarUint() uint64 {
|
||||||
|
if r.Err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
var b uint8
|
var b uint8
|
||||||
r.Err = binary.Read(r.R, binary.LittleEndian, &b)
|
r.Err = binary.Read(r.R, binary.LittleEndian, &b)
|
||||||
|
|
||||||
|
@ -55,17 +59,17 @@ func (r *BinReader) VarUint() uint64 {
|
||||||
return uint64(b)
|
return uint64(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarBytes reads the next set of bytes from the underlying reader.
|
// ReadBytes reads the next set of bytes from the underlying reader.
|
||||||
// VarUInt is used to determine how large that slice is
|
// ReadVarUInt() is used to determine how large that slice is
|
||||||
func (r *BinReader) VarBytes() []byte {
|
func (r *BinReader) ReadBytes() []byte {
|
||||||
n := r.VarUint()
|
n := r.ReadVarUint()
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
r.Read(b)
|
r.ReadLE(b)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarString calls VarBytes and casts the results as a string
|
// ReadString calls ReadBytes and casts the results as a string
|
||||||
func (r *BinReader) VarString() string {
|
func (r *BinReader) ReadString() string {
|
||||||
b := r.VarBytes()
|
b := r.ReadBytes()
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
|
@ -14,25 +14,28 @@ type BinWriter struct {
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes into the underlying io.Writer from an object v in LE format
|
// WriteLE writes into the underlying io.Writer from an object v in little-endian format
|
||||||
func (w *BinWriter) Write(v interface{}) {
|
func (w *BinWriter) WriteLE(v interface{}) {
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Err = binary.Write(w.W, binary.LittleEndian, v)
|
w.Err = binary.Write(w.W, binary.LittleEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteBigEnd writes into the underlying io.Writer from an object v in BE format
|
// WriteBE writes into the underlying io.Writer from an object v in big-endian format
|
||||||
// Only used for IP and PORT. Additional method makes the default LittleEndian case clean
|
func (w *BinWriter) WriteBE(v interface{}) {
|
||||||
func (w *BinWriter) WriteBigEnd(v interface{}) {
|
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Err = binary.Write(w.W, binary.BigEndian, v)
|
w.Err = binary.Write(w.W, binary.BigEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarUint writes a uint64 into the underlying writer
|
// WriteVarUint writes a uint64 into the underlying writer using variable-length encoding
|
||||||
func (w *BinWriter) VarUint(val uint64) {
|
func (w *BinWriter) WriteVarUint(val uint64) {
|
||||||
|
if w.Err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
w.Err = errors.New("value out of range")
|
w.Err = errors.New("value out of range")
|
||||||
return
|
return
|
||||||
|
@ -63,13 +66,13 @@ func (w *BinWriter) VarUint(val uint64) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarBytes writes a variable length byte array into the underlying io.Writer
|
// WriteBytes writes a variable length byte array into the underlying io.Writer
|
||||||
func (w *BinWriter) VarBytes(b []byte) {
|
func (w *BinWriter) WriteBytes(b []byte) {
|
||||||
w.VarUint(uint64(len(b)))
|
w.WriteVarUint(uint64(len(b)))
|
||||||
w.Write(b)
|
w.WriteLE(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
//VarString casts the string as a byte slice and calls VarBytes
|
// WriteString writes a variable length string into the underlying io.Writer
|
||||||
func (w *BinWriter) VarString(s string) {
|
func (w *BinWriter) WriteString(s string) {
|
||||||
w.VarBytes([]byte(s))
|
w.WriteBytes([]byte(s))
|
||||||
}
|
}
|
|
@ -12,9 +12,9 @@ func TestWriteVarUint1(t *testing.T) {
|
||||||
val = uint64(1)
|
val = uint64(1)
|
||||||
buf = new(bytes.Buffer)
|
buf = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
if err := WriteVarUint(buf, val); err != nil {
|
bw := BinWriter{W: buf}
|
||||||
t.Fatal(err)
|
bw.WriteVarUint(val)
|
||||||
}
|
assert.Nil(t, bw.Err)
|
||||||
assert.Equal(t, 1, buf.Len())
|
assert.Equal(t, 1, buf.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,13 +23,14 @@ func TestWriteVarUint1000(t *testing.T) {
|
||||||
val = uint64(1000)
|
val = uint64(1000)
|
||||||
buf = new(bytes.Buffer)
|
buf = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
|
bw := BinWriter{W: buf}
|
||||||
if err := WriteVarUint(buf, val); err != nil {
|
bw.WriteVarUint(val)
|
||||||
t.Fatal(err)
|
assert.Nil(t, bw.Err)
|
||||||
}
|
|
||||||
assert.Equal(t, 3, buf.Len())
|
assert.Equal(t, 3, buf.Len())
|
||||||
assert.Equal(t, byte(0xfd), buf.Bytes()[0])
|
assert.Equal(t, byte(0xfd), buf.Bytes()[0])
|
||||||
res := ReadVarUint(buf)
|
br := BinReader{R: buf}
|
||||||
|
res := br.ReadVarUint()
|
||||||
|
assert.Nil(t, br.Err)
|
||||||
assert.Equal(t, val, res)
|
assert.Equal(t, val, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,13 +39,14 @@ func TestWriteVarUint100000(t *testing.T) {
|
||||||
val = uint64(100000)
|
val = uint64(100000)
|
||||||
buf = new(bytes.Buffer)
|
buf = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
|
bw := BinWriter{W: buf}
|
||||||
if err := WriteVarUint(buf, val); err != nil {
|
bw.WriteVarUint(val)
|
||||||
t.Fatal(err)
|
assert.Nil(t, bw.Err)
|
||||||
}
|
|
||||||
assert.Equal(t, 5, buf.Len())
|
assert.Equal(t, 5, buf.Len())
|
||||||
assert.Equal(t, byte(0xfe), buf.Bytes()[0])
|
assert.Equal(t, byte(0xfe), buf.Bytes()[0])
|
||||||
res := ReadVarUint(buf)
|
br := BinReader{R: buf}
|
||||||
|
res := br.ReadVarUint()
|
||||||
|
assert.Nil(t, br.Err)
|
||||||
assert.Equal(t, val, res)
|
assert.Equal(t, val, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,12 +55,13 @@ func TestWriteVarUint100000000000(t *testing.T) {
|
||||||
val = uint64(1000000000000)
|
val = uint64(1000000000000)
|
||||||
buf = new(bytes.Buffer)
|
buf = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
|
bw := BinWriter{W: buf}
|
||||||
if err := WriteVarUint(buf, val); err != nil {
|
bw.WriteVarUint(val)
|
||||||
t.Fatal(err)
|
assert.Nil(t, bw.Err)
|
||||||
}
|
|
||||||
assert.Equal(t, 9, buf.Len())
|
assert.Equal(t, 9, buf.Len())
|
||||||
assert.Equal(t, byte(0xff), buf.Bytes()[0])
|
assert.Equal(t, byte(0xff), buf.Bytes()[0])
|
||||||
res := ReadVarUint(buf)
|
br := BinReader{R: buf}
|
||||||
|
res := br.ReadVarUint()
|
||||||
|
assert.Nil(t, br.Err)
|
||||||
assert.Equal(t, val, res)
|
assert.Equal(t, val, res)
|
||||||
}
|
}
|
103
pkg/util/io.go
103
pkg/util/io.go
|
@ -1,103 +0,0 @@
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Variable length integer, can be encoded to save space according to the value typed.
|
|
||||||
// len 1 uint8
|
|
||||||
// len 3 0xfd + uint16
|
|
||||||
// len 5 0xfe = uint32
|
|
||||||
// len 9 0xff = uint64
|
|
||||||
// For more information about this:
|
|
||||||
// https://github.com/neo-project/neo/wiki/Network-Protocol
|
|
||||||
|
|
||||||
// ReadVarUint reads a variable unsigned integer and returns it as a uint64.
|
|
||||||
func ReadVarUint(r io.Reader) uint64 {
|
|
||||||
var b uint8
|
|
||||||
binary.Read(r, binary.LittleEndian, &b)
|
|
||||||
switch b {
|
|
||||||
case 0xfd:
|
|
||||||
var v uint16
|
|
||||||
binary.Read(r, binary.LittleEndian, &v)
|
|
||||||
return uint64(v)
|
|
||||||
case 0xfe:
|
|
||||||
var v uint32
|
|
||||||
binary.Read(r, binary.LittleEndian, &v)
|
|
||||||
return uint64(v)
|
|
||||||
case 0xff:
|
|
||||||
var v uint64
|
|
||||||
binary.Read(r, binary.LittleEndian, &v)
|
|
||||||
return v
|
|
||||||
default:
|
|
||||||
return uint64(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarUint writes a variable unsigned integer.
|
|
||||||
func WriteVarUint(w io.Writer, val uint64) error {
|
|
||||||
if val < 0xfd {
|
|
||||||
return binary.Write(w, binary.LittleEndian, uint8(val))
|
|
||||||
}
|
|
||||||
if val < 0xFFFF {
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, byte(0xfd)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, uint16(val))
|
|
||||||
}
|
|
||||||
if val < 0xFFFFFFFF {
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, byte(0xfe)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, uint32(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := binary.Write(w, binary.LittleEndian, byte(0xff)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return binary.Write(w, binary.LittleEndian, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarBytes reads a variable length byte array.
|
|
||||||
func ReadVarBytes(r io.Reader) ([]byte, error) {
|
|
||||||
n := ReadVarUint(r)
|
|
||||||
b := make([]byte, n)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, b); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarString reads a variable length string.
|
|
||||||
func ReadVarString(r io.Reader) (string, error) {
|
|
||||||
b, err := ReadVarBytes(r)
|
|
||||||
return string(b), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarString writes a variable length string.
|
|
||||||
func WriteVarString(w io.Writer, s string) error {
|
|
||||||
return WriteVarBytes(w, []byte(s))
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarBytes writes a variable length byte array.
|
|
||||||
func WriteVarBytes(w io.Writer, b []byte) error {
|
|
||||||
if err := WriteVarUint(w, uint64(len(b))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binary.Write(w, binary.LittleEndian, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read2000Uint256Hashes attempt to read 2000 Uint256 hashes from
|
|
||||||
// the given byte array.
|
|
||||||
func Read2000Uint256Hashes(b []byte) ([]Uint256, error) {
|
|
||||||
r := bytes.NewReader(b)
|
|
||||||
lenHashes := ReadVarUint(r)
|
|
||||||
hashes := make([]Uint256, lenHashes)
|
|
||||||
if err := binary.Read(r, binary.LittleEndian, hashes); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return hashes, nil
|
|
||||||
}
|
|
Loading…
Reference in a new issue