forked from TrueCloudLab/neoneo-go
Block binary decoding + transaction types
This commit is contained in:
parent
0c9d2dd04e
commit
55b4ab4192
5 changed files with 96 additions and 15 deletions
|
@ -25,7 +25,7 @@ type Block struct {
|
||||||
// seperator ? fixed to 1
|
// seperator ? fixed to 1
|
||||||
_sep uint8
|
_sep uint8
|
||||||
// Script used to validate the block
|
// Script used to validate the block
|
||||||
Script []byte
|
Script *Witness
|
||||||
// transaction list
|
// transaction list
|
||||||
Transactions []*Transaction
|
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.Nonce)
|
||||||
err = binary.Read(r, binary.LittleEndian, &b.NextMiner)
|
err = binary.Read(r, binary.LittleEndian, &b.NextMiner)
|
||||||
err = binary.Read(r, binary.LittleEndian, &b._sep)
|
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)
|
b.Script = &Witness{}
|
||||||
// err = binary.Read(r, binary.LittleEndian, &txs)
|
if err := b.Script.DecodeBinary(r); err != nil {
|
||||||
// err = binary.Read(r, binary.LittleEndian, &n)
|
return err
|
||||||
// fmt.Println(n)
|
}
|
||||||
|
|
||||||
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,62 @@
|
||||||
package core
|
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.
|
// 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
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
// Witness ...
|
// Witness ...
|
||||||
type Witness struct {
|
type Witness struct {
|
||||||
|
@ -10,7 +13,16 @@ type Witness struct {
|
||||||
|
|
||||||
// DecodeBinary implements the payload interface.
|
// DecodeBinary implements the payload interface.
|
||||||
func (wit *Witness) DecodeBinary(r io.Reader) error {
|
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.
|
// EncodeBinary implements the payload interface.
|
||||||
|
|
|
@ -59,12 +59,13 @@ func (p *AddressList) DecodeBinary(r io.Reader) error {
|
||||||
var lenList uint8
|
var lenList uint8
|
||||||
binary.Read(r, binary.LittleEndian, &lenList)
|
binary.Read(r, binary.LittleEndian, &lenList)
|
||||||
|
|
||||||
|
p.Addrs = make([]*AddrWithTime, lenList)
|
||||||
for i := 0; i < int(4); i++ {
|
for i := 0; i < int(4); i++ {
|
||||||
address := &AddrWithTime{}
|
addr := &AddrWithTime{}
|
||||||
if err := address.DecodeBinary(r); err != nil {
|
if err := addr.DecodeBinary(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.Addrs = append(p.Addrs, address)
|
p.Addrs[i] = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -258,7 +258,8 @@ func (s *Server) handleInvCmd(inv *payload.Inventory, peer Peer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleBlockCmd(block *core.Block, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue