From 55b4ab4192f5db59d45022c99b0e5e872bb88d01 Mon Sep 17 00:00:00 2001 From: anthdm Date: Wed, 31 Jan 2018 11:47:54 +0100 Subject: [PATCH] Block binary decoding + transaction types --- pkg/core/block.go | 25 +++++++++++----- pkg/core/transaction.go | 60 ++++++++++++++++++++++++++++++++++++- pkg/core/witness.go | 16 ++++++++-- pkg/network/payload/addr.go | 7 +++-- pkg/network/server.go | 3 +- 5 files changed, 96 insertions(+), 15 deletions(-) diff --git a/pkg/core/block.go b/pkg/core/block.go index 1b5016a1c..5e8eece3c 100644 --- a/pkg/core/block.go +++ b/pkg/core/block.go @@ -25,7 +25,7 @@ type Block struct { // seperator ? fixed to 1 _sep uint8 // Script used to validate the block - Script []byte + Script *Witness // transaction list Transactions []*Transaction } @@ -45,14 +45,23 @@ func (b *Block) DecodeBinary(r io.Reader) error { err = binary.Read(r, binary.LittleEndian, &b.Nonce) err = binary.Read(r, binary.LittleEndian, &b.NextMiner) err = binary.Read(r, binary.LittleEndian, &b._sep) - var n uint8 - err = binary.Read(r, binary.LittleEndian, &n) - err = binary.Read(r, binary.LittleEndian, &n) - // txs := make([]byte, n) - // err = binary.Read(r, binary.LittleEndian, &txs) - // err = binary.Read(r, binary.LittleEndian, &n) - // fmt.Println(n) + b.Script = &Witness{} + if err := b.Script.DecodeBinary(r); err != nil { + return err + } + + var lentx uint8 + err = binary.Read(r, binary.LittleEndian, &lentx) + + b.Transactions = make([]*Transaction, lentx) + for i := 0; i < int(lentx); i++ { + tx := &Transaction{} + if err := tx.DecodeBinary(r); err != nil { + return err + } + b.Transactions[i] = tx + } return err } diff --git a/pkg/core/transaction.go b/pkg/core/transaction.go index 905a7b158..da582461f 100644 --- a/pkg/core/transaction.go +++ b/pkg/core/transaction.go @@ -1,4 +1,62 @@ package core +import ( + "encoding/binary" + "io" +) + +// TransactionType is the type of a transaction. +type TransactionType uint8 + +// String implements the stringer interface. +func (t TransactionType) String() string { + switch t { + case MinerTX: + return "miner transaction" + case IssueTX: + return "issue transaction" + case ClaimTX: + return "claim transaction" + case EnrollmentTX: + return "enrollment transaction" + case VotingTX: + return "voting transaction" + case RegisterTX: + return "register transaction" + case ContractTX: + return "contract transaction" + case AgencyTX: + return "agency transaction" + default: + return "" + } +} + +// All processes in NEO system are recorded in transactions. +// There are several types of transactions. +const ( + MinerTX TransactionType = 0x00 + IssueTX = 0x01 + ClaimTX = 0x02 + EnrollmentTX = 0x20 + VotingTX = 0x24 + RegisterTX = 0x40 + ContractTX = 0x80 + AgencyTX = 0xb0 +) + // Transaction is a process recorded in the NEO system. -type Transaction struct{} +type Transaction struct { + Type TransactionType +} + +// DecodeBinary implements the payload interface. +func (t *Transaction) DecodeBinary(r io.Reader) error { + err := binary.Read(r, binary.LittleEndian, &t.Type) + return err +} + +// EncodeBinary implements the payload interface. +func (t *Transaction) EncodeBinary(w io.Writer) error { + return nil +} diff --git a/pkg/core/witness.go b/pkg/core/witness.go index 2b30be5a6..e9256c358 100644 --- a/pkg/core/witness.go +++ b/pkg/core/witness.go @@ -1,6 +1,9 @@ package core -import "io" +import ( + "encoding/binary" + "io" +) // Witness ... type Witness struct { @@ -10,7 +13,16 @@ type Witness struct { // DecodeBinary implements the payload interface. func (wit *Witness) DecodeBinary(r io.Reader) error { - return nil + var lenb uint8 + + err := binary.Read(r, binary.LittleEndian, &lenb) + wit.InvocationScript = make([]byte, lenb) + binary.Read(r, binary.LittleEndian, &wit.InvocationScript) + err = binary.Read(r, binary.LittleEndian, &lenb) + wit.VerificationScript = make([]byte, lenb) + binary.Read(r, binary.LittleEndian, &wit.VerificationScript) + + return err } // EncodeBinary implements the payload interface. diff --git a/pkg/network/payload/addr.go b/pkg/network/payload/addr.go index 0403161ec..b5376524a 100644 --- a/pkg/network/payload/addr.go +++ b/pkg/network/payload/addr.go @@ -59,12 +59,13 @@ func (p *AddressList) DecodeBinary(r io.Reader) error { var lenList uint8 binary.Read(r, binary.LittleEndian, &lenList) + p.Addrs = make([]*AddrWithTime, lenList) for i := 0; i < int(4); i++ { - address := &AddrWithTime{} - if err := address.DecodeBinary(r); err != nil { + addr := &AddrWithTime{} + if err := addr.DecodeBinary(r); err != nil { return err } - p.Addrs = append(p.Addrs, address) + p.Addrs[i] = addr } return nil diff --git a/pkg/network/server.go b/pkg/network/server.go index 56e71d0e5..30a003bfb 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -258,7 +258,8 @@ func (s *Server) handleInvCmd(inv *payload.Inventory, peer Peer) error { } func (s *Server) handleBlockCmd(block *core.Block, peer Peer) error { - fmt.Println("received a block yyyyyyeeeeeehhhhh!") + fmt.Println("Block received") + fmt.Printf("%+v\n", block) return nil }