//UtxoManagerのテスト3 public static void Test6() { string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath); string ufadbPath = ufadb.GetPath(); if (File.Exists(ufadbPath)) File.Delete(ufadbPath); UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath); string ufpdbPath = ufpdb.GetPath(); if (File.Exists(ufpdbPath)) File.Delete(ufpdbPath); UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath); string ufptempdbPath = ufptempdb.GetPath(); if (File.Exists(ufptempdbPath)) File.Delete(ufptempdbPath); UtxoDB utxodb = new UtxoDB(basepath); string utxodbPath = utxodb.GetPath(); if (File.Exists(utxodbPath)) File.Delete(utxodbPath); UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); int length = 100; Sha256Ripemd160Hash[] addrs = new Sha256Ripemd160Hash[length]; long[] bis = new long[length]; int[] tis = new int[length]; int[] tois = new int[length]; Creacoin[] cs = new Creacoin[length]; for (int i = 0; i < length; i++) { addrs[i] = new Sha256Ripemd160Hash(BitConverter.GetBytes(i)); bis[i] = 65536.RandomNum(); tis[i] = 65536.RandomNum(); tois[i] = 65536.RandomNum(); cs[i] = new Creacoin(65536.RandomNum()); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); utxodb.Open(); for (int i = 0; i < length; i++) utxom.AddUtxo(addrs[i], bis[i], tis[i], tois[i], cs[i]); utxom.SaveUFPTemp(); utxodb.Close(); stopwatch.Stop(); Console.WriteLine(string.Join(":", "test6_5", stopwatch.ElapsedMilliseconds.ToString() + "ms")); UtxoManager utxom2 = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); Utxo[] utxos = new Utxo[length]; stopwatch.Reset(); stopwatch.Start(); utxodb.Open(); for (int i = 0; i < length; i++) utxos[i] = utxom.FindUtxo(addrs[i], bis[i], tis[i], tois[i]); utxodb.Close(); stopwatch.Stop(); Console.WriteLine(string.Join(":", "test6_6", stopwatch.ElapsedMilliseconds.ToString() + "ms")); for (int i = 0; i < length; i++) { if (utxos[i].blockIndex != bis[i]) throw new Exception("test6_1"); if (utxos[i].txIndex != tis[i]) throw new Exception("test6_2"); if (utxos[i].txOutIndex != tois[i]) throw new Exception("test6_3"); if (utxos[i].amount.rawAmount != cs[i].rawAmount) throw new Exception("test6_4"); } }
//UtxoManagerのテスト5 public static void Test16() { string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath); string ufadbPath = ufadb.GetPath(); if (File.Exists(ufadbPath)) File.Delete(ufadbPath); UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath); string ufpdbPath = ufpdb.GetPath(); if (File.Exists(ufpdbPath)) File.Delete(ufpdbPath); UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath); string ufptempdbPath = ufptempdb.GetPath(); if (File.Exists(ufptempdbPath)) File.Delete(ufptempdbPath); UtxoDB utxodb = new UtxoDB(basepath); string utxodbPath = utxodb.GetPath(); if (File.Exists(utxodbPath)) File.Delete(utxodbPath); UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); BlockGenerator bg = new BlockGenerator(); Block[] blks = new Block[100]; BlockContext[] blkCons = new BlockContext[blks.Length]; for (int i = 0; i < blks.Length; i++) { blkCons[i] = bg.CreateNextValidBlock(); blks[i] = blkCons[i].block; Console.WriteLine("block" + i.ToString() + " created."); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < blks.Length; i++) { utxodb.Open(); utxom.ApplyBlock(blks[i], blkCons[i].prevTxOutss); utxom.SaveUFPTemp(); utxodb.Close(); } stopwatch.Stop(); Console.WriteLine(string.Join(":", "test16_1", stopwatch.ElapsedMilliseconds.ToString() + "ms")); Console.WriteLine("test16_succeeded"); }
//UtxoManagerのテスト1 public static void Test4() { string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath); string ufadbPath = ufadb.GetPath(); if (File.Exists(ufadbPath)) File.Delete(ufadbPath); UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath); string ufpdbPath = ufpdb.GetPath(); if (File.Exists(ufpdbPath)) File.Delete(ufpdbPath); UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath); string ufptempdbPath = ufptempdb.GetPath(); if (File.Exists(ufptempdbPath)) File.Delete(ufptempdbPath); UtxoDB utxodb = new UtxoDB(basepath); string utxodbPath = utxodb.GetPath(); if (File.Exists(utxodbPath)) File.Delete(utxodbPath); UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); if (File.Exists(ufadbPath)) throw new Exception("test4_9"); utxodb.Open(); Sha256Ripemd160Hash address1 = new Sha256Ripemd160Hash(new byte[] { 0 }); Sha256Ripemd160Hash address2 = new Sha256Ripemd160Hash(new byte[] { 1 }); Sha256Ripemd160Hash address3 = new Sha256Ripemd160Hash(new byte[] { 2 }); Utxo utxoNull = utxom.FindUtxo(address1, 65536.RandomNum(), 65536.RandomNum(), 65536.RandomNum()); if (utxoNull != null) throw new Exception("test4_1"); long bi1 = 65536.RandomNum(); int ti1 = 65536.RandomNum(); int toi1 = 65536.RandomNum(); Creacoin c1 = new Creacoin(65536.RandomNum()); long bi2 = 65536.RandomNum(); int ti2 = 65536.RandomNum(); int toi2 = 65536.RandomNum(); Creacoin c2 = new Creacoin(65536.RandomNum()); long bi3 = 65536.RandomNum(); int ti3 = 65536.RandomNum(); int toi3 = 65536.RandomNum(); Creacoin c3 = new Creacoin(65536.RandomNum()); utxom.AddUtxo(address1, bi1, ti1, toi1, c1); utxom.AddUtxo(address2, bi1, ti1, toi1, c1); utxom.AddUtxo(address3, bi1, ti1, toi1, c1); utxom.AddUtxo(address1, bi2, ti2, toi2, c2); utxom.AddUtxo(address2, bi2, ti2, toi2, c2); utxom.AddUtxo(address3, bi2, ti2, toi2, c2); utxom.AddUtxo(address1, bi3, ti3, toi3, c3); utxom.AddUtxo(address2, bi3, ti3, toi3, c3); utxom.AddUtxo(address3, bi3, ti3, toi3, c3); //2014/12/17追加 GetAllUtxosLatestFirstの試験 List<Utxo> utxos1 = utxom.GetAllUtxosLatestFirst(address1); List<Utxo> utxos2 = utxom.GetAllUtxosLatestFirst(address2); List<Utxo> utxos3 = utxom.GetAllUtxosLatestFirst(address3); if (utxos1.Count != 3) throw new Exception("test4_19"); if (utxos2.Count != 3) throw new Exception("test4_20"); if (utxos3.Count != 3) throw new Exception("test4_21"); long max = Math.Max(Math.Max(bi1, bi2), bi3); if (utxos1[0].blockIndex != max) throw new Exception("test4_22"); if (utxos2[0].blockIndex != max) throw new Exception("test4_23"); if (utxos3[0].blockIndex != max) throw new Exception("test4_24"); //2014/12/17追加(終) Utxo utxo1 = utxom.FindUtxo(address1, bi1, ti1, toi1); if (utxo1 == null) throw new Exception("test4_2"); if (utxo1.blockIndex != bi1) throw new Exception("test4_3"); if (utxo1.txIndex != ti1) throw new Exception("test4_4"); if (utxo1.txOutIndex != toi1) throw new Exception("test4_5"); if (utxo1.amount.rawAmount != c1.rawAmount) throw new Exception("test4_6"); utxom.AddUtxo(address1, bi1, ti1, toi1, c1); utxom.RemoveUtxo(address1, bi1, ti1, toi1); utxom.RemoveUtxo(address1, bi1, ti1, toi1); bool flag = false; try { utxom.RemoveUtxo(address1, bi1, ti1, toi1); } catch (InvalidOperationException) { flag = true; } if (!flag) throw new Exception("test4_7"); Utxo utxoNull2 = utxom.FindUtxo(address1, bi1, ti1, toi1); if (utxoNull2 != null) throw new Exception("test4_8"); utxom.SaveUFPTemp(); utxodb.Close(); if (!File.Exists(ufpdbPath)) throw new Exception("test4_9"); if (!File.Exists(ufptempdbPath)) throw new Exception("test4_10"); if (!File.Exists(utxodbPath)) throw new Exception("test4_11"); UtxoManager utxom2 = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); if (File.Exists(ufptempdbPath)) throw new Exception("test4_13"); utxodb.Open(); Utxo utxo2 = utxom.FindUtxo(address2, bi1, ti1, toi1); if (utxo2 == null) throw new Exception("test4_14"); if (utxo2.blockIndex != bi1) throw new Exception("test4_15"); if (utxo2.txIndex != ti1) throw new Exception("test4_16"); if (utxo2.txOutIndex != toi1) throw new Exception("test4_17"); if (utxo2.amount.rawAmount != c1.rawAmount) throw new Exception("test4_18"); utxodb.Close(); Console.WriteLine("test4_succeeded"); }
//UtxoManagerのテスト4 public static void Test15() { string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath); string ufadbPath = ufadb.GetPath(); if (File.Exists(ufadbPath)) File.Delete(ufadbPath); UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath); string ufpdbPath = ufpdb.GetPath(); if (File.Exists(ufpdbPath)) File.Delete(ufpdbPath); UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath); string ufptempdbPath = ufptempdb.GetPath(); if (File.Exists(ufptempdbPath)) File.Delete(ufptempdbPath); UtxoDB utxodb = new UtxoDB(basepath); string utxodbPath = utxodb.GetPath(); if (File.Exists(utxodbPath)) File.Delete(utxodbPath); UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb); BlockGenerator bg = new BlockGenerator(); Block[] blks = new Block[100]; BlockContext[] blkCons = new BlockContext[blks.Length]; for (int i = 0; i < blks.Length; i++) { blkCons[i] = bg.CreateNextValidBlock(); blks[i] = blkCons[i].block; Console.WriteLine("block" + i.ToString() + " created."); } for (int i = 0; i < blks.Length; i++) { utxodb.Open(); utxom.ApplyBlock(blks[i], blkCons[i].prevTxOutss); utxom.SaveUFPTemp(); utxodb.Close(); utxodb.Open(); foreach (var address in blkCons[i].unspentTxOuts.Keys) foreach (var toc in blkCons[i].unspentTxOuts[address]) if (utxom.FindUtxo(address, toc.bIndex, toc.txIndex, toc.txOutIndex) == null) throw new Exception("test15_1"); foreach (var address in blkCons[i].spentTxOuts.Keys) foreach (var toc in blkCons[i].spentTxOuts[address]) if (utxom.FindUtxo(address, toc.bIndex, toc.txIndex, toc.txOutIndex) != null) throw new Exception("test15_2"); utxodb.Close(); Console.WriteLine("block" + i.ToString() + " apply tested."); } for (int i = blks.Length - 1; i > 0; i--) { utxodb.Open(); utxom.RevertBlock(blks[i], blkCons[i].prevTxOutss); utxom.SaveUFPTemp(); utxodb.Close(); utxodb.Open(); foreach (var address in blkCons[i - 1].unspentTxOuts.Keys) foreach (var toc in blkCons[i - 1].unspentTxOuts[address]) if (utxom.FindUtxo(address, toc.bIndex, toc.txIndex, toc.txOutIndex) == null) throw new Exception("test15_3"); foreach (var address in blkCons[i - 1].spentTxOuts.Keys) foreach (var toc in blkCons[i - 1].spentTxOuts[address]) if (utxom.FindUtxo(address, toc.bIndex, toc.txIndex, toc.txOutIndex) != null) throw new Exception("test15_4"); utxodb.Close(); Console.WriteLine("block" + i.ToString() + " revert tested."); } Console.WriteLine("test15_succeeded"); }