From 50661f0e683b4975894a0e8fe16024724adef72d Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 19 Mar 2015 22:46:54 +0000 Subject: peer suspension to disallow reconnect after disconnect on fatal error for set period (PeerSuspensionInterval) --- eth/protocol.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'eth') diff --git a/eth/protocol.go b/eth/protocol.go index 6d610a663..1999d9807 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -42,6 +42,7 @@ const ( ErrGenesisBlockMismatch ErrNoStatusMsg ErrExtraStatusMsg + ErrSuspendedPeer ) var errorToString = map[int]string{ @@ -53,6 +54,7 @@ var errorToString = map[int]string{ ErrGenesisBlockMismatch: "Genesis block mismatch", ErrNoStatusMsg: "No status message", ErrExtraStatusMsg: "Extra status message", + ErrSuspendedPeer: "Suspended peer", } // ethProtocol represents the ethereum wire protocol @@ -85,7 +87,7 @@ type chainManager interface { type blockPool interface { AddBlockHashes(next func() (common.Hash, bool), peerId string) AddBlock(block *types.Block, peerId string) - AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool) + AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) RemovePeer(peerId string) } @@ -288,7 +290,7 @@ func (self *ethProtocol) handle() error { // to simplify backend interface adding a new block // uses AddPeer followed by AddBlock only if peer is the best peer // (or selected as new best peer) - if self.blockPool.AddPeer(request.TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) { + if best, _ := self.blockPool.AddPeer(request.TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect); best { self.blockPool.AddBlock(request.Block, self.id) } @@ -334,9 +336,12 @@ func (self *ethProtocol) handleStatus() error { return self.protoError(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, self.protocolVersion) } - self.peer.Infof("Peer is [eth] capable (%d/%d). TD=%v H=%x\n", status.ProtocolVersion, status.NetworkId, status.TD, status.CurrentBlock[:4]) + _, suspended := self.blockPool.AddPeer(status.TD, status.CurrentBlock, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) + if suspended { + return self.protoError(ErrSuspendedPeer, "") + } - self.blockPool.AddPeer(status.TD, status.CurrentBlock, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) + self.peer.Infof("Peer is [eth] capable (%d/%d). TD=%v H=%x\n", status.ProtocolVersion, status.NetworkId, status.TD, status.CurrentBlock[:4]) return nil } -- cgit From 391e89d70a43b4a2153db8acac9a6af7a4f76adf Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 19 Mar 2015 22:53:15 +0000 Subject: use own total difficulty to limit best peer - update blockpool td by subscribing to ChainHeadEvent - if ahead of best peer, demote it - addPeer now take own td as current td - removePeer now take own td as current td - add relevant tests to peers_test - eth: backend now calls blockpool with eth.eventMux and chainManager.Td --- eth/backend.go | 3 ++- eth/protocol_test.go | 13 +++---------- 2 files changed, 5 insertions(+), 11 deletions(-) (limited to 'eth') diff --git a/eth/backend.go b/eth/backend.go index afe314d74..141c6c605 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -195,7 +195,8 @@ func New(config *Config) (*Ethereum, error) { hasBlock := eth.chainManager.HasBlock insertChain := eth.chainManager.InsertChain - eth.blockPool = blockpool.New(hasBlock, insertChain, eth.pow.Verify) + td := eth.chainManager.Td() + eth.blockPool = blockpool.New(hasBlock, insertChain, eth.pow.Verify, eth.EventMux(), td) netprv, err := config.nodeKey() if err != nil { diff --git a/eth/protocol_test.go b/eth/protocol_test.go index 7620b3854..8ca6d1be6 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -41,17 +41,10 @@ type testChainManager struct { type testBlockPool struct { addBlockHashes func(next func() (common.Hash, bool), peerId string) addBlock func(block *types.Block, peerId string) (err error) - addPeer func(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool) + addPeer func(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) removePeer func(peerId string) } -// func (self *testTxPool) GetTransactions() (txs []*types.Transaction) { -// if self.getTransactions != nil { -// txs = self.getTransactions() -// } -// return -// } - func (self *testTxPool) AddTransactions(txs []*types.Transaction) { if self.addTransactions != nil { self.addTransactions(txs) @@ -93,9 +86,9 @@ func (self *testBlockPool) AddBlock(block *types.Block, peerId string) { } } -func (self *testBlockPool) AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestBlockHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool) { +func (self *testBlockPool) AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestBlockHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) { if self.addPeer != nil { - best = self.addPeer(td, currentBlock, peerId, requestBlockHashes, requestBlocks, peerError) + best, suspended = self.addPeer(td, currentBlock, peerId, requestBlockHashes, requestBlocks, peerError) } return } -- cgit From 0578df9467bb00be967da7798cc0ea8b6e7e48d7 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 19 Mar 2015 13:02:56 +0000 Subject: remove eth/wallet.go (only commented out content) --- eth/wallet.go | 80 ----------------------------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 eth/wallet.go (limited to 'eth') diff --git a/eth/wallet.go b/eth/wallet.go deleted file mode 100644 index 9ec834309..000000000 --- a/eth/wallet.go +++ /dev/null @@ -1,80 +0,0 @@ -package eth - -/* -import ( - "crypto/ecdsa" - "errors" - "math/big" - - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" -) - -type Account struct { - w *Wallet -} - -func (self *Account) Transact(to *Account, value, gas, price *big.Int, data []byte) error { - return self.w.transact(self, to, value, gas, price, data) -} - -func (self *Account) Address() []byte { - return nil -} - -func (self *Account) PrivateKey() *ecdsa.PrivateKey { - return nil -} - -type Wallet struct{} - -func NewWallet() *Wallet { - return &Wallet{} -} - -func (self *Wallet) GetAccount(i int) *Account { -} - -func (self *Wallet) transact(from, to *Account, value, gas, price *big.Int, data []byte) error { - if from.PrivateKey() == nil { - return errors.New("accounts is not owned (no private key available)") - } - - var createsContract bool - if to == nil { - createsContract = true - } - - var msg *types.Transaction - if contractCreation { - msg = types.NewContractCreationTx(value, gas, price, data) - } else { - msg = types.NewTransactionMessage(to.Address(), value, gas, price, data) - } - - state := self.chainManager.TransState() - nonce := state.GetNonce(key.Address()) - - msg.SetNonce(nonce) - msg.SignECDSA(from.PriateKey()) - - // Do some pre processing for our "pre" events and hooks - block := self.chainManager.NewBlock(from.Address()) - coinbase := state.GetOrNewStateObject(from.Address()) - coinbase.SetGasPool(block.GasLimit()) - self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) - - err := self.obj.TxPool().Add(tx) - if err != nil { - return nil, err - } - state.SetNonce(key.Address(), nonce+1) - - if contractCreation { - addr := core.AddressFromMessage(tx) - pipelogger.Infof("Contract addr %x\n", addr) - } - - return tx, nil -} -*/ -- cgit From ecd10d2cf765072cd74347b9e0ca2bb85091450f Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 20 Mar 2015 18:00:54 +0100 Subject: iterator returned wrong value --- eth/protocol.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'eth') diff --git a/eth/protocol.go b/eth/protocol.go index 1999d9807..494c1c1bb 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -213,8 +213,7 @@ func (self *ethProtocol) handle() error { var i int iter := func() (hash common.Hash, ok bool) { - var h common.Hash - err := msgStream.Decode(&h) + err := msgStream.Decode(&hash) if err == rlp.EOL { return common.Hash{}, false } else if err != nil { -- cgit