Ejemplo n.º 1
0
        public void TestInitialize()
        {
            Initialize(mockedServices: true);
            ApiKeyAuthentication = AppSettings.RestAdminAPIKey;

            rpcClientFactoryMock = server.Services.GetRequiredService <IRpcClientFactory>() as RpcClientFactoryMock;

            if (rpcClientFactoryMock != null)
            {
                rpcClientFactoryMock.AddKnownBlock(0, HelperTools.HexStringToByteArray(TestBase.genesisBlock));

                rpcClientFactoryMock.Reset(); // remove calls that are used to test node connection when adding a new node
            }
        }
Ejemplo n.º 2
0
        public override void Initialize(bool mockedServices = false, IEnumerable <KeyValuePair <string, string> > overridenSettings = null)
        {
            base.Initialize(mockedServices, overridenSettings);
            // setup repositories
            NodeRepository       = server.Services.GetRequiredService <INodeRepository>() as NodeRepositoryPostgres;
            TxRepositoryPostgres = server.Services.GetRequiredService <ITxRepository>() as TxRepositoryPostgres;
            FeeQuoteRepository   = server.Services.GetRequiredService <IFeeQuoteRepository>() as FeeQuoteRepositoryPostgres;

            MinerId = server.Services.GetRequiredService <IMinerId>();

            // setup common services

            Nodes          = server.Services.GetRequiredService <INodes>();
            BlockChainInfo = server.Services.GetRequiredService <IBlockChainInfo>();
            RpcMultiClient = server.Services.GetRequiredService <IRpcMultiClient>();

            EventBus = server.Services.GetRequiredService <IEventBus>();

            rpcClientFactoryMock   = server.Services.GetRequiredService <IRpcClientFactory>() as RpcClientFactoryMock;
            feeQuoteRepositoryMock = server.Services.GetRequiredService <IFeeQuoteRepository>() as FeeQuoteRepositoryMock;
            FeeQuoteRepositoryMock.quoteExpiryMinutes = quoteExpiryMinutes;

            if (rpcClientFactoryMock != null)
            {
                rpcClientFactoryMock.SetUpTransaction(
                    txC3Hex,
                    txC2Hex,
                    txZeroFeeHex,
                    txZeroFeeInput1Hex,
                    txZeroFeeInput2Hex,
                    tx2Hex,
                    tx2Input1Hex,
                    tx2Input2Hex);

                rpcClientFactoryMock.AddKnownBlock(0, HelperTools.HexStringToByteArray(genesisBlock));

                rpcClientFactoryMock.Reset(); // remove calls that are used to test node connection when adding a new node
            }
        }
Ejemplo n.º 3
0
        public async Task <(long, string)> CreateAndPublishNewBlockWithTxs(IRpcClient rpcClient, long?blockHeightToStartFork, Transaction[] transactions, bool noPublish = false, bool bigBlock = false)
        {
            string blockHash   = null;
            long   blockHeight = await rpcClient.GetBlockCountAsync();

            if (blockHeight == 0)
            {
                var blockStream = await rpcClient.GetBlockAsStreamAsync(await rpcClient.GetBestBlockHashAsync());

                var firstBlock = HelperTools.ParseByteStreamToBlock(blockStream);
                rpcClientFactoryMock.AddKnownBlock(blockHeight, firstBlock.ToBytes());
                PublishBlockHashToEventBus(await rpcClient.GetBestBlockHashAsync());
            }
            PubKey pubKey = new Key().PubKey;

            if (transactions != null)
            {
                NBitcoin.Block lastBlock;
                if (blockHeightToStartFork.HasValue)
                {
                    lastBlock = NBitcoin.Block.Load(await rpcClient.GetBlockByHeightAsBytesAsync(blockHeightToStartFork.Value), Network.Main);
                }
                else
                {
                    lastBlock = HelperTools.ParseByteStreamToBlock(await rpcClient.GetBlockAsStreamAsync(await rpcClient.GetBestBlockHashAsync()));
                }
                var block = lastBlock.CreateNextBlockWithCoinbase(pubKey, new Money(50, MoneyUnit.MilliBTC), new ConsensusFactory());
                foreach (var transaction in transactions)
                {
                    block.AddTransaction(transaction);
                }

                if (!bigBlock)
                {
                    block.Check();
                }

                if (blockHeightToStartFork.HasValue)
                {
                    blockHeight = blockHeightToStartFork.Value;
                }

                if (bigBlock)
                {
                    rpcClientFactoryMock.AddBigKnownBlock(++blockHeight, block);
                }
                else
                {
                    rpcClientFactoryMock.AddKnownBlock(++blockHeight, block.ToBytes());
                }


                blockHash = block.GetHash().ToString();
                if (!noPublish)
                {
                    PublishBlockHashToEventBus(block.GetHash().ToString());
                }
            }

            return(blockHeight, blockHash);
        }
 public void Initialize()
 {
     rpcClientFactoryMock = new RpcClientFactoryMock();
     rpcClientFactoryMock.AddKnownBlock(0, HelperTools.HexStringToByteArray(TestBase.genesisBlock));
 }
