aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain
diff options
context:
space:
mode:
Diffstat (limited to 'ethchain')
-rw-r--r--ethchain/block.go10
-rw-r--r--ethchain/block_chain.go7
-rw-r--r--ethchain/block_manager.go44
-rw-r--r--ethchain/fees.go8
-rw-r--r--ethchain/transaction_pool.go22
5 files changed, 54 insertions, 37 deletions
diff --git a/ethchain/block.go b/ethchain/block.go
index 0678f64e2..34ddf9fec 100644
--- a/ethchain/block.go
+++ b/ethchain/block.go
@@ -135,7 +135,7 @@ func (block *Block) GetContract(addr []byte) *Contract {
}
func (block *Block) UpdateContract(addr []byte, contract *Contract) {
// Make sure the state is synced
- contract.State().Sync()
+ //contract.State().Sync()
block.state.Update(string(addr), string(contract.RlpEncode()))
}
@@ -298,12 +298,6 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
tx := NewTransactionFromValue(txes.Get(i))
block.transactions[i] = tx
-
- /*
- if ethutil.Config.Debug {
- ethutil.Config.Db.Put(tx.Hash(), ethutil.Encode(tx))
- }
- */
}
}
@@ -335,7 +329,7 @@ func NewUncleBlockFromValue(header *ethutil.Value) *Block {
}
func (block *Block) String() string {
- return fmt.Sprintf("Block(%x):\nPrevHash:%x\nUncleSha:%x\nCoinbase:%x\nRoot:%x\nTxSha:%x\nDiff:%v\nTime:%d\nNonce:%x", block.Hash(), block.PrevHash, block.UncleSha, block.Coinbase, block.state.Root, block.TxSha, block.Difficulty, block.Time, block.Nonce)
+ return fmt.Sprintf("Block(%x):\nPrevHash:%x\nUncleSha:%x\nCoinbase:%x\nRoot:%x\nTxSha:%x\nDiff:%v\nTime:%d\nNonce:%x\nTxs:%d\n", block.Hash(), block.PrevHash, block.UncleSha, block.Coinbase, block.state.Root, block.TxSha, block.Difficulty, block.Time, block.Nonce, len(block.transactions))
}
//////////// UNEXPORTED /////////////////
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 54f48bc60..5b55782a9 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -104,7 +104,6 @@ func (bc *BlockChain) GetChainFromHash(hash []byte, max uint64) []interface{} {
currentHash = block.PrevHash
chain = append(chain, block.Value().Val)
- //chain = append([]interface{}{block.RlpValue().Value}, chain...)
num--
}
@@ -141,7 +140,9 @@ func (bc *BlockChain) Add(block *Block) {
bc.CurrentBlock = block
bc.LastBlockHash = block.Hash()
- ethutil.Config.Db.Put(block.Hash(), block.RlpEncode())
+ encodedBlock := block.RlpEncode()
+ ethutil.Config.Db.Put(block.Hash(), encodedBlock)
+ ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock)
}
func (bc *BlockChain) GetBlock(hash []byte) *Block {
@@ -177,8 +178,6 @@ func (bc *BlockChain) writeBlockInfo(block *Block) {
func (bc *BlockChain) Stop() {
if bc.CurrentBlock != nil {
- ethutil.Config.Db.Put([]byte("LastBlock"), bc.CurrentBlock.RlpEncode())
-
log.Println("[CHAIN] Stopped")
}
}
diff --git a/ethchain/block_manager.go b/ethchain/block_manager.go
index d9cdcd2d9..4e72f51ba 100644
--- a/ethchain/block_manager.go
+++ b/ethchain/block_manager.go
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"github.com/ethereum/eth-go/ethutil"
+ "github.com/ethereum/eth-go/ethwire"
"github.com/obscuren/secp256k1-go"
"log"
"math"
@@ -18,10 +19,6 @@ type BlockProcessor interface {
ProcessBlock(block *Block)
}
-func CalculateBlockReward(block *Block, uncleLength int) *big.Int {
- return BlockReward
-}
-
type BlockManager struct {
// Mutex for locking the block processor. Blocks can only be handled one at a time
mutex sync.Mutex
@@ -46,9 +43,9 @@ type BlockManager struct {
func AddTestNetFunds(block *Block) {
for _, addr := range []string{
"8a40bfaa73256b60764c1bf40675a99083efb075", // Gavin
- "93658b04240e4bd4046fd2d6d417d20f146f4b43", // Jeffrey
+ "e6716f9544a56c530d868e4bfbacb172315bdead", // Jeffrey
"1e12515ce3e0f817a4ddef9ca55788a1d66bd2df", // Vit
- "80c01a26338f0d905e295fccb71fa9ea849ffa12", // Alex
+ "1a26338f0d905e295fccb71fa9ea849ffa12aaf4", // Alex
} {
//log.Println("2^200 Wei to", addr)
codedAddr, _ := hex.DecodeString(addr)
@@ -70,14 +67,17 @@ func NewBlockManager(speaker PublicSpeaker) *BlockManager {
if bm.bc.CurrentBlock == nil {
AddTestNetFunds(bm.bc.genesisBlock)
+
+ bm.bc.genesisBlock.State().Sync()
// Prepare the genesis block
bm.bc.Add(bm.bc.genesisBlock)
- log.Printf("Genesis: %x\n", bm.bc.genesisBlock.Hash())
//log.Printf("root %x\n", bm.bc.genesisBlock.State().Root)
//bm.bc.genesisBlock.PrintHash()
}
+ log.Printf("Last block: %x\n", bm.bc.CurrentBlock.Hash())
+
return bm
}
@@ -115,12 +115,6 @@ func (bm *BlockManager) ProcessBlock(block *Block) error {
return nil
}
- /*
- if ethutil.Config.Debug {
- log.Printf("[BMGR] Processing block(%x)\n", hash)
- }
- */
-
// Check if we have the parent hash, if it isn't known we discard it
// Reasons might be catching up or simply an invalid block
if !bm.bc.HasBlock(block.PrevHash) && bm.bc.CurrentBlock != nil {
@@ -142,13 +136,12 @@ func (bm *BlockManager) ProcessBlock(block *Block) error {
}
if !block.State().Cmp(bm.bc.CurrentBlock.State()) {
- //if block.State().Root != state.Root {
return fmt.Errorf("Invalid merkle root. Expected %x, got %x", block.State().Root, bm.bc.CurrentBlock.State().Root)
}
// Calculate the new total difficulty and sync back to the db
if bm.CalculateTD(block) {
- // Sync the current block's state to the database
+ // Sync the current block's state to the database and cancelling out the deferred Undo
bm.bc.CurrentBlock.State().Sync()
// Add the block to the chain
bm.bc.Add(block)
@@ -172,7 +165,7 @@ func (bm *BlockManager) ProcessBlock(block *Block) error {
*/
// Broadcast the valid block back to the wire
- //bm.Speaker.Broadcast(ethwire.MsgBlockTy, []interface{}{block.RlpValue().Value})
+ bm.Speaker.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
// If there's a block processor present, pass in the block for further
// processing
@@ -251,6 +244,18 @@ func (bm *BlockManager) ValidateBlock(block *Block) error {
return nil
}
+func CalculateBlockReward(block *Block, uncleLength int) *big.Int {
+ base := new(big.Int)
+ for i := 0; i < uncleLength; i++ {
+ base.Add(base, UncleInclusionReward)
+ }
+ return base.Add(base, BlockReward)
+}
+
+func CalculateUncleReward(block *Block) *big.Int {
+ return UncleReward
+}
+
func (bm *BlockManager) AccumelateRewards(processor *Block, block *Block) error {
// Get the coinbase rlp data
addr := processor.GetAddr(block.Coinbase)
@@ -259,7 +264,12 @@ func (bm *BlockManager) AccumelateRewards(processor *Block, block *Block) error
processor.UpdateAddr(block.Coinbase, addr)
- // TODO Reward each uncle
+ for _, uncle := range block.Uncles {
+ uncleAddr := processor.GetAddr(uncle.Coinbase)
+ uncleAddr.AddFee(CalculateUncleReward(uncle))
+
+ processor.UpdateAddr(uncle.Coinbase, uncleAddr)
+ }
return nil
}
diff --git a/ethchain/fees.go b/ethchain/fees.go
index 8f1646ab4..57017cdc9 100644
--- a/ethchain/fees.go
+++ b/ethchain/fees.go
@@ -13,7 +13,13 @@ var DataFee *big.Int = new(big.Int)
var CryptoFee *big.Int = new(big.Int)
var ExtroFee *big.Int = new(big.Int)
-var BlockReward *big.Int = big.NewInt(1500000000000000000)
+var BlockReward *big.Int = big.NewInt(1.5e+18)
+
+var UncleReward *big.Int = big.NewInt(1.125e+18)
+
+//var UncleReward *big.Int = big.NewInt(2e18)
+var UncleInclusionReward *big.Int = big.NewInt(1.875e+17)
+
var Period1Reward *big.Int = new(big.Int)
var Period2Reward *big.Int = new(big.Int)
var Period3Reward *big.Int = new(big.Int)
diff --git a/ethchain/transaction_pool.go b/ethchain/transaction_pool.go
index c2d65a2a7..75a8aa5d1 100644
--- a/ethchain/transaction_pool.go
+++ b/ethchain/transaction_pool.go
@@ -106,17 +106,25 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error
}
}
- // Subtract the amount from the senders account
- sender.Amount.Sub(sender.Amount, totAmount)
- sender.Nonce += 1
-
// Get the receiver
receiver := block.GetAddr(tx.Recipient)
- // Add the amount to receivers account which should conclude this transaction
- receiver.Amount.Add(receiver.Amount, tx.Value)
+ sender.Nonce += 1
+
+ // Send Tx to self
+ if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
+ // Subtract the fee
+ sender.Amount.Sub(sender.Amount, new(big.Int).Mul(TxFee, TxFeeRat))
+ } else {
+ // Subtract the amount from the senders account
+ sender.Amount.Sub(sender.Amount, totAmount)
+
+ // Add the amount to receivers account which should conclude this transaction
+ receiver.Amount.Add(receiver.Amount, tx.Value)
+
+ block.UpdateAddr(tx.Recipient, receiver)
+ }
block.UpdateAddr(tx.Sender(), sender)
- block.UpdateAddr(tx.Recipient, receiver)
return
}