Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/block.go

139 lines
3.2 KiB

11 years ago
package main
import (
11 years ago
"fmt"
11 years ago
"time"
_"bytes"
11 years ago
)
type Block struct {
11 years ago
// The number of this block
11 years ago
number uint32
11 years ago
// Hash to the previous block
11 years ago
prevHash string
11 years ago
// Uncles of this block
11 years ago
uncles []*Block
coinbase string
// state xxx
difficulty uint32
11 years ago
// Creation time
11 years ago
time time.Time
nonce uint32
11 years ago
// List of transactions and/or contracts
11 years ago
transactions []*Transaction
11 years ago
}
11 years ago
// Creates a new block. This is currently for testing
11 years ago
func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
11 years ago
time: time.Now(),
11 years ago
}
return block
}
func (block *Block) Update() {
}
11 years ago
11 years ago
// Returns a hash of the block
11 years ago
func (block *Block) Hash() string {
return Sha256Hex(block.MarshalRlp())
}
func (block *Block) MarshalRlp() []byte {
11 years ago
// Marshal the transactions of this block
11 years ago
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
11 years ago
// Cast it to a string (safe)
11 years ago
encTx[i] = string(tx.MarshalRlp())
}
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
11 years ago
header := []interface{}{
11 years ago
block.number,
block.prevHash,
11 years ago
// Sha of uncles
"",
block.coinbase,
11 years ago
// root state
"",
string(Sha256Bin([]byte(RlpEncode(encTx)))),
block.difficulty,
block.time.String(),
block.nonce,
11 years ago
// extra?
11 years ago
}
11 years ago
11 years ago
// Encode a slice interface which contains the header and the list of transactions.
return Encode([]interface{}{header, encTx})
11 years ago
}
func (block *Block) UnmarshalRlp(data []byte) {
11 years ago
t, _ := Decode(data,0)
11 years ago
// interface slice assertion
11 years ago
if slice, ok := t.([]interface{}); ok {
11 years ago
// interface slice assertion
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 {
11 years ago
// Create transaction slice equal to decoded tx interface slice
block.transactions = make([]*Transaction, len(txSlice))
11 years ago
// Unmarshal transactions
for i, tx := range txSlice {
if t, ok := tx.([]byte); ok {
tx := &Transaction{}
11 years ago
// Use the unmarshaled data to unmarshal the transaction
// t is still decoded.
tx.UnmarshalRlp(t)
block.transactions[i] = tx
}
}
11 years ago
}
}
11 years ago
}