C# Class BitSharper.BlockChain

A BlockChain holds a series of Block objects, links them together, and knows how to verify that the chain follows the rules of the NetworkParameters for this chain.
A BlockChain requires a Wallet to receive transactions that it finds during the initial download. However, if you don't care about this, you can just pass in an empty wallet and nothing bad will happen.

A newly constructed BlockChain is empty. To fill it up, use a Peer object to download the chain from the network.

Notes

The 'chain' can actually be a tree although in normal operation it can be thought of as a simple list. In such a situation there are multiple stories of the economy competing to become the one true consensus. This can happen naturally when two miners solve a block within a few seconds of each other, or it can happen when the chain is under attack.

A reference to the head block of every chain is stored. If you can reach the genesis block by repeatedly walking through the prevBlock pointers, then we say this is a full chain. If you cannot reach the genesis block we say it is an orphan chain.

Orphan chains can occur when blocks are solved and received during the initial block chain download, or if we connect to a peer that doesn't send us blocks in order.

Show file Open project: TangibleCryptography/BitSharper Class Usage Examples

Public Methods

Method Description
Add ( Block block ) : bool

Processes a received block and tries to add it to the chain. If there's something wrong with the block an exception is thrown. If the block is OK but cannot be connected to the chain at this time, returns false. If the block can be connected to the chain, returns true.

AddWallet ( Wallet wallet ) : void

Add a wallet to the BlockChain. Note that the wallet will be unaffected by any blocks received while it was not part of this BlockChain. This method is useful if the wallet has just been created, and its keys have never been in use, or if the wallet has been loaded along with the BlockChain

BlockChain ( NetworkParameters @params, IBlockStore blockStore ) : System

Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending and receiving coins but rather, just want to explore the network data structures.

BlockChain ( NetworkParameters @params, IEnumerable wallets, IBlockStore blockStore ) : System

Constructs a BlockChain connected to the given list of wallets and a store.

BlockChain ( NetworkParameters @params, Wallet wallet, IBlockStore blockStore ) : System

Constructs a BlockChain connected to the given wallet and store. To obtain a Wallet you can construct one from scratch, or you can deserialize a saved wallet from disk using Wallet.LoadFromFile.

For the store you can use a MemoryBlockStore if you don't care about saving the downloaded data, or a BoundedOverheadBlockStore if you'd like to ensure fast start-up the next time you run the program.

Private Methods

Method Description
Add ( Block block, bool tryConnecting ) : bool
CheckDifficultyTransitions ( StoredBlock storedPrev, StoredBlock storedNext ) : void

Throws an exception if the blocks difficulty is not correct.

ConnectBlock ( StoredBlock newStoredBlock, StoredBlock storedPrev, IDictionary newTransactions ) : void
FindSplit ( StoredBlock newChainHead, StoredBlock chainHead ) : StoredBlock

Locates the point in the chain at which newStoredBlock and chainHead diverge. Returns null if no split point was found (ie they are part of the same chain).

GetPartialChain ( StoredBlock higher, StoredBlock lower ) : IList

Returns the set of contiguous blocks between 'higher' and 'lower'. Higher is included, lower is not.

HandleNewBestChain ( StoredBlock newChainHead ) : void

Called as part of connecting a block when the new block results in a different chain having higher total work.

ScanTransactions ( Block block, IDictionary walletToTxMap ) : void

For the transactions in the given block, update the txToWalletMap such that each wallet maps to a list of transactions for which it is relevant.

SendTransactionsToWallet ( StoredBlock block, NewBlockType blockType, IDictionary newTransactions ) : void
TryConnectingUnconnected ( ) : void

For each block in unconnectedBlocks, see if we can now fit it on top of the chain and if so, do so.

Method Details

Add() public method

Processes a received block and tries to add it to the chain. If there's something wrong with the block an exception is thrown. If the block is OK but cannot be connected to the chain at this time, returns false. If the block can be connected to the chain, returns true.
public Add ( Block block ) : bool
block Block
return bool

AddWallet() public method

Add a wallet to the BlockChain. Note that the wallet will be unaffected by any blocks received while it was not part of this BlockChain. This method is useful if the wallet has just been created, and its keys have never been in use, or if the wallet has been loaded along with the BlockChain
public AddWallet ( Wallet wallet ) : void
wallet Wallet
return void

BlockChain() public method

Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending and receiving coins but rather, just want to explore the network data structures.
public BlockChain ( NetworkParameters @params, IBlockStore blockStore ) : System
@params NetworkParameters
blockStore IBlockStore
return System

BlockChain() public method

Constructs a BlockChain connected to the given list of wallets and a store.
public BlockChain ( NetworkParameters @params, IEnumerable wallets, IBlockStore blockStore ) : System
@params NetworkParameters
wallets IEnumerable
blockStore IBlockStore
return System

BlockChain() public method

Constructs a BlockChain connected to the given wallet and store. To obtain a Wallet you can construct one from scratch, or you can deserialize a saved wallet from disk using Wallet.LoadFromFile.
For the store you can use a MemoryBlockStore if you don't care about saving the downloaded data, or a BoundedOverheadBlockStore if you'd like to ensure fast start-up the next time you run the program.
public BlockChain ( NetworkParameters @params, Wallet wallet, IBlockStore blockStore ) : System
@params NetworkParameters
wallet Wallet
blockStore IBlockStore
return System