2013-12-26 11:45:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-12-28 01:24:16 +00:00
|
|
|
"fmt"
|
2013-12-27 20:22:57 +00:00
|
|
|
"time"
|
2013-12-26 11:45:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Block struct {
|
2013-12-27 20:22:57 +00:00
|
|
|
RlpSerializer
|
|
|
|
|
|
|
|
number uint32
|
|
|
|
prevHash string
|
|
|
|
uncles []*Block
|
|
|
|
coinbase string
|
|
|
|
// state xxx
|
|
|
|
difficulty int
|
|
|
|
time time.Time
|
|
|
|
nonce int
|
|
|
|
transactions []*Transaction
|
2013-12-26 11:45:52 +00: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 20:22:57 +00:00
|
|
|
|
|
|
|
time: time.Now(),
|
2013-12-26 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
func (block *Block) Update() {
|
|
|
|
}
|
2013-12-27 20:22:57 +00: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-28 01:24:16 +00:00
|
|
|
header := []interface{}{
|
2013-12-27 20:22:57 +00:00
|
|
|
block.number,
|
2013-12-28 01:24:16 +00:00
|
|
|
//block.prevHash,
|
2013-12-27 20:22:57 +00:00
|
|
|
// Sha of uncles
|
2013-12-28 01:24:16 +00:00
|
|
|
//block.coinbase,
|
2013-12-27 20:22:57 +00:00
|
|
|
// root state
|
2013-12-28 01:24:16 +00:00
|
|
|
//Sha256Bin([]byte(RlpEncode(encTx))),
|
|
|
|
//block.difficulty,
|
|
|
|
//block.time,
|
|
|
|
//block.nonce,
|
2013-12-27 20:22:57 +00:00
|
|
|
// extra?
|
2013-12-28 01:24:16 +00:00
|
|
|
}
|
2013-12-27 20:22:57 +00:00
|
|
|
|
2013-12-28 01:24:16 +00:00
|
|
|
return Encode([]interface{}{header, encTx})
|
2013-12-27 20:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (block *Block) UnmarshalRlp(data []byte) {
|
2013-12-28 01:24:16 +00: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 20:22:57 +00:00
|
|
|
}
|