plugeth/block.go

183 lines
4.5 KiB
Go
Raw Normal View History

2013-12-26 11:45:52 +00:00
package main
import (
"fmt"
2013-12-27 20:22:57 +00:00
"time"
2013-12-28 14:18:08 +00:00
_"bytes"
2014-01-02 23:43:49 +00:00
_"encoding/hex"
2013-12-26 11:45:52 +00:00
)
type Block struct {
2013-12-29 00:36:59 +00:00
// The number of this block
2013-12-27 20:22:57 +00:00
number uint32
2013-12-29 00:36:59 +00:00
// Hash to the previous block
2013-12-27 20:22:57 +00:00
prevHash string
2013-12-29 00:36:59 +00:00
// Uncles of this block
2013-12-27 20:22:57 +00:00
uncles []*Block
coinbase string
// state xxx
2014-01-02 22:02:24 +00:00
state *Trie
2013-12-28 14:18:08 +00:00
difficulty uint32
2013-12-29 00:36:59 +00:00
// Creation time
2013-12-29 22:53:12 +00:00
time int64
2013-12-28 14:18:08 +00:00
nonce uint32
2013-12-29 00:36:59 +00:00
// List of transactions and/or contracts
2013-12-27 20:22:57 +00:00
transactions []*Transaction
2013-12-29 22:53:12 +00: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 11:45:52 +00:00
}
2013-12-29 00:36:59 +00:00
// Creates a new block. This is currently for testing
2014-01-02 22:02:24 +00:00
func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
2013-12-26 11:45:52 +00:00
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
2013-12-28 14:18:08 +00:00
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
2013-12-29 22:53:12 +00:00
time: time.Now().Unix(),
2013-12-26 11:45:52 +00:00
}
return block
}
2014-01-02 22:02:24 +00: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-02 23:43:49 +00:00
2014-01-02 22:02:24 +00:00
for _, tx := range txes {
2014-01-02 23:43:49 +00: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)
2014-01-02 23:43:49 +00:00
}
block.UpdateContract(addr, contract)
}
2014-01-02 22:02:24 +00:00
}
return block
}
2014-01-02 23:43:49 +00:00
func (block *Block) GetContract(addr []byte) *Contract {
data := block.state.Get(string(addr))
if data == "" {
return nil
}
2014-01-02 23:43:49 +00: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)
2014-01-08 22:42:11 +00:00
return false
}
contract.amount -= fee
block.state.Update(string(addr), string(contract.MarshalRlp()))
data := block.state.Get(string(block.coinbase))
2014-01-08 22:42:11 +00:00
// 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 11:45:52 +00:00
}
2013-12-27 20:22:57 +00:00
2013-12-29 00:36:59 +00:00
// Returns a hash of the block
2014-01-02 22:02:24 +00:00
func (block *Block) Hash() []byte {
return Sha256Bin(block.MarshalRlp())
2013-12-27 20:22:57 +00:00
}
func (block *Block) MarshalRlp() []byte {
2013-12-29 00:36:59 +00:00
// Marshal the transactions of this block
2013-12-27 20:22:57 +00:00
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
2013-12-29 00:36:59 +00:00
// Cast it to a string (safe)
2013-12-27 20:22:57 +00:00
encTx[i] = string(tx.MarshalRlp())
}
2013-12-28 14:18:08 +00:00
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
2013-12-28 01:24:16 +00:00
header := []interface{}{
2013-12-27 20:22:57 +00:00
block.number,
2013-12-28 14:18:08 +00:00
block.prevHash,
2013-12-27 20:22:57 +00:00
// Sha of uncles
2013-12-28 14:18:08 +00:00
"",
block.coinbase,
2013-12-27 20:22:57 +00:00
// root state
2014-01-02 22:02:24 +00:00
block.state.root,
2013-12-29 22:53:12 +00:00
// Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
2013-12-28 14:18:08 +00:00
block.difficulty,
2013-12-29 22:53:12 +00:00
uint64(block.time),
2013-12-28 14:18:08 +00:00
block.nonce,
2013-12-29 22:53:12 +00:00
block.extra,
2013-12-28 01:24:16 +00:00
}
2013-12-27 20:22:57 +00:00
2013-12-29 22:53:12 +00:00
// TODO
uncles := []interface{}{}
2013-12-29 00:36:59 +00:00
// Encode a slice interface which contains the header and the list of transactions.
2013-12-29 22:53:12 +00:00
return Encode([]interface{}{header, encTx, uncles})
2013-12-27 20:22:57 +00:00
}
func (block *Block) UnmarshalRlp(data []byte) {
2014-01-08 22:42:11 +00:00
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
2013-12-28 01:24:16 +00:00
}
2013-12-27 20:22:57 +00:00
}