Merge pull request #1059 from obscuren/cleanup

Cleanup
This commit is contained in:
Jeffrey Wilcke 2015-05-21 04:11:36 -07:00
commit bf7dcfce36
5 changed files with 37 additions and 36 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"gopkg.in/fatih/set.v0"
) )
const ( const (
@ -346,50 +347,39 @@ func AccumulateRewards(statedb *state.StateDB, block *types.Block) {
} }
func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *types.Block) error { func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *types.Block) error {
//ancestors := set.New() ancestors := set.New()
//uncles := set.New() uncles := set.New()
ancestors := make(map[common.Hash]struct{})
uncles := make(map[common.Hash]struct{})
ancestorHeaders := make(map[common.Hash]*types.Header) ancestorHeaders := make(map[common.Hash]*types.Header)
for _, ancestor := range sm.bc.GetAncestors(block, 7) { for _, ancestor := range sm.bc.GetAncestors(block, 7) {
ancestorHeaders[ancestor.Hash()] = ancestor.Header() ancestorHeaders[ancestor.Hash()] = ancestor.Header()
//ancestors.Add(ancestor.Hash()) ancestors.Add(ancestor.Hash())
ancestors[ancestor.Hash()] = struct{}{}
// Include ancestors uncles in the uncle set. Uncles must be unique. // Include ancestors uncles in the uncle set. Uncles must be unique.
for _, uncle := range ancestor.Uncles() { for _, uncle := range ancestor.Uncles() {
//uncles.Add(uncle.Hash()) uncles.Add(uncle.Hash())
uncles[uncle.Hash()] = struct{}{}
} }
} }
//uncles.Add(block.Hash()) uncles.Add(block.Hash())
uncles[block.Hash()] = struct{}{}
for i, uncle := range block.Uncles() { for i, uncle := range block.Uncles() {
hash := uncle.Hash() hash := uncle.Hash()
//if uncles.Has(hash) { if uncles.Has(hash) {
if _, has := uncles[hash]; has {
// Error not unique // Error not unique
return UncleError("uncle[%d](%x) not unique", i, hash[:4]) return UncleError("uncle[%d](%x) not unique", i, hash[:4])
} }
uncles[hash] = struct{}{} uncles.Add(hash)
//if ancestors.Has(hash) { if ancestors.Has(hash) {
if _, has := ancestors[hash]; has { branch := fmt.Sprintf(" O - %x\n |\n", block.Hash())
var branch string ancestors.Each(func(item interface{}) bool {
//ancestors.Each(func(item interface{}) bool {
for hash := range ancestors {
branch += fmt.Sprintf(" O - %x\n |\n", hash) branch += fmt.Sprintf(" O - %x\n |\n", hash)
//return true return true
} })
//})
branch += fmt.Sprintf(" O - %x\n |\n", block.Hash())
glog.Infoln(branch) glog.Infoln(branch)
return UncleError("uncle[%d](%x) is ancestor", i, hash[:4]) return UncleError("uncle[%d](%x) is ancestor", i, hash[:4])
} }
//if !ancestors.Has(uncle.ParentHash) { if !ancestors.Has(uncle.ParentHash) {
if _, has := ancestors[uncle.ParentHash]; !has {
return UncleError("uncle[%d](%x)'s parent unknown (%x)", i, hash[:4], uncle.ParentHash[0:4]) return UncleError("uncle[%d](%x)'s parent unknown (%x)", i, hash[:4], uncle.ParentHash[0:4])
} }

View File

@ -213,7 +213,7 @@ func New(config *Config) (*Ethereum, error) {
// Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files) // Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files)
const dbCount = 3 const dbCount = 3
ethdb.OpenFileLimit = 256 / (dbCount + 1) ethdb.OpenFileLimit = 128 / (dbCount + 1)
newdb := config.NewDB newdb := config.NewDB
if newdb == nil { if newdb == nil {

View File

@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/iterator" "github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/opt"
) )
@ -24,9 +25,17 @@ type LDBDatabase struct {
quit chan struct{} quit chan struct{}
} }
// NewLDBDatabase returns a LevelDB wrapped object. LDBDatabase does not persist data by
// it self but requires a background poller which syncs every X. `Flush` should be called
// when data needs to be stored and written to disk.
func NewLDBDatabase(file string) (*LDBDatabase, error) { func NewLDBDatabase(file string) (*LDBDatabase, error) {
// Open the db // Open the db
db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: OpenFileLimit}) db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: OpenFileLimit})
// check for curruption and attempt to recover
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
db, err = leveldb.RecoverFile(file, nil)
}
// (re) check for errors and abort if opening of the db failed
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -44,21 +53,15 @@ func (self *LDBDatabase) makeQueue() {
self.queue = make(map[string][]byte) self.queue = make(map[string][]byte)
} }
// Put puts the given key / value to the queue
func (self *LDBDatabase) Put(key []byte, value []byte) { func (self *LDBDatabase) Put(key []byte, value []byte) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
self.queue[string(key)] = value self.queue[string(key)] = value
/*
value = rle.Compress(value)
err := self.db.Put(key, value, nil)
if err != nil {
fmt.Println("Error put", err)
}
*/
} }
// Get returns the given key if it's present.
func (self *LDBDatabase) Get(key []byte) ([]byte, error) { func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
@ -76,6 +79,7 @@ func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
return rle.Decompress(dat) return rle.Decompress(dat)
} }
// Delete deletes the key from the queue and database
func (self *LDBDatabase) Delete(key []byte) error { func (self *LDBDatabase) Delete(key []byte) error {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
@ -100,6 +104,7 @@ func (self *LDBDatabase) NewIterator() iterator.Iterator {
return self.db.NewIterator(nil, nil) return self.db.NewIterator(nil, nil)
} }
// Flush flushes out the queue to leveldb
func (self *LDBDatabase) Flush() error { func (self *LDBDatabase) Flush() error {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()

View File

@ -39,6 +39,10 @@ func New(eth core.Backend, mux *event.TypeMux, pow pow.PoW) *Miner {
return miner return miner
} }
// update keeps track of the downloader events. Please be aware that this is a one shot type of update loop.
// It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
// and halt your mining operation for as long as the DOS continues.
func (self *Miner) update() { func (self *Miner) update() {
events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{}) events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
for ev := range events.Chan() { for ev := range events.Chan() {
@ -59,6 +63,8 @@ func (self *Miner) update() {
self.Start(self.coinbase, self.threads) self.Start(self.coinbase, self.threads)
} }
} }
// unsubscribe. we're only interested in this event once
events.Unsubscribe()
} }
} }

View File

@ -881,7 +881,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
var ( var (
from = common.HexToAddress(fromStr) from = common.HexToAddress(fromStr)
to = common.HexToAddress(toStr) to = common.HexToAddress(toStr)
value = common.NewValue(valueStr) value = common.Big(valueStr)
gas = common.Big(gasStr) gas = common.Big(gasStr)
price = common.Big(gasPriceStr) price = common.Big(gasPriceStr)
data []byte data []byte
@ -928,9 +928,9 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
var tx *types.Transaction var tx *types.Transaction
if contractCreation { if contractCreation {
tx = types.NewContractCreationTx(value.BigInt(), gas, price, data) tx = types.NewContractCreationTx(value, gas, price, data)
} else { } else {
tx = types.NewTransactionMessage(to, value.BigInt(), gas, price, data) tx = types.NewTransactionMessage(to, value, gas, price, data)
} }
state := self.backend.ChainManager().TxState() state := self.backend.ChainManager().TxState()