go-ethereum/block.go

234 lines
5.9 KiB
Go
Raw Normal View History

2013-12-26 13:45:52 +02:00
package main
import (
"fmt"
2013-12-27 22:22:57 +02:00
"time"
2013-12-28 16:18:08 +02:00
_"bytes"
2014-01-03 01:43:49 +02:00
_"encoding/hex"
2013-12-26 13:45:52 +02:00
)
type Block struct {
2013-12-29 02:36:59 +02:00
// The number of this block
2013-12-27 22:22:57 +02:00
number uint32
2013-12-29 02:36:59 +02:00
// Hash to the previous block
2013-12-27 22:22:57 +02:00
prevHash string
2013-12-29 02:36:59 +02:00
// Uncles of this block
2013-12-27 22:22:57 +02:00
uncles []*Block
coinbase string
// state xxx
2014-01-03 00:02:24 +02:00
state *Trie
2013-12-28 16:18:08 +02:00
difficulty uint32
2013-12-29 02:36:59 +02:00
// Creation time
2013-12-30 00:53:12 +02:00
time int64
2013-12-28 16:18:08 +02:00
nonce uint32
2013-12-29 02:36:59 +02:00
// List of transactions and/or contracts
2013-12-27 22:22:57 +02:00
transactions []*Transaction
2013-12-30 00:53:12 +02:00
extra string
}
// New block takes a raw encoded string
func NewBlock(raw []byte) *Block {
block := &Block{}
block.UnmarshalRlp(raw)
return block
2013-12-26 13:45:52 +02:00
}
2013-12-29 02:36:59 +02:00
// Creates a new block. This is currently for testing
2014-01-03 00:02:24 +02:00
func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
2013-12-26 13:45:52 +02:00
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-30 00:53:12 +02:00
time: time.Now().Unix(),
2013-12-26 13:45:52 +02:00
}
return block
}
2014-01-03 00:02:24 +02:00
func CreateBlock(root string, num int, prevHash string, base string, difficulty int, nonce int, extra string, txes []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: txes,
number: uint32(num),
prevHash: prevHash,
coinbase: base,
difficulty: uint32(difficulty),
nonce: uint32(nonce),
time: time.Now().Unix(),
extra: extra,
}
block.state = NewTrie(Db, root)
2014-01-03 01:43:49 +02:00
2014-01-03 00:02:24 +02:00
for _, tx := range txes {
2014-01-03 01:43:49 +02:00
// Create contract if there's no recipient
if tx.recipient == "" {
addr := tx.Hash()
contract := NewContract(tx.value, []byte(""))
block.state.Update(string(addr), string(contract.MarshalRlp()))
for i, val := range tx.data {
contract.state.Update(string(NumberToBytes(uint64(i), 32)), val)
//contract.state.Update(string(Encode(uint32(i))), val)
2014-01-03 01:43:49 +02:00
}
block.UpdateContract(addr, contract)
}
2014-01-03 00:02:24 +02:00
}
return block
}
2014-01-03 01:43:49 +02:00
func (block *Block) GetContract(addr []byte) *Contract {
data := block.state.Get(string(addr))
if data == "" {
return nil
}
2014-01-03 01:43:49 +02:00
contract := &Contract{}
contract.UnmarshalRlp([]byte(data))
return contract
}
func (block *Block) UpdateContract(addr []byte, contract *Contract) {
block.state.Update(string(addr), string(contract.MarshalRlp()))
}
func (block *Block) PayFee(addr []byte, fee uint64) bool {
contract := block.GetContract(addr)
// If we can't pay the fee return
if contract == nil || contract.amount < fee {
fmt.Println("Contract has insufficient funds", contract.amount, fee)
return false
}
contract.amount -= fee
block.state.Update(string(addr), string(contract.MarshalRlp()))
data := block.state.Get(string(block.coinbase))
println(data)
// Get the ether (coinbase) and add the fee (gief fee to miner)
ether := NewEtherFromData([]byte(data))
ether.amount += fee
block.state.Update(string(block.coinbase), string(ether.MarshalRlp()))
return true
2013-12-26 13:45:52 +02:00
}
2013-12-27 22:22:57 +02:00
2013-12-29 02:36:59 +02:00
// Returns a hash of the block
2014-01-03 00:02:24 +02:00
func (block *Block) Hash() []byte {
return Sha256Bin(block.MarshalRlp())
2013-12-27 22:22:57 +02:00
}
func (block *Block) MarshalRlp() []byte {
2013-12-29 02:36:59 +02:00
// Marshal the transactions of this block
2013-12-27 22:22:57 +02:00
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
2013-12-29 02:36:59 +02:00
// Cast it to a string (safe)
2013-12-27 22:22:57 +02:00
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
2014-01-03 00:02:24 +02:00
block.state.root,
2013-12-30 00:53:12 +02:00
// Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
2013-12-28 16:18:08 +02:00
block.difficulty,
2013-12-30 00:53:12 +02:00
uint64(block.time),
2013-12-28 16:18:08 +02:00
block.nonce,
2013-12-30 00:53:12 +02:00
block.extra,
2013-12-28 03:24:16 +02:00
}
2013-12-27 22:22:57 +02:00
2013-12-30 00:53:12 +02:00
// TODO
uncles := []interface{}{}
2013-12-29 02:36:59 +02:00
// Encode a slice interface which contains the header and the list of transactions.
2013-12-30 00:53:12 +02:00
return Encode([]interface{}{header, encTx, uncles})
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)
2013-12-29 02:36:59 +02:00
// interface slice assertion
2013-12-28 03:24:16 +02:00
if slice, ok := t.([]interface{}); ok {
2013-12-29 02:36:59 +02:00
// interface slice assertion
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)
}
2014-01-03 00:02:24 +02:00
if prevHash, ok := header[1].([]uint8); ok {
2013-12-28 16:18:08 +02:00
block.prevHash = string(prevHash)
}
// sha of uncles is header[2]
if coinbase, ok := header[3].([]byte); ok {
block.coinbase = string(coinbase)
}
2014-01-03 00:02:24 +02:00
if state, ok := header[4].([]uint8); ok {
// XXX The database is currently a global variable defined in testing.go
// This will eventually go away and the database will grabbed from the public server
// interface
block.state = NewTrie(Db, string(state))
}
2013-12-28 16:18:08 +02:00
// 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)
}
2013-12-30 00:53:12 +02:00
// It's either 8bit or 64
if time, ok := header[7].(uint8); ok {
block.time = int64(time)
}
if time, ok := header[7].(uint64); ok {
block.time = int64(time)
2013-12-28 16:18:08 +02:00
}
if nonce, ok := header[8].(uint8); ok {
block.nonce = uint32(nonce)
}
2013-12-30 00:53:12 +02:00
if extra, ok := header[9].([]byte); ok {
block.extra = string(extra)
}
2013-12-28 16:18:08 +02:00
}
if txSlice, ok := slice[1].([]interface{}); ok {
2013-12-29 02:36:59 +02:00
// Create transaction slice equal to decoded tx interface slice
2013-12-28 16:18:08 +02:00
block.transactions = make([]*Transaction, len(txSlice))
2013-12-29 02:36:59 +02:00
// Unmarshal transactions
2013-12-28 16:18:08 +02:00
for i, tx := range txSlice {
if t, ok := tx.([]byte); ok {
tx := &Transaction{}
2013-12-29 02:36:59 +02:00
// Use the unmarshaled data to unmarshal the transaction
// t is still decoded.
2013-12-28 16:18:08 +02:00
tx.UnmarshalRlp(t)
block.transactions[i] = tx
}
}
2013-12-28 03:24:16 +02:00
}
}
2013-12-27 22:22:57 +02:00
}