public IndexedBlockStore(NoSqlRepository index, BlockStore store) { if(index == null) throw new ArgumentNullException("index"); if(store == null) throw new ArgumentNullException("store"); _Index = index; _Store = store; }
public void CanEnumerateBlockCountRange() { var store = new BlockStore(@"data\blocks", Network.Main); var expectedBlock = store.Enumerate(false).Skip(4).First(); var actualBlocks = store.Enumerate(false, 4, 2).ToArray(); Assert.Equal(2, actualBlocks.Length); Assert.Equal(expectedBlock.Item.Header.GetHash(), actualBlocks[0].Item.Header.GetHash()); Assert.True(actualBlocks[0].Item.CheckMerkleRoot()); }
public void BlockDirectoryScanSpeed() { //TestUtils.EnsureNew("BlockDirectoryScanSpeed"); var completeScan = Bench(() => { BlockStore store = new BlockStore(this.BlockStoreFolder, Network.Main); //BlockStore other = new BlockStore(@"BlockDirectoryScanSpeed", Network.Main); foreach (var block in store.Enumerate(false)) { } }); //var headersOnlyScan = Bench(() => //{ // BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); // var count = store.Enumerate(true).Count(); //}); }
public BlockChain(string dbName, byte[] genesisBlockHash) { memPool = new MemPool(); UTXOStore = new UTXOStore(); ActiveContractSet = new ActiveContractSet(); BlockStore = new BlockStore(); BlockNumberDifficulties = new BlockNumberDifficulties(); ChainTip = new ChainTip(); Timestamps = new BlockTimestamps(); GenesisBlockHash = genesisBlockHash; _DBContext = new DBContext(dbName); OwnResource(_DBContext); //var listener = new EventLoopMessageListener<QueueAction>(HandleQueueAction, "BlockChain listener"); //OwnResource(MessageProducer<QueueAction>.Instance.AddMessageListener(listener)); var buffer = new BufferBlock <QueueAction>(); QueueAction.Target = buffer; using (var dbTx = _DBContext.GetTransactionContext()) { var chainTip = ChainTip.Context(dbTx).Value; //TODO: check if makred as main? Tip = chainTip == null ? null : BlockStore.GetBlock(dbTx, chainTip); if (Tip != null) { BlockChainTrace.Information("Tip's block number is " + Tip.Value.header.blockNumber); //Try to validate orphans of tip, if any. this would be the case when a sync action was interrupted BlockStore.Orphans(dbTx, Tip.Key).ToList().ForEach(t => new HandleBlockAction(t.Key, t.Value, true).Publish()); } else { BlockChainTrace.Information("No tip."); } InitBlockTimestamps(dbTx); } var consumer = ConsumeAsync(buffer); }
public void BlockDirectoryScanSpeed() { //TestUtils.EnsureNew("BlockDirectoryScanSpeed"); var completeScan = Bench(() => { BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); //BlockStore other = new BlockStore(@"BlockDirectoryScanSpeed", Network.Main); foreach (var block in store.Enumerate(false, new DiskBlockPosRange(new DiskBlockPos(50, 0)))) { //other.Append(block.Item); } }); var headersOnlyScan = Bench(() => { BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); var count = store.Enumerate(true).Count(); }); }
public static void CanCalculatePowPosCorrectly() { var store = new BlockStore(TestDataLocations.BlockFolderLocation, Network.Main); var chain = store.GetChain(); var stakeChain = new MemoryStakeChain(Network.Main); var indexStore = new IndexedBlockStore(new InMemoryNoSqlRepository(), store); var reindexed = indexStore.ReIndex(); Assert.Equal(reindexed, 103952); foreach (var chainedBlock in chain.EnumerateAfter(chain.Genesis)) { var block = indexStore.Get(chainedBlock.HashBlock); var blockstake = new BlockStake(block); stakeChain.Set(chainedBlock.HashBlock, blockstake); Assert.True(stakeChain.CheckPowPosAndTarget(chainedBlock, blockstake, Network.Main)); } }
public void CanStoreBlocksInMultipleFiles() { BlockStore store = CreateBlockStore(); store.MaxFileSize = 10; // Verify break all block in one respective file with extreme settings. var allBlocks = StoredBlock.EnumerateFile(TestDataLocations.DataBlockFolder("blk0001.dat"), network: Network.StratisMain).Take(10).ToList(); foreach (StoredBlock s in allBlocks) { store.Append(s.Item); } var storedBlocks = store.Enumerate(true).ToList(); Assert.Equal(allBlocks.Count, storedBlocks.Count); Assert.Equal(11, store.Folder.GetFiles().Length); //10 files + lock file }
public void BlockDirectoryScanSpeed() { //TestUtils.EnsureNew("BlockDirectoryScanSpeed"); var completeScan = Bench(() => { BlockStore store = new BlockStore(@"Z:\Bitcoin\blocks", Network.Main); //BlockStore other = new BlockStore(@"BlockDirectoryScanSpeed", Network.Main); foreach(var block in store.Enumerate(false)) { } }); //var headersOnlyScan = Bench(() => //{ // BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); // var count = store.Enumerate(true).Count(); //}); }
/// <summary> /// Method used to scan all blockchain files, identify transactions and execute callback method for each transaction. /// </summary> /// <param name="datapath">Path to blockchain files.</param> /// <param name="callback">Callback closure method, executed for each found transaction.</param> public void Runner(string datapath, Action <StoredBlock, Transaction> callback) { ConcurrentBag <Task> listOftasks = new ConcurrentBag <Task>(); BlockStore store = new BlockStore(datapath, Network.Main); foreach (var file in new Helper().GetFiles(datapath, store.FileRegex).OrderByDescending(f => f.FullName)) { listOftasks.Add(Task.Run(() => { foreach (var transaction in store.EnumerateFile(file).SelectMany(block => block.Item.Transactions, (block, transactions) => new { block, transactions })) { callback(transaction.block, transaction.transactions); queue.Enqueue(new KeyValuePair <byte[], byte[]>(transaction.transactions.GetHash().ToBytes(), transaction.block.Item.GetHash().ToBytes())); queue.Enqueue(new KeyValuePair <byte[], byte[]>(transaction.block.Item.GetHash().ToBytes(), System.Text.Encoding.UTF8.GetBytes(file.FullName))); } })); } Task.WaitAll(listOftasks.ToArray()); }
public void CanCalculateDifficulty() { var histories = File.ReadAllLines(TestDataLocations.DataFolder(@"targethistory.csv")); var store = new BlockStore(TestDataLocations.BlockFolderLocation, Network.Main); // todo: load the chain with a header only file ConcurrentChain chain = store.GetChain(); foreach (var history in histories) { var height = int.Parse(history.Split(',')[0]); var expectedTarget = new Target(new BigInteger(history.Split(',')[1].Trim(), 10)); var block = chain.GetBlock(height).Header; Assert.Equal(expectedTarget, block.Bits); var target = chain.GetWorkRequired(Network.Main, height); Assert.Equal(expectedTarget, target); } }
public void CanCalculateDifficulty() { var histories = File.ReadAllLines("data/targethistory.csv"); var store = new BlockStore(@"download\blocks", Network.Main); // todo: load the chain with a heder only file ConcurrentChain chain = store.GetChain(); foreach (var history in histories) { var height = int.Parse(history.Split(',')[0]); var expectedTarget = new Target(BigInteger.Parse(history.Split(',')[1])); var block = chain.GetBlock(height).Header; Assert.Equal(expectedTarget, block.Bits); var target = chain.GetWorkRequired(Network.Main, height); Assert.Equal(expectedTarget, target); } }
public bool BlockChangeStatus(string id, string status) { try { var blockStore = new BlockStore(); bool dt = blockStore.BlockChangeStatus(id, status); if (dt) { return(true); } else { return(false); } } catch (Exception) { return(false); } }
public bool UpdateBlock(BlockModel model) { try { var blockStore = new BlockStore(); var dt = blockStore.UpdateBlock(model); if (dt) { return(true); } else { return(false); } } catch (Exception) { return(false); } }
public static void Read(Store store, IDeserializer context, out BlockStore blockStore) { var tileSize = context.Read <int>("tilesize"); blockStore = new BlockStore(tileSize); var materials = context.ReadList <KeyValuePair <MaterialType, IList <int> > >("materials", Read); foreach (var kvp in materials) { blockStore.Materials[kvp.Key].AddRange(kvp.Value); } var tiles = context.ReadList <ISpriteTemplate, Store>("tiles", store, Read); blockStore.Tiles.AddRange(tiles); var flags = context.ReadList <KeyValuePair <int, TileFlags> >("flags", Read); foreach (var flag in flags) { blockStore[flag.Key] = flag.Value; } }
public void CanStoreBlocks() { BlockStore store = CreateBlockStore(); var allBlocks = StoredBlock.EnumerateFile(TestDataLocations.DataBlockFolder("blk0001.dat")).Take(50).ToList(); foreach (StoredBlock s in allBlocks) { store.Append(s.Item); } var storedBlocks = store.Enumerate(true).ToList(); Assert.Equal(allBlocks.Count, storedBlocks.Count); foreach (StoredBlock s in allBlocks) { StoredBlock retrieved = store.Enumerate(true).First(b => b.Item.GetHash() == s.Item.GetHash()); Assert.True(retrieved.Item.HeaderOnly); } }
//TODO: should asset that the block came from main? Types.Block GetBlock(byte[] key) { using (TransactionContext context = _DBContext.GetTransactionContext()) { var location = BlockStore.GetLocation(context, key); if (location == LocationEnum.Main) { try { var bk = BlockStore.GetBlock(context, key); return(bk == null ? null : bk.Value); } catch (Exception e) { BlockChainTrace.Error("GetBlock", e); return(null); } } return(null); } }
public void CanCalculateDifficulty() { var histories = File.ReadAllLines(TestDataLocations.DataFolder(@"targethistory.csv")); var store = new BlockStore(TestDataLocations.BlockFolderLocation, Network.Main); // todo: load the chain with a header only file ConcurrentChain chain = store.GetChain(); var stakeChain = new MemoryStakeChain(Network.Main); var indexStore = new IndexedBlockStore(new InMemoryNoSqlRepository(), store); var reindexed = indexStore.ReIndex(); Assert.Equal(reindexed, 103952); var lastIndex = 0; foreach (var history in histories) { var height = int.Parse(history.Split(',')[0]); var expectedTarget = new Target(new BigInteger(history.Split(',')[1].Trim(), 10)); var chainedBlock = chain.GetBlock(height); for (int i = height; i > lastIndex; i--) { var g = chain.GetBlock(i); var block = indexStore.Get(g.HashBlock); stakeChain.Set(g.HashBlock, new BlockStake(block)); } lastIndex = height; Assert.Equal(expectedTarget, chainedBlock.Header.Bits); var target = stakeChain.GetWorkRequired(chainedBlock, stakeChain.Get(chainedBlock.HashBlock), Network.Main.Consensus); //var target = chain.GetWorkRequired(Network.Main, height); Assert.Equal(expectedTarget, target); } }
public IList <FontBlockViewModel> GetListBlockFont(string section) { try { var datas = new List <FontBlockViewModel>(); var blockStore = new BlockStore(); var dt = blockStore.GetListBlockFont(section); var listFrontModelTab1 = new FontBlockViewModel(); var listFrontModelTab2 = new FontBlockViewModel(); var listFrontModelTab3 = new FontBlockViewModel(); var listModel1 = new List <BlockModel>(); var listModel2 = new List <BlockModel>(); var listModel3 = new List <BlockModel>(); var model = new BlockModel(); for (int i = 0; i < dt.Rows.Count; i++) { switch (int.Parse(dt.Rows[i]["BLOCK_TAB"].ToString())) { case 1: model = new BlockModel() { ID = int.Parse(dt.Rows[i]["BLOCK_ID"].ToString()), TITLE = dt.Rows[i]["BLOCK_TITLE"].ToString(), CONTENT = dt.Rows[i]["BLOCK_CONTENT"].ToString(), TAB = dt.Rows[i]["BLOCK_TAB"].ToString(), POSITION = dt.Rows[i]["BLOCK_POSITION"].ToString(), STATUS = dt.Rows[i]["BLOCK_STATUS"].ToString().Trim() == "1", IMAGE = dt.Rows[i]["BLOCK_IMAGE"].ToString() }; listModel1.Add(model); listFrontModelTab1.Id = 1; break; case 2: model = new BlockModel() { ID = int.Parse(dt.Rows[i]["BLOCK_ID"].ToString()), TITLE = dt.Rows[i]["BLOCK_TITLE"].ToString(), CONTENT = dt.Rows[i]["BLOCK_CONTENT"].ToString(), TAB = dt.Rows[i]["BLOCK_TAB"].ToString(), POSITION = dt.Rows[i]["BLOCK_POSITION"].ToString(), STATUS = dt.Rows[i]["BLOCK_STATUS"].ToString().Trim() == "1", IMAGE = dt.Rows[i]["BLOCK_IMAGE"].ToString() // CREATED_DATE = DateTime.Parse(dt.Rows[i]["CREATED_DATE"].ToString()) }; listModel2.Add(model); listFrontModelTab2.Id = 2; break; } } listFrontModelTab1.ListBlockFeature = listModel1; listFrontModelTab2.ListBlockFeature = listModel2; datas.Add(listFrontModelTab1); datas.Add(listFrontModelTab2); return(datas); } catch (Exception ex) { return(null); } }
public void CanEnumerateBlockInAFileRange() { var store = new BlockStore(@"data\blocks", Network.Main); var result = store.Enumerate(new DiskBlockPosRange(new DiskBlockPos(0, 0), new DiskBlockPos(1, 0))).ToList(); Assert.Equal(300, result.Count); }
public void BenchmarkBlockIndexing() { Stopwatch watch = new Stopwatch(); watch.Start(); BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); IndexedBlockStore indexed = new IndexedBlockStore(new InMemoryNoSqlRepository(), store); indexed.ReIndex(); watch.Stop(); var time = watch.Elapsed; }
public void CanBuildChainFromBlocks() { var store = new BlockStore(@"data\blocks", Network.Main); var chain = store.GetChain(); Assert.True(chain.Height == 599); }
public void BenchmarkCreateChainFromBlocks() { BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); ConcurrentChain chain = null; var fullBuild = Bench(() => { chain = store.GetChain(); }); }
public void CanReIndex() { var source = new BlockStore(@"data\blocks", Network.Main); var store = CreateBlockStore("CanReIndexFolder"); store.AppendAll(source.Enumerate(false).Take(100).Select(b => b.Item)); var test = new IndexedBlockStore(new SQLiteNoSqlRepository("CanReIndex", true), store); var reIndexed = test.ReIndex(); Assert.Equal(100, reIndexed); int i = 0; foreach(var b in store.Enumerate(true)) { var result = test.Get(b.Item.GetHash()); Assert.Equal(result.GetHash(), b.Item.GetHash()); i++; } Assert.Equal(100, i); var last = source.Enumerate(false).Skip(100).FirstOrDefault(); store.Append(last.Item); reIndexed = test.ReIndex(); Assert.Equal(1, reIndexed); reIndexed = test.ReIndex(); Assert.Equal(0, reIndexed); }
public ISpriteTemplate ToSprite(BlockStore blocks) { var sprite = new TileStencilSprite(this, blocks); return(sprite); }
public TileStencilSprite(TileStencil stencil, BlockStore blocks) { this.stencil = stencil; this.blocks = blocks; }
public static void Play() { var tx2 = new Transaction("010000000188932931d6bc23d3182183ab5e9707980ab4aaf9db3c6d3be42e5c0ce783b0ba00000000844730440220100aaa46e8a4f9d4589f935f5457f263a911f3712ee50688732c1274face6bb30220155b48760fd6ffbc0842761db03d824954567e8cd499883318d1f1c705f7d4d7012103dccb6bd324ae3c60afb85d9a0e5f300284faf2d71c40dbb7732d8fa79ee930f71976a914edc80be0e4f342a54a4fbe6e31e8e58b6849972a88acffffffff02204e0000000000001976a91430a5d35558ade668b8829a2a0f60a3f10358327e88ac70110100000000001976a914edc80be0e4f342a54a4fbe6e31e8e58b6849972a88ac00000000"); var oo = tx2.GetHash(); var scriptPubKey = new Script(Encoders.Hex.DecodeData("a91467c205a82218e3b1327095f1e10dd676c6ce09df87")); ScriptEvaluationContext eval = new ScriptEvaluationContext(); var result = eval.VerifyScript(tx2.Inputs[0].ScriptSig, scriptPubKey, tx2, 0); CancellationTokenSource s1 = new CancellationTokenSource(); CancellationTokenSource s2 = new CancellationTokenSource(TimeSpan.FromSeconds(1)); try { Thread.Sleep(3000); CancellationTokenSource.CreateLinkedTokenSource(s1.Token, s2.Token).Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException ex) { bool a = s2.Token == ex.CancellationToken; } //1,132 //NodeServer server = new NodeServer(Network.Main); //server.RegisterPeerTableRepository(new SqLitePeerTableRepository("PeerCachePlay")); //server.BuildChain(new StreamObjectStream<ChainChange>(File.Open("MainChain.dat", FileMode.OpenOrCreate))); var from = new DateTimeOffset(new DateTime(2012, 12, 1)); var to = new DateTimeOffset(new DateTime(2013, 1, 20)); var chain = new Chain(new StreamObjectStream <ChainChange>(File.Open("MainChain.dat", FileMode.OpenOrCreate))); var blocks = chain.ToEnumerable(false) .SkipWhile(c => c.Header.BlockTime < from) .TakeWhile(c => c.Header.BlockTime < to) .ToArray(); //index.ReIndex(); //Console.WriteLine(""); BlockStore store = new BlockStore("E:\\Bitcoin\\blocks", Network.Main); IndexedBlockStore index = new IndexedBlockStore(new SQLiteNoSqlRepository("PlayIndex"), store); var target = Money.Parse("1100"); var margin = Money.Parse("1"); var dest = Network.CreateFromBase58Data <BitcoinAddress>("1FrtkNXastDoMAaorowys27AKQERxgmZjY"); var en = new CultureInfo("en-US"); FileStream fs = File.Open("logs", FileMode.Create); var writer = new StreamWriter(fs); writer.WriteLine("time,height,txid,value"); var lines = from header in blocks let block = index.Get(header.HashBlock) from tx in block.Transactions from txout in tx.Outputs //where txout.ScriptPubKey.GetDestination() == dest.ID where target - margin < txout.Value && txout.Value < target + margin select new { Block = block, Height = header.Height, Transaction = tx, TxOut = txout }; foreach (var line in lines) { writer.WriteLine( line.Block.Header.BlockTime.ToString(en) + "," + line.Height + "," + line.Transaction.GetHash() + "," + line.TxOut.Value.ToString()); } writer.Flush(); Process.Start(@"E:\Chocolatey\lib\notepadplusplus.commandline.6.4.5\tools\notepad++.exe", "\"" + new FileInfo(fs.Name).FullName + "\""); //foreach(var b in blocks) //{ // var block = index.Get(b.HashBlock); // foreach(var tx in block.Transactions) // { // foreach(var txout in tx.Outputs) // { // var pa = new PayToPubkeyHashTemplate() // .ExtractScriptPubKeyParameters(txout.ScriptPubKey); // if(pa != null && pa == dest.ID) // { // if(target - margin < txout.Value && txout.Value < target + margin) // { // writer.WriteLine(b.Header.BlockTime.ToString(en) + "," + b.Height + "," + tx.GetHash() + "," + txout.Value.ToString()); // } // } // } // } //} //They were purchased anonymously at a restaurant halfway between Sandwich, IL, and Chicago, in Naperville, IL, about the second week of December, 2012. Taxing my memory, the total was 1,132 + x(single digit).xxx... and immediately put in a freshly created InstaWallet bitcoin wallet. A couple/three days later I split the three accounts up into 1,000, 132, and x.xxx... wallets, always using InstaWallet while in my possession. Hope that helps with your plans. //var txt = File.ReadAllText("data/difficultyhistory.csv"); //var lines = txt.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries); //StringBuilder builder = new StringBuilder(); //foreach(var line in lines) //{ // var fields = line.Split(','); // builder.AppendLine(fields[0] + "," + fields[2]); //} //File.WriteAllText("targethistory.csv",builder.ToString()); ////PlaySplit(); //var oo = new PubKey(TestUtils.ParseHex("02bbc9fccbe03de928fc66fcd176fbe69d3641677970c6f8d558aa72f72e35e0cb")).GetAddress(Network.TestNet); //RPCClient client = RPCClientTests.CreateRPCClient(); ////https://tpfaucet.appspot.com/ //var scan = new Key(TestUtils.ParseHex("cc411aab02edcd3bccf484a9ba5280d4a774e6f81eac8ebec9cb1c2e8f73020a")); //var addr = new BitcoinStealthAddress("waPYjXyrTrvXjZHmMGdqs9YTegpRDpx97H5G3xqLehkgyrrZKsxGCmnwKexpZjXTCskUWwYywdUvrZK7L2vejeVZSYHVns61gm8VfU", Network.TestNet); //var sender = new BitcoinSecret("cRjSUV1LqN2F8MsGnLE2JKfCP75kbWGFRroNQeXHC429jqVFgmW3", Network.TestNet); //using(var fs = File.Open("Data.txt", FileMode.Create)) //{ // StreamWriter writer = new StreamWriter(fs); // foreach(var c in client.ListUnspent()) // { // var ephem = new Key(); // writer.WriteLine("---"); // writer.WriteLine("Ephem Private key : " + Encoders.Hex.EncodeData(ephem.ToBytes())); // Transaction tx = new Transaction(); // tx.Version = 1; // tx.AddInput(new TxIn(c.OutPoint)); // var pay = addr.CreatePayment(ephem); // writer.WriteLine("Metadata hash : " + pay.Metadata.Hash); // writer.WriteLine("Metadata script : " + pay.Metadata.Script); // writer.WriteLine("Metadata Nonce : " + pay.Metadata.Nonce); // writer.WriteLine("Metadata Ephem Key : " + pay.Metadata.EphemKey.ToHex()); // pay.AddToTransaction(tx, c.Amount); // tx.SignAll(sender); // client.SendRawTransaction(tx); // fs.Flush(); // } //} //var p = new PubKey(Encoders.Hex.DecodeData("03b4e5d3cf889840c75f0dd02ebda946151bf37e56cb888c6002c2ae5288e56de7")); //var o = Network.CreateFromBase58Data("mvXf4sF4C1w5KgQyasbEWxqVyqbLNtVdnY"); //var sender = new BitcoinSecret("cRjSUV1LqN2F8MsGnLE2JKfCP75kbWGFRroNQeXHC429jqVFgmW3", Network.TestNet).Key; ////var addr = secret.Key.PubKey.GetAddress(Network.TestNet); //mwdJkHRNJi1fEwHBx6ikWFFuo2rLBdri2h // //var receiver = new BitcoinStealthAddress("waPV5rHToBq3NoR7y5J9UdE7aUbuqJybNpE88Dve7WgWhEfvMrcuaSvF6tSQ3Fbe8dErL6ks8byJPcp3QCK2HHviGCSjg42VgMAPJb", Network.TestNet); //Key ephemKey = new Key(Encoders.Hex.DecodeData("9daed68ad37754305e82740a6252cf80765c36d29a55158b1a19ed29914f0cb1")); //var ephemKeyStr = Encoders.Hex.EncodeData(ephemKey.ToBytes()); //var scanStr = Encoders.Hex.EncodeData(receiver.ScanPubKey.ToBytes()); //var spendStr = Encoders.Hex.EncodeData(receiver.SpendPubKeys[0].ToBytes()); //var payment = receiver.CreatePayment(ephemKey); //var tx = new Transaction(); //tx.Version = 1; //tx.Inputs.Add(new TxIn(new OutPoint(new uint256("d65e2274f6fde9515a35655d54e79243d5a17355f6943d6c16a63083a8769ea3"), 1))); //payment.AddToTransaction(tx, Money.Parse("0.51")); //tx.Inputs[0].ScriptSig = new PayToPubkeyHashTemplate().GenerateScriptPubKey(sender.PubKey.GetAddress(Network.TestNet)); //var hash = tx.Inputs[0].ScriptSig.SignatureHash(tx, 0, SigHash.All); //var sig = sender.Sign(hash); //tx.Inputs[0].ScriptSig = new PayToPubkeyHashTemplate().GenerateScriptSig(new TransactionSignature(sig, SigHash.All), sender.PubKey); //var result = Script.VerifyScript(tx.Inputs[0].ScriptSig, new Script("OP_DUP OP_HASH160 b0b594bb2d2ca509b817f27e9280f6471807af26 OP_EQUALVERIFY OP_CHECKSIG"), tx, 0); //var bytes = Encoders.Hex.EncodeData(tx.ToBytes()); //var client = new NodeServer(Network.TestNet); //var node = client.GetNodeByEndpoint(new IPEndPoint(IPAddress.Parse("95.85.39.28"), 18333)); //node.VersionHandshake(); //node.SendMessage(new TxPayload(tx)); ////var store = new BlockStore(@"E:\Bitcoin\blocks", Network.Main); ////foreach(var un in store.EnumerateFolder()) ////{ // var expectedSize = un.Header.ItemSize; // var actualSize = un.Item.GetSerializedSize(); //} //var test = new IndexedBlockStore(new SQLiteNoSqlRepository("Play", true), new BlockStore(@"E:\Bitcoin\blocks", Network.Main)); //test.ReIndex(); //var i = 0; //Stopwatch watch = new Stopwatch(); //watch.Start(); //foreach(var b in test.Store.Enumerate(false, new DiskBlockPosRange(new DiskBlockPos(137,0))).Take(144)) //{ // i++; //} //watch.Stop(); }
public Program() { _store = BlockStore.Create(MemoryMappedFile.CreateNew(Guid.NewGuid().ToString(), 10000)); }
private FileStore(BlockStore blocks) { _blocks = blocks; }
private static void InsertMultiThreaded(DirectoryInfo directory, DocumentStore store) { var queue = new BlockingCollection <Tuple <Block, List <Transaction> > >(500); var filesQueue = new BlockingCollection <FileInfo>(); var files = directory.EnumerateFiles("blk*.dat") .OrderByDescending(x => x.Name); foreach (var file in files) { filesQueue.Add(file); } var source = new CancellationTokenSource(); int processedRecords = 0; int insertedRecords = 0; var workers = new List <Task>(); for (int i = 0; i < 3; i++) { var worker = Task.Factory.StartNew(t => { var token = (CancellationToken)t; var bulkInsert = store.BulkInsert(); while (!token.IsCancellationRequested) { // We are waiting for no more than 5 seconds a time. if (!queue.TryTake(out var tuple)) { Thread.Sleep(250); continue; } bulkInsert.Store(tuple.Item1); foreach (Transaction tx in tuple.Item2) { bulkInsert.Store(tx); } Interlocked.Increment(ref insertedRecords); } }, source.Token); workers.Add(worker); } for (int i = 0; i < 10; i++) { var processor = Task.Factory.StartNew(t => { var token = (CancellationToken)t; while (!token.IsCancellationRequested) { // We are waiting for no more than 5 seconds a time. if (!filesQueue.TryTake(out var file)) { Thread.Sleep(250); continue; } Console.WriteLine(); Console.WriteLine($"Processing {file.Name}"); var blockStore = new BlockStore(file.DirectoryName, Network.Main); foreach (var blk in blockStore.EnumerateFile(file)) { if (blk.Item == null) { continue; } try { var blockModel = new Block(blk.Item); var transactions = new List <Transaction>(); foreach (var tx in blk.Item.Transactions) { if (tx == null) { continue; } transactions.Add(new Transaction(blk.Item, tx)); } queue.Add(new Tuple <Block, List <Transaction> >(blockModel, transactions), token); } catch (Exception e) { Console.WriteLine(e); } Interlocked.Increment(ref processedRecords); } } }, source.Token); workers.Add(processor); } while (true) { while (Console.KeyAvailable == false) { Console.Write($"\rProcessed: {processedRecords}, Inserted: {insertedRecords}, In-Queue: {processedRecords-insertedRecords}"); Thread.Sleep(2000); // Loop until input is entered. } var cki = Console.ReadKey(); if (cki.Key == ConsoleKey.Q) { break; } } source.Cancel(); Task.WaitAll(workers.ToArray()); }
public void TestInitialize() { _file = MemoryMappedFile.CreateNew(Guid.NewGuid().ToString(), 1000000); _store = BlockStore.Create(_file); }
private TimeSpan BenchmarkTemplate(Action<TxOut> act) { Stopwatch watch = new Stopwatch(); watch.Start(); BlockStore store = new BlockStore(@"E:\Bitcoin\blocks\", Network.Main); foreach(var txout in store.EnumerateFolder().Take(150000).SelectMany(o => o.Item.Transactions.SelectMany(t => t.Outputs))) { act(txout); } watch.Stop(); return watch.Elapsed; }
public void AssertThat_BlockStoreOpen_Throws_WhenGivenUninitializedFile() { BlockStore.Open(MemoryMappedFile.CreateNew(Guid.NewGuid().ToString(), 1000000)); }
private BufferStore(BlockStore blocks) { Blocks = blocks; }
public static void Play() { //1,132 //NodeServer server = new NodeServer(Network.Main); //server.RegisterPeerTableRepository(new SqLitePeerTableRepository("PeerCachePlay")); //server.BuildChain(new StreamObjectStream<ChainChange>(File.Open("MainChain.dat", FileMode.OpenOrCreate))); var from = new DateTimeOffset(new DateTime(2012, 12, 1)); var to = new DateTimeOffset(new DateTime(2013, 1, 20)); var chain = new Chain(new StreamObjectStream<ChainChange>(File.Open("MainChain.dat", FileMode.OpenOrCreate))); var blocks = chain.ToEnumerable(false) .SkipWhile(c => c.Header.BlockTime < from) .TakeWhile(c => c.Header.BlockTime < to) .ToArray(); //index.ReIndex(); //Console.WriteLine(""); BlockStore store = new BlockStore("E:\\Bitcoin\\blocks", Network.Main); IndexedBlockStore index = new IndexedBlockStore(new SQLiteNoSqlRepository("PlayIndex"), store); var target = Money.Parse("1100"); var margin = Money.Parse("1"); var dest = Network.CreateFromBase58Data<BitcoinAddress>("1FrtkNXastDoMAaorowys27AKQERxgmZjY"); var en = new CultureInfo("en-US"); FileStream fs = File.Open("logs", FileMode.Create); var writer = new StreamWriter(fs); writer.WriteLine("time,height,txid,value"); var lines = from header in blocks let block = index.Get(header.HashBlock) from tx in block.Transactions from txout in tx.Outputs //where txout.ScriptPubKey.GetDestination() == dest.ID where target - margin < txout.Value && txout.Value < target + margin select new { Block = block, Height = header.Height, Transaction = tx, TxOut = txout }; foreach(var line in lines) { writer.WriteLine( line.Block.Header.BlockTime.ToString(en) + "," + line.Height + "," + line.Transaction.GetHash() + "," + line.TxOut.Value.ToString()); } writer.Flush(); Process.Start(@"E:\Chocolatey\lib\notepadplusplus.commandline.6.4.5\tools\notepad++.exe", "\"" + new FileInfo(fs.Name).FullName + "\""); //foreach(var b in blocks) //{ // var block = index.Get(b.HashBlock); // foreach(var tx in block.Transactions) // { // foreach(var txout in tx.Outputs) // { // var pa = new PayToPubkeyHashTemplate() // .ExtractScriptPubKeyParameters(txout.ScriptPubKey); // if(pa != null && pa == dest.ID) // { // if(target - margin < txout.Value && txout.Value < target + margin) // { // writer.WriteLine(b.Header.BlockTime.ToString(en) + "," + b.Height + "," + tx.GetHash() + "," + txout.Value.ToString()); // } // } // } // } //} //They were purchased anonymously at a restaurant halfway between Sandwich, IL, and Chicago, in Naperville, IL, about the second week of December, 2012. Taxing my memory, the total was 1,132 + x(single digit).xxx... and immediately put in a freshly created InstaWallet bitcoin wallet. A couple/three days later I split the three accounts up into 1,000, 132, and x.xxx... wallets, always using InstaWallet while in my possession. Hope that helps with your plans. //var txt = File.ReadAllText("data/difficultyhistory.csv"); //var lines = txt.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries); //StringBuilder builder = new StringBuilder(); //foreach(var line in lines) //{ // var fields = line.Split(','); // builder.AppendLine(fields[0] + "," + fields[2]); //} //File.WriteAllText("targethistory.csv",builder.ToString()); ////PlaySplit(); //var oo = new PubKey(TestUtils.ParseHex("02bbc9fccbe03de928fc66fcd176fbe69d3641677970c6f8d558aa72f72e35e0cb")).GetAddress(Network.TestNet); //RPCClient client = RPCClientTests.CreateRPCClient(); ////https://tpfaucet.appspot.com/ //var scan = new Key(TestUtils.ParseHex("cc411aab02edcd3bccf484a9ba5280d4a774e6f81eac8ebec9cb1c2e8f73020a")); //var addr = new BitcoinStealthAddress("waPYjXyrTrvXjZHmMGdqs9YTegpRDpx97H5G3xqLehkgyrrZKsxGCmnwKexpZjXTCskUWwYywdUvrZK7L2vejeVZSYHVns61gm8VfU", Network.TestNet); //var sender = new BitcoinSecret("cRjSUV1LqN2F8MsGnLE2JKfCP75kbWGFRroNQeXHC429jqVFgmW3", Network.TestNet); //using(var fs = File.Open("Data.txt", FileMode.Create)) //{ // StreamWriter writer = new StreamWriter(fs); // foreach(var c in client.ListUnspent()) // { // var ephem = new Key(); // writer.WriteLine("---"); // writer.WriteLine("Ephem Private key : " + Encoders.Hex.EncodeData(ephem.ToBytes())); // Transaction tx = new Transaction(); // tx.Version = 1; // tx.AddInput(new TxIn(c.OutPoint)); // var pay = addr.CreatePayment(ephem); // writer.WriteLine("Metadata hash : " + pay.Metadata.Hash); // writer.WriteLine("Metadata script : " + pay.Metadata.Script); // writer.WriteLine("Metadata Nonce : " + pay.Metadata.Nonce); // writer.WriteLine("Metadata Ephem Key : " + pay.Metadata.EphemKey.ToHex()); // pay.AddToTransaction(tx, c.Amount); // tx.SignAll(sender); // client.SendRawTransaction(tx); // fs.Flush(); // } //} //var p = new PubKey(Encoders.Hex.DecodeData("03b4e5d3cf889840c75f0dd02ebda946151bf37e56cb888c6002c2ae5288e56de7")); //var o = Network.CreateFromBase58Data("mvXf4sF4C1w5KgQyasbEWxqVyqbLNtVdnY"); //var sender = new BitcoinSecret("cRjSUV1LqN2F8MsGnLE2JKfCP75kbWGFRroNQeXHC429jqVFgmW3", Network.TestNet).Key; ////var addr = secret.Key.PubKey.GetAddress(Network.TestNet); //mwdJkHRNJi1fEwHBx6ikWFFuo2rLBdri2h // //var receiver = new BitcoinStealthAddress("waPV5rHToBq3NoR7y5J9UdE7aUbuqJybNpE88Dve7WgWhEfvMrcuaSvF6tSQ3Fbe8dErL6ks8byJPcp3QCK2HHviGCSjg42VgMAPJb", Network.TestNet); //Key ephemKey = new Key(Encoders.Hex.DecodeData("9daed68ad37754305e82740a6252cf80765c36d29a55158b1a19ed29914f0cb1")); //var ephemKeyStr = Encoders.Hex.EncodeData(ephemKey.ToBytes()); //var scanStr = Encoders.Hex.EncodeData(receiver.ScanPubKey.ToBytes()); //var spendStr = Encoders.Hex.EncodeData(receiver.SpendPubKeys[0].ToBytes()); //var payment = receiver.CreatePayment(ephemKey); //var tx = new Transaction(); //tx.Version = 1; //tx.Inputs.Add(new TxIn(new OutPoint(new uint256("d65e2274f6fde9515a35655d54e79243d5a17355f6943d6c16a63083a8769ea3"), 1))); //payment.AddToTransaction(tx, Money.Parse("0.51")); //tx.Inputs[0].ScriptSig = new PayToPubkeyHashTemplate().GenerateScriptPubKey(sender.PubKey.GetAddress(Network.TestNet)); //var hash = tx.Inputs[0].ScriptSig.SignatureHash(tx, 0, SigHash.All); //var sig = sender.Sign(hash); //tx.Inputs[0].ScriptSig = new PayToPubkeyHashTemplate().GenerateScriptSig(new TransactionSignature(sig, SigHash.All), sender.PubKey); //var result = Script.VerifyScript(tx.Inputs[0].ScriptSig, new Script("OP_DUP OP_HASH160 b0b594bb2d2ca509b817f27e9280f6471807af26 OP_EQUALVERIFY OP_CHECKSIG"), tx, 0); //var bytes = Encoders.Hex.EncodeData(tx.ToBytes()); //var client = new NodeServer(Network.TestNet); //var node = client.GetNodeByEndpoint(new IPEndPoint(IPAddress.Parse("95.85.39.28"), 18333)); //node.VersionHandshake(); //node.SendMessage(new TxPayload(tx)); ////var store = new BlockStore(@"E:\Bitcoin\blocks", Network.Main); ////foreach(var un in store.EnumerateFolder()) ////{ // var expectedSize = un.Header.ItemSize; // var actualSize = un.Item.GetSerializedSize(); //} //var test = new IndexedBlockStore(new SQLiteNoSqlRepository("Play", true), new BlockStore(@"E:\Bitcoin\blocks", Network.Main)); //test.ReIndex(); //var i = 0; //Stopwatch watch = new Stopwatch(); //watch.Start(); //foreach(var b in test.Store.Enumerate(false, new DiskBlockPosRange(new DiskBlockPos(137,0))).Take(144)) //{ // i++; //} //watch.Stop(); }
private Block PushStore(BlockStore store, Transaction tx, Block prev = null) { if(prev == null) prev = Network.Main.GetGenesis(); var b = new Block() { Header = { Nonce = RandomUtils.GetUInt32(), HashPrevBlock = prev.GetHash() }, Transactions = { tx } }; store.Append(b); return b; }
public Application() { this.block_store = this.db_manager.Block; this.services = new ServiceContainer(); }
//void HandleQueueAction(QueueAction action) async Task ConsumeAsync(ISourceBlock <QueueAction> source) { while (await source.OutputAvailableAsync()) { var action = source.Receive(); try { //((dynamic)this).Handle((dynamic)action); if (action is HandleBlockAction) { ((HandleBlockAction)action).SetResult(HandleBlock(action as HandleBlockAction)); } else if (action is GetActiveContractsAction) { ((GetActiveContractsAction)action).SetResult(GetActiveContracts()); } else if (action is GetContractPointedOutputsAction) { ((GetContractPointedOutputsAction)action).SetResult(GetContractPointedOutputs( ((GetContractPointedOutputsAction)action).ContractHash)); } else if (action is HandleOrphansOfTxAction) { HandleOrphansOfTransaction(action as HandleOrphansOfTxAction); } else if (action is GetIsContractActiveAction) { ((GetIsContractActiveAction)action).SetResult(IsContractActive( ((GetIsContractActiveAction)action).ContractHash)); } //else if (action is GetUTXOAction) //((GetUTXOAction)action).SetResult(GetUTXO(((GetUTXOAction)action).Outpoint, ((GetUTXOAction)action).IsInBlock)); else if (action is GetIsConfirmedUTXOExistAction) { var outpoint = ((GetIsConfirmedUTXOExistAction)action).Outpoint; ((GetIsConfirmedUTXOExistAction)action).SetResult(IsConfirmedUTXOExist(outpoint)); } else if (action is GetContractCodeAction) { ((GetContractCodeAction)action).SetResult(GetContractCode( ((GetContractCodeAction)action).ContractHash)); } else if (action is HandleTransactionAction) { ((HandleTransactionAction)action).SetResult(HandleTransaction(((HandleTransactionAction)action).Tx, ((HandleTransactionAction)action).CheckInDb)); } else if (action is GetBlockAction) { ((GetBlockAction)action).SetResult(GetBlock(((GetBlockAction)action).BkHash)); } else if (action is GetTxAction) { ((GetTxAction)action).SetResult(GetTransaction(((GetTxAction)action).TxHash)); } else if (action is ExecuteContractAction) { var executeContractAction = ((ExecuteContractAction)action); Types.Transaction tx; var result = AssembleAutoTx(executeContractAction.ContractHash, executeContractAction.Message, out tx, executeContractAction.Message != null); ((ExecuteContractAction)action).SetResult(new Tuple <bool, Types.Transaction>(result, tx)); } //TODO: remove else if (action is GetUTXOSetAction) { var getUTXOSetAction = (GetUTXOSetAction)action; HashDictionary <List <Types.Output> > txOutputs; HashDictionary <Types.Transaction> txs; GetUTXOSet(getUTXOSetAction.Predicate, out txOutputs, out txs); getUTXOSetAction.SetResult(new Tuple <HashDictionary <List <Types.Output> >, HashDictionary <Types.Transaction> >(txOutputs, txs)); } //TODO: rename else if (action is GetUTXOSetAction2) { using (var dbTx = _DBContext.GetTransactionContext()) { ((GetUTXOSetAction2)action).SetResult(UTXOStore.All(dbTx).ToList()); } } #if TEST else if (action is GetBlockLocationAction) { using (var dbTx = _DBContext.GetTransactionContext()) { ((GetBlockLocationAction)action).SetResult(BlockStore.GetLocation(dbTx, ((GetBlockLocationAction)action).Block)); } } #endif } catch (Exception e) { #if DEBUG BlockChainTrace.Error("Blockchain request handle got an exception.\n\nOriginal caller's stack:\n\n" + action.StackTrace + "\n\nException:\n\n", e); #else BlockChainTrace.Error("Blockchain request exception", e); #endif } } }
public void CheckBlockProofOfStake() { var totalblocks = 5000; // fill only a small portion so test wont be too long var mainStore = new BlockStore(TestDataLocations.BlockFolderLocation, Network.StratisMain); // create the stores BlockStore store = CreateBlockStore(); var index = 0; var blockStore = new NoSqlBlockRepository(); foreach (StoredBlock storedBlock in mainStore.Enumerate(false).Take(totalblocks)) { store.Append(storedBlock.Item); blockStore.PutAsync(storedBlock.Item); index++; } // build the chain ConcurrentChain chain = store.GetChain(); // fill the transaction store var trxStore = new NoSqlTransactionRepository(); var mapStore = new BlockTransactionMapStore(); foreach (ChainedBlock chainedBlock in chain.ToEnumerable(false).Take(totalblocks)) { Block block = blockStore.GetBlock(chainedBlock.HashBlock); foreach (Transaction blockTransaction in block.Transactions) { trxStore.Put(blockTransaction); mapStore.PutAsync(blockTransaction.GetHash(), block.GetHash()); } } RPCClient client = null; if (!pos_RPCClientTests.noClient) { client = new RPCClient(new NetworkCredential("rpcuser", "rpcpassword"), new Uri("http://127.0.0.1:" + Network.StratisMain.RPCPort), Network.StratisMain); } var stakeChain = new MemoryStakeChain(Network.StratisMain); // validate the stake trasnaction foreach (ChainedBlock item in chain.ToEnumerable(false).Take(totalblocks).ToList()) { Block block = blockStore.GetBlock(item.HashBlock); BlockStake blockStake; Assert.True(BlockValidator.CheckAndComputeStake(blockStore, trxStore, mapStore, stakeChain, chain, item, block, out blockStake)); stakeChain.Set(item.HashBlock, blockStake); if (item.Height == 1125) { var g = block.ToHex(); } if (client != null) { RPCBlock fetched = client.GetRPCBlockAsync(item.HashBlock).Result; Assert.Equal(uint256.Parse(fetched.modifierv2), blockStake.StakeModifierV2); Assert.Equal(uint256.Parse(fetched.proofhash), blockStake.HashProof); } } }
public void Load(string blockFolder) { var store = new BlockStore(blockFolder, this._Tester.Client.Configuration.Network); foreach (var block in store.Enumerate(false)) { SubmitBlock(block.Item); } }