quorum/block.go

72 lines
1.4 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-26 03:45:52 -08:00
)
type Block struct {
2013-12-27 12:22:57 -08:00
RlpSerializer
number uint32
prevHash string
uncles []*Block
coinbase string
// state xxx
difficulty int
time time.Time
nonce int
transactions []*Transaction
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-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
func (block *Block) Hash() string {
return Sha256Hex(block.MarshalRlp())
}
func (block *Block) MarshalRlp() []byte {
// Encoding method requires []interface{} type. It's actual a slice of strings
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
encTx[i] = string(tx.MarshalRlp())
}
2013-12-27 17:24:16 -08:00
header := []interface{}{
2013-12-27 12:22:57 -08:00
block.number,
2013-12-27 17:24:16 -08:00
//block.prevHash,
2013-12-27 12:22:57 -08:00
// Sha of uncles
2013-12-27 17:24:16 -08:00
//block.coinbase,
2013-12-27 12:22:57 -08:00
// root state
2013-12-27 17:24:16 -08:00
//Sha256Bin([]byte(RlpEncode(encTx))),
//block.difficulty,
//block.time,
//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-27 17:24:16 -08:00
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
fmt.Printf("%q\n", data)
t, _ := Decode(data,0)
if slice, ok := t.([]interface{}); ok {
if txes, ok := slice[1].([]interface{}); ok {
fmt.Println(txes[0])
}
}
2013-12-27 12:22:57 -08:00
}