Example #1
0
        public async Task <SyncBlockInfo> RewindToBestChain(SyncConnection connection)
        {
            IBlockchainClient client = clientFactory.Create(connection);

            while (true)
            {
                SyncBlockInfo block = storage.GetLatestBlock();

                if (block == null)
                {
                    return(null);
                }

                string currentHash = await client.GetblockHashAsync(block.BlockIndex);

                if (currentHash == block.BlockHash)
                {
                    return(block);
                }

                log.LogDebug($"Rewinding block {block.BlockIndex}({block.BlockHash})");

                await storage.DeleteBlockAsync(block.BlockHash);
            }
        }
        public async Task <StatsConnection> StatsConnection()
        {
            SyncConnection    connection = syncConnection;
            IBlockchainClient client     = clientFactory.Create(connection);

            int clientConnection = await client.GetConnectionCountAsync();

            return(new StatsConnection {
                Connections = clientConnection
            });
        }
        public async Task <string> SendTransaction(string transactionHex)
        {
            // todo: consider adding support for retries.
            // todo: check how a failure is porpageted

            SyncConnection connection = syncConnection;
            Transaction    trx        = null;

            // parse the trx;
            trx = connection.Network.Consensus.ConsensusFactory.CreateTransaction(transactionHex);
            trx.PrecomputeHash(false, true);

            IBlockchainClient client = clientFactory.Create(connection);
            string            trxid  = await client.SentRawTransactionAsync(transactionHex);

            if (trx.GetHash().ToString() != trxid)
            {
                throw new Exception($"node trxid = {trxid}, serialized trxid = {trx.GetHash().ToString()}");
            }

            storageOperations.InsertMempoolTransactions(new SyncBlockTransactionsOperation
            {
                Transactions = new List <Transaction> {
                    trx
                }
            });

            return(trxid);
        }
Example #4
0
        public override async Task OnExecute()
        {
            IBlockchainClient client = clientFactory.Create(connection);

            List <string> allIndexes = mongoData.GetBlockIndexIndexes();

            if (allIndexes.Count == BlockIndexer.ExpectedNumberOfIndexes)
            {
                Runner.GlobalState.IndexModeCompleted = true;
            }

            Runner.GlobalState.PullingTip = null;
            Runner.GlobalState.StoreTip   = null;

            Runner.GlobalState.StoreTip = await syncOperations.RewindToLastCompletedBlockAsync();

            if (Runner.GlobalState.StoreTip == null)
            {
                // No blocks in store start from zero
                // push the genesis block to store
                int    start       = 0;
                string genesisHash = await client.GetblockHashAsync(start);


                log.LogInformation($"Processing genesis hash = {genesisHash}");

                BlockInfo genesisBlock = await client.GetBlockAsync(genesisHash);

                SyncBlockTransactionsOperation block = syncOperations.FetchFullBlock(connection, genesisBlock);

                StorageBatch genesisBatch = new StorageBatch();
                storageOperations.AddToStorageBatch(genesisBatch, block);
                Runner.GlobalState.StoreTip = storageOperations.PushStorageBatch(genesisBatch);
            }

            BlockInfo fetchedBlock = await client.GetBlockAsync(Runner.GlobalState.StoreTip.BlockHash);

            if (fetchedBlock == null)
            {
                // check if the fullnode is ahead of the indexer height
                int fullnodeTipHeight = client.GetBlockCount();
                if (fullnodeTipHeight < Runner.GlobalState.StoreTip.BlockIndex)
                {
                    throw new ApplicationException($"Full node at height {fullnodeTipHeight} which is behind the Indexer at height {Runner.GlobalState.StoreTip.BlockIndex}");
                }

                // reorg happend while indexer was offline rewind the indexer database
                Runner.GlobalState.PullingTip = null;
                Runner.GlobalState.StoreTip   = null;

                Runner.GlobalState.StoreTip = await syncOperations.RewindToBestChain(connection);
            }

            // update the chains tip
            Runner.GlobalState.ChainTipHeight = syncOperations.GetBlockCount(client);
        }
 public NftComputationService(ILogger <NftComputationService> logger,
                              ICirrusMongoDb mongoDb,
                              ISmartContractHandlersFactory <NonFungibleTokenComputedTable> logReaderFactory,
                              ICryptoClientFactory clientFactory,
                              SyncConnection connection)
 {
     this.logger           = logger;
     this.mongoDb          = mongoDb;
     this.logReaderFactory = logReaderFactory;
     cirrusClient          = (CirrusClient)clientFactory.Create(connection);
 }
Example #6
0
 public ComputeSmartContractService(ILogger <ComputeSmartContractService <T> > logger,
                                    ICirrusMongoDb db,
                                    ISmartContractHandlersFactory <T> logReaderFactory,
                                    ICryptoClientFactory clientFactory,
                                    SyncConnection connection,
                                    IMongoDatabase mongoDatabase)
 {
     this.logger           = logger;
     mongoDb               = db;
     this.logReaderFactory = logReaderFactory;
     this.mongoDatabase    = mongoDatabase;
     cirrusClient          = (CirrusClient)clientFactory.Create(connection);
     emptyContract         = new T();
 }