Contract creation, updating and fee processing

pull/2/head
obscuren 11 years ago
parent f97716ebe2
commit 15bf5ee4db
  1. 31
      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

Loading…
Cancel
Save