示例#1
0
        // TODO replace with two functions:
        // IsContractActive(contractHash), which checks if the contract is in the ACS on disk or in the contractpool;
        // bool IsContractGeneratedTransactionValid(dbtx, contracthash, ptx), which raises an exception if called with a missing contract
        //public static bool IsContractGeneratedTransactionValid(TransactionContext dbTx, TransactionValidation.PointedTransaction ptx, byte[] contractHash)
        //{
        //	var chainTip = new ChainTip().Context(dbTx).Value;
        //	var tipBlockHeader = chainTip == null ? null : new BlockStore().GetBlock(dbTx, chainTip).Value.header;
        //	return xValid(ptx, contractHash, utxos, tipBlockHeader);
        //}

        public void InitBlockTimestamps(TransactionContext dbTx)
        {
            if (Tip != null)
            {
                var timestamps = new List <long>();
                var itr        = Tip.Value;

                while (itr != null && timestamps.Count < BlockTimestamps.SIZE)
                {
                    timestamps.Add(itr.header.timestamp);

                    if (itr.header.parent.Length == 0)
                    {
                        break;
                    }

                    var bk = BlockStore.GetBlock(dbTx, itr.header.parent);

                    System.Diagnostics.Debug.Assert(bk != null);

                    itr = bk.Value;
                }
                Timestamps.Init(timestamps.ToArray());
            }
        }
示例#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);
        }
示例#3
0
        //TODO: should asset that the block came from main?
        Types.Block GetBlock(byte[] key)
        {
            using (TransactionContext context = _DBContext.GetTransactionContext())
            {
                var location = BlockStore.GetLocation(context, key);

                if (location == LocationEnum.Main)
                {
                    try
                    {
                        var bk = BlockStore.GetBlock(context, key);
                        return(bk == null ? null : bk.Value);
                    } catch (Exception e)
                    {
                        BlockChainTrace.Error("GetBlock", e);
                        return(null);
                    }
                }

                return(null);
            }
        }