Removed events from the state manager

This commit is contained in:
obscuren 2014-10-29 14:20:42 +01:00
parent fb4113dab4
commit 9e2f071d26
10 changed files with 64 additions and 84 deletions

View File

@ -309,9 +309,13 @@ out:
} }
} }
// TODO figure out whether we were catching up
// If caught up and just a new block has been propagated:
// sm.eth.EventMux().Post(NewBlockEvent{block})
// otherwise process and don't emit anything
var err error var err error
for i, block := range blocks { for i, block := range blocks {
err = self.eth.StateManager().Process(block, false) err = self.eth.StateManager().Process(block)
if err != nil { if err != nil {
poollogger.Infoln(err) poollogger.Infoln(err)
poollogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4]) poollogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4])

View File

@ -98,6 +98,8 @@ func main() {
// Leave the Println. This needs clean output for piping // Leave the Println. This needs clean output for piping
fmt.Printf("%s\n", block.State().Dump()) fmt.Printf("%s\n", block.State().Dump())
fmt.Println(block)
os.Exit(0) os.Exit(0)
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"sort" "sort"
_ "strconv"
"time" "time"
"github.com/ethereum/go-ethereum/ethcrypto" "github.com/ethereum/go-ethereum/ethcrypto"
@ -240,15 +239,19 @@ func (block *Block) SetUncles(uncles []*Block) {
block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles())) block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles()))
} }
func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) { func (self *Block) SetReceipts(receipts Receipts) {
self.receipts = receipts self.receipts = receipts
self.setTransactions(txs) self.SetReceiptHash(receipts)
} }
func (block *Block) setTransactions(txs []*Transaction) { func (self *Block) SetTransactions(txs Transactions) {
block.transactions = txs self.setTransactions(txs)
self.SetTransactionHash(txs)
}
block.LogsBloom = CreateBloom(txs) func (block *Block) setTransactions(txs Transactions) {
block.transactions = txs
block.LogsBloom = CreateBloom(block)
} }
func (self *Block) SetTransactionHash(transactions Transactions) { func (self *Block) SetTransactionHash(transactions Transactions) {
@ -424,22 +427,7 @@ func (self *Block) Size() ethutil.StorageSize {
return ethutil.StorageSize(len(self.RlpEncode())) return ethutil.StorageSize(len(self.RlpEncode()))
} }
/* // Implement RlpEncodable
func DeriveReceiptHash(receipts Receipts) (sha []byte) { func (self *Block) RlpData() interface{} {
trie := ethtrie.New(ethutil.Config.Db, "") return self.Value().Val
for i, receipt := range receipts {
trie.Update(string(ethutil.NewValue(i).Encode()), string(ethutil.NewValue(receipt.RlpData()).Encode()))
}
switch trie.Root.(type) {
case string:
sha = []byte(trie.Root.(string))
case []byte:
sha = trie.Root.([]byte)
default:
panic(fmt.Sprintf("invalid root type %T", trie.Root))
}
return sha
} }
*/

View File

@ -6,9 +6,10 @@ import (
"github.com/ethereum/go-ethereum/vm" "github.com/ethereum/go-ethereum/vm"
) )
func CreateBloom(txs Transactions) []byte { func CreateBloom(block *Block) []byte {
bin := new(big.Int) bin := new(big.Int)
for _, tx := range txs { bin.Or(bin, bloom9(block.Coinbase))
for _, tx := range block.Transactions() {
bin.Or(bin, LogsBloom(tx.logs)) bin.Or(bin, LogsBloom(tx.logs))
} }
@ -36,9 +37,7 @@ func bloom9(b []byte) *big.Int {
r := new(big.Int) r := new(big.Int)
for _, i := range []int{0, 2, 4} { for _, i := range []int{0, 2, 4} {
t := big.NewInt(1) t := big.NewInt(1)
r.Or(r, t.Lsh(t, uint(b[i+1])+256*(uint(b[i])&1)))
//r |= 1 << (uint64(b[i+1]) + 256*(uint64(b[i])&1))
r.Or(r, t.Rsh(t, uint(b[i+1])+256*(uint(b[i])&1)))
} }
return r return r

View File

@ -87,8 +87,6 @@ func (bc *ChainManager) Reset() {
bc.genesisBlock.state.Trie.Sync() bc.genesisBlock.state.Trie.Sync()
// Prepare the genesis block // Prepare the genesis block
bc.Add(bc.genesisBlock) bc.Add(bc.genesisBlock)
//fk := append([]byte("bloom"), bc.genesisBlock.Hash()...)
//bc.Ethereum.Db().Put(fk, make([]byte, 255))
bc.CurrentBlock = bc.genesisBlock bc.CurrentBlock = bc.genesisBlock
bc.SetTotalDifficulty(ethutil.Big("0")) bc.SetTotalDifficulty(ethutil.Big("0"))

View File

@ -170,12 +170,6 @@ func (self *Filter) FilterMessages(msgs []*ethstate.Message) []*ethstate.Message
} }
func (self *Filter) bloomFilter(block *Block) bool { func (self *Filter) bloomFilter(block *Block) bool {
//fk := append([]byte("bloom"), block.Hash()...)
//bin, err := self.eth.Db().Get(fk)
//if err != nil {
// fmt.Println(err)
//}
// TODO update to the new bloom filter // TODO update to the new bloom filter
bloom := NewBloomFilter(nil) bloom := NewBloomFilter(nil)

View File

@ -101,7 +101,7 @@ func (self *StateManager) Stop() {
func (self *StateManager) updateThread() { func (self *StateManager) updateThread() {
for ev := range self.events.Chan() { for ev := range self.events.Chan() {
for _, block := range ev.(Blocks) { for _, block := range ev.(Blocks) {
err := self.Process(block, false) err := self.Process(block)
if err != nil { if err != nil {
statelogger.Infoln(err) statelogger.Infoln(err)
statelogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4]) statelogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4])
@ -206,7 +206,7 @@ done:
return receipts, handled, unhandled, erroneous, err return receipts, handled, unhandled, erroneous, err
} }
func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { func (sm *StateManager) Process(block *Block) (err error) {
// Processing a blocks may never happen simultaneously // Processing a blocks may never happen simultaneously
sm.mutex.Lock() sm.mutex.Lock()
defer sm.mutex.Unlock() defer sm.mutex.Unlock()
@ -281,18 +281,9 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) {
sm.transState = state.Copy() sm.transState = state.Copy()
// Create a bloom bin for this block
//filter := sm.createBloomFilter(state)
// Persist the data
//fk := append([]byte("bloom"), block.Hash()...)
//sm.eth.Db().Put(fk, filter.Bin())
statelogger.Infof("Imported block #%d (%x...)\n", block.Number, block.Hash()[0:4]) statelogger.Infof("Imported block #%d (%x...)\n", block.Number, block.Hash()[0:4])
if dontReact == false {
sm.eth.EventMux().Post(NewBlockEvent{block})
state.Manifest().Reset() state.Manifest().Reset()
}
sm.eth.TxPool().RemoveSet(block.Transactions()) sm.eth.TxPool().RemoveSet(block.Transactions())
} else { } else {

View File

@ -231,7 +231,7 @@ func (self *StateTransition) TransitionState() (err error) {
msg.Output = ret msg.Output = ret
} else { } else {
// Add default LOG. Default = big(sender.addr) + 1 // Add default LOG. Default = big(sender.addr) + 1
addr := ethutil.BigD(sender.Address()) addr := ethutil.BigD(receiver.Address())
tx.addLog(vm.Log{sender.Address(), [][]byte{addr.Add(addr, ethutil.Big1).Bytes()}, nil}) tx.addLog(vm.Log{sender.Address(), [][]byte{addr.Add(addr, ethutil.Big1).Bytes()}, nil})
} }
} }

View File

@ -85,11 +85,7 @@ func (miner *Miner) listener() {
for { for {
select { select {
case event, isopen := <-miner.events.Chan(): case event := <-miner.events.Chan():
if !isopen {
return
}
switch event := event.(type) { switch event := event.(type) {
case ethchain.NewBlockEvent: case ethchain.NewBlockEvent:
miner.stopMining() miner.stopMining()
@ -114,16 +110,13 @@ func (miner *Miner) listener() {
} }
} }
miner.txs = newtxs miner.txs = newtxs
// Setup a fresh state to mine on
//miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase, miner.txs)
} else { } else {
if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 {
logger.Infoln("Adding uncle block") logger.Infoln("Adding uncle block")
miner.uncles = append(miner.uncles, block) miner.uncles = append(miner.uncles, block)
} }
} }
miner.startMining()
case ethchain.TxEvent: case ethchain.TxEvent:
if event.Type == ethchain.TxPre { if event.Type == ethchain.TxPre {
@ -141,6 +134,8 @@ func (miner *Miner) listener() {
// Apply new transactions // Apply new transactions
miner.txs = append(miner.txs, event.Tx) miner.txs = append(miner.txs, event.Tx)
} }
miner.startMining()
} }
} }
@ -159,8 +154,12 @@ func (miner *Miner) startMining() {
} }
func (miner *Miner) stopMining() { func (miner *Miner) stopMining() {
println("stop mining")
_, isopen := <-miner.powQuitChan
if isopen {
close(miner.powQuitChan) close(miner.powQuitChan)
<-miner.powDone }
//<-miner.powDone
} }
func (self *Miner) mineNewBlock() { func (self *Miner) mineNewBlock() {
@ -187,10 +186,9 @@ func (self *Miner) mineNewBlock() {
} }
self.ethereum.TxPool().RemoveSet(erroneous) self.ethereum.TxPool().RemoveSet(erroneous)
self.txs = append(txs, unhandledTxs...) self.txs = append(txs, unhandledTxs...)
self.block.SetReceiptHash(receipts)
// Set the transactions to the block so the new SHA3 can be calculated self.block.SetReceipts(receipts)
self.block.SetReceipts(receipts, txs) self.block.SetTransactions(txs)
// Accumulate the rewards included for this block // Accumulate the rewards included for this block
stateManager.AccumelateRewards(self.block.State(), self.block, parent) stateManager.AccumelateRewards(self.block.State(), self.block, parent)
@ -203,7 +201,7 @@ func (self *Miner) mineNewBlock() {
nonce := self.pow.Search(self.block, self.powQuitChan) nonce := self.pow.Search(self.block, self.powQuitChan)
if nonce != nil { if nonce != nil {
self.block.Nonce = nonce self.block.Nonce = nonce
err := self.ethereum.StateManager().Process(self.block, false) err := self.ethereum.StateManager().Process(self.block)
if err != nil { if err != nil {
logger.Infoln(err) logger.Infoln(err)
} else { } else {
@ -212,7 +210,10 @@ func (self *Miner) mineNewBlock() {
logger.Infoln(self.block) logger.Infoln(self.block)
// Gather the new batch of transactions currently in the tx pool // Gather the new batch of transactions currently in the tx pool
self.txs = self.ethereum.TxPool().CurrentTransactions() self.txs = self.ethereum.TxPool().CurrentTransactions()
self.ethereum.EventMux().Post(ethchain.NewBlockEvent{self.block})
} }
// Continue mining on the next block
self.startMining()
} }
self.powDone <- struct{}{}
} }

View File

@ -168,7 +168,24 @@ func New(db ethutil.Database, Root interface{}) *Trie {
r := copyRoot(Root) r := copyRoot(Root)
p := copyRoot(Root) p := copyRoot(Root)
return &Trie{cache: NewCache(db), Root: r, prevRoot: p} trie := &Trie{cache: NewCache(db), Root: r, prevRoot: p}
trie.setRoot(Root)
return trie
}
func (self *Trie) setRoot(root interface{}) {
switch t := root.(type) {
case string:
if t == "" {
root = ethcrypto.Sha3([]byte(""))
}
self.Root = root
case []byte:
self.Root = root
default:
self.Root = self.cache.PutValue(root, true)
}
} }
/* /*
@ -182,14 +199,7 @@ func (t *Trie) Update(key, value string) {
k := CompactHexDecode(key) k := CompactHexDecode(key)
root := t.UpdateState(t.Root, k, value) root := t.UpdateState(t.Root, k, value)
switch root.(type) { t.setRoot(root)
case string:
t.Root = root
case []byte:
t.Root = root
default:
t.Root = t.cache.PutValue(root, true)
}
} }
func (t *Trie) Get(key string) string { func (t *Trie) Get(key string) string {
@ -209,14 +219,7 @@ func (t *Trie) Delete(key string) {
k := CompactHexDecode(key) k := CompactHexDecode(key)
root := t.deleteState(t.Root, k) root := t.deleteState(t.Root, k)
switch root.(type) { t.setRoot(root)
case string:
t.Root = root
case []byte:
t.Root = root
default:
t.Root = t.cache.PutValue(root, true)
}
} }
func (self *Trie) GetRoot() []byte { func (self *Trie) GetRoot() []byte {