go-ethereum/block.go

125 lines
2.7 KiB
Go
Raw Normal View History

2013-12-26 13:45:52 +02:00
package main
import (
2013-12-28 03:24:16 +02:00
"fmt"
2013-12-27 22:22:57 +02:00
"time"
2013-12-28 16:18:08 +02:00
_"bytes"
2013-12-26 13:45:52 +02:00
)
type Block struct {
2013-12-27 22:22:57 +02:00
number uint32
prevHash string
uncles []*Block
coinbase string
// state xxx
2013-12-28 16:18:08 +02:00
difficulty uint32
2013-12-27 22:22:57 +02:00
time time.Time
2013-12-28 16:18:08 +02:00
nonce uint32
2013-12-27 22:22:57 +02:00
transactions []*Transaction
2013-12-26 13:45:52 +02: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 16:18:08 +02:00
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
2013-12-27 22:22:57 +02:00
time: time.Now(),
2013-12-26 13:45:52 +02:00
}
return block
}
func (block *Block) Update() {
}
2013-12-27 22:22:57 +02: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 16:18:08 +02:00
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
2013-12-28 03:24:16 +02:00
header := []interface{}{
2013-12-27 22:22:57 +02:00
block.number,
2013-12-28 16:18:08 +02:00
block.prevHash,
2013-12-27 22:22:57 +02:00
// Sha of uncles
2013-12-28 16:18:08 +02:00
"",
block.coinbase,
2013-12-27 22:22:57 +02:00
// root state
2013-12-28 16:18:08 +02:00
"",
string(Sha256Bin([]byte(RlpEncode(encTx)))),
block.difficulty,
block.time.String(),
block.nonce,
2013-12-27 22:22:57 +02:00
// extra?
2013-12-28 03:24:16 +02:00
}
2013-12-27 22:22:57 +02:00
2013-12-28 16:18:08 +02:00
encoded := Encode([]interface{}{header, encTx})
return encoded
2013-12-27 22:22:57 +02:00
}
func (block *Block) UnmarshalRlp(data []byte) {
2013-12-28 03:24:16 +02:00
t, _ := Decode(data,0)
if slice, ok := t.([]interface{}); ok {
2013-12-28 16:18:08 +02: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 {
block.transactions = make([]*Transaction, len(txSlice))
for i, tx := range txSlice {
if t, ok := tx.([]byte); ok {
tx := &Transaction{}
tx.UnmarshalRlp(t)
block.transactions[i] = tx
}
}
2013-12-28 03:24:16 +02:00
}
}
2013-12-27 22:22:57 +02:00
}