Beispiel #1
0
        public object ResolveWithId(Type serviceType, object id)
        {
            var key = new Keyed(serviceType, id);

            if (keyedRegistrations.ContainsKey(key))
            {
                return(keyedRegistrations[key].Resolve());
            }
            throw new CendrusIocException($"No registration for {serviceType.FullName} and id {id.ToString()}");
        }
Beispiel #2
0
        List <Keyed <Types.Block> > GetOldMainChainStartFromLeafToFork(Keyed <Types.Block> fork, byte[] tip)
        {
            var itr  = GetBlock(tip);
            var list = new List <Keyed <Types.Block> >();

            while (itr.Value.header.parent.Length > 0 && !itr.Key.SequenceEqual(fork.Key))
            {
                list.Add(itr);
                itr = GetBlock(itr.Value.header.parent);
            }

            return(list);
        }
Beispiel #3
0
        List <Keyed <Types.Block> > GetNewMainChainStartFromForkToLeaf(Keyed <Types.Block> leaf, out Keyed <Types.Block> fork)
        {
            var list = new List <Keyed <Types.Block> >();

            do
            {
                list.Insert(0, leaf);
                leaf = GetBlock(leaf.Value.header.parent);
            } while (_BlockChain.BlockStore.IsLocation(_DbTx, leaf.Key, LocationEnum.Branch));

            fork = leaf;

            return(list);
        }
Beispiel #4
0
        void Reorg()
        {
            var originalTip = _BlockChain.Tip;

            Keyed <Types.Block> fork = null;
            var newMainChain         = GetNewMainChainStartFromForkToLeaf(new Keyed <Types.Block>(_BkHash, _Bk), out fork);

            _BlockChain.ChainTip.Context(_DbTx).Value = fork.Key;
            _BlockChain.Tip = fork;
            _BlockChain.InitBlockTimestamps(_DbTx);

            var oldMainChain = GetOldMainChainStartFromLeafToFork(fork, originalTip.Key);

            foreach (var block in oldMainChain)
            {
                UndoBlock(block.Value, block.Key);
            }

            //append new chain
            foreach (var _bk in newMainChain)
            {
                var action = new BlockVerificationHelper(
                    _BlockChain,
                    _DbTx,
                    _bk.Key,
                    _bk.Value,
                    false,
                    true,
                    ConfirmedTxs,
                    UnconfirmedTxs,
                    QueueActions);

                BlockChainTrace.Information($"new main chain bk {_bk.Value.header.blockNumber} {action.Result.BkResultEnum}", _bk.Value);

                if (action.Result.BkResultEnum == BkResultEnum.Rejected)
                {
                    _BlockChain.ChainTip.Context(_DbTx).Value = originalTip.Key;
                    _BlockChain.Tip = originalTip;
                    _BlockChain.InitBlockTimestamps(_DbTx);

                    BlockChainTrace.Information("reorganization undo", _bk.Value);
                    Result = new BkResult(BkResultEnum.Rejected);
                    return;
                }
            }
        }
Beispiel #5
0
        public void Render()
        {
            if (Value != null)
            {
                return;
            }

            var parentKey = new byte[] { };

            if (Parent != null)
            {
                Parent.Render();
                parentKey = Parent.Value.Key;
            }

            uint   version = 1;
            string date    = "2000-02-02";

            var blockHeader = new Types.BlockHeader(
                version,
                parentKey,
                0,
                new byte[] { },
                new byte[] { },
                new byte[] { },
                ListModule.OfSeq <byte[]>(new List <byte[]>()),
                DateTime.Parse(date).ToBinary(),
                1,
                new byte[] { }
                );

            //_TestTransactionPool.Render();

            //var block = new Types.Block(blockHeader, ListModule.OfSeq<Types.Transaction>(_TestTransactionPool.Values.Select(t => t.Value.Value)));
            var block = new Types.Block(blockHeader, ListModule.OfSeq <Types.Transaction>(_Transactions));

//			byte[] key = Merkle.blockHasher.Invoke(block);
            byte[] key = Merkle.blockHeaderHasher.Invoke(block.header);

            Value = new Keyed <Types.Block>(key, block);

            TestTrace.Transaction(Tag, key);
        }
Beispiel #6
0
        public void Render()
        {
            if (Value != null)
            {
                return;
            }

            var outputs = new List <Types.Output>();

            for (var i = 0; i < Outputs; i++)
            {
                outputs.Add(Util.GetOutput());
            }

            var inputs = new List <Types.Outpoint>();

            foreach (Point point in Inputs)
            {
                point.RefTransaction.Render();
                inputs.Add(new Types.Outpoint(point.RefTransaction.Value.Key, point.Index));
            }

            var hashes = new List <byte[]>();

            //hack Concensus into giving a different hash per each tx created
            var version = (uint)1;             //_Random.Next(1000);

            Types.Transaction transaction = new Types.Transaction(version,
                                                                  ListModule.OfSeq(inputs),
                                                                  ListModule.OfSeq(hashes),
                                                                  ListModule.OfSeq(outputs),
                                                                  null);

            byte[] key = Merkle.transactionHasher.Invoke(transaction);
            Value = new Keyed <Types.Transaction>(key, transaction);

            TestTrace.Transaction(Tag, key);
        }