From 771f64397fd8638e9a40a4b9ecc64a9b70d6f2e4 Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 13:51:34 +0200 Subject: [PATCH 01/11] Stop peers when they don't respond to ping/pong. Might fix ethereum/go-ethereum#78 --- peer.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/peer.go b/peer.go index eed5bec30..9cc892d8b 100644 --- a/peer.go +++ b/peer.go @@ -18,6 +18,8 @@ const ( outputBufferSize = 50 // Current protocol version ProtocolVersion = 17 + // Interval for ping/pong message + pingPongTimer = 30 * time.Second ) type DiscReason byte @@ -243,7 +245,7 @@ func (p *Peer) writeMessage(msg *ethwire.Msg) { err := ethwire.WriteMessage(p.conn, msg) if err != nil { - ethutil.Config.Log.Debugln("Can't send message:", err) + ethutil.Config.Log.Debugln("[PEER] Can't send message:", err) // Stop the client if there was an error writing to it p.Stop() return @@ -253,7 +255,7 @@ func (p *Peer) writeMessage(msg *ethwire.Msg) { // Outbound message handler. Outbound messages are handled here func (p *Peer) HandleOutbound() { // The ping timer. Makes sure that every 2 minutes a ping is send to the peer - pingTimer := time.NewTicker(30 * time.Second) + pingTimer := time.NewTicker(pingPongTimer) serviceTimer := time.NewTicker(5 * time.Minute) out: @@ -264,8 +266,14 @@ out: p.writeMessage(msg) p.lastSend = time.Now() - // Ping timer sends a ping to the peer each 2 minutes + // Ping timer case <-pingTimer.C: + timeSince := time.Since(time.Unix(p.lastPong, 0)) + if p.pingStartTime.IsZero() == false && timeSince > (pingPongTimer+10*time.Second) { + ethutil.Config.Log.Infof("[PEER] Peer did not respond to latest pong fast enough, it took %s, disconnecting.\n", timeSince) + p.Stop() + return + } p.writeMessage(ethwire.NewMessage(ethwire.MsgPingTy, "")) p.pingStartTime = time.Now() @@ -563,6 +571,7 @@ func (p *Peer) Stop() { // Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here p.ethereum.RemovePeer(p) + ethutil.Config.Log.Debugln("[PEER] Stopped peer:", p.conn.RemoteAddr()) } func (p *Peer) pushHandshake() error { From 1b40f69ce5166fbe8a13709caf31f50107fa3bdf Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 14:59:38 +0200 Subject: [PATCH 02/11] Prevent peer stop crash by removing logging --- peer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/peer.go b/peer.go index 9cc892d8b..80975ff81 100644 --- a/peer.go +++ b/peer.go @@ -571,7 +571,6 @@ func (p *Peer) Stop() { // Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here p.ethereum.RemovePeer(p) - ethutil.Config.Log.Debugln("[PEER] Stopped peer:", p.conn.RemoteAddr()) } func (p *Peer) pushHandshake() error { From 2995d6c281b83f5bb055a22093b2b94e64c477d3 Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 15:02:41 +0200 Subject: [PATCH 03/11] Validate minimum gasPrice and reject if not met --- ethchain/transaction_pool.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ethchain/transaction_pool.go b/ethchain/transaction_pool.go index ba2ffcef5..d4175d973 100644 --- a/ethchain/transaction_pool.go +++ b/ethchain/transaction_pool.go @@ -22,6 +22,7 @@ type TxMsgTy byte const ( TxPre = iota TxPost + minGasPrice = 1000000 ) type TxMsg struct { @@ -172,6 +173,12 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error { return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender()) } + if tx.IsContract() { + if tx.GasPrice.Cmp(big.NewInt(minGasPrice)) < 0 { + return fmt.Errorf("[TXPL] Gasprice to low, %s given should be at least %d.", tx.GasPrice, minGasPrice) + } + } + // Increment the nonce making each tx valid only once to prevent replay // attacks From 2e6cf42011a4176a01f3e3f777cc1ddc4125511f Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 17:15:18 +0200 Subject: [PATCH 04/11] Fix BigMax to return the biggest number, not the smallest --- ethutil/big.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethutil/big.go b/ethutil/big.go index 1c25a4784..7af6f7414 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -68,8 +68,8 @@ func BigCopy(src *big.Int) *big.Int { // Returns the maximum size big integer func BigMax(x, y *big.Int) *big.Int { if x.Cmp(y) <= 0 { - return x + return y } - return y + return x } From 753f749423df7d5fba55a4080383d215db8e0fc7 Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 17:22:06 +0200 Subject: [PATCH 05/11] Implement CalcGasPrice for ethereum/go-ethereum#77 --- ethchain/block.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ethchain/block.go b/ethchain/block.go index 73e29f878..780c60869 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -154,6 +154,35 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool { return true } +func (block *Block) CalcGasLimit(parent *Block) *big.Int { + if block.Number == big.NewInt(0) { + return ethutil.BigPow(10, 6) + } + previous := new(big.Int).Mul(big.NewInt(1023), parent.GasLimit) + current := new(big.Rat).Mul(new(big.Rat).SetInt(block.GasUsed), big.NewRat(6, 5)) + curInt := new(big.Int).Div(current.Num(), current.Denom()) + + result := new(big.Int).Add(previous, curInt) + result.Div(result, big.NewInt(1024)) + + min := ethutil.BigPow(10, 4) + + return ethutil.BigMax(min, result) + /* + base := new(big.Int) + base2 := new(big.Int) + parentGL := bc.CurrentBlock.GasLimit + parentUsed := bc.CurrentBlock.GasUsed + + base.Mul(parentGL, big.NewInt(1024-1)) + base2.Mul(parentUsed, big.NewInt(6)) + base2.Div(base2, big.NewInt(5)) + base.Add(base, base2) + base.Div(base, big.NewInt(1024)) + */ + +} + func (block *Block) BlockInfo() BlockInfo { bi := BlockInfo{} data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...)) From 69044fe5774840a49de3f881a490db52e907affb Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 17:22:43 +0200 Subject: [PATCH 06/11] Refactor to use new method --- ethchain/block_chain.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index b45d254b5..5e6ce46e1 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -72,19 +72,7 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block { block.Number = new(big.Int).Add(bc.CurrentBlock.Number, ethutil.Big1) - // max(10000, (parent gas limit * (1024 - 1) + (parent gas used * 6 / 5)) / 1024) - base := new(big.Int) - base2 := new(big.Int) - parentGL := bc.CurrentBlock.GasLimit - parentUsed := bc.CurrentBlock.GasUsed - - base.Mul(parentGL, big.NewInt(1024-1)) - base2.Mul(parentUsed, big.NewInt(6)) - base2.Div(base2, big.NewInt(5)) - base.Add(base, base2) - base.Div(base, big.NewInt(1024)) - - block.GasLimit = ethutil.BigMax(big.NewInt(10000), base) + block.GasLimit = block.CalcGasLimit(bc.CurrentBlock) } return block From bdc206885a1d9c730464f60ec65557403720be1e Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 10 Jun 2014 17:23:32 +0200 Subject: [PATCH 07/11] Don't mine transactions if they would go over the GasLimit implements ethereum/go-ethereum#77 further. --- ethchain/error.go | 18 ++++++++++++++++++ ethchain/state_manager.go | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/ethchain/error.go b/ethchain/error.go index 8d37b0208..29896bc59 100644 --- a/ethchain/error.go +++ b/ethchain/error.go @@ -2,6 +2,7 @@ package ethchain import ( "fmt" + "math/big" ) // Parent error. In case a parent is unknown this error will be thrown @@ -43,6 +44,23 @@ func IsValidationErr(err error) bool { return ok } +type GasLimitErr struct { + Message string + Is, Max *big.Int +} + +func IsGasLimitErr(err error) bool { + _, ok := err.(*GasLimitErr) + + return ok +} +func (err *GasLimitErr) Error() string { + return err.Message +} +func GasLimitError(is, max *big.Int) *GasLimitErr { + return &GasLimitErr{Message: fmt.Sprintf("GasLimit error. Max %s, transaction would take it to %s", max, is), Is: is, Max: max} +} + type NonceErr struct { Message string Is, Exp uint64 diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index f1c09b819..aea5433ff 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -114,6 +114,8 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra // Process each transaction/contract var receipts []*Receipt var validTxs []*Transaction + var ignoredTxs []*Transaction // Transactions which go over the gasLimit + totalUsedGas := big.NewInt(0) for _, tx := range txs { usedGas, err := sm.ApplyTransaction(state, block, tx) @@ -121,6 +123,12 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra if IsNonceErr(err) { continue } + if IsGasLimitErr(err) { + ignoredTxs = append(ignoredTxs, tx) + // We need to figure out if we want to do something with thse txes + ethutil.Config.Log.Debugln("Gastlimit:", err) + continue + } ethutil.Config.Log.Infoln(err) } @@ -151,6 +159,7 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac script []byte ) totalGasUsed = big.NewInt(0) + snapshot := state.Snapshot() // Apply the transaction to the current state gas, err = sm.Ethereum.TxPool().ProcessTransaction(tx, state, false) @@ -190,6 +199,14 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac } } + parent := sm.bc.GetBlock(block.PrevHash) + total := new(big.Int).Add(block.GasUsed, totalGasUsed) + limit := block.CalcGasLimit(parent) + if total.Cmp(limit) > 0 { + state.Revert(snapshot) + err = GasLimitError(total, limit) + } + return } From 97cc76214350b3ef9b0c15f53d06c684e01ede37 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 11 Jun 2014 10:28:18 +0200 Subject: [PATCH 08/11] Expose GasLimit to ethPub --- ethpub/types.go | 3 ++- ethutil/common.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ethpub/types.go b/ethpub/types.go index 6893c7e09..40ac32a27 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -46,6 +46,7 @@ type PBlock struct { Transactions string `json:"transactions"` Time int64 `json:"time"` Coinbase string `json:"coinbase"` + GasLimit string `json:"gasLimit"` } // Creates a new QML Block from a chain block @@ -64,7 +65,7 @@ func NewPBlock(block *ethchain.Block) *PBlock { return nil } - return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Hex(block.Coinbase)} + return &PBlock{ref: block, Number: int(block.Number.Uint64()), GasLimit: block.GasLimit.String(), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Hex(block.Coinbase)} } func (self *PBlock) ToString() string { diff --git a/ethutil/common.go b/ethutil/common.go index c7973eb92..ddaf78f88 100644 --- a/ethutil/common.go +++ b/ethutil/common.go @@ -18,8 +18,8 @@ var ( Wei = big.NewInt(1) ) -// Currency to string // +// Currency to string // Returns a string representing a human readable format func CurrencyToString(num *big.Int) string { switch { From e090d131c38bef3c5f2d0f5e9fa28f5d0ed06659 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 11 Jun 2014 11:40:40 +0200 Subject: [PATCH 09/11] Implemented counting of usedGas --- ethchain/state_manager.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index aea5433ff..6fb664e3d 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -140,6 +140,9 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra validTxs = append(validTxs, tx) } + // Update the total gas used for the block (to be mined) + block.GasUsed = totalUsedGas + return receipts, validTxs } From 71ab5d52b692a42cc3af034e5ff1aab5b4b6477d Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 11 Jun 2014 11:40:50 +0200 Subject: [PATCH 10/11] Exposed usedGas through ethPub --- ethpub/types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethpub/types.go b/ethpub/types.go index 40ac32a27..868fd2713 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -47,6 +47,7 @@ type PBlock struct { Time int64 `json:"time"` Coinbase string `json:"coinbase"` GasLimit string `json:"gasLimit"` + GasUsed string `json:"gasUsed"` } // Creates a new QML Block from a chain block @@ -65,7 +66,7 @@ func NewPBlock(block *ethchain.Block) *PBlock { return nil } - return &PBlock{ref: block, Number: int(block.Number.Uint64()), GasLimit: block.GasLimit.String(), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Hex(block.Coinbase)} + return &PBlock{ref: block, Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), GasLimit: block.GasLimit.String(), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Hex(block.Coinbase)} } func (self *PBlock) ToString() string { From 1938bfcddfd2722880a692c59cad344b611711c8 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 11 Jun 2014 16:16:57 +0200 Subject: [PATCH 11/11] Fix compare --- ethchain/block.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethchain/block.go b/ethchain/block.go index 780c60869..fee4a2d59 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -155,9 +155,10 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool { } func (block *Block) CalcGasLimit(parent *Block) *big.Int { - if block.Number == big.NewInt(0) { + if block.Number.Cmp(big.NewInt(0)) == 0 { return ethutil.BigPow(10, 6) } + previous := new(big.Int).Mul(big.NewInt(1023), parent.GasLimit) current := new(big.Rat).Mul(new(big.Rat).SetInt(block.GasUsed), big.NewRat(6, 5)) curInt := new(big.Int).Div(current.Num(), current.Denom())