Updated serialisation

This commit is contained in:
obscuren 2013-12-29 23:53:12 +01:00
parent 0747aa3a3b
commit 0edcbc695e
1 changed files with 31 additions and 11 deletions

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"fmt" _"fmt"
"time" "time"
_"bytes" _"bytes"
) )
@ -17,14 +17,24 @@ type Block struct {
// state xxx // state xxx
difficulty uint32 difficulty uint32
// Creation time // Creation time
time time.Time time int64
nonce uint32 nonce uint32
// List of transactions and/or contracts // List of transactions and/or contracts
transactions []*Transaction transactions []*Transaction
extra string
}
// New block takes a raw encoded string
func NewBlock(raw []byte) *Block {
block := &Block{}
block.UnmarshalRlp(raw)
return block
} }
// Creates a new block. This is currently for testing // Creates a new block. This is currently for testing
func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block { func CreateBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{ block := &Block{
// Slice of transactions to include in this block // Slice of transactions to include in this block
transactions: transactions, transactions: transactions,
@ -33,8 +43,7 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
coinbase: "me", coinbase: "me",
difficulty: 10, difficulty: 10,
nonce: 0, nonce: 0,
time: time.Now().Unix(),
time: time.Now(),
} }
return block return block
@ -65,15 +74,18 @@ func (block *Block) MarshalRlp() []byte {
block.coinbase, block.coinbase,
// root state // root state
"", "",
string(Sha256Bin([]byte(RlpEncode(encTx)))), // Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
block.difficulty, block.difficulty,
block.time.String(), uint64(block.time),
block.nonce, block.nonce,
// extra? block.extra,
} }
// TODO
uncles := []interface{}{}
// Encode a slice interface which contains the header and the list of transactions. // Encode a slice interface which contains the header and the list of transactions.
return Encode([]interface{}{header, encTx}) return Encode([]interface{}{header, encTx, uncles})
} }
func (block *Block) UnmarshalRlp(data []byte) { func (block *Block) UnmarshalRlp(data []byte) {
@ -109,13 +121,21 @@ func (block *Block) UnmarshalRlp(data []byte) {
block.difficulty = uint32(difficulty) block.difficulty = uint32(difficulty)
} }
if time, ok := header[7].([]byte); ok { // It's either 8bit or 64
fmt.Sprintf("Time is: ", string(time)) if time, ok := header[7].(uint8); ok {
block.time = int64(time)
}
if time, ok := header[7].(uint64); ok {
block.time = int64(time)
} }
if nonce, ok := header[8].(uint8); ok { if nonce, ok := header[8].(uint8); ok {
block.nonce = uint32(nonce) block.nonce = uint32(nonce)
} }
if extra, ok := header[9].([]byte); ok {
block.extra = string(extra)
}
} }
if txSlice, ok := slice[1].([]interface{}); ok { if txSlice, ok := slice[1].([]interface{}); ok {