本文整理汇总了C#中Peer.StartBlockChainDownload方法的典型用法代码示例。如果您正苦于以下问题:C# Peer.StartBlockChainDownload方法的具体用法?C# Peer.StartBlockChainDownload怎么用?C# Peer.StartBlockChainDownload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Peer
的用法示例。
在下文中一共展示了Peer.StartBlockChainDownload方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static void Run(string[] args)
{
// TODO: Assumes production network not testnet. Make it selectable.
var @params = NetworkParameters.ProdNet();
try
{
// Decode the private key from Satoshi's Base58 variant. If 51 characters long then it's from BitCoins
// "dumpprivkey" command and includes a version byte and checksum. Otherwise assume it's a raw key.
EcKey key;
if (args[0].Length == 51)
{
var dumpedPrivateKey = new DumpedPrivateKey(@params, args[0]);
key = dumpedPrivateKey.Key;
}
else
{
var privKey = Base58.DecodeToBigInteger(args[0]);
key = new EcKey(privKey);
}
Console.WriteLine("Address from private key is: " + key.ToAddress(@params));
// And the address ...
var destination = new Address(@params, args[1]);
// Import the private key to a fresh wallet.
var wallet = new Wallet(@params);
wallet.AddKey(key);
// Find the transactions that involve those coins.
using (var conn = new NetworkConnection(IPAddress.Loopback, @params, 0, 60000))
using (var blockStore = new MemoryBlockStore(@params))
{
var chain = new BlockChain(@params, wallet, blockStore);
var peer = new Peer(@params, conn, chain);
peer.Start();
peer.StartBlockChainDownload().Await();
// And take them!
Console.WriteLine("Claiming " + Utils.BitcoinValueToFriendlyString(wallet.GetBalance()) + " coins");
wallet.SendCoins(peer, destination, wallet.GetBalance());
// Wait a few seconds to let the packets flush out to the network (ugly).
Thread.Sleep(5000);
peer.Disconnect();
}
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("First arg should be private key in Base58 format. Second argument should be address to send to.");
}
}
示例2: Run
public static void Run(string[] args)
{
var file = new FileInfo(args[0]);
var wallet = Wallet.LoadFromFile(file);
Console.WriteLine(wallet.ToString());
// Set up the components and link them together.
var @params = NetworkParameters.TestNet();
var blockStore = new MemoryBlockStore(@params);
var conn = new NetworkConnection(IPAddress.Loopback, @params,
blockStore.GetChainHead().Height, 60000);
var chain = new BlockChain(@params, wallet, blockStore);
var peer = new Peer(@params, conn, chain);
peer.Start();
wallet.CoinsReceived +=
(sender, e) =>
{
Console.WriteLine();
Console.WriteLine("Received tx " + e.Tx.HashAsString);
Console.WriteLine(e.Tx.ToString());
};
// Now download and process the block chain.
var progress = peer.StartBlockChainDownload();
var max = progress.Count; // Racy but no big deal.
if (max > 0)
{
Console.WriteLine("Downloading block chain. " + (max > 1000 ? "This may take a while." : ""));
var current = max;
while (current > 0)
{
var pct = 100.0 - (100.0*(current/(double) max));
Console.WriteLine(string.Format("Chain download {0}% done", (int) pct));
progress.Await(TimeSpan.FromSeconds(1));
current = progress.Count;
}
}
peer.Disconnect();
wallet.SaveToFile(file);
Console.WriteLine();
Console.WriteLine("Done!");
Console.WriteLine();
Console.WriteLine(wallet.ToString());
}
示例3: StartBlockChainDownloadFromPeer
private void StartBlockChainDownloadFromPeer(Peer peer)
{
lock (this)
{
peer.BlocksDownloaded += (sender, e) => _downloadListener.OnBlocksDownloaded((Peer) sender, e.Block, e.BlocksLeft);
peer.ChainDownloadStarted += (sender, e) => _downloadListener.OnChainDownloadStarted((Peer) sender, e.BlocksLeft);
try
{
peer.StartBlockChainDownload();
}
catch (IOException e)
{
_log.Error("failed to start block chain download from " + peer, e);
return;
}
_downloadPeer = peer;
}
}
示例4: Run
public static void Run(string[] args)
{
var testNet = args.Length > 0 && string.Equals(args[0], "testnet", StringComparison.InvariantCultureIgnoreCase);
var @params = testNet ? NetworkParameters.TestNet() : NetworkParameters.ProdNet();
var filePrefix = testNet ? "pingservice-testnet" : "pingservice-prodnet";
// Try to read the wallet from storage, create a new one if not possible.
Wallet wallet;
var walletFile = new FileInfo(filePrefix + ".wallet");
try
{
wallet = Wallet.LoadFromFile(walletFile);
}
catch (IOException)
{
wallet = new Wallet(@params);
wallet.Keychain.Add(new EcKey());
wallet.SaveToFile(walletFile);
}
// Fetch the first key in the wallet (should be the only key).
var key = wallet.Keychain[0];
// Load the block chain, if there is one stored locally.
Console.WriteLine("Reading block store from disk");
using (var blockStore = new BoundedOverheadBlockStore(@params, new FileInfo(filePrefix + ".blockchain")))
{
// Connect to the localhost node. One minute timeout since we won't try any other peers
Console.WriteLine("Connecting ...");
using (var conn = new NetworkConnection(IPAddress.Loopback, @params, blockStore.GetChainHead().Height, 60000))
{
var chain = new BlockChain(@params, wallet, blockStore);
var peer = new Peer(@params, conn, chain);
peer.Start();
// We want to know when the balance changes.
wallet.CoinsReceived +=
(sender, e) =>
{
// Running on a peer thread.
Debug.Assert(!e.NewBalance.Equals(0));
// It's impossible to pick one specific identity that you receive coins from in BitCoin as there
// could be inputs from many addresses. So instead we just pick the first and assume they were all
// owned by the same person.
var input = e.Tx.Inputs[0];
var from = input.FromAddress;
var value = e.Tx.GetValueSentToMe(wallet);
Console.WriteLine("Received " + Utils.BitcoinValueToFriendlyString(value) + " from " + from);
// Now send the coins back!
var sendTx = wallet.SendCoins(peer, from, value);
Debug.Assert(sendTx != null); // We should never try to send more coins than we have!
Console.WriteLine("Sent coins back! Transaction hash is " + sendTx.HashAsString);
wallet.SaveToFile(walletFile);
};
var progress = peer.StartBlockChainDownload();
var max = progress.Count; // Racy but no big deal.
if (max > 0)
{
Console.WriteLine("Downloading block chain. " + (max > 1000 ? "This may take a while." : ""));
var current = max;
var lastPercent = 0;
while (current > 0)
{
var pct = 100.0 - (100.0*(current/(double) max));
if ((int) pct != lastPercent)
{
Console.WriteLine(string.Format("Chain download {0}% done", (int) pct));
lastPercent = (int) pct;
}
progress.Await(TimeSpan.FromSeconds(1));
current = progress.Count;
}
}
Console.WriteLine("Send coins to: " + key.ToAddress(@params));
Console.WriteLine("Waiting for coins to arrive. Press Ctrl-C to quit.");
// The peer thread keeps us alive until something kills the process.
}
}
}
示例5: StartBlockChainDownloadFromPeer
private void StartBlockChainDownloadFromPeer(Peer peer)
{
Log.DebugFormat("HandleNewPeer: {0}", peer);
lock (this)
{
peer.BlocksDownloaded += OnPeerOnBlocksDownloaded;
peer.ChainDownloadStarted += OnPeerOnChainDownloadStarted;
try
{
peer.StartBlockChainDownload();
}
catch (IOException e)
{
Log.Error("failed to start block chain download from " + peer, e);
return;
}
_downloadPeer = peer;
}
}