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

183 lines
4.5 KiB

11 years ago
package main
import (
"fmt"
11 years ago
"time"
_"bytes"
_"encoding/hex"
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
11 years ago
state *Trie
difficulty uint32
11 years ago
// Creation time
time int64
nonce uint32
11 years ago
// List of transactions and/or contracts
11 years ago
transactions []*Transaction
extra string
}
// New block takes a raw encoded string
func NewBlock(raw []byte) *Block {
block := &Block{}
block.UnmarshalRlp(raw)
return block
11 years ago
}
11 years ago
// Creates a new block. This is currently for testing
11 years ago
func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
11 years ago
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
time: time.Now().Unix(),
11 years ago
}
return block
}
11 years ago
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)
11 years ago
for _, tx := range txes {
// 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)
}
block.UpdateContract(addr, contract)
}
11 years ago
}
return block
}
func (block *Block) GetContract(addr []byte) *Contract {
data := block.state.Get(string(addr))
if data == "" {
return nil
}
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))
// 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
11 years ago
}
11 years ago
11 years ago
// Returns a hash of the block
11 years ago
func (block *Block) Hash() []byte {
return Sha256Bin(block.MarshalRlp())
11 years ago
}
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
11 years ago
block.state.root,
// Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
block.difficulty,
uint64(block.time),
block.nonce,
block.extra,
11 years ago
}
11 years ago
// TODO
uncles := []interface{}{}
11 years ago
// Encode a slice interface which contains the header and the list of transactions.
return Encode([]interface{}{header, encTx, uncles})
11 years ago
}
func (block *Block) UnmarshalRlp(data []byte) {
decoder := NewRlpDecoder(data)
header := decoder.Get(0)
block.number = uint32(header.Get(0).AsUint())
block.prevHash = header.Get(1).AsString()
// sha of uncles is header[2]
block.coinbase = header.Get(3).AsString()
block.state = NewTrie(Db, header.Get(4).AsString())
block.difficulty = uint32(header.Get(5).AsUint())
block.time = int64(header.Get(6).AsUint())
block.nonce = uint32(header.Get(7).AsUint())
block.extra = header.Get(8).AsString()
txes := decoder.Get(1)
block.transactions = make([]*Transaction, txes.Length())
for i := 0; i < txes.Length(); i++ {
tx := &Transaction{}
tx.UnmarshalRlp(txes.Get(i).AsBytes())
block.transactions[i] = tx
11 years ago
}
11 years ago
}