quorum/block.go

139 lines
3.2 KiB
Go
Raw Normal View History

2013-12-26 03:45:52 -08:00
package main
import (
2013-12-27 17:24:16 -08:00
"fmt"
2013-12-27 12:22:57 -08:00
"time"
2013-12-28 06:18:08 -08:00
_"bytes"
2013-12-26 03:45:52 -08:00
)
type Block struct {
2013-12-28 16:36:59 -08:00
// The number of this block
2013-12-27 12:22:57 -08:00
number uint32
2013-12-28 16:36:59 -08:00
// Hash to the previous block
2013-12-27 12:22:57 -08:00
prevHash string
2013-12-28 16:36:59 -08:00
// Uncles of this block
2013-12-27 12:22:57 -08:00
uncles []*Block
coinbase string
// state xxx
2013-12-28 06:18:08 -08:00
difficulty uint32
2013-12-28 16:36:59 -08:00
// Creation time
2013-12-27 12:22:57 -08:00
time time.Time
2013-12-28 06:18:08 -08:00
nonce uint32
2013-12-28 16:36:59 -08:00
// List of transactions and/or contracts
2013-12-27 12:22:57 -08:00
transactions []*Transaction
2013-12-26 03:45:52 -08:00
}
2013-12-28 16:36:59 -08:00
// Creates a new block. This is currently for testing
2013-12-26 03:45:52 -08:00
func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
2013-12-28 06:18:08 -08:00
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
2013-12-27 12:22:57 -08:00
time: time.Now(),
2013-12-26 03:45:52 -08:00
}
return block
}
func (block *Block) Update() {
}
2013-12-27 12:22:57 -08:00
2013-12-28 16:36:59 -08:00
// Returns a hash of the block
2013-12-27 12:22:57 -08:00
func (block *Block) Hash() string {
return Sha256Hex(block.MarshalRlp())
}
func (block *Block) MarshalRlp() []byte {
2013-12-28 16:36:59 -08:00
// Marshal the transactions of this block
2013-12-27 12:22:57 -08:00
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
2013-12-28 16:36:59 -08:00
// Cast it to a string (safe)
2013-12-27 12:22:57 -08:00
encTx[i] = string(tx.MarshalRlp())
}
2013-12-28 06:18:08 -08:00
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
2013-12-27 17:24:16 -08:00
header := []interface{}{
2013-12-27 12:22:57 -08:00
block.number,
2013-12-28 06:18:08 -08:00
block.prevHash,
2013-12-27 12:22:57 -08:00
// Sha of uncles
2013-12-28 06:18:08 -08:00
"",
block.coinbase,
2013-12-27 12:22:57 -08:00
// root state
2013-12-28 06:18:08 -08:00
"",
string(Sha256Bin([]byte(RlpEncode(encTx)))),
block.difficulty,
block.time.String(),
block.nonce,
2013-12-27 12:22:57 -08:00
// extra?
2013-12-27 17:24:16 -08:00
}
2013-12-27 12:22:57 -08:00
2013-12-28 16:36:59 -08:00
// Encode a slice interface which contains the header and the list of transactions.
return Encode([]interface{}{header, encTx})
2013-12-27 12:22:57 -08:00
}
func (block *Block) UnmarshalRlp(data []byte) {
2013-12-27 17:24:16 -08:00
t, _ := Decode(data,0)
2013-12-28 16:36:59 -08:00
// interface slice assertion
2013-12-27 17:24:16 -08:00
if slice, ok := t.([]interface{}); ok {
2013-12-28 16:36:59 -08:00
// interface slice assertion
2013-12-28 06:18:08 -08:00
if header, ok := slice[0].([]interface{}); ok {
if number, ok := header[0].(uint8); ok {
block.number = uint32(number)
}
if prevHash, ok := header[1].([]byte); ok {
block.prevHash = string(prevHash)
}
// sha of uncles is header[2]
if coinbase, ok := header[3].([]byte); ok {
block.coinbase = string(coinbase)
}
// state is header[header[4]
// sha is header[5]
// It's either 8bit or 64
if difficulty, ok := header[6].(uint8); ok {
block.difficulty = uint32(difficulty)
}
if difficulty, ok := header[6].(uint64); ok {
block.difficulty = uint32(difficulty)
}
if time, ok := header[7].([]byte); ok {
fmt.Sprintf("Time is: ", string(time))
}
if nonce, ok := header[8].(uint8); ok {
block.nonce = uint32(nonce)
}
}
if txSlice, ok := slice[1].([]interface{}); ok {
2013-12-28 16:36:59 -08:00
// Create transaction slice equal to decoded tx interface slice
2013-12-28 06:18:08 -08:00
block.transactions = make([]*Transaction, len(txSlice))
2013-12-28 16:36:59 -08:00
// Unmarshal transactions
2013-12-28 06:18:08 -08:00
for i, tx := range txSlice {
if t, ok := tx.([]byte); ok {
tx := &Transaction{}
2013-12-28 16:36:59 -08:00
// Use the unmarshaled data to unmarshal the transaction
// t is still decoded.
2013-12-28 06:18:08 -08:00
tx.UnmarshalRlp(t)
block.transactions[i] = tx
}
}
2013-12-27 17:24:16 -08:00
}
}
2013-12-27 12:22:57 -08:00
}