From 15bf5ee4db6485619fb942ae288bd9659d4d6d73 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 4 Jan 2014 00:31:06 +0100 Subject: [PATCH] Contract creation, updating and fee processing --- block.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/block.go b/block.go index 39858b0fd..00317573c 100644 --- a/block.go +++ b/block.go @@ -1,7 +1,7 @@ package main import ( - _"fmt" + "fmt" "time" _"bytes" _"encoding/hex" @@ -73,7 +73,8 @@ func CreateBlock(root string, num int, prevHash string, base string, difficulty contract := NewContract(tx.value, []byte("")) block.state.Update(string(addr), string(contract.MarshalRlp())) for i, val := range tx.data { - contract.state.Update(string(Encode(uint32(i))), val) + contract.state.Update(string(NumberToBytes(uint64(i), 32)), val) + //contract.state.Update(string(Encode(uint32(i))), val) } block.UpdateContract(addr, contract) } @@ -84,6 +85,10 @@ func CreateBlock(root string, num int, prevHash string, base string, difficulty func (block *Block) GetContract(addr []byte) *Contract { data := block.state.Get(string(addr)) + if data == "" { + return nil + } + contract := &Contract{} contract.UnmarshalRlp([]byte(data)) @@ -94,7 +99,27 @@ func (block *Block) UpdateContract(addr []byte, contract *Contract) { block.state.Update(string(addr), string(contract.MarshalRlp())) } -func (block *Block) Update() { + +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 } // Returns a hash of the block