More mining rework

This commit is contained in:
Maran
2014-03-20 11:20:29 +01:00
parent 2be2fc7974
commit ae837c4719
7 changed files with 171 additions and 39 deletions
+3
View File
@@ -304,6 +304,9 @@ 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\nTxs:%d\n", block.Hash(), block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Nonce, len(block.transactions))
}
func (block *Block) GetRoot() interface{} {
return block.state.trie.Root
}
//////////// UNEXPORTED /////////////////
func (block *Block) header() []interface{} {
+1 -2
View File
@@ -44,7 +44,6 @@ func (bc *BlockChain) NewBlock(coinbase []byte, txs []*Transaction) *Block {
hash = bc.LastBlockHash
lastBlockTime = bc.CurrentBlock.Time
}
block := CreateBlock(
root,
hash,
@@ -181,8 +180,8 @@ func (bc *BlockChain) SetTotalDifficulty(td *big.Int) {
// Add a block to the chain and record addition information
func (bc *BlockChain) Add(block *Block) {
bc.writeBlockInfo(block)
// Prepare the genesis block
bc.CurrentBlock = block
bc.LastBlockHash = block.Hash()
+5 -11
View File
@@ -11,7 +11,7 @@ import (
)
type PoW interface {
Search(block *Block, minerChan chan ethutil.React) []byte
Search(block *Block, reactChan chan ethutil.React) []byte
Verify(hash []byte, diff *big.Int, nonce []byte) bool
}
@@ -19,7 +19,7 @@ type EasyPow struct {
hash *big.Int
}
func (pow *EasyPow) Search(block *Block, minerChan chan ethutil.React) []byte {
func (pow *EasyPow) Search(block *Block, reactChan chan ethutil.React) []byte {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
hash := block.HashNoNonce()
diff := block.Difficulty
@@ -28,15 +28,9 @@ func (pow *EasyPow) Search(block *Block, minerChan chan ethutil.React) []byte {
for {
select {
case chanMessage := <-minerChan:
if _, ok := chanMessage.Resource.(*Block); ok {
log.Println("BREAKING OUT: BLOCK")
return nil
}
if _, ok := chanMessage.Resource.(*Transaction); ok {
log.Println("BREAKING OUT: TX")
return nil
}
case <-reactChan:
log.Println("[pow] Received reactor event; breaking out.")
return nil
default:
i++
if i%1234567 == 0 {
+8 -20
View File
@@ -50,9 +50,6 @@ type StateManager struct {
// Comparative state it used for comparing and validating end
// results
compState *State
// Mining state, solely used for mining
miningState *State
}
func NewStateManager(ethereum EthManager) *StateManager {
@@ -65,7 +62,6 @@ func NewStateManager(ethereum EthManager) *StateManager {
bc: ethereum.BlockChain(),
}
sm.procState = ethereum.BlockChain().CurrentBlock.State()
return sm
}
@@ -73,10 +69,6 @@ func (sm *StateManager) ProcState() *State {
return sm.procState
}
func (sm *StateManager) MiningState() *State {
return sm.miningState
}
// Watches any given address and puts it in the address state store
func (sm *StateManager) WatchAddr(addr []byte) *AccountState {
//XXX account := sm.bc.CurrentBlock.state.GetAccount(addr)
@@ -105,8 +97,6 @@ func (sm *StateManager) MakeContract(tx *Transaction) {
sm.procState.states[string(tx.Hash()[12:])] = contract.state
}
}
func (sm *StateManager) ApplyTransaction(block *Block, tx *Transaction) {
}
func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
// Process each transaction/contract
@@ -136,17 +126,13 @@ func (sm *StateManager) Prepare(processer *State, comparative *State) {
sm.procState = processer
}
func (sm *StateManager) PrepareMiningState() {
sm.miningState = sm.BlockChain().CurrentBlock.State()
}
// Default prepare function
func (sm *StateManager) PrepareDefault(block *Block) {
sm.Prepare(sm.BlockChain().CurrentBlock.State(), block.State())
}
// Block processing and validating with a given (temporarily) state
func (sm *StateManager) ProcessBlock(block *Block) error {
func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
@@ -155,7 +141,6 @@ func (sm *StateManager) ProcessBlock(block *Block) error {
// nodes this won't happen because Commit would have been called
// before that.
defer sm.bc.CurrentBlock.Undo()
hash := block.Hash()
if sm.bc.HasBlock(hash) {
@@ -207,7 +192,9 @@ func (sm *StateManager) ProcessBlock(block *Block) error {
}
ethutil.Config.Log.Infof("[STATE] Added block #%d (%x)\n", block.BlockInfo().Number, block.Hash())
sm.Ethereum.Reactor().Post("newBlock", block)
if dontReact == false {
sm.Ethereum.Reactor().Post("newBlock", block)
}
} else {
fmt.Println("total diff failed")
}
@@ -285,15 +272,16 @@ func CalculateUncleReward(block *Block) *big.Int {
}
func (sm *StateManager) AccumelateRewards(block *Block) error {
// Get the coinbase rlp data
//XXX addr := processor.state.GetAccount(block.Coinbase)
addr := sm.procState.GetAccount(block.Coinbase)
// Reward amount of ether to the coinbase address
addr.AddFee(CalculateBlockReward(block, len(block.Uncles)))
//XXX processor.state.UpdateAccount(block.Coinbase, addr)
sm.procState.UpdateAccount(block.Coinbase, addr)
var acc []byte
copy(acc, block.Coinbase)
sm.procState.UpdateAccount(acc, addr)
for _, uncle := range block.Uncles {
uncleAddr := sm.procState.GetAccount(uncle.Coinbase)
uncleAddr.AddFee(CalculateUncleReward(uncle))
+4 -5
View File
@@ -91,7 +91,6 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
// Process transaction validates the Tx and processes funds from the
// sender to the recipient.
func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error) {
log.Println("Processing TX")
defer func() {
if r := recover(); r != nil {
log.Println(r)
@@ -105,11 +104,11 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error
// funds won't invalidate this transaction but simple ignores it.
totAmount := new(big.Int).Add(tx.Value, new(big.Int).Mul(TxFee, TxFeeRat))
if sender.Amount.Cmp(totAmount) < 0 {
return errors.New("Insufficient amount in sender's account")
return errors.New("[TXPL] Insufficient amount in sender's account")
}
if sender.Nonce != tx.Nonce {
return fmt.Errorf("Invalid nonce %d(%d)", tx.Nonce, sender.Nonce)
return fmt.Errorf("[TXPL] Invalid account nonce, state nonce is %d transactoin nonce is %d instead", sender.Nonce, tx.Nonce)
}
// Get the receiver
@@ -145,7 +144,7 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
block := pool.Ethereum.BlockChain().CurrentBlock
// Something has gone horribly wrong if this happens
if block == nil {
return errors.New("No last block on the block chain")
return errors.New("[TXPL] No last block on the block chain")
}
// Get the sender
@@ -156,7 +155,7 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
if sender.Amount.Cmp(totAmount) < 0 {
return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.Sender())
return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
}
// Increment the nonce making each tx valid only once to prevent replay