private static async Task <BlockTemplate> MineBlockAsync(TestChainContext testChainContext, Script scriptPubKey, TxMempool mempool, MempoolSchedulerLock mempoolLock, bool getMutatedBlock = false) { BlockTemplate newBlock = CreateBlockTemplate(testChainContext, scriptPubKey, mempool, mempoolLock); if (getMutatedBlock) { BuildMutatedBlock(newBlock); } newBlock.Block.UpdateMerkleRoot(); TryFindNonceForProofOfWork(testChainContext, newBlock); if (!getMutatedBlock) { await ValidateBlock(testChainContext, newBlock); } else { CheckBlockIsMutated(newBlock); } return(newBlock); }
private static async Task <Block> MineAMutatedBlockAsync(TestChainContext context) { List <Block> blocks = await TestChainFactory.MineBlocksWithLastBlockMutatedAsync(context, 1, MinerScriptPubKey); Block block = blocks.Last(); return(block); }
private async Task <(TestChainContext context, IPEndPoint peerEndPoint)> InitialiseContextAndPeerEndpointAsync(Mock <IPeerAddressManager> mockPeerAddressManager = null) { string dataDir = GetTestDirectoryPath(this); TestChainContext context = await TestChainFactory.CreateAsync(KnownNetworks.RegTest, dataDir, mockPeerAddressManager); var peerEndPoint = new IPEndPoint(IPAddress.Parse("1.2.3.4"), context.Network.DefaultPort); context.PeerAddressManager.AddPeer(peerEndPoint, peerEndPoint.Address.MapToIPv6()); return(context, peerEndPoint); }
private static void MockPeerConnection(TestChainContext context, bool whiteListedPeer) { var connectionManagerBehavior = new ConnectionManagerBehavior(context.ConnectionManager, context.LoggerFactory) { Whitelisted = whiteListedPeer }; var peer = new Mock <INetworkPeer>(); peer.Setup(p => p.Behavior <IConnectionManagerBehavior>()).Returns(connectionManagerBehavior); context.MockReadOnlyNodesCollection.Setup(s => s.FindByEndpoint(It.IsAny <IPEndPoint>())).Returns(peer.Object); }
private static void TryFindNonceForProofOfWork(TestChainContext testChainContext, BlockTemplate newBlock) { int maxTries = int.MaxValue; while (maxTries > 0 && !newBlock.Block.CheckProofOfWork()) { ++newBlock.Block.Header.Nonce; --maxTries; } if (maxTries == 0) { throw new XunitException("Test failed no blocks found"); } }
private static BlockTemplate CreateBlockTemplate(TestChainContext testChainContext, Script scriptPubKey, TxMempool mempool, MempoolSchedulerLock mempoolLock) { PowBlockDefinition blockAssembler = new PowBlockDefinition(testChainContext.Consensus, testChainContext.DateTimeProvider, testChainContext.LoggerFactory as LoggerFactory, mempool, mempoolLock, new MinerSettings(testChainContext.NodeSettings), testChainContext.Network, testChainContext.ConsensusRules, new NodeDeployments(testChainContext.Network, testChainContext.ChainIndexer)); BlockTemplate newBlock = blockAssembler.Build(testChainContext.ChainIndexer.Tip, scriptPubKey); int nHeight = testChainContext.ChainIndexer.Tip.Height + 1; // Height first in coinbase required for block.version=2 Transaction txCoinbase = newBlock.Block.Transactions[0]; txCoinbase.Inputs[0] = TxIn.CreateCoinbase(nHeight); return(newBlock); }
/// <summary> /// Mine new blocks in to the consensus database and the chain. /// </summary> private static async Task <List <Block> > MineBlocksAsync(TestChainContext testChainContext, int count, Script receiver, bool mutateLastBlock) { var blockPolicyEstimator = new BlockPolicyEstimator(new MempoolSettings(testChainContext.NodeSettings), testChainContext.LoggerFactory, testChainContext.NodeSettings); var mempool = new TxMempool(testChainContext.DateTimeProvider, blockPolicyEstimator, testChainContext.LoggerFactory, testChainContext.NodeSettings); var mempoolLock = new MempoolSchedulerLock(); // Simple block creation, nothing special yet: var blocks = new List <Block>(); for (int i = 0; i < count; i++) { BlockTemplate newBlock = await MineBlockAsync(testChainContext, receiver, mempool, mempoolLock, mutateLastBlock&& i == count - 1); blocks.Add(newBlock.Block); } return(blocks); }
private static async Task <Block> Mine2BlocksAndCreateABlockWithBadPrevHashAsync(TestChainContext context) { List <Block> blocks = await TestChainFactory.MineBlocksAsync(context, 2, MinerScriptPubKey); Block block = blocks.First(); block.Header.HashPrevBlock = context.ChainIndexer.Tip.HashBlock; return(block); }
private static async Task ValidateBlock(TestChainContext testChainContext, BlockTemplate newBlock) { var res = await testChainContext.Consensus.BlockMinedAsync(newBlock.Block); Assert.NotNull(res); }
public static async Task <List <Block> > MineBlocksAsync(TestChainContext testChainContext, int count, Script receiver) { return(await MineBlocksAsync(testChainContext, count, receiver, false)); }
public static async Task <List <Block> > MineBlocksWithLastBlockMutatedAsync(TestChainContext testChainContext, int count, Script receiver) { return(await MineBlocksAsync(testChainContext, count, receiver, true)); }
/// <summary> /// Creates test chain with a consensus loop. /// </summary> public static async Task <TestChainContext> CreateAsync(Network network, string dataDir, Mock <IPeerAddressManager> mockPeerAddressManager = null) { var testChainContext = new TestChainContext() { Network = network }; testChainContext.NodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); testChainContext.ConnectionSettings = new ConnectionManagerSettings(testChainContext.NodeSettings); testChainContext.LoggerFactory = testChainContext.NodeSettings.LoggerFactory; testChainContext.DateTimeProvider = DateTimeProvider.Default; testChainContext.Signals = new Signals.Signals(testChainContext.NodeSettings.LoggerFactory, null); testChainContext.AsyncProvider = new AsyncProvider(testChainContext.NodeSettings.LoggerFactory, testChainContext.Signals, new Mock <INodeLifetime>().Object); network.Consensus.Options = new ConsensusOptions(); //new FullNodeBuilderConsensusExtension.PowConsensusRulesRegistration().RegisterRules(network.Consensus); var consensusSettings = new ConsensusSettings(testChainContext.NodeSettings); testChainContext.Checkpoints = new Checkpoints(); testChainContext.ChainIndexer = new ChainIndexer(network); testChainContext.ChainState = new ChainState(); testChainContext.InitialBlockDownloadState = new InitialBlockDownloadState(testChainContext.ChainState, testChainContext.Network, consensusSettings, new Checkpoints(), testChainContext.NodeSettings.LoggerFactory, testChainContext.DateTimeProvider); var inMemoryCoinView = new InMemoryCoinView(new HashHeightPair(testChainContext.ChainIndexer.Tip)); var cachedCoinView = new CachedCoinView(network, new Checkpoints(), inMemoryCoinView, DateTimeProvider.Default, testChainContext.LoggerFactory, new NodeStats(testChainContext.DateTimeProvider, testChainContext.LoggerFactory), new ConsensusSettings(testChainContext.NodeSettings)); var dataFolder = new DataFolder(TestBase.AssureEmptyDir(dataDir)); testChainContext.PeerAddressManager = mockPeerAddressManager == null ? new PeerAddressManager(DateTimeProvider.Default, dataFolder, testChainContext.LoggerFactory, new SelfEndpointTracker(testChainContext.LoggerFactory, testChainContext.ConnectionSettings)) : mockPeerAddressManager.Object; testChainContext.MockConnectionManager = new Mock <IConnectionManager>(); testChainContext.MockReadOnlyNodesCollection = new Mock <IReadOnlyNetworkPeerCollection>(); testChainContext.MockConnectionManager.Setup(s => s.ConnectedPeers).Returns(testChainContext.MockReadOnlyNodesCollection.Object); testChainContext.MockConnectionManager.Setup(s => s.NodeSettings).Returns(testChainContext.NodeSettings); testChainContext.MockConnectionManager.Setup(s => s.ConnectionSettings).Returns(testChainContext.ConnectionSettings); testChainContext.ConnectionManager = testChainContext.MockConnectionManager.Object; var dateTimeProvider = new DateTimeProvider(); testChainContext.PeerBanning = new PeerBanning(testChainContext.ConnectionManager, testChainContext.LoggerFactory, testChainContext.DateTimeProvider, testChainContext.PeerAddressManager); var deployments = new NodeDeployments(testChainContext.Network, testChainContext.ChainIndexer); testChainContext.ConsensusRules = new PowConsensusRuleEngine(testChainContext.Network, testChainContext.LoggerFactory, testChainContext.DateTimeProvider, testChainContext.ChainIndexer, deployments, consensusSettings, testChainContext.Checkpoints, cachedCoinView, testChainContext.ChainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, testChainContext.LoggerFactory), testChainContext.AsyncProvider, new ConsensusRulesContainer()).SetupRulesEngineParent(); testChainContext.HeaderValidator = new HeaderValidator(testChainContext.ConsensusRules, testChainContext.LoggerFactory); testChainContext.IntegrityValidator = new IntegrityValidator(testChainContext.ConsensusRules, testChainContext.LoggerFactory); testChainContext.PartialValidator = new PartialValidator(testChainContext.AsyncProvider, testChainContext.ConsensusRules, testChainContext.LoggerFactory); testChainContext.FullValidator = new FullValidator(testChainContext.ConsensusRules, testChainContext.LoggerFactory); var dBreezeSerializer = new DataStoreSerializer(network.Consensus.ConsensusFactory); var blockRepository = new BlockRepository(testChainContext.Network, dataFolder, testChainContext.LoggerFactory, dBreezeSerializer); var blockStoreFlushCondition = new BlockStoreQueueFlushCondition(testChainContext.ChainState, testChainContext.InitialBlockDownloadState); var blockStore = new BlockStoreQueue(testChainContext.ChainIndexer, testChainContext.ChainState, blockStoreFlushCondition, new Mock <StoreSettings>().Object, blockRepository, testChainContext.LoggerFactory, new Mock <INodeStats>().Object, testChainContext.AsyncProvider); blockStore.Initialize(); testChainContext.Consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir); await testChainContext.Consensus.InitializeAsync(testChainContext.ChainIndexer.Tip); return(testChainContext); }