本文整理汇总了C#中Chain.GetBlock方法的典型用法代码示例。如果您正苦于以下问题:C# Chain.GetBlock方法的具体用法?C# Chain.GetBlock怎么用?C# Chain.GetBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chain
的用法示例。
在下文中一共展示了Chain.GetBlock方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanForkSidePartialChain
public void CanForkSidePartialChain()
{
var block = TestUtils.CreateFakeBlock();
Chain side = new Chain(block.Header, 10, new StreamObjectStream<ChainChange>());
Chain main = new Chain(block.Header, 10, new StreamObjectStream<ChainChange>());
AppendBlock(side, main);
AppendBlock(side, main);
var common = AppendBlock(side, main);
var sideb = AppendBlock(side);
var mainb1 = AppendBlock(main);
var mainb2 = AppendBlock(main);
var mainb3 = AppendBlock(main);
Assert.Equal(common.HashBlock, side.SetTip(main.Tip).HashBlock);
Assert.NotNull(side.GetBlock(mainb1.HashBlock));
Assert.NotNull(side.GetBlock(mainb2.HashBlock));
Assert.NotNull(side.GetBlock(mainb3.HashBlock));
Assert.NotNull(side.GetBlock(common.HashBlock));
Assert.Null(side.GetBlock(sideb.HashBlock));
Assert.Equal(common.HashBlock, side.SetTip(sideb).HashBlock);
Assert.Null(side.GetBlock(mainb1.HashBlock));
Assert.Null(side.GetBlock(mainb2.HashBlock));
Assert.Null(side.GetBlock(mainb3.HashBlock));
Assert.NotNull(side.GetBlock(sideb.HashBlock));
}
示例2: CanCalculateDifficulty
public void CanCalculateDifficulty()
{
var o = Network.Main.ProofOfWorkLimit;
var main = new Chain(LoadMainChain());
var histories = File.ReadAllText("data/targethistory.csv").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach(var history in histories)
{
var height = int.Parse(history.Split(',')[0]);
var expectedTarget = new Target(BigInteger.Parse(history.Split(',')[1]));
var block = main.GetBlock(height).Header;
Assert.Equal(expectedTarget, block.Bits);
var target = main.GetWorkRequired(Network.Main, height);
Assert.Equal(expectedTarget, target);
}
}
示例3: CanForkSide
public void CanForkSide()
{
Chain side = new Chain(Network.Main);
Chain main = new Chain(Network.Main);
AppendBlock(side, main);
AppendBlock(side, main);
var common = AppendBlock(side, main);
var sideb = AppendBlock(side);
var mainb1 = AppendBlock(main);
var mainb2 = AppendBlock(main);
var mainb3 = AppendBlock(main);
Assert.Equal(common.HashBlock, side.SetTip(main.Tip).HashBlock);
Assert.NotNull(side.GetBlock(mainb1.HashBlock));
Assert.NotNull(side.GetBlock(mainb2.HashBlock));
Assert.NotNull(side.GetBlock(mainb3.HashBlock));
Assert.NotNull(side.GetBlock(common.HashBlock));
Assert.Null(side.GetBlock(sideb.HashBlock));
Assert.Equal(common.HashBlock, side.SetTip(sideb).HashBlock);
Assert.Null(side.GetBlock(mainb1.HashBlock));
Assert.Null(side.GetBlock(mainb2.HashBlock));
Assert.Null(side.GetBlock(mainb3.HashBlock));
Assert.NotNull(side.GetBlock(sideb.HashBlock));
}
示例4: CanForkBackwardPartialChain
public void CanForkBackwardPartialChain()
{
Chain chain = new Chain(TestUtils.CreateFakeBlock().Header, 10, new StreamObjectStream<ChainChange>());
AppendBlock(chain);
AppendBlock(chain);
var fork = AppendBlock(chain);
//Test single block back fork
var last = AppendBlock(chain);
Assert.Equal(14, chain.Height);
Assert.Equal(14, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
Assert.Equal(13, chain.Height);
Assert.Equal(13, fork.Height);
Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
Assert.Null(chain.GetBlock(last.HashBlock));
Assert.NotNull(chain.GetBlock(fork.HashBlock));
//Test 3 blocks back fork
var b1 = AppendBlock(chain);
var b2 = AppendBlock(chain);
last = AppendBlock(chain);
Assert.Equal(16, chain.Height);
Assert.Equal(16, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
Assert.Equal(13, chain.Height);
Assert.Equal(13, fork.Height);
Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
Assert.Null(chain.GetBlock(last.HashBlock));
Assert.Null(chain.GetBlock(b1.HashBlock));
Assert.Null(chain.GetBlock(b2.HashBlock));
chain.SetTip(last);
Assert.Equal(16, chain.Height);
Assert.Equal(16, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
}
示例5: CanForkBackward
public void CanForkBackward()
{
Chain chain = new Chain(Network.Main);
AppendBlock(chain);
AppendBlock(chain);
var fork = AppendBlock(chain);
//Test single block back fork
var last = AppendBlock(chain);
Assert.Equal(4, chain.Height);
Assert.Equal(4, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
Assert.Equal(3, chain.Height);
Assert.Equal(3, fork.Height);
Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
Assert.Null(chain.GetBlock(last.HashBlock));
Assert.NotNull(chain.GetBlock(fork.HashBlock));
//Test 3 blocks back fork
var b1 = AppendBlock(chain);
var b2 = AppendBlock(chain);
last = AppendBlock(chain);
Assert.Equal(6, chain.Height);
Assert.Equal(6, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
Assert.Equal(fork.HashBlock, chain.SetTip(fork).HashBlock);
Assert.Equal(3, chain.Height);
Assert.Equal(3, fork.Height);
Assert.Equal(fork.HashBlock, chain.Tip.HashBlock);
Assert.Null(chain.GetBlock(last.HashBlock));
Assert.Null(chain.GetBlock(b1.HashBlock));
Assert.Null(chain.GetBlock(b2.HashBlock));
chain.SetTip(last);
Assert.Equal(6, chain.Height);
Assert.Equal(6, last.Height);
Assert.Equal(last.HashBlock, chain.Tip.HashBlock);
}
示例6: 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);
}
}
示例7: 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;
}