Commit 1e7757b6 authored by Pavel Pavlov's avatar Pavel Pavlov

code research and analysis Stratis

parent f0168c8d
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DeStream.Stratis.Bitcoin.Configuration; using DeStream.Stratis.Bitcoin.Configuration;
using NBitcoin; using NBitcoin;
...@@ -18,6 +20,7 @@ using Stratis.Bitcoin.IntegrationTests.Common; ...@@ -18,6 +20,7 @@ using Stratis.Bitcoin.IntegrationTests.Common;
using Stratis.Bitcoin.IntegrationTests.Common.EnvironmentMockUpHelpers; using Stratis.Bitcoin.IntegrationTests.Common.EnvironmentMockUpHelpers;
using Stratis.Bitcoin.Interfaces; using Stratis.Bitcoin.Interfaces;
using Stratis.Bitcoin.Utilities; using Stratis.Bitcoin.Utilities;
using static Stratis.Bitcoin.BlockPulling.BlockPuller;
namespace DeStream.DeStreamD.ForTest namespace DeStream.DeStreamD.ForTest
{ {
...@@ -27,7 +30,97 @@ namespace DeStream.DeStreamD.ForTest ...@@ -27,7 +30,97 @@ namespace DeStream.DeStreamD.ForTest
{ {
MainAsync(args).Wait(); MainAsync(args).Wait();
} }
private class TransactionNode
{
public uint256 Hash = null;
public Transaction Transaction = null;
public List<TransactionNode> DependsOn = new List<TransactionNode>();
public TransactionNode(Transaction tx)
{
this.Transaction = tx;
this.Hash = tx.GetHash();
}
}
private static List<Transaction> Reorder(List<Transaction> transactions)
{
if (transactions.Count == 0)
return transactions;
var result = new List<Transaction>();
Dictionary<uint256, TransactionNode> dictionary = transactions.ToDictionary(t => t.GetHash(), t => new TransactionNode(t));
foreach (TransactionNode transaction in dictionary.Select(d => d.Value))
{
foreach (TxIn input in transaction.Transaction.Inputs)
{
TransactionNode node = dictionary.TryGet(input.PrevOut.Hash);
if (node != null)
{
transaction.DependsOn.Add(node);
}
}
}
while (dictionary.Count != 0)
{
foreach (TransactionNode node in dictionary.Select(d => d.Value).ToList())
{
foreach (TransactionNode parent in node.DependsOn.ToList())
{
if (!dictionary.ContainsKey(parent.Hash))
node.DependsOn.Remove(parent);
}
if (node.DependsOn.Count == 0)
{
result.Add(node.Transaction);
dictionary.Remove(node.Hash);
}
}
}
return result;
}
public static Block[] GenerateStratis(IFullNode node, BitcoinSecret dest, int blockCount, List<Transaction> passedTransactions = null, bool broadcast = true)
{
FullNode fullNode = (FullNode)node;
//BitcoinSecret dest = this.MinerSecret;
var blocks = new List<Block>();
//DateTimeOffset now = this.MockTime == null ? DateTimeOffset.UtcNow : this.MockTime.Value;
for (int i = 0; i < blockCount; i++)
{
uint nonce = 0;
var block = new Block();
block.Header.HashPrevBlock = fullNode.Chain.Tip.HashBlock;
block.Header.Bits = block.Header.GetWorkRequired(fullNode.Network, fullNode.Chain.Tip);
block.Header.UpdateTime(DateTimeOffset.UtcNow, fullNode.Network, fullNode.Chain.Tip);
var coinbase = new Transaction();
coinbase.AddInput(TxIn.CreateCoinbase(fullNode.Chain.Height + 1));
coinbase.AddOutput(new TxOut(fullNode.Network.GetReward(fullNode.Chain.Height + 1), dest.GetAddress()));
block.AddTransaction(coinbase);
if (passedTransactions?.Any() ?? false)
{
passedTransactions = Reorder(passedTransactions);
block.Transactions.AddRange(passedTransactions);
}
block.UpdateMerkleRoot();
while (!block.CheckProofOfWork())
block.Header.Nonce = ++nonce;
blocks.Add(block);
if (broadcast)
{
uint256 blockHash = block.GetHash();
var newChain = new ChainedHeader(block.Header, blockHash, fullNode.Chain.Tip);
ChainedHeader oldTip = fullNode.Chain.SetTip(newChain);
fullNode.ConsensusLoop().Puller.InjectBlock(blockHash, new DownloadedBlock { Length = block.GetSerializedSize(), Block = block }, CancellationToken.None);
}
}
return blocks.ToArray();
}
public static async Task MainAsync(string[] args) public static async Task MainAsync(string[] args)
{ {
try try
...@@ -43,7 +136,7 @@ namespace DeStream.DeStreamD.ForTest ...@@ -43,7 +136,7 @@ namespace DeStream.DeStreamD.ForTest
Console.WriteLine($"current network: {network.Name}"); Console.WriteLine($"current network: {network.Name}");
// NOTES: running BTC and STRAT side by side is not possible yet as the flags for serialization are static // NOTES: running BTC and STRAT side by side is not possible yet as the flags for serialization are static
IFullNode node = new FullNodeBuilder() FullNode node = (FullNode)new FullNodeBuilder()
.UseNodeSettings(nodeSettings) .UseNodeSettings(nodeSettings)
.UseBlockStore() .UseBlockStore()
.UsePosConsensus() .UsePosConsensus()
...@@ -53,18 +146,25 @@ namespace DeStream.DeStreamD.ForTest ...@@ -53,18 +146,25 @@ namespace DeStream.DeStreamD.ForTest
.UseApi() .UseApi()
.AddRPC() .AddRPC()
.Build(); .Build();
if (node != null)
await node.RunAsync();
//(node.NodeService<IInitialBlockDownloadState>() as InitialBlockDownloadStateMock).SetIsInitialBlockDownload(false, DateTime.UtcNow.AddMinutes(5)); (node.NodeService<IInitialBlockDownloadState>() as InitialBlockDownloadStateMock).SetIsInitialBlockDownload(false, DateTime.UtcNow.AddMinutes(5));
//Mnemonic _mnemonic1 = node.NodeService<IWalletManager>().CreateWallet("123456", "mywallet"); Mnemonic _mnemonic1 = node.NodeService<IWalletManager>().CreateWallet("123456", "mywallet");
//Mnemonic _mnemonic2 = node.NodeService<IWalletManager>().CreateWallet("123456", "mywallet"); //Mnemonic _mnemonic2 = node.NodeService<IWalletManager>().CreateWallet("123456", "mywallet");
//HdAddress _addr = node.NodeService<IWalletManager>().GetUnusedAddress(new WalletAccountReference("mywallet", "account 0")); HdAddress _addr = node.NodeService<IWalletManager>().GetUnusedAddress(new WalletAccountReference("mywallet", "account 0"));
//Wallet _wallet = node.NodeService<IWalletManager>().GetWalletByName("mywallet"); Wallet _wallet = node.NodeService<IWalletManager>().GetWalletByName("mywallet");
//Key key = _wallet.GetExtendedPrivateKeyForAddress("123456", _addr).PrivateKey; Key _key = _wallet.GetExtendedPrivateKeyForAddress("123456", _addr).PrivateKey;
//stratisSender.SetDummyMinerSecret(new BitcoinSecret(key, stratisSender.FullNode.Network)); BitcoinSecret bitcoinSecret = new BitcoinSecret(_key, node.Network);
//int maturity = (int)stratisSender.FullNode.Network.Consensus.CoinbaseMaturity;
//stratisSender.GenerateStratis(maturity + 5);
int _maturity = (int)node.Network.Consensus.CoinbaseMaturity;
GenerateStratis(node, bitcoinSecret, 10);
TestHelper.WaitLoop(() => TestHelper.IsNodeSynced((FullNode)node));
//// wait for block repo for block sync to work //// wait for block repo for block sync to work
//TestHelper.WaitLoop(() => TestHelper.IsNodeSynced(stratisSender)); //TestHelper.WaitLoop(() => TestHelper.IsNodeSynced(stratisSender));
...@@ -84,7 +184,6 @@ namespace DeStream.DeStreamD.ForTest ...@@ -84,7 +184,6 @@ namespace DeStream.DeStreamD.ForTest
//Transaction trx = walletTransactionHandler.BuildTransaction(transactionBuildContext); //Transaction trx = walletTransactionHandler.BuildTransaction(transactionBuildContext);
int qwe = 1; int qwe = 1;
NodeBuilder builder = NodeBuilder.Create(node); NodeBuilder builder = NodeBuilder.Create(node);
......
...@@ -2,11 +2,11 @@ regtest=1 ...@@ -2,11 +2,11 @@ regtest=1
rest=1 rest=1
server=1 server=1
txindex=1 txindex=1
rpcuser=72bd1ed09d10a2a2de2cdab36417713892497247 rpcuser=f8ea820eaff15b70836adb49d67e6836d3b1ce81
rpcpassword=72bd1ed09d10a2a2de2cdab36417713892497247 rpcpassword=f8ea820eaff15b70836adb49d67e6836d3b1ce81
port=13167 port=11680
rpcport=11254 rpcport=11647
apiport=13638 apiport=12790
printtoconsole=1 printtoconsole=1
keypool=10 keypool=10
agentprefix=node13167 agentprefix=node11680
__cookie__:ed7b9212f66c0994d74fed43687126831c6238840af33ec19ee19a2df7e14137 __cookie__:5ec2c47925207702410fc0fef36bf1e58d7028ae14d9010327102c9a92becc9c
\ No newline at end of file \ No newline at end of file
regtest=1
rest=1
server=1
txindex=1
rpcuser=24633a316c7f32e05a289d1f6cf8a6c028483b2c
rpcpassword=24633a316c7f32e05a289d1f6cf8a6c028483b2c
port=11524
rpcport=11016
apiport=13539
printtoconsole=1
keypool=10
agentprefix=node11524
__cookie__:d5907bec5b991b8c0bb248c68e23d58c54a43da78a43dde9bc15b58becf4401e
\ No newline at end of file
[2018-07-25 14:52:18.4782 1] INFO: Stratis.Bitcoin.FullNode.Initialize Full node initialized on RegTest
[2018-07-25 14:52:18.4782 1] INFO: Stratis.Bitcoin.FullNode.Start Starting node...
[2018-07-25 14:52:18.8851 1] INFO: Stratis.Bitcoin.Base.BaseFeature+<StartChainAsync>d__26.MoveNext Loading finalized block height
[2018-07-25 14:52:18.8851 6] INFO: Stratis.Bitcoin.Base.BaseFeature+<StartChainAsync>d__26.MoveNext Loading chain
[2018-07-25 14:52:18.8851 10] INFO: Stratis.Bitcoin.Base.BaseFeature+<StartChainAsync>d__26.MoveNext Chain loaded at height 0
[2018-07-25 14:52:18.8851 10] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext FlushChain starting
[2018-07-25 14:52:18.8851 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext Periodic peer flush... starting
[2018-07-25 14:52:19.9005 10] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext Consensus Loop starting
[2018-07-25 14:52:19.9005 1] INFO: Stratis.Bitcoin.Features.MemoryPool.MempoolManager+<LoadPoolAsync>d__25.MoveNext Loading Memory Pool...
[2018-07-25 14:52:19.9005 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext MemoryPool.RelayWorker starting
[2018-07-25 14:52:19.9192 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext wallet persist job starting
[2018-07-25 14:52:19.9316 1] INFO: Stratis.Bitcoin.Features.Wallet.WalletSyncManager.Start WalletSyncManager initialized. Wallet at block 0.
[2018-07-25 14:52:19.9316 1] INFO: Stratis.Bitcoin.Features.RPC.RPCFeature.Initialize RPC Server listening on:
http://[::1]:11016/
http://127.0.0.1:11016/
[2018-07-25 14:52:19.9316 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext DiscoverPeersAsync starting
[2018-07-25 14:52:19.9476 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext PeerConnectorAddNode.ConnectAsync starting
[2018-07-25 14:52:19.9476 7] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext PeerConnectorDiscovery.ConnectAsync starting
[2018-07-25 14:52:19.9476 1] INFO: Stratis.Bitcoin.Connection.ConnectionManager.StartNodeServer Node listening on:
0.0.0.0:11524
[2018-07-25 14:52:19.9476 12] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext PeriodicLog starting
[2018-07-25 14:52:23.4804 17] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:52:23 agent node13167-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: No Wallet
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Connection======
[2018-07-25 14:52:23.7961 11] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext WalletManager.DownloadChain starting
[2018-07-25 14:52:24.9657 6] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:52:24 agent node11524-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: No Wallet
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Connection======
[2018-07-25 14:52:27.2913 11] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext WalletManager.DownloadChain starting
[2018-07-25 14:52:28.5257 17] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:52:28 agent node13167-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: 0 Wallet.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
[2018-07-25 14:53:16.8332 11] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:52:32 agent node11524-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: 0 Wallet.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
[2018-07-25 14:53:16.9189 6] INFO: Stratis.Bitcoin.Utilities.AsyncLoop+<>c__DisplayClass16_0+<<StartAsync>b__0>d.MoveNext WalletManager.DownloadChain stopping
[2018-07-25 14:54:05.3880 6] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:53:24 agent node11524-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: 0 Wallet.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
[2018-07-25 14:54:08.9765 17] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:52:33 agent node13167-StratisBitcoin:1.1.2
Headers.Height: 105 Headers.Hash: 4c9a904263bff10e39b7424f33d1c4afe28aa0cfb074e6da66d0414a6f3fdb80
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 1 Consensus.Hash: 1bc770b0838bb60b0029537c56e24930b69d08b25e46b0139386b3e5aa3271ec
Wallet.Height: 0 Wallet.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
[2018-07-25 14:55:30.2543 6] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:54:16 agent node11524-StratisBitcoin:1.1.2
Headers.Height: 0 Headers.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 0 Consensus.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Wallet.Height: 0 Wallet.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
[2018-07-25 14:55:30.3469 17] INFO: Stratis.Bitcoin.FullNode.<StartPeriodicLog>b__75_0 ======Node stats====== 07/25/2018 14:54:16 agent node13167-StratisBitcoin:1.1.2
Headers.Height: 105 Headers.Hash: 4c9a904263bff10e39b7424f33d1c4afe28aa0cfb074e6da66d0414a6f3fdb80
BlockStore.Height: 0 BlockStore.Hash: 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
Consensus.Height: 1 Consensus.Hash: 1bc770b0838bb60b0029537c56e24930b69d08b25e46b0139386b3e5aa3271ec
Wallet.Height: 105 Wallet.Hash: 4c9a904263bff10e39b7424f33d1c4afe28aa0cfb074e6da66d0414a6f3fdb80
======BlockStore======
Pending Blocks: 0
Batch Size: 0 kb / 5000 kb
=======Mempool=======
MempoolSize: 0 DynamicSize: 0 kb OrphanSize: 0
======Wallets======
Wallet: mywallet, Confirmed balance: 0.00000000
======Connection======
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment