当前位置: 首页>>代码示例>>C#>>正文


C# Chain.FindFork方法代码示例

本文整理汇总了C#中Chain.FindFork方法的典型用法代码示例。如果您正苦于以下问题:C# Chain.FindFork方法的具体用法?C# Chain.FindFork怎么用?C# Chain.FindFork使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Chain的用法示例。


在下文中一共展示了Chain.FindFork方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Process

        public void Process(Chain mainChain, IBlockProvider blockProvider)
        {
            bool newChain = false;
            if(!Chain.Initialized)
            {
                newChain = true;

                var firstBlock = mainChain.GetBlock(StartHeight);
                Chain.Initialize(firstBlock.Header, StartHeight);
            }
            var forkBlock = mainChain.FindFork(Chain);
            if(forkBlock.HashBlock != Chain.Tip.HashBlock)
            {
                Chain.SetTip(Chain.GetBlock(forkBlock.Height));
                foreach(var e in Account.GetInChain(Chain, false)
                                            .Where(e => e.Reason != AccountEntryReason.ChainBlockChanged))
                {
                    var neutralized = e.Neutralize();
                    Account.PushAccountEntry(neutralized);
                }
            }

            var unprocessedBlocks = mainChain.ToEnumerable(true)
                                       .TakeWhile(block => block != forkBlock)
                                       .Concat(newChain ? new ChainedBlock[] { forkBlock } : new ChainedBlock[0])
                                       .Reverse().ToArray();
            foreach(var block in unprocessedBlocks)
            {
                List<byte[]> searchedData = new List<byte[]>();
                Scanner.GetScannedPushData(searchedData);
                foreach(var unspent in Account.Unspent)
                {
                    searchedData.Add(unspent.OutPoint.ToBytes());
                }

                var fullBlock = blockProvider.GetBlock(block.HashBlock, searchedData);
                if(fullBlock != null)
                {
                    foreach(var spent in Scanner.FindSpent(fullBlock, Account.Unspent))
                    {
                        var entry = new AccountEntry(AccountEntryReason.Outcome,
                                                    block.HashBlock,
                                                    spent, -spent.TxOut.Value);
                        Account.PushAccountEntry(entry);
                    }
                    foreach(var coins in Scanner.ScanCoins(fullBlock, (int)block.Height))
                    {
                        int i = 0;
                        foreach(var output in coins.Coins.Outputs)
                        {
                            if(!output.IsNull)
                            {
                                var entry = new AccountEntry(AccountEntryReason.Income, block.HashBlock,
                                                    new Spendable(new OutPoint(coins.TxId, i), output), output.Value);
                                Account.PushAccountEntry(entry);
                            }
                            i++;
                        }
                    }
                }
                Chain.GetOrAdd(block.Header);
            }
        }
开发者ID:royosherove,项目名称:NBitcoin,代码行数:63,代码来源:ScanState.cs

示例2: CanSaveChain

        public void CanSaveChain()
        {
            var stream = new StreamObjectStream<ChainChange>();
            Chain chain = new Chain(Network.Main, stream);
            AppendBlock(chain);
            AppendBlock(chain);
            var fork = AppendBlock(chain);
            AppendBlock(chain);

            stream.Rewind();

            var chain2 = new Chain(stream);
            Assert.True(chain.SameTip(chain2));

            stream.WriteNext(new ChainChange()
            {
                Add = false,
                HeightOrBackstep = 1
            });
            stream.Rewind();

            var chain3 = new Chain(stream);
            AssertHeight(stream, 3);
            var actualFork = chain3.FindFork(chain);
            Assert.Equal(fork.HashBlock, actualFork.HashBlock);
        }
开发者ID:nikropht,项目名称:NBitcoin,代码行数:26,代码来源:ChainTests.cs

示例3: Process

        public bool Process(Chain mainChain, IBlockProvider blockProvider)
        {
            var chain = Chain.Clone();
            var chainPosition = chain.Changes.Position;
            var account = Account.Clone();
            var accountPosition = account.Entries.Position;

            bool newChain = false;
            if(!chain.Initialized)
            {
                newChain = true;

                var firstBlock = mainChain.GetBlock(StartHeight);
                chain.Initialize(firstBlock.Header, StartHeight);
            }
            var forkBlock = mainChain.FindFork(chain);
            if(forkBlock.HashBlock != chain.Tip.HashBlock)
            {
                var subChain = chain.CreateSubChain(forkBlock, false, chain.Tip, true);
                chain.SetTip(chain.GetBlock(forkBlock.Height));
                foreach(var e in account.GetInChain(subChain, true)
                                        .Where(c => c.Reason != AccountEntryReason.Lock && c.Reason != AccountEntryReason.Unlock)
                                        .Reverse())
                {
                    var neutralized = e.Neutralize();
                    account.PushAccountEntry(neutralized);
                }
            }

            var unprocessedBlocks = mainChain.ToEnumerable(true)
                                       .TakeWhile(block => block != forkBlock)
                                       .Concat(newChain ? new ChainedBlock[] { forkBlock } : new ChainedBlock[0])
                                       .Reverse().ToArray();
            foreach(var block in unprocessedBlocks)
            {
                List<byte[]> searchedData = new List<byte[]>();
                Scanner.GetScannedPushData(searchedData);
                foreach(var unspent in account.Unspent)
                {
                    searchedData.Add(unspent.OutPoint.ToBytes());
                }

                var fullBlock = blockProvider.GetBlock(block.HashBlock, searchedData);
                if(fullBlock == null)
                    continue;

                List<Tuple<OutPoint, AccountEntry>> spents = new List<Tuple<OutPoint, AccountEntry>>();
                foreach(var spent in FindSpent(fullBlock, account.Unspent))
                {
                    var entry = new AccountEntry(AccountEntryReason.Outcome,
                                                block.HashBlock,
                                                spent, -spent.TxOut.Value);
                    spents.Add(Tuple.Create(entry.Spendable.OutPoint, entry));
                }

                if(CheckDoubleSpend)
                {
                    var spentsDico = spents.ToDictionary(t => t.Item1, t => t.Item2);
                    foreach(var spent in Scanner.FindSpent(fullBlock))
                    {
                        if(!spentsDico.ContainsKey(spent.PrevOut))
                            return false;
                    }
                }

                foreach(var spent in spents)
                {
                    if(account.PushAccountEntry(spent.Item2) == null)
                        return false;
                }

                foreach(var coins in Scanner.ScanCoins(fullBlock, (int)block.Height))
                {
                    int i = 0;
                    foreach(var output in coins.Coins.Outputs)
                    {
                        if(!output.IsNull)
                        {
                            var entry = new AccountEntry(AccountEntryReason.Income, block.HashBlock,
                                                new Spendable(new OutPoint(coins.TxId, i), output), output.Value);
                            if(account.PushAccountEntry(entry) == null)
                                return false;
                        }
                        i++;
                    }
                }

                chain.GetOrAdd(block.Header);
            }

            account.Entries.GoTo(accountPosition);
            Account.Entries.WriteNext(account.Entries);
            Account.Process();

            chain.Changes.GoTo(chainPosition);
            Chain.Changes.WriteNext(chain.Changes);
            Chain.Process();
            return true;
        }
开发者ID:nikropht,项目名称:NBitcoin,代码行数:99,代码来源:ScanState.cs


注:本文中的Chain.FindFork方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。