This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor PeerGroup and cover it with unit tests (#76)
- change block height type from BInt ot Int - add ability to request reversed block headers in LESPeer - create BlockHelper for requesting last block header from storage or checkpoint block - create BlockValidator for validating received chain of block headers - extract peer group state to separate class - extract peer creating logic into PeerProvider class - in PeerGroup handle forks: request reversed headers and look for fork point
- Loading branch information
Showing
20 changed files
with
783 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class BlockHelper { | ||
private let storage: ISpvStorage | ||
private let network: INetwork | ||
|
||
init(storage: ISpvStorage, network: INetwork) { | ||
self.storage = storage | ||
self.network = network | ||
} | ||
|
||
} | ||
|
||
extension BlockHelper: IBlockHelper { | ||
|
||
var lastBlockHeader: BlockHeader { | ||
return storage.lastBlockHeader ?? network.checkpointBlock | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class BlockValidator { | ||
|
||
func validate(blockHeaders: [BlockHeader], from blockHeader: BlockHeader) throws { | ||
guard let firstBlockHeader = blockHeaders.first else { | ||
throw ValidationError.invalidChain | ||
} | ||
|
||
guard firstBlockHeader.hashHex == blockHeader.hashHex else { | ||
throw ValidationError.forkDetected | ||
} | ||
|
||
var previousHeader = blockHeader | ||
|
||
for blockHeader in blockHeaders.dropFirst() { | ||
guard blockHeader.parentHash == previousHeader.hashHex else { | ||
throw ValidationError.invalidChain | ||
} | ||
previousHeader = blockHeader | ||
} | ||
} | ||
|
||
} | ||
|
||
extension BlockValidator { | ||
|
||
enum ValidationError: Error { | ||
case forkDetected | ||
case invalidChain | ||
case invalidProofOfWork | ||
} | ||
|
||
} |
Oops, something went wrong.