使用 C# .NET 查询本地比特币区块链
问题描述
我正在尝试仅使用本地存储的区块链(通过 Bitcoin Core 下载)来检查给定比特币地址的余额。 类似的东西(通过使用 NBitCoin 和/或 QBitNinja),但不需要访问网络:
private static readonly QBitNinjaClient client = new QBitNinjaClient(Network.Main);
public decimal CheckBalance(BitcoinPubKeyAddress address)
{
var balanceModel = client.GetBalance(address, true).Result;
decimal balance = 0;
if (balanceModel.Operations.Count > 0)
{
var unspentCoins = new List<Coin>();
foreach (var operation in balanceModel.Operations)
unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
balance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
}
return balance;
}
上面的例子需要访问网络。 我需要离线做同样的事情。 我想出了这样的东西,但显然它不起作用: public decimal CheckBalanceLocal(BitcoinPubKeyAddress address) { var node = Node.ConnectToLocal(Network.Main); node.VersionHandshake(); var chain = node.GetChain();
var store = new BlockStore(@"F:\Program Files\Bitcoin\Cache\blocks", Network.Main);
var index = new IndexedBlockStore(new InMemoryNoSqlRepository(), store);
index.ReIndex();
var headers = chain.ToEnumerable(false).ToArray();
var balance = (
from header in headers
select index.Get(header.HashBlock)
into block
from tx in block.Transactions
from txout in tx.Outputs
where txout.ScriptPubKey.GetDestinationAddress(Network.Main) == address
select txout.Value.ToDecimal(MoneyUnit.BTC)).Sum();
return balance;
}
它在查询期间挂起 我想要一些东西而不是 InMemoryNoSqlRepository 存储在文件中,以防止使用 ReIndex() 减慢一切 我的要求是以与第一种方法相同的方式检查余额,但通过查询存储在我的磁盘上的块。
其实我需要的可能只是这个问题的答案
回复区
1)
1
1
1"'`--
1
回答