Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Jan 5, 2015
1 parent b0854fb commit 6abf8ef
Show file tree
Hide file tree
Showing 41 changed files with 2,303 additions and 1,177 deletions.
6 changes: 5 additions & 1 deletion cmd/ethereum/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ var (
DumpNumber int
VmType int
ImportChain string
SHH bool
Dial bool
)

// flags specific to cli client
Expand Down Expand Up @@ -94,6 +96,8 @@ func Init() {
flag.BoolVar(&StartWebSockets, "ws", false, "start websocket server")
flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)")
flag.BoolVar(&UseSeed, "seed", true, "seed peers")
flag.BoolVar(&SHH, "shh", true, "whisper protocol (on)")
flag.BoolVar(&Dial, "dial", true, "dial out connections (on)")
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
Expand All @@ -105,7 +109,7 @@ func Init() {
flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block")
flag.StringVar(&ImportChain, "chain", "", "Imports fiven chain")
flag.StringVar(&ImportChain, "chain", "", "Imports given chain")

flag.BoolVar(&Dump, "dump", false, "output the ethereum state in JSON format. Sub args [number, hash]")
flag.StringVar(&DumpHash, "hash", "", "specify arg in hex")
Expand Down
16 changes: 9 additions & 7 deletions cmd/ethereum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ func main() {
NATType: PMPGateway,
PMPGateway: PMPGateway,
KeyRing: KeyRing,
Shh: SHH,
Dial: Dial,
})

if err != nil {
clilogger.Fatalln(err)
}

utils.KeyTasks(ethereum.KeyManager(), KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)

if Dump {
Expand Down Expand Up @@ -112,13 +116,6 @@ func main() {
return
}

// better reworked as cases
if StartJsConsole {
InitJsConsole(ethereum)
} else if len(InputFile) > 0 {
ExecJsFile(ethereum, InputFile)
}

if StartRpc {
utils.StartRpc(ethereum, RpcPort)
}
Expand All @@ -129,6 +126,11 @@ func main() {

utils.StartEthereum(ethereum, UseSeed)

if StartJsConsole {
InitJsConsole(ethereum)
} else if len(InputFile) > 0 {
ExecJsFile(ethereum, InputFile)
}
// this blocks the thread
ethereum.WaitForShutdown()
}
54 changes: 23 additions & 31 deletions core/transaction_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"gopkg.in/fatih/set.v0"
)

var txplogger = logger.NewLogger("TXP")
Expand Down Expand Up @@ -38,7 +37,7 @@ type TxPool struct {
quit chan bool
// The actual pool
//pool *list.List
pool *set.Set
txs map[string]*types.Transaction

SecondaryProcessor TxProcessor

Expand All @@ -49,21 +48,19 @@ type TxPool struct {

func NewTxPool(eventMux *event.TypeMux) *TxPool {
return &TxPool{
pool: set.New(),
txs: make(map[string]*types.Transaction),
queueChan: make(chan *types.Transaction, txPoolQueueSize),
quit: make(chan bool),
eventMux: eventMux,
}
}

func (pool *TxPool) addTransaction(tx *types.Transaction) {
pool.pool.Add(tx)

// Broadcast the transaction to the rest of the peers
pool.eventMux.Post(TxPreEvent{tx})
}

func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
hash := tx.Hash()
if pool.txs[string(hash)] != nil {
return fmt.Errorf("Known transaction (%x)", hash[0:4])
}

if len(tx.To()) != 0 && len(tx.To()) != 20 {
return fmt.Errorf("Invalid recipient. len = %d", len(tx.To()))
}
Expand Down Expand Up @@ -95,18 +92,17 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
return nil
}

func (self *TxPool) Add(tx *types.Transaction) error {
hash := tx.Hash()
if self.pool.Has(tx) {
return fmt.Errorf("Known transaction (%x)", hash[0:4])
}
func (self *TxPool) addTx(tx *types.Transaction) {
self.txs[string(tx.Hash())] = tx
}

func (self *TxPool) Add(tx *types.Transaction) error {
err := self.ValidateTransaction(tx)
if err != nil {
return err
}

self.addTransaction(tx)
self.addTx(tx)

var to string
if len(tx.To()) > 0 {
Expand All @@ -124,7 +120,7 @@ func (self *TxPool) Add(tx *types.Transaction) error {
}

func (self *TxPool) Size() int {
return self.pool.Size()
return len(self.txs)
}

func (self *TxPool) AddTransactions(txs []*types.Transaction) {
Expand All @@ -137,43 +133,39 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) {
}
}

func (pool *TxPool) GetTransactions() []*types.Transaction {
txList := make([]*types.Transaction, pool.Size())
func (self *TxPool) GetTransactions() (txs types.Transactions) {
txs = make(types.Transactions, self.Size())
i := 0
pool.pool.Each(func(v interface{}) bool {
txList[i] = v.(*types.Transaction)
for _, tx := range self.txs {
txs[i] = tx
i++
}

return true
})

return txList
return
}

func (pool *TxPool) RemoveInvalid(query StateQuery) {
var removedTxs types.Transactions
pool.pool.Each(func(v interface{}) bool {
tx := v.(*types.Transaction)
for _, tx := range pool.txs {
sender := query.GetAccount(tx.From())
err := pool.ValidateTransaction(tx)
if err != nil || sender.Nonce >= tx.Nonce() {
removedTxs = append(removedTxs, tx)
}
}

return true
})
pool.RemoveSet(removedTxs)
}

func (self *TxPool) RemoveSet(txs types.Transactions) {
for _, tx := range txs {
self.pool.Remove(tx)
delete(self.txs, string(tx.Hash()))
}
}

func (pool *TxPool) Flush() []*types.Transaction {
txList := pool.GetTransactions()
pool.pool.Clear()
pool.txs = make(map[string]*types.Transaction)

return txList
}
Expand Down
77 changes: 35 additions & 42 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ func (self *Header) HashNoNonce() []byte {
}

type Block struct {
header *Header
uncles []*Header
transactions Transactions
Td *big.Int
// Preset Hash for mock
HeaderHash []byte
ParentHeaderHash []byte
header *Header
uncles []*Header
transactions Transactions
Td *big.Int

receipts Receipts
Reward *big.Int
Expand Down Expand Up @@ -99,41 +102,19 @@ func NewBlockWithHeader(header *Header) *Block {
}

func (self *Block) DecodeRLP(s *rlp.Stream) error {
if _, err := s.List(); err != nil {
return err
}

var header Header
if err := s.Decode(&header); err != nil {
return err
}

var transactions []*Transaction
if err := s.Decode(&transactions); err != nil {
return err
var extblock struct {
Header *Header
Txs []*Transaction
Uncles []*Header
TD *big.Int // optional
}

var uncleHeaders []*Header
if err := s.Decode(&uncleHeaders); err != nil {
return err
}

var tdBytes []byte
if err := s.Decode(&tdBytes); err != nil {
// If this block comes from the network that's fine. If loaded from disk it should be there
// Blocks don't store their Td when propagated over the network
} else {
self.Td = ethutil.BigD(tdBytes)
}

if err := s.ListEnd(); err != nil {
if err := s.Decode(&extblock); err != nil {
return err
}

self.header = &header
self.uncles = uncleHeaders
self.transactions = transactions

self.header = extblock.Header
self.uncles = extblock.Uncles
self.transactions = extblock.Txs
self.Td = extblock.TD
return nil
}

Expand Down Expand Up @@ -189,23 +170,35 @@ func (self *Block) RlpDataForStorage() interface{} {
// Header accessors (add as you need them)
func (self *Block) Number() *big.Int { return self.header.Number }
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
func (self *Block) ParentHash() []byte { return self.header.ParentHash }
func (self *Block) Bloom() []byte { return self.header.Bloom }
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
func (self *Block) Time() int64 { return int64(self.header.Time) }
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
func (self *Block) Hash() []byte { return self.header.Hash() }
func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }

// Implement block.Pow
// Implement pow.Block
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
func (self *Block) N() []byte { return self.header.Nonce }
func (self *Block) HashNoNonce() []byte {
return crypto.Sha3(ethutil.Encode(self.header.rlpData(false)))
func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() }

func (self *Block) Hash() []byte {
if self.HeaderHash != nil {
return self.HeaderHash
} else {
return self.header.Hash()
}
}

func (self *Block) ParentHash() []byte {
if self.ParentHeaderHash != nil {
return self.ParentHeaderHash
} else {
return self.header.ParentHash
}
}

func (self *Block) String() string {
Expand Down
Loading

0 comments on commit 6abf8ef

Please sign in to comment.