Skip to content

Commit fdb34b7

Browse files
authored
Merge pull request #15996 from karalabe/drop-redundant-methods
core, eth, les, light: get rid of redundant methods
2 parents 07d4a02 + 566d5c0 commit fdb34b7

10 files changed

Lines changed: 54 additions & 65 deletions

File tree

‎core/blockchain.go‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,6 @@ func (bc *BlockChain) GasLimit() uint64 {
312312
return bc.currentBlock.GasLimit()
313313
}
314314

315-
// LastBlockHash return the hash of the HEAD block.
316-
func (bc *BlockChain) LastBlockHash() common.Hash {
317-
bc.mu.RLock()
318-
defer bc.mu.RUnlock()
319-
320-
return bc.currentBlock.Hash()
321-
}
322-
323315
// CurrentBlock retrieves the current head block of the canonical chain. The
324316
// block is retrieved from the blockchain's internal cache.
325317
func (bc *BlockChain) CurrentBlock() *types.Block {
@@ -338,15 +330,6 @@ func (bc *BlockChain) CurrentFastBlock() *types.Block {
338330
return bc.currentFastBlock
339331
}
340332

341-
// Status returns status information about the current chain such as the HEAD Td,
342-
// the HEAD hash and the hash of the genesis block.
343-
func (bc *BlockChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
344-
bc.mu.RLock()
345-
defer bc.mu.RUnlock()
346-
347-
return bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()), bc.currentBlock.Hash(), bc.genesisBlock.Hash()
348-
}
349-
350333
// SetProcessor sets the processor required for making state modifications.
351334
func (bc *BlockChain) SetProcessor(processor Processor) {
352335
bc.procmu.Lock()
@@ -1006,7 +989,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
1006989
stats.report(chain, i)
1007990
}
1008991
// Append a single chain head event if we've progressed the chain
1009-
if lastCanon != nil && bc.LastBlockHash() == lastCanon.Hash() {
992+
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
1010993
events = append(events, ChainHeadEvent{lastCanon})
1011994
}
1012995
return 0, events, coalescedLogs, nil

‎eth/downloader/downloader.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ type LightChain interface {
164164
// CurrentHeader retrieves the head header from the local chain.
165165
CurrentHeader() *types.Header
166166

167-
// GetTdByHash returns the total difficulty of a local block.
168-
GetTdByHash(common.Hash) *big.Int
167+
// GetTd returns the total difficulty of a local block.
168+
GetTd(common.Hash, uint64) *big.Int
169169

170170
// InsertHeaderChain inserts a batch of headers into the local chain.
171171
InsertHeaderChain([]*types.Header, int) (int, error)
@@ -1218,7 +1218,8 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
12181218
// L: Request new headers up from 11 (R's TD was higher, it must have something)
12191219
// R: Nothing to give
12201220
if d.mode != LightSync {
1221-
if !gotHeaders && td.Cmp(d.blockchain.GetTdByHash(d.blockchain.CurrentBlock().Hash())) > 0 {
1221+
head := d.blockchain.CurrentBlock()
1222+
if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
12221223
return errStallingPeer
12231224
}
12241225
}
@@ -1230,7 +1231,8 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
12301231
// queued for processing when the header download completes. However, as long as the
12311232
// peer gave us something useful, we're already happy/progressed (above check).
12321233
if d.mode == FastSync || d.mode == LightSync {
1233-
if td.Cmp(d.lightchain.GetTdByHash(d.lightchain.CurrentHeader().Hash())) > 0 {
1234+
head := d.lightchain.CurrentHeader()
1235+
if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
12341236
return errStallingPeer
12351237
}
12361238
}

‎eth/downloader/downloader_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error {
299299
return fmt.Errorf("non existent block: %x", hash[:4])
300300
}
301301

302-
// GetTdByHash retrieves the block's total difficulty from the canonical chain.
303-
func (dl *downloadTester) GetTdByHash(hash common.Hash) *big.Int {
302+
// GetTd retrieves the block's total difficulty from the canonical chain.
303+
func (dl *downloadTester) GetTd(hash common.Hash, number uint64) *big.Int {
304304
dl.lock.RLock()
305305
defer dl.lock.RUnlock()
306306

‎eth/handler.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,14 @@ func (pm *ProtocolManager) handle(p *peer) error {
257257
p.Log().Debug("Ethereum peer connected", "name", p.Name())
258258

259259
// Execute the Ethereum handshake
260-
td, head, genesis := pm.blockchain.Status()
261-
if err := p.Handshake(pm.networkId, td, head, genesis); err != nil {
260+
var (
261+
genesis = pm.blockchain.Genesis()
262+
head = pm.blockchain.CurrentHeader()
263+
hash = head.Hash()
264+
number = head.Number.Uint64()
265+
td = pm.blockchain.GetTd(hash, number)
266+
)
267+
if err := p.Handshake(pm.networkId, td, hash, genesis.Hash()); err != nil {
262268
p.Log().Debug("Ethereum handshake failed", "err", err)
263269
return err
264270
}

‎eth/helper_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te
166166
tp := &testPeer{app: app, net: net, peer: peer}
167167
// Execute any implicitly requested handshakes and return
168168
if shake {
169-
td, head, genesis := pm.blockchain.Status()
170-
tp.handshake(nil, td, head, genesis)
169+
var (
170+
genesis = pm.blockchain.Genesis()
171+
head = pm.blockchain.CurrentHeader()
172+
td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64())
173+
)
174+
tp.handshake(nil, td, head.Hash(), genesis.Hash())
171175
}
172176
return tp, errc
173177
}

‎eth/protocol_test.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ func TestStatusMsgErrors63(t *testing.T) { testStatusMsgErrors(t, 63) }
4242

4343
func testStatusMsgErrors(t *testing.T, protocol int) {
4444
pm := newTestProtocolManagerMust(t, downloader.FullSync, 0, nil, nil)
45-
td, currentBlock, genesis := pm.blockchain.Status()
45+
var (
46+
genesis = pm.blockchain.Genesis()
47+
head = pm.blockchain.CurrentHeader()
48+
td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64())
49+
)
4650
defer pm.Stop()
4751

4852
tests := []struct {
@@ -55,16 +59,16 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
5559
wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
5660
},
5761
{
58-
code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, td, currentBlock, genesis},
62+
code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, td, head.Hash(), genesis.Hash()},
5963
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
6064
},
6165
{
62-
code: StatusMsg, data: statusData{uint32(protocol), 999, td, currentBlock, genesis},
66+
code: StatusMsg, data: statusData{uint32(protocol), 999, td, head.Hash(), genesis.Hash()},
6367
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 1)"),
6468
},
6569
{
66-
code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, td, currentBlock, common.Hash{3}},
67-
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000 (!= %x)", genesis[:8]),
70+
code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, td, head.Hash(), common.Hash{3}},
71+
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000 (!= %x)", genesis.Hash().Bytes()[:8]),
6872
},
6973
}
7074

‎les/handler.go‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ type BlockChain interface {
7777
GetHeader(hash common.Hash, number uint64) *types.Header
7878
GetHeaderByHash(hash common.Hash) *types.Header
7979
CurrentHeader() *types.Header
80-
GetTdByHash(hash common.Hash) *big.Int
80+
GetTd(hash common.Hash, number uint64) *big.Int
8181
InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)
8282
Rollback(chain []common.Hash)
83-
Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
8483
GetHeaderByNumber(number uint64) *types.Header
8584
GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash
86-
LastBlockHash() common.Hash
8785
Genesis() *types.Block
8886
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
8987
}
@@ -262,9 +260,14 @@ func (pm *ProtocolManager) handle(p *peer) error {
262260
p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
263261

264262
// Execute the LES handshake
265-
td, head, genesis := pm.blockchain.Status()
266-
headNum := core.GetBlockNumber(pm.chainDb, head)
267-
if err := p.Handshake(td, head, headNum, genesis, pm.server); err != nil {
263+
var (
264+
genesis = pm.blockchain.Genesis()
265+
head = pm.blockchain.CurrentHeader()
266+
hash = head.Hash()
267+
number = head.Number.Uint64()
268+
td = pm.blockchain.GetTd(hash, number)
269+
)
270+
if err := p.Handshake(td, hash, number, genesis.Hash(), pm.server); err != nil {
268271
p.Log().Debug("Light Ethereum handshake failed", "err", err)
269272
return err
270273
}
@@ -1135,12 +1138,15 @@ type NodeInfo struct {
11351138

11361139
// NodeInfo retrieves some protocol metadata about the running host node.
11371140
func (self *ProtocolManager) NodeInfo() *NodeInfo {
1141+
head := self.blockchain.CurrentHeader()
1142+
hash := head.Hash()
1143+
11381144
return &NodeInfo{
11391145
Network: self.networkId,
1140-
Difficulty: self.blockchain.GetTdByHash(self.blockchain.LastBlockHash()),
1146+
Difficulty: self.blockchain.GetTd(hash, head.Number.Uint64()),
11411147
Genesis: self.blockchain.Genesis().Hash(),
11421148
Config: self.blockchain.Config(),
1143-
Head: self.blockchain.LastBlockHash(),
1149+
Head: hash,
11441150
}
11451151
}
11461152

‎les/helper_test.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,12 @@ func newTestPeer(t *testing.T, name string, version int, pm *ProtocolManager, sh
227227
}
228228
// Execute any implicitly requested handshakes and return
229229
if shake {
230-
td, head, genesis := pm.blockchain.Status()
231-
headNum := pm.blockchain.CurrentHeader().Number.Uint64()
232-
tp.handshake(t, td, head, headNum, genesis)
230+
var (
231+
genesis = pm.blockchain.Genesis()
232+
head = pm.blockchain.CurrentHeader()
233+
td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64())
234+
)
235+
tp.handshake(t, td, head.Hash(), head.Number.Uint64(), genesis.Hash())
233236
}
234237
return tp, errc
235238
}

‎light/lightchain.go‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,6 @@ func (self *LightChain) GasLimit() uint64 {
176176
return self.hc.CurrentHeader().GasLimit
177177
}
178178

179-
// LastBlockHash return the hash of the HEAD block.
180-
func (self *LightChain) LastBlockHash() common.Hash {
181-
self.mu.RLock()
182-
defer self.mu.RUnlock()
183-
184-
return self.hc.CurrentHeader().Hash()
185-
}
186-
187-
// Status returns status information about the current chain such as the HEAD Td,
188-
// the HEAD hash and the hash of the genesis block.
189-
func (self *LightChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
190-
self.mu.RLock()
191-
defer self.mu.RUnlock()
192-
193-
header := self.hc.CurrentHeader()
194-
hash := header.Hash()
195-
return self.GetTd(hash, header.Number.Uint64()), hash, self.genesisBlock.Hash()
196-
}
197-
198179
// Reset purges the entire blockchain, restoring it to its genesis state.
199180
func (bc *LightChain) Reset() {
200181
bc.ResetWithGenesisBlock(bc.genesisBlock)
@@ -337,7 +318,7 @@ func (self *LightChain) postChainEvents(events []interface{}) {
337318
for _, event := range events {
338319
switch ev := event.(type) {
339320
case core.ChainEvent:
340-
if self.LastBlockHash() == ev.Hash {
321+
if self.CurrentHeader().Hash() == ev.Hash {
341322
self.chainHeadFeed.Send(core.ChainHeadEvent{Block: ev.Block})
342323
}
343324
self.chainFeed.Send(ev)

‎tests/block_test_util.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (t *BlockTest) Run() error {
120120
if err != nil {
121121
return err
122122
}
123-
cmlast := chain.LastBlockHash()
123+
cmlast := chain.CurrentBlock().Hash()
124124
if common.Hash(t.json.BestBlock) != cmlast {
125125
return fmt.Errorf("last block hash validation mismatch: want: %x, have: %x", t.json.BestBlock, cmlast)
126126
}

0 commit comments

Comments
 (0)