public void TestDBreezeInsertOrder() { using (NodeContext ctx = NodeContext.Create()) { using (var engine = new DBreeze.DBreezeEngine(ctx.FolderName + "/2")) { var data = new[] { new uint256(3), new uint256(2), new uint256(2439425), new uint256(5), new uint256(243945), new uint256(10), new uint256(Hashes.Hash256(new byte[0])), new uint256(Hashes.Hash256(new byte[] { 1 })), new uint256(Hashes.Hash256(new byte[] { 2 })), }; Array.Sort(data, new UInt256Comparer()); using (var tx = engine.GetTransaction()) { foreach (var d in data) { tx.Insert("Table", d.ToBytes(false), d.ToBytes()); } tx.Commit(); } var data2 = new uint256[data.Length]; using (var tx = engine.GetTransaction()) { int i = 0; foreach (var row in tx.SelectForward <byte[], byte[]>("Table")) { data2[i++] = new uint256(row.Key, false); } } Assert.True(data.SequenceEqual(data2)); } } }
public void TestDBreezeSerialization() { using (NodeContext ctx = NodeContext.Create()) { var genesis = ctx.Network.GetGenesis(); var genesisChainedBlock = new ChainedBlock(genesis.Header, ctx.Network.GenesisHash, 0); var chained = this.MakeNext(genesisChainedBlock, ctx.Network); ctx.PersistentCoinView.SaveChangesAsync(new UnspentOutputs[] { new UnspentOutputs(genesis.Transactions[0].GetHash(), new Coins(genesis.Transactions[0], 0)) }, null, genesisChainedBlock.HashBlock, chained.HashBlock).Wait(); Assert.NotNull(ctx.PersistentCoinView.FetchCoinsAsync(new[] { genesis.Transactions[0].GetHash() }).Result.UnspentOutputs[0]); Assert.Null(ctx.PersistentCoinView.FetchCoinsAsync(new[] { new uint256() }).Result.UnspentOutputs[0]); var previous = chained; chained = this.MakeNext(this.MakeNext(genesisChainedBlock, ctx.Network), ctx.Network); chained = this.MakeNext(this.MakeNext(genesisChainedBlock, ctx.Network), ctx.Network); ctx.PersistentCoinView.SaveChangesAsync(new UnspentOutputs[0], null, previous.HashBlock, chained.HashBlock).Wait(); Assert.Equal(chained.HashBlock, ctx.PersistentCoinView.GetBlockHashAsync().GetAwaiter().GetResult()); ctx.ReloadPersistentCoinView(); Assert.Equal(chained.HashBlock, ctx.PersistentCoinView.GetBlockHashAsync().GetAwaiter().GetResult()); Assert.NotNull(ctx.PersistentCoinView.FetchCoinsAsync(new[] { genesis.Transactions[0].GetHash() }).Result.UnspentOutputs[0]); Assert.Null(ctx.PersistentCoinView.FetchCoinsAsync(new[] { new uint256() }).Result.UnspentOutputs[0]); } }
public void CanRewind() { using (NodeContext ctx = NodeContext.Create()) { var cacheCoinView = new CachedCoinView(ctx.PersistentCoinView, DateTimeProvider.Default, this.loggerFactory); var tester = new CoinViewTester(cacheCoinView); var coins = tester.CreateCoins(5); var coin = tester.CreateCoins(1); // 1 var h1 = tester.NewBlock(); cacheCoinView.FlushAsync().Wait(); Assert.True(tester.Exists(coins[2])); Assert.True(tester.Exists(coin[0])); tester.Spend(coins[2]); tester.Spend(coin[0]); //2 tester.NewBlock(); //3 tester.NewBlock(); //4 var coin2 = tester.CreateCoins(1); tester.NewBlock(); Assert.True(tester.Exists(coins[0])); Assert.True(tester.Exists(coin2[0])); Assert.False(tester.Exists(coins[2])); Assert.False(tester.Exists(coin[0])); //1 tester.Rewind(); Assert.False(tester.Exists(coin2[0])); Assert.True(tester.Exists(coins[2])); Assert.True(tester.Exists(coin[0])); tester.Spend(coins[2]); tester.Spend(coin[0]); //2 var h2 = tester.NewBlock(); cacheCoinView.FlushAsync().Wait(); Assert.False(tester.Exists(coins[2])); Assert.False(tester.Exists(coin[0])); //1 Assert.True(h1 == tester.Rewind()); Assert.True(tester.Exists(coins[2])); Assert.True(tester.Exists(coin[0])); var coins2 = tester.CreateCoins(7); tester.Spend(coins2[0]); coin2 = tester.CreateCoins(1); tester.Spend(coin2[0]); //2 tester.NewBlock(); Assert.True(tester.Exists(coins2[1])); Assert.False(tester.Exists(coins2[0])); cacheCoinView.FlushAsync().Wait(); //3 tester.NewBlock(); //2 tester.Rewind(); Assert.True(tester.Exists(coins2[1])); Assert.False(tester.Exists(coins2[0])); Assert.False(tester.Exists(coin2[0])); Assert.True(tester.Exists(coins[2])); Assert.True(tester.Exists(coin[0])); } }