Example #1
0
 public static ApiTip from_tip(ChainTip tip)
 {
     return(new ApiTip
     {
         Height = tip.Height,
         LastBlockPushed = HexUtil.to_hex(tip.LastBlockH.Value),
         PrevBlockToLast = HexUtil.to_hex(tip.PrevBlockH.Value),
         TotalDifficulty = tip.TotalDifficulty.into_num()
     });
 }
Example #2
0
        public BlockChain(string dbName, byte[] genesisBlockHash)
        {
            memPool                 = new MemPool();
            UTXOStore               = new UTXOStore();
            ActiveContractSet       = new ActiveContractSet();
            BlockStore              = new BlockStore();
            BlockNumberDifficulties = new BlockNumberDifficulties();
            ChainTip                = new ChainTip();
            Timestamps              = new BlockTimestamps();
            GenesisBlockHash        = genesisBlockHash;

            _DBContext = new DBContext(dbName);
            OwnResource(_DBContext);

            //var listener = new EventLoopMessageListener<QueueAction>(HandleQueueAction, "BlockChain listener");
            //OwnResource(MessageProducer<QueueAction>.Instance.AddMessageListener(listener));
            var buffer = new BufferBlock <QueueAction>();

            QueueAction.Target = buffer;

            using (var dbTx = _DBContext.GetTransactionContext())
            {
                var chainTip = ChainTip.Context(dbTx).Value;

                //TODO: check if makred as main?
                Tip = chainTip == null ? null : BlockStore.GetBlock(dbTx, chainTip);

                if (Tip != null)
                {
                    BlockChainTrace.Information("Tip's block number is " + Tip.Value.header.blockNumber);

                    //Try to validate orphans of tip, if any. this would be the case when a sync action was interrupted
                    BlockStore.Orphans(dbTx, Tip.Key).ToList().ForEach(t => new HandleBlockAction(t.Key, t.Value, true).Publish());
                }
                else
                {
                    BlockChainTrace.Information("No tip.");
                }

                InitBlockTimestamps(dbTx);
            }

            var consumer = ConsumeAsync(buffer);
        }