/// <exception cref="BlockStoreException"/> public void Put(StoredBlock block) { lock (this) { var hash = block.BlockHeader.Hash; _blockMap[hash] = block; } }
public MemoryBlockStore(NetworkParameters @params) { _blockMap = new Dictionary<Sha256Hash, StoredBlock>(); // Insert the genesis block. var genesisHeader = @params.GenesisBlock.CloneAsHeader(); var storedGenesis = new StoredBlock(genesisHeader, genesisHeader.GetWork(), 0); Put(storedGenesis); SetChainHead(storedGenesis); }
/// <exception cref="BlockStoreException"/> public void Put(StoredBlock block) { lock (this) { try { var hash = block.BlockHeader.Hash; // Append to the end of the file. Record.Write(_channel, block); _blockCache[hash] = block; while (_blockCache.Count > 2050) { _blockCache.RemoveAt(0); } } catch (IOException e) { throw new BlockStoreException(e); } } }
/// <summary> /// Adds the given block to the internal serializable set of blocks in which this transaction appears. This is /// used by the wallet to ensure transactions that appear on side chains are recorded properly even though the /// block stores do not save the transaction data at all. /// </summary> public void AddBlockAppearance(StoredBlock block) { if (AppearsIn == null) { AppearsIn = new HashSet<StoredBlock>(); } AppearsIn.Add(block); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="BlockStoreException"/> private void Load(FileInfo file) { _log.InfoFormat("Reading block store from {0}", file); using (var input = file.OpenRead()) { // Read a version byte. var version = input.Read(); if (version == -1) { // No such file or the file was empty. throw new FileNotFoundException(file.Name + " does not exist or is empty"); } if (version != 1) { throw new BlockStoreException("Bad version number: " + version); } // Chain head pointer is the first thing in the file. var chainHeadHash = new byte[32]; if (StreamExtensions.Read(input, chainHeadHash) < chainHeadHash.Length) throw new BlockStoreException("Truncated block store: cannot read chain head hash"); _chainHead = new Sha256Hash(chainHeadHash); _log.InfoFormat("Read chain head from disk: {0}", _chainHead); var now = Environment.TickCount; // Rest of file is raw block headers. var headerBytes = new byte[Block.HeaderSize]; try { while (true) { // Read a block from disk. if (StreamExtensions.Read(input, headerBytes) < 80) { // End of file. break; } // Parse it. var b = new Block(_params, headerBytes); // Look up the previous block it connects to. var prev = Get(b.PreviousBlockHash); StoredBlock s; if (prev == null) { // First block in the stored chain has to be treated specially. if (b.Equals(_params.GenesisBlock)) { s = new StoredBlock(_params.GenesisBlock.CloneAsHeader(), _params.GenesisBlock.GetWork(), 0); } else { throw new BlockStoreException("Could not connect " + b.Hash + " to " + b.PreviousBlockHash); } } else { // Don't try to verify the genesis block to avoid upsetting the unit tests. b.VerifyHeader(); // Calculate its height and total chain work. s = prev.Build(b); } // Save in memory. _blockMap[b.Hash] = s; } } catch (ProtocolException e) { // Corrupted file. throw new BlockStoreException(e); } catch (VerificationException e) { // Should not be able to happen unless the file contains bad blocks. throw new BlockStoreException(e); } var elapsed = Environment.TickCount - now; _log.InfoFormat("Block chain read complete in {0}ms", elapsed); } }
/// <exception cref="BlockStoreException"/> private void CreateNewStore(NetworkParameters @params, FileInfo file) { // Create a new block store if the file wasn't found or anything went wrong whilst reading. _blockMap.Clear(); try { if (_stream != null) { _stream.Dispose(); } _stream = file.OpenWrite(); // Do not append, create fresh. _stream.Write(1); // Version. } catch (IOException e1) { // We could not load a block store nor could we create a new one! throw new BlockStoreException(e1); } try { // Set up the genesis block. When we start out fresh, it is by definition the top of the chain. var genesis = @params.GenesisBlock.CloneAsHeader(); var storedGenesis = new StoredBlock(genesis, genesis.GetWork(), 0); _chainHead = storedGenesis.BlockHeader.Hash; _stream.Write(_chainHead.Bytes); Put(storedGenesis); } catch (IOException e) { throw new BlockStoreException(e); } }
/// <exception cref="BitcoinSharp.Core.Exceptions.VerificationException"/> /// <exception cref="BitcoinSharp.Core.Exceptions.ScriptException"/> private void Receive(Transaction transaction, StoredBlock storedBlock, BlockChain.NewBlockType blockType, bool reorg) { lock (this) { // Runs in a peer thread. var previousBalance = GetBalance(); var transactionHash = transaction.Hash; var bestChain = blockType == BlockChain.NewBlockType.BestChain; var sideChain = blockType == BlockChain.NewBlockType.SideChain; var valueSentFromMe = transaction.GetValueSentFromMe(this); var valueSentToMe = transaction.GetValueSentToMe(this); var valueDifference = (long) (valueSentToMe - valueSentFromMe); if (!reorg) { Log.InfoFormat("Received tx{0} for {1} BTC: {2}", sideChain ? " on a side chain" : "", Utils.BitcoinValueToFriendlyString(valueDifference), transaction.HashAsString); } // If this transaction is already in the wallet we may need to move it into a different WalletPool. At the very // least we need to ensure we're manipulating the canonical object rather than a duplicate. Transaction walletTransaction; if (Pending.TryGetValue(transactionHash, out walletTransaction)) { Pending.Remove(transactionHash); Log.Info(" <-pending"); // A transaction we created appeared in a block. Probably this is a spend we broadcast that has been // accepted by the network. // // Mark the tx as appearing in this block so we can find it later after a re-org. walletTransaction.AddBlockAppearance(storedBlock); if (bestChain) { if (valueSentToMe.Equals(0)) { // There were no change transactions so this tx is fully spent. Log.Info(" ->spent"); Debug.Assert(!Spent.ContainsKey(walletTransaction.Hash), "TX in both pending and spent pools"); Spent[walletTransaction.Hash] = walletTransaction; } else { // There was change back to us, or this tx was purely a spend back to ourselves (perhaps for // anonymization purposes). Log.Info(" ->unspent"); Debug.Assert(!Unspent.ContainsKey(walletTransaction.Hash), "TX in both pending and unspent pools"); Unspent[walletTransaction.Hash] = walletTransaction; } } else if (sideChain) { // The transaction was accepted on an inactive side chain, but not yet by the best chain. Log.Info(" ->inactive"); // It's OK for this to already be in the inactive WalletPool because there can be multiple independent side // chains in which it appears: // // b1 --> b2 // \-> b3 // \-> b4 (at this point it's already present in 'inactive' if (_inactive.ContainsKey(walletTransaction.Hash)) Log.Info("Saw a transaction be incorporated into multiple independent side chains"); _inactive[walletTransaction.Hash] = walletTransaction; // Put it back into the pending WalletPool, because 'pending' means 'waiting to be included in best chain'. Pending[walletTransaction.Hash] = walletTransaction; } } else { if (!reorg) { // Mark the tx as appearing in this block so we can find it later after a re-org. transaction.AddBlockAppearance(storedBlock); } // This TX didn't originate with us. It could be sending us coins and also spending our own coins if keys // are being shared between different wallets. if (sideChain) { Log.Info(" ->inactive"); _inactive[transaction.Hash] = transaction; } else if (bestChain) { ProcessTransactionFromBestChain(transaction); } } Log.InfoFormat("Balance is now: {0}", Utils.BitcoinValueToFriendlyString(GetBalance())); // Inform anyone interested that we have new coins. Note: we may be re-entered by the event listener, // so we must not make assumptions about our state after this loop returns! For example, // the balance we just received might already be spent! if (!reorg && bestChain && valueDifference > 0 && CoinsReceived != null) { lock (CoinsReceived) { CoinsReceived(this, new WalletCoinsReceivedEventArgs(transaction, previousBalance, GetBalance())); } } } }
/// <exception cref="System.IO.IOException"/> public static void Write(Stream channel, StoredBlock block) { using (var buf = ByteBuffer.Allocate(Size)) { buf.PutInt((int) block.Height); var chainWorkBytes = block.ChainWork.ToByteArray(); Debug.Assert(chainWorkBytes.Length <= ChainWorkBytes, "Ran out of space to store chain work!"); if (chainWorkBytes.Length < ChainWorkBytes) { // Pad to the right size. buf.Put(EmptyBytes, 0, ChainWorkBytes - chainWorkBytes.Length); } buf.Put(chainWorkBytes); buf.Put(block.BlockHeader.BitcoinSerialize()); buf.Position = 0; channel.Position = channel.Length; channel.Write(buf.ToArray()); channel.Position = channel.Length - Size; } }
/// <summary> /// Called as part of connecting a block when the new block results in a different chain having higher total work. /// </summary> /// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> /// <exception cref="VerificationException" /> private void HandleNewBestChain(StoredBlock newChainHead) { // This chain has overtaken the one we currently believe is best. Reorganize is required. // // Firstly, calculate the block at which the chain diverged. We only need to examine the // chain from beyond this block to find differences. var splitPoint = FindSplit(newChainHead, _chainHead); Log.InfoFormat("Re-organize after split at height {0}", splitPoint.Height); Log.InfoFormat("Old chain head: {0}", _chainHead.BlockHeader.HashAsString); Log.InfoFormat("New chain head: {0}", newChainHead.BlockHeader.HashAsString); Log.InfoFormat("Split at block: {0}", splitPoint.BlockHeader.HashAsString); // Then build a list of all blocks in the old part of the chain and the new part. var oldBlocks = GetPartialChain(_chainHead, splitPoint); var newBlocks = GetPartialChain(newChainHead, splitPoint); // Now inform the wallet. This is necessary so the set of currently active transactions (that we can spend) // can be updated to take into account the re-organize. We might also have received new coins we didn't have // before and our previous spends might have been undone. foreach (var wallet in _wallets) { wallet.Reorganize(oldBlocks, newBlocks); } // Update the pointer to the best known block. ChainHead = newChainHead; }
/// <summary> /// Returns the set of contiguous blocks between 'higher' and 'lower'. Higher is included, lower is not. /// </summary> /// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> private IList<StoredBlock> GetPartialChain(StoredBlock higher, StoredBlock lower) { Debug.Assert(higher.Height > lower.Height); var results = new LinkedList<StoredBlock>(); var cursor = higher; while (true) { results.AddLast(cursor); cursor = cursor.GetPrev(_blockStore); Debug.Assert(cursor != null, "Ran off the end of the chain"); if (cursor.Equals(lower)) break; } return results.ToList(); }
/// <summary> /// 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). /// </summary> /// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> private StoredBlock FindSplit(StoredBlock newChainHead, StoredBlock chainHead) { var currentChainCursor = chainHead; var newChainCursor = newChainHead; // Loop until we find the block both chains have in common. Example: // // A -> B -> C -> D // \--> E -> F -> G // // findSplit will return block B. chainHead = D and newChainHead = G. while (!currentChainCursor.Equals(newChainCursor)) { if (currentChainCursor.Height > newChainCursor.Height) { currentChainCursor = currentChainCursor.GetPrev(_blockStore); Debug.Assert(newChainCursor != null, "Attempt to follow an orphan chain"); } else { newChainCursor = newChainCursor.GetPrev(_blockStore); Debug.Assert(currentChainCursor != null, "Attempt to follow an orphan chain"); } } return currentChainCursor; }
/// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> /// <exception cref="VerificationException" /> private void ConnectBlock(StoredBlock newStoredBlock, StoredBlock previousStoredBlock, IEnumerable<KeyValuePair<IDefaultWallet, List<Transaction>>> newTransactions) { if (previousStoredBlock.Equals(_chainHead)) { // This block connects to the best known block, it is a normal continuation of the system. ChainHead = newStoredBlock; Log.DebugFormat("Chain is now {0} blocks high", _chainHead.Height); if (newTransactions != null) SendTransactionsToWallet(newStoredBlock, NewBlockType.BestChain, newTransactions); } else { // This block connects to somewhere other than the top of the best known chain. We treat these differently. // // Note that we send the transactions to the wallet FIRST, even if we're about to re-organize this block // to become the new best chain head. This simplifies handling of the re-org in the Wallet class. var haveNewBestChain = newStoredBlock.MoreWorkThan(_chainHead); if (haveNewBestChain) { Log.Info("Block is causing a re-organize"); } else { var splitPoint = FindSplit(newStoredBlock, _chainHead); var splitPointHash = splitPoint != null ? splitPoint.BlockHeader.HashAsString : "?"; Log.InfoFormat("Block forks the chain at {0}, but it did not cause a reorganize:{1}{2}", splitPointHash, Environment.NewLine, newStoredBlock); } // We may not have any transactions if we received only a header. That never happens today but will in // future when GetHeaders is used as an optimization. if (newTransactions != null) { SendTransactionsToWallet(newStoredBlock, NewBlockType.SideChain, newTransactions); } if (haveNewBestChain) HandleNewBestChain(newStoredBlock); } }
/// <summary> /// Throws an exception if the blocks difficulty is not correct. /// </summary> /// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> /// <exception cref="VerificationException" /> private void CheckDifficultyTransitions(StoredBlock previousStoredBlock, StoredBlock nextStoredBlock) { var previousBlockHeader = previousStoredBlock.BlockHeader; Log.DebugFormat("Previous Block Header: {0}", previousBlockHeader); var nextBlockHeader = nextStoredBlock.BlockHeader; Log.DebugFormat("Next Block Header: {0}", nextBlockHeader); //Check if this is supposed to be a difficulty transition point. if ((previousStoredBlock.Height + 1) % _networkParameters.Interval != 0) { // No ... so check the difficulty didn't actually change. if (nextBlockHeader.TargetDifficulty != previousBlockHeader.TargetDifficulty) { throw new VerificationException("Unexpected change in difficulty at height " + previousStoredBlock.Height + ": " + nextBlockHeader.TargetDifficulty.ToString("x") + " vs " + previousBlockHeader.TargetDifficulty.ToString("x")); } //Since this is not a difficulty transition point. return. return; } // We need to find a block far back in the chain. It's OK that this is expensive because it only occurs every // two weeks after the initial block chain download. var now = Environment.TickCount; var cursor = _blockStore.Get(previousBlockHeader.Hash); for (var i = 0; i < _networkParameters.Interval - 1; i++) { if (cursor == null) { // This should never happen. If it does, it means we are following an incorrect or busted chain. throw new VerificationException( "Difficulty transition point but we did not find a way back to the genesis block."); } cursor = _blockStore.Get(cursor.BlockHeader.PreviousBlockHash); } Log.DebugFormat("Difficulty transition traversal took {0}ms", Environment.TickCount - now); var blockIntervalAgo = cursor.BlockHeader; var timespan = (int) (previousBlockHeader.TimeSeconds - blockIntervalAgo.TimeSeconds); // Limit the adjustment step. if (timespan < _networkParameters.TargetTimespan / 4) timespan = _networkParameters.TargetTimespan / 4; if (timespan > _networkParameters.TargetTimespan * 4) timespan = _networkParameters.TargetTimespan * 4; var newDifficulty = Utils.DecodeCompactBits(blockIntervalAgo.TargetDifficulty); newDifficulty = newDifficulty.Multiply(BigInteger.ValueOf(timespan)); newDifficulty = newDifficulty.Divide(BigInteger.ValueOf(_networkParameters.TargetTimespan)); if (newDifficulty.CompareTo(_networkParameters.ProofOfWorkLimit) > 0) { Log.DebugFormat("Difficulty hit proof of work limit: {0}", newDifficulty.ToString(16)); newDifficulty = _networkParameters.ProofOfWorkLimit; } var accuracyBytes = (int) (nextBlockHeader.TargetDifficulty >> 24) - 3; var receivedDifficulty = nextBlockHeader.GetDifficultyTargetAsInteger(); // The calculated difficulty is to a higher precision than received, so reduce here. var mask = BigInteger.ValueOf(0xFFFFFF).ShiftLeft(accuracyBytes * 8); newDifficulty = newDifficulty.And(mask); if (newDifficulty.CompareTo(receivedDifficulty) != 0) { throw new VerificationException("Network provided difficulty bits do not match what was calculated: " + "\r\n" + receivedDifficulty.ToString(16) + " = next block difficulty\r\n" + newDifficulty.ToString(16) + " = calculated difficulty"); } }
/// <exception cref="VerificationException" /> private static void SendTransactionsToWallet(StoredBlock block, NewBlockType blockType, IEnumerable<KeyValuePair<IDefaultWallet, List<Transaction>>> newTransactions) { foreach (var item in newTransactions) { try { foreach (var transaction in item.Value) { item.Key.Receive(transaction, block, blockType); } } catch (ScriptException e) { // We don't want scripts we don't understand to break the block chain so just note that this tx was // not scanned here and continue. Log.WarnFormat("Failed to parse a script: {0}", e); } } }
/// <summary> /// Constructs a BlockChain connected to the given list of wallets and a store. /// </summary> /// <exception cref="BitcoinSharp.Blockchain.Store.BlockStoreException" /> public BlockChain(NetworkParameters networkParameters, IEnumerable<IDefaultWallet> wallets, IBlockStore blockStore) { _blockStore = blockStore; _chainHead = blockStore.GetChainHead(); Log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.BlockHeader); _networkParameters = networkParameters; _wallets = new List<IDefaultWallet>(wallets); }
/// <exception cref="BlockStoreException"/> public void SetChainHead(StoredBlock chainHead) { lock (this) { try { _chainHead = chainHead.BlockHeader.Hash; // Write out new hash to the first 32 bytes of the file past one (first byte is version number). var originalPos = _channel.Position; _channel.Position = 1; var bytes = _chainHead.Bytes; _channel.Write(bytes, 0, bytes.Length); _channel.Position = originalPos; } catch (IOException e) { throw new BlockStoreException(e); } } }
/// <exception cref="BlockStoreException"/> private void CreateNewStore(NetworkParameters @params, FileInfo file) { // Create a new block store if the file wasn't found or anything went wrong whilst reading. _blockCache.Clear(); try { if (_channel != null) { _channel.Dispose(); _channel = null; } if (file.Exists) { try { file.Delete(); } catch (IOException) { throw new BlockStoreException("Could not delete old store in order to recreate it"); } } _channel = file.Create(); // Create fresh. _channel.Write(FileFormatVersion); } catch (IOException e1) { // We could not load a block store nor could we create a new one! throw new BlockStoreException(e1); } try { // Set up the genesis block. When we start out fresh, it is by definition the top of the chain. var genesis = @params.GenesisBlock.CloneAsHeader(); var storedGenesis = new StoredBlock(genesis, genesis.GetWork(), 0); _chainHead = storedGenesis.BlockHeader.Hash; _channel.Write(_chainHead.Bytes); Put(storedGenesis); } catch (IOException e) { throw new BlockStoreException(e); } }
/// <summary> /// Returns true if this objects chainWork is higher than the others. /// </summary> public bool MoreWorkThan(StoredBlock other) { return _chainWork.CompareTo(other._chainWork) > 0; }
/// <exception cref="BlockStoreException"/> public void Put(StoredBlock block) { lock (this) { try { var hash = block.BlockHeader.Hash; Debug.Assert(!_blockMap.ContainsKey(hash), "Attempt to insert duplicate"); // Append to the end of the file. The other fields in StoredBlock will be recalculated when it's reloaded. var bytes = block.BlockHeader.BitcoinSerialize(); _stream.Write(bytes); _stream.Flush(); _blockMap[hash] = block; } catch (IOException e) { throw new BlockStoreException(e); } } }
/// <exception cref="BlockStoreException"/> public void SetChainHead(StoredBlock chainHead) { lock (this) { try { _chainHead = chainHead.BlockHeader.Hash; // Write out new hash to the first 32 bytes of the file past one (first byte is version number). _stream.Seek(1, SeekOrigin.Begin); var bytes = _chainHead.Bytes; _stream.Write(bytes, 0, bytes.Length); } catch (IOException e) { throw new BlockStoreException(e); } } }
/// <exception cref="BlockStoreException"/> public void SetChainHead(StoredBlock chainHead) { _chainHead = chainHead; }
/// <summary> /// Called by the <see cref="BitcoinSharp.Core.BlockChain"/> when we receive a new block that sends coins to one of our addresses or /// spends coins from one of our addresses (note that a single transaction can do both). /// </summary> /// <remarks> /// This is necessary for the internal book-keeping Wallet does. When a transaction is received that sends us /// coins it is added to a WalletPool so we can use it later to create spends. When a transaction is received that /// consumes outputs they are marked as spent so they won't be used in future.<p/> /// A transaction that spends our own coins can be received either because a spend we created was accepted by the /// network and thus made it into a block, or because our keys are being shared between multiple instances and /// some other node spent the coins instead. We still have to know about that to avoid accidentally trying to /// double spend.<p/> /// A transaction may be received multiple times if is included into blocks in parallel chains. The blockType /// parameter describes whether the containing block is on the main/best chain or whether it's on a presently /// inactive side chain. We must still record these transactions and the blocks they appear in because a future /// block might change which chain is best causing a reorganize. A re-org can totally change our balance! /// </remarks> /// <exception cref="BitcoinSharp.Core.Exceptions.VerificationException"/> /// <exception cref="BitcoinSharp.Core.Exceptions.ScriptException"/> public void Receive(Transaction transaction, StoredBlock storedBlock, BlockChain.NewBlockType blockType) { lock (this) { Receive(transaction, storedBlock, blockType, false); } }