aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <obscuren@obscura.com>2014-01-04 07:31:06 +0800
committerobscuren <obscuren@obscura.com>2014-01-04 07:31:06 +0800
commit15bf5ee4db6485619fb942ae288bd9659d4d6d73 (patch)
tree3559b2fad04988c6add20f22ba6e9f27b1061618
parentf97716ebe287349729821d826141e73a6e55702e (diff)
downloadgo-tangerine-15bf5ee4db6485619fb942ae288bd9659d4d6d73.tar.gz
go-tangerine-15bf5ee4db6485619fb942ae288bd9659d4d6d73.tar.zst
go-tangerine-15bf5ee4db6485619fb942ae288bd9659d4d6d73.zip
Contract creation, updating and fee processing
-rw-r--r--block.go31
1 files 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