aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--ethereum.go37
-rw-r--r--ethutil/config.go2
-rw-r--r--ethutil/encoding.go3
-rw-r--r--ethutil/helpers.go4
-rw-r--r--ethutil/rlp.go8
-rw-r--r--ethutil/trie.go9
-rw-r--r--ethutil/value.go2
-rw-r--r--peer.go12
13 files changed, 96 insertions, 72 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
}
diff --git a/ethereum.go b/ethereum.go
index bd6caac08..c54303795 100644
--- a/ethereum.go
+++ b/ethereum.go
@@ -85,7 +85,6 @@ func New(caps Caps, usePnp bool) (*Ethereum, error) {
Nonce: nonce,
serverCaps: caps,
nat: nat,
- MaxPeers: 5,
}
ethereum.TxPool = ethchain.NewTxPool()
ethereum.TxPool.Speaker = ethereum
@@ -114,28 +113,32 @@ func (s *Ethereum) ProcessPeerList(addrs []string) {
}
func (s *Ethereum) ConnectToPeer(addr string) error {
- var alreadyConnected bool
+ if s.peers.Len() < s.MaxPeers {
+ var alreadyConnected bool
- eachPeer(s.peers, func(p *Peer, v *list.Element) {
- if p.conn == nil {
- return
- }
- phost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String())
- ahost, _, _ := net.SplitHostPort(addr)
+ eachPeer(s.peers, func(p *Peer, v *list.Element) {
+ if p.conn == nil {
+ return
+ }
+ phost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String())
+ ahost, _, _ := net.SplitHostPort(addr)
- if phost == ahost {
- alreadyConnected = true
- return
+ if phost == ahost {
+ alreadyConnected = true
+ return
+ }
+ })
+
+ if alreadyConnected {
+ return nil
}
- })
- if alreadyConnected {
- return nil
- }
+ peer := NewOutboundPeer(addr, s, s.serverCaps)
- peer := NewOutboundPeer(addr, s, s.serverCaps)
+ s.peers.PushBack(peer)
- s.peers.PushBack(peer)
+ log.Printf("[SERV] Adding peer %d / %d\n", s.peers.Len(), s.MaxPeers)
+ }
return nil
}
diff --git a/ethutil/config.go b/ethutil/config.go
index b361a3079..e3b5f0275 100644
--- a/ethutil/config.go
+++ b/ethutil/config.go
@@ -43,7 +43,7 @@ func ReadConfig(base string) *config {
}
}
- Config = &config{ExecPath: path, Debug: true, Ver: "0.2.2"}
+ Config = &config{ExecPath: path, Debug: true, Ver: "0.2.3"}
Config.Log = NewLogger(LogFile|LogStd, 0)
}
diff --git a/ethutil/encoding.go b/ethutil/encoding.go
index 207548c93..1f661947a 100644
--- a/ethutil/encoding.go
+++ b/ethutil/encoding.go
@@ -3,7 +3,6 @@ package ethutil
import (
"bytes"
"encoding/hex"
- _ "fmt"
"strings"
)
@@ -36,7 +35,7 @@ func CompactEncode(hexSlice []int) string {
func CompactDecode(str string) []int {
base := CompactHexDecode(str)
base = base[:len(base)-1]
- if base[0] >= 2 { // && base[len(base)-1] != 16 {
+ if base[0] >= 2 {
base = append(base, 16)
}
if base[0]%2 == 1 {
diff --git a/ethutil/helpers.go b/ethutil/helpers.go
index 2e3aeb9a3..aa0f79a04 100644
--- a/ethutil/helpers.go
+++ b/ethutil/helpers.go
@@ -58,3 +58,7 @@ func MatchingNibbleLength(a, b []int) int {
func Hex(d []byte) string {
return hex.EncodeToString(d)
}
+func FromHex(str string) []byte {
+ h, _ := hex.DecodeString(str)
+ return h
+}
diff --git a/ethutil/rlp.go b/ethutil/rlp.go
index 025d269a0..e633f5f1d 100644
--- a/ethutil/rlp.go
+++ b/ethutil/rlp.go
@@ -86,13 +86,6 @@ func DecodeWithReader(reader *bytes.Buffer) interface{} {
// TODO Use a bytes.Buffer instead of a raw byte slice.
// Cleaner code, and use draining instead of seeking the next bytes to read
func Decode(data []byte, pos uint64) (interface{}, uint64) {
- /*
- if pos > uint64(len(data)-1) {
- log.Println(data)
- log.Panicf("index out of range %d for data %q, l = %d", pos, data, len(data))
- }
- */
-
var slice []interface{}
char := int(data[pos])
switch {
@@ -131,7 +124,6 @@ func Decode(data []byte, pos uint64) (interface{}, uint64) {
case char <= 0xff:
l := uint64(data[pos]) - 0xf7
- //b := BigD(data[pos+1 : pos+1+l]).Uint64()
b := ReadVarint(bytes.NewReader(data[pos+1 : pos+1+l]))
pos = pos + l + 1
diff --git a/ethutil/trie.go b/ethutil/trie.go
index 7140a1b36..95abca602 100644
--- a/ethutil/trie.go
+++ b/ethutil/trie.go
@@ -98,22 +98,25 @@ func (cache *Cache) Undo() {
// Please note that the data isn't persisted unless `Sync` is
// explicitly called.
type Trie struct {
- Root interface{}
+ prevRoot interface{}
+ Root interface{}
//db Database
cache *Cache
}
func NewTrie(db Database, Root interface{}) *Trie {
- return &Trie{cache: NewCache(db), Root: Root}
+ return &Trie{cache: NewCache(db), Root: Root, prevRoot: Root}
}
// Save the cached value to the database.
func (t *Trie) Sync() {
t.cache.Commit()
+ t.prevRoot = t.Root
}
func (t *Trie) Undo() {
t.cache.Undo()
+ t.Root = t.prevRoot
}
/*
@@ -181,6 +184,7 @@ func (t *Trie) GetNode(node interface{}) *Value {
}
func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} {
+
if value != "" {
return t.InsertState(node, key, value)
} else {
@@ -241,6 +245,7 @@ func (t *Trie) InsertState(node interface{}, key []int, value interface{}) inter
// Check for "special" 2 slice type node
if currentNode.Len() == 2 {
// Decode the key
+
k := CompactDecode(currentNode.Get(0).Str())
v := currentNode.Get(1).Raw()
diff --git a/ethutil/value.go b/ethutil/value.go
index 701ece5bb..d3a38f87f 100644
--- a/ethutil/value.go
+++ b/ethutil/value.go
@@ -98,6 +98,8 @@ func (val *Value) Str() string {
return string(a)
} else if a, ok := val.Val.(string); ok {
return a
+ } else if a, ok := val.Val.(byte); ok {
+ return string(a)
}
return ""
diff --git a/peer.go b/peer.go
index 926d34eb3..53fdfa8fc 100644
--- a/peer.go
+++ b/peer.go
@@ -294,7 +294,9 @@ func (p *Peer) HandleInbound() {
if err != nil {
if ethutil.Config.Debug {
- log.Printf("[PEER] Block (%x) err %v", block.Hash()[:4], err)
+ log.Printf("[PEER] Block %x failed\n", block.Hash())
+ log.Printf("[PEER] %v\n", err)
+ log.Println(block)
}
break
} else {
@@ -467,7 +469,7 @@ func (p *Peer) pushHandshake() error {
pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes()
msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{
- uint32(4), uint32(0), p.Version, byte(p.caps), p.port, pubkey,
+ uint32(5), uint32(0), p.Version, byte(p.caps), p.port, pubkey,
})
p.QueueMessage(msg)
@@ -494,7 +496,7 @@ func (p *Peer) pushPeers() {
func (p *Peer) handleHandshake(msg *ethwire.Msg) {
c := msg.Data
- if c.Get(0).Uint() != 4 {
+ if c.Get(0).Uint() != 5 {
log.Println("Invalid peer version. Require protocol v4")
p.Stop()
return
@@ -527,7 +529,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
// Get a reference to the peers version
p.Version = c.Get(2).Str()
- log.Println(p)
+ log.Println("[PEER]", p)
}
func (p *Peer) String() string {
@@ -544,7 +546,7 @@ func (p *Peer) String() string {
strConnectType = "disconnected"
}
- return fmt.Sprintf("peer [%s] (%s) %v %s [%s]", strConnectType, strBoundType, p.conn.RemoteAddr(), p.Version, p.caps)
+ return fmt.Sprintf("[%s] (%s) %v %s [%s]", strConnectType, strBoundType, p.conn.RemoteAddr(), p.Version, p.caps)
}