Ejemplo n.º 5
0
        public void Initialize(bool mockedServices = false)
        {
            SyncTest.WaitOne(); // tests must not run in parallel since each test first deletes database
            try
            {
                // setup call back server
                serverCallback = CallbackServer.GetTestServer(Callback.Url, Callback);
                clientCallback = serverCallback.CreateClient();

                //setup server
                server = TestServerBase.CreateServer(mockedServices, serverCallback, dbConnectionString);
                client = server.CreateClient();

                // setup repositories
                NodeRepository       = server.Services.GetRequiredService <INodeRepository>() as NodeRepositoryPostgres;
                TxRepositoryPostgres = server.Services.GetRequiredService <ITxRepository>() as TxRepositoryPostgres;
                FeeQuoteRepository   = server.Services.GetRequiredService <IFeeQuoteRepository>() as FeeQuoteRepositoryPostgres;

                BlockChainInfo = server.Services.GetRequiredService <IBlockChainInfo>();
                MinerId        = server.Services.GetRequiredService <IMinerId>();

                // setup common services
                loggerFactory = server.Services.GetRequiredService <ILoggerFactory>();
                loggerTest    = loggerFactory.CreateLogger(LOG_CATEGORY);

                Nodes          = server.Services.GetRequiredService <INodes>();
                blockChainInfo = server.Services.GetRequiredService <IBlockChainInfo>();
                rpcMultiClient = server.Services.GetRequiredService <IRpcMultiClient>();

                eventBus = server.Services.GetRequiredService <IEventBus>();

                rpcClientFactoryMock   = server.Services.GetRequiredService <IRpcClientFactory>() as RpcClientFactoryMock;
                feeQuoteRepositoryMock = server.Services.GetRequiredService <IFeeQuoteRepository>() as FeeQuoteRepositoryMock;
                FeeQuoteRepositoryMock.quoteExpiryMinutes = quoteExpiryMinutes;

                if (rpcClientFactoryMock != null)
                {
                    rpcClientFactoryMock.SetUpTransaction(
                        txC3Hex,
                        txC2Hex,
                        txZeroFeeHex,
                        txZeroFeeInput1Hex,
                        txZeroFeeInput2Hex,
                        tx2Hex,
                        tx2Input1Hex,
                        tx2Input2Hex);

                    rpcClientFactoryMock.AddKnownBlock(0, HelperTools.HexStringToByteArray(genesisBlock));

                    rpcClientFactoryMock.Reset(); // remove calls that are used to test node connection when adding a new node
                }
                loggerTest.LogInformation($"Path: {Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)}");
                loggerTest.LogInformation($"ConnString: {dbConnectionString}");
            }
            catch
            {
                SyncTest.Reset();
                // If there was error during initialization, let the other tests run (although they will probably also fail)
                throw;
            }
        }