Block binary decoding + transaction types

This commit is contained in:
anthdm 2018-01-31 11:47:54 +01:00
parent 0c9d2dd04e
commit 55b4ab4192
5 changed files with 96 additions and 15 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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.

View file

@ -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

View file

@ -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
}