bug fixes (TCP + uint256) and started core part (#14)
* Fixed TCP read + Uint256 reversed array + started on some core pieces * Disabled some debug output + muted test * 0.5.0
This commit is contained in:
parent
5aed624f1d
commit
628656483a
27 changed files with 824 additions and 337 deletions
|
@ -4,39 +4,113 @@ import (
|
|||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
. "github.com/CityOfZion/neo-go/pkg/util"
|
||||
"github.com/CityOfZion/neo-go/pkg/util"
|
||||
)
|
||||
|
||||
// BlockBase holds the base info of a block
|
||||
type BlockBase struct {
|
||||
Version uint32
|
||||
// hash of the previous block.
|
||||
PrevBlock Uint256
|
||||
PrevHash util.Uint256
|
||||
// Root hash of a transaction list.
|
||||
MerkleRoot Uint256
|
||||
MerkleRoot util.Uint256
|
||||
// The time stamp of each block must be later than previous block's time stamp.
|
||||
// Generally the difference of two block's time stamp is about 15 seconds and imprecision is allowed.
|
||||
// The height of the block must be exactly equal to the height of the previous block plus 1.
|
||||
Timestamp uint32
|
||||
// height of the block
|
||||
Height uint32
|
||||
// Random number
|
||||
Nonce uint64
|
||||
// index/height of the block
|
||||
Index uint32
|
||||
// Random number also called nonce
|
||||
ConsensusData uint64
|
||||
// contract addresss of the next miner
|
||||
NextMiner Uint160
|
||||
NextConsensus util.Uint160
|
||||
// fixed to 1
|
||||
_sep uint8
|
||||
_ uint8 // padding
|
||||
// Script used to validate the block
|
||||
Script *Witness
|
||||
}
|
||||
|
||||
// BlockHead holds the head info of a block
|
||||
type BlockHead struct {
|
||||
// DecodeBinary implements the payload interface.
|
||||
func (b *BlockBase) DecodeBinary(r io.Reader) error {
|
||||
binary.Read(r, binary.LittleEndian, &b.Version)
|
||||
binary.Read(r, binary.LittleEndian, &b.PrevHash)
|
||||
binary.Read(r, binary.LittleEndian, &b.MerkleRoot)
|
||||
binary.Read(r, binary.LittleEndian, &b.Timestamp)
|
||||
binary.Read(r, binary.LittleEndian, &b.Index)
|
||||
binary.Read(r, binary.LittleEndian, &b.ConsensusData)
|
||||
binary.Read(r, binary.LittleEndian, &b.NextConsensus)
|
||||
|
||||
var padding uint8
|
||||
binary.Read(r, binary.LittleEndian, &padding)
|
||||
if padding != 1 {
|
||||
return fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
||||
}
|
||||
|
||||
b.Script = &Witness{}
|
||||
return b.Script.DecodeBinary(r)
|
||||
}
|
||||
|
||||
// Hash return the hash of the block.
|
||||
// When calculating the hash value of the block, instead of calculating the entire block,
|
||||
// only first seven fields in the block head will be calculated, which are
|
||||
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
|
||||
// Since MerkleRoot already contains the hash value of all transactions,
|
||||
// the modification of transaction will influence the hash value of the block.
|
||||
func (b *BlockBase) Hash() (hash util.Uint256, err error) {
|
||||
buf := new(bytes.Buffer)
|
||||
if err = b.encodeHashableFields(buf); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Double hash the encoded fields.
|
||||
hash = sha256.Sum256(buf.Bytes())
|
||||
hash = sha256.Sum256(hash.ToSlice())
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
// encodeHashableFields will only encode the fields used for hashing.
|
||||
// see Hash() for more information about the fields.
|
||||
func (b *BlockBase) encodeHashableFields(w io.Writer) error {
|
||||
err := binary.Write(w, binary.LittleEndian, &b.Version)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.PrevHash)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.MerkleRoot)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.Timestamp)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.Index)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.ConsensusData)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.NextConsensus)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Header holds the head info of a block
|
||||
type Header struct {
|
||||
BlockBase
|
||||
// fixed to 0
|
||||
_sep1 uint8
|
||||
_ uint8 // padding
|
||||
}
|
||||
|
||||
// DecodeBinary impelements the Payload interface.
|
||||
func (h *Header) DecodeBinary(r io.Reader) error {
|
||||
if err := h.BlockBase.DecodeBinary(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var padding uint8
|
||||
binary.Read(r, binary.LittleEndian, &padding)
|
||||
if padding != 0 {
|
||||
return fmt.Errorf("format error: padding must equal 0 got %d", padding)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EncodeBinary impelements the Payload interface.
|
||||
func (h *Header) EncodeBinary(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Block represents one block in the chain.
|
||||
|
@ -46,18 +120,33 @@ type Block struct {
|
|||
Transactions []*Transaction
|
||||
}
|
||||
|
||||
// encodeHashableFields will only encode the fields used for hashing.
|
||||
// see Hash() for more information about the fields.
|
||||
func (b *Block) encodeHashableFields(w io.Writer) error {
|
||||
err := binary.Write(w, binary.LittleEndian, &b.Version)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.PrevBlock)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.MerkleRoot)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.Timestamp)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.Height)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.Nonce)
|
||||
err = binary.Write(w, binary.LittleEndian, &b.NextMiner)
|
||||
// Header returns a pointer to the head of the block (BlockHead).
|
||||
func (b *Block) Header() *Header {
|
||||
return &Header{
|
||||
BlockBase: b.BlockBase,
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
// Verify the integrity of the block.
|
||||
func (b *Block) Verify(full bool) bool {
|
||||
// The first TX has to be a miner transaction.
|
||||
if b.Transactions[0].Type != MinerTX {
|
||||
return false
|
||||
}
|
||||
|
||||
// If the first TX is a minerTX then all others cant.
|
||||
for _, tx := range b.Transactions[1:] {
|
||||
if tx.Type == MinerTX {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: When full is true, do a full verification.
|
||||
if full {
|
||||
log.Println("full verification of blocks is not yet implemented")
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// EncodeBinary encodes the block to the given writer.
|
||||
|
@ -65,25 +154,13 @@ func (b *Block) EncodeBinary(w io.Writer) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// DecodeBinary decods the block from the given reader.
|
||||
// DecodeBinary decodes the block from the given reader.
|
||||
func (b *Block) DecodeBinary(r io.Reader) error {
|
||||
err := binary.Read(r, binary.LittleEndian, &b.Version)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.PrevBlock)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.MerkleRoot)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.Timestamp)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.Height)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.Nonce)
|
||||
err = binary.Read(r, binary.LittleEndian, &b.NextMiner)
|
||||
err = binary.Read(r, binary.LittleEndian, &b._sep)
|
||||
|
||||
b.Script = &Witness{}
|
||||
if err := b.Script.DecodeBinary(r); err != nil {
|
||||
if err := b.BlockBase.DecodeBinary(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var lentx uint8
|
||||
err = binary.Read(r, binary.LittleEndian, &lentx)
|
||||
|
||||
lentx := util.ReadVarUint(r)
|
||||
b.Transactions = make([]*Transaction, lentx)
|
||||
for i := 0; i < int(lentx); i++ {
|
||||
tx := &Transaction{}
|
||||
|
@ -93,23 +170,5 @@ func (b *Block) DecodeBinary(r io.Reader) error {
|
|||
b.Transactions[i] = tx
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash return the hash of the block.
|
||||
// When calculating the hash value of the block, instead of calculating the entire block,
|
||||
// only first seven fields in the block head will be calculated, which are
|
||||
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
|
||||
// Since MerkleRoot already contains the hash value of all transactions,
|
||||
// the modification of transaction will influence the hash value of the block.
|
||||
func (b *Block) Hash() (hash Uint256, err error) {
|
||||
buf := new(bytes.Buffer)
|
||||
if err = b.encodeHashableFields(buf); err != nil {
|
||||
return
|
||||
}
|
||||
hash = sha256.Sum256(buf.Bytes())
|
||||
return
|
||||
}
|
||||
|
||||
// Size implements the payload interface.
|
||||
func (b *Block) Size() uint32 { return 0 }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue