Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
WIP: buildable version - fix interface and signature mismatches
Browse files Browse the repository at this point in the history
NOTE: bc.HasHeader, bc.GetHeader, ... etc funcs SHOULD use hash, uint64 signature
b/c more safer. HOWEVER, I just made the incoming implementation use hash
because it was easier and fast and a POC, so far.
  • Loading branch information
whilei committed May 28, 2018
1 parent b39e149 commit 231de35
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
8 changes: 8 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ type BlockChain struct {
indexesDb ethdb.Database
}

func (bc *BlockChain) GetHeaderByHash(h common.Hash) *types.Header {
return bc.hc.GetHeader(h)
}

func (bc *BlockChain) GetBlockByHash(h common.Hash) *types.Block {
return bc.GetBlock(h)
}

// NewBlockChain returns a fully initialised block chain using information
// available in the database. It initialises the default Ethereum Validator and
// Processor.
Expand Down
16 changes: 8 additions & 8 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type Downloader struct {
// LightChain encapsulates functions required to synchronise a light chain.
type LightChain interface {
// HasHeader verifies a header's presence in the local chain.
HasHeader(common.Hash, uint64) bool
HasHeader(common.Hash) bool

// GetHeaderByHash retrieves a header from the local chain.
GetHeaderByHash(common.Hash) *types.Header
Expand All @@ -184,7 +184,7 @@ type LightChain interface {
CurrentHeader() *types.Header

// GetTd returns the total difficulty of a local block.
GetTd(common.Hash, uint64) *big.Int
GetTd(common.Hash) *big.Int

// InsertHeaderChain inserts a batch of headers into the local chain.
InsertHeaderChain([]*types.Header, int) (int, error)
Expand All @@ -198,7 +198,7 @@ type BlockChain interface {
LightChain

// HasBlockAndState verifies block and associated states' presence in the local chain.
HasBlockAndState(common.Hash, uint64) bool
HasBlockAndState(common.Hash) bool

// GetBlockByHash retrieves a block from the local chain.
GetBlockByHash(common.Hash) *types.Block
Expand Down Expand Up @@ -726,7 +726,7 @@ func (d *Downloader) findAncestor(p *peer, height uint64) (uint64, error) {
continue
}
// Otherwise check if we already know the header or not
if (d.mode == FullSync && d.blockchain.HasBlockAndState(headers[i].Hash(), headers[i].Number.Uint64())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash(), headers[i].Number.Uint64())) {
if (d.mode == FullSync && d.blockchain.HasBlockAndState(headers[i].Hash())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash())) {
number, hash = headers[i].Number.Uint64(), headers[i].Hash()

// If every header is known, even future ones, the peer straight out lied about its head
Expand Down Expand Up @@ -791,7 +791,7 @@ func (d *Downloader) findAncestor(p *peer, height uint64) (uint64, error) {
arrived = true

// Modify the search interval based on the response
if (d.mode == FullSync && !d.blockchain.HasBlockAndState(headers[0].Hash(), headers[0].Number.Uint64())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash(), headers[0].Number.Uint64())) {
if (d.mode == FullSync && !d.blockchain.HasBlockAndState(headers[0].Hash())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash())) {
end = check
break
}
Expand Down Expand Up @@ -1273,7 +1273,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er
// R: Nothing to give
if d.mode != LightSync {
head := d.blockchain.CurrentBlock()
if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash())) > 0 {
return errStallingPeer
}
}
Expand All @@ -1286,7 +1286,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er
// peer gave us something useful, we're already happy/progressed (above check).
if d.mode == FastSync || d.mode == LightSync {
head := d.lightchain.CurrentHeader()
if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
if td.Cmp(d.lightchain.GetTd(head.Hash())) > 0 {
return errStallingPeer
}
}
Expand Down Expand Up @@ -1316,7 +1316,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er
// Collect the yet unknown headers to mark them as uncertain
unknown := make([]*types.Header, 0, len(headers))
for _, header := range chunk {
if !d.lightchain.HasHeader(header.Hash(), header.Number.Uint64()) {
if !d.lightchain.HasHeader(header.Hash()) {
unknown = append(unknown, header)
}
}
Expand Down
15 changes: 11 additions & 4 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,17 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
return nil, errIncompatibleConfig
}
// Construct the different synchronisation mechanisms
manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeader,
blockchain.GetBlock, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead,
blockchain.GetTd, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
manager.removePeer)
//manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeader,
// blockchain.GetBlock, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead,
// blockchain.GetTd, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
// manager.removePeer)

// func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn) *Downloader
m := downloader.FullSync
if manager.fastSync == 1 {
m = downloader.FastSync
}
manager.downloader = downloader.New(m, chaindb, manager.eventMux, blockchain, nil, manager.removePeer)

validator := func(block *types.Block, parent *types.Block) error {
return core.ValidateHeader(config, pow, block.Header(), parent.Header(), true, false)
Expand Down
3 changes: 2 additions & 1 deletion eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
if atomic.LoadUint32(&pm.fastSync) == 1 {
mode = downloader.FastSync
}
if !pm.downloader.Synchronise(peer.id, pHead, pTd, mode) {
if err := pm.downloader.Synchronise(peer.id, pHead, pTd, mode); err != nil {
glog.V(logger.Info).Infoln("downloader failed to syncronise", "mode=", mode, "peer=", peer.String())
return
}
atomic.StoreUint32(&pm.synced, 1) // Mark initial sync done
Expand Down

0 comments on commit 231de35

Please sign in to comment.