예제 #1
0
        /// <summary>
        /// Try connecting to a peer. If we exceed the number of connections, delay and try
        /// again.
        /// </summary>
        /// <exception cref="ThreadInterruptedException"/>
        private void AllocateNextPeer()
        {
            PeerAddress address;

            if (!_inactives.TryDequeue(out address))
            {
                return;
            }

            try
            {
                var peer            = new Peer(networkParams, address, _blockStore.GetChainHead().Height, _chain);
                var workerAllocated = peerConnectionPool.TryAllocateWorker(token => ConnectAndRun(peer, token));

                if (!workerAllocated)
                {
                    _inactives.Enqueue(address);
                    return;
                }
            }
            catch (BlockStoreException e)
            {
                // Fatal error
                Log.Error("Block store corrupt?", e);
                _running = false;
                throw new Exception(e.Message, e);
            }
        }
예제 #2
0
        /// <exception cref="BlockStoreException"/>
        public static Block MakeSolvedTestBlock(NetworkParameters networkParams, IBlockStore blockStore)
        {
            var b = blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(networkParams));

            b.Solve();
            return(b);
        }
예제 #3
0
 /// <summary>
 /// Constructs a BlockChain connected to the given wallet and store. To obtain a <see cref="Wallet">Wallet</see> you can construct
 /// one from scratch, or you can deserialize a saved wallet from disk using <see cref="Wallet.LoadFromFile(System.IO.FileInfo)">Wallet.LoadFromFile(System.IO.FileInfo)</see><p />
 /// </summary>
 /// <remarks>
 /// For the store you can use a <see cref="MemoryBlockStore">MemoryBlockStore</see> if you don't care about saving the downloaded data, or a
 /// <see cref="BoundedOverheadBlockStore">BoundedOverheadBlockStore</see> if you'd like to ensure fast start-up the next time you run the program.
 /// </remarks>
 public BlockChain(NetworkParameters @params, Wallet wallet, IBlockStore blockStore)
 {
     _blockStore = blockStore;
     _chainHead  = blockStore.GetChainHead();
     _log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.Header);
     _params = @params;
     _wallet = wallet;
 }
예제 #4
0
 /// <summary>
 /// Constructs a BlockChain connected to the given list of wallets and a store.
 /// </summary>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters @params, IEnumerable <Wallet> wallets, IBlockStore blockStore)
 {
     _blockStore = blockStore;
     _chainHead  = blockStore.GetChainHead();
     _log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.Header);
     _params  = @params;
     _wallets = new List <Wallet>(wallets);
 }
예제 #5
0
        // Emulates receiving a valid block that builds on top of the chain.
        private BlockPair CreateFakeBlock(params Transaction[] transactions)
        {
            var b = _blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(_params));

            foreach (var tx in transactions)
            {
                b.AddTransaction(tx);
            }
            b.Solve();
            var pair = new BlockPair();

            pair.Block       = b;
            pair.StoredBlock = _blockStore.GetChainHead().Build(b);
            _blockStore.Put(pair.StoredBlock);
            _blockStore.SetChainHead(pair.StoredBlock);
            return(pair);
        }
예제 #6
0
 // Emulates receiving a valid block that builds on top of the chain.
 public static BlockPair CreateFakeBlock(NetworkParameters @params, IBlockStore blockStore, params Transaction[] transactions)
 {
     var b = MakeTestBlock(@params, blockStore);
     // Coinbase tx was already added.
     foreach (var tx in transactions)
         b.AddTransaction(tx);
     b.Solve();
     var pair = new BlockPair();
     pair.Block = b;
     pair.StoredBlock = blockStore.GetChainHead().Build(b);
     blockStore.Put(pair.StoredBlock);
     blockStore.SetChainHead(pair.StoredBlock);
     return pair;
 }
예제 #7
0
        // Emulates receiving a valid block that builds on top of the chain.
        public static BlockPair CreateFakeBlock(NetworkParameters networkParams, IBlockStore blockStore, params Transaction[] transactions)
        {
            var b = MakeTestBlock(networkParams, blockStore);

            // Coinbase tx was already added.
            foreach (var tx in transactions)
            {
                b.AddTransaction(tx);
            }
            b.Solve();
            var pair = new BlockPair();

            pair.Block       = b;
            pair.StoredBlock = blockStore.GetChainHead().Build(b);
            blockStore.Put(pair.StoredBlock);
            blockStore.SetChainHead(pair.StoredBlock);
            return(pair);
        }
예제 #8
0
 /// <summary>
 /// Constructs a BlockChain connected to the given list of wallets and a store.
 /// </summary>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters @params, IEnumerable<Wallet> wallets, IBlockStore blockStore)
 {
     _blockStore = blockStore;
     _chainHead = blockStore.GetChainHead();
     _log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.Header);
     _params = @params;
     _wallets = new List<Wallet>(wallets);
 }
예제 #9
0
 /// <exception cref="BlockStoreException"/>
 public static Block MakeTestBlock(NetworkParameters @params, IBlockStore blockStore)
 {
     return blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(@params));
 }
예제 #10
0
 /// <exception cref="BlockStoreException"/>
 public static Block MakeSolvedTestBlock(NetworkParameters @params, IBlockStore blockStore)
 {
     var b = blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(@params));
     b.Solve();
     return b;
 }
예제 #11
0
 /// <summary>
 /// Constructs a BlockChain connected to the given wallet and store. To obtain a <see cref="Wallet">Wallet</see> you can construct
 /// one from scratch, or you can deserialize a saved wallet from disk using <see cref="Wallet.LoadFromFile(System.IO.FileInfo)">Wallet.LoadFromFile(System.IO.FileInfo)</see><p />
 /// </summary>
 /// <remarks>
 /// For the store you can use a <see cref="MemoryBlockStore">MemoryBlockStore</see> if you don't care about saving the downloaded data, or a
 /// <see cref="BoundedOverheadBlockStore">BoundedOverheadBlockStore</see> if you'd like to ensure fast start-up the next time you run the program.
 /// </remarks>
 public BlockChain(NetworkParameters @params, Wallet wallet, IBlockStore blockStore)
 {
     _blockStore = blockStore;
     _chainHead = blockStore.GetChainHead();
     _log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.Header);
     _params = @params;
     _wallet = wallet;
 }
예제 #12
0
 /// <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);
 }
예제 #13
0
 /// <exception cref="BlockStoreException"/>
 public static Block MakeTestBlock(NetworkParameters networkParams, IBlockStore blockStore)
 {
     return(blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(networkParams)));
 }