protected void onRemoveSwap(string[] args) { if (args.Length != 1) { Console.WriteLine("Only source hahs necessary"); } var sourceHashStr = args.ElementAtOrDefault(0); if (string.IsNullOrEmpty(sourceHashStr)) { Console.WriteLine("SourceHash is null or empty!"); } var sourceHash = Hash.Parse(sourceHashStr); var InProgressTag = ".inprogress"; var storage = new KeyStoreStorage(_cli.Nexus.CreateKeyStoreAdapter("swaps")); var inProgressMap = new StorageMap(InProgressTag, storage); if (inProgressMap.ContainsKey(sourceHash)) { inProgressMap.Remove(sourceHash); Console.WriteLine($"SourceHash {sourceHash} has been removed from in progress swaps"); } else { Console.WriteLine($"Swap with sourceHash {sourceHash} not in progress"); } }
public void TestDBStorageVisitMap() { var storage = new KeyStoreStorage(CreateKeyStoreAdapterTest("test2")); var testMapKey = Encoding.UTF8.GetBytes($".test._valueMap"); var testMapKey2 = Encoding.UTF8.GetBytes($".test2._valueMap"); var testMap = new StorageMap(testMapKey, storage); var testMap2 = new StorageMap(testMapKey2, storage); testMap.Set("test1", "Value1"); testMap.Set("test2", "Value2"); testMap.Set("test3", "Value3"); testMap.Set("test4", "Value4"); testMap2.Set <BigInteger, string>(new BigInteger(1), "Value21"); testMap2.Set <BigInteger, string>(new BigInteger(2), "Value22"); testMap2.Set <BigInteger, string>(new BigInteger(3), "Value23"); testMap2.Set <BigInteger, string>(new BigInteger(4), "Value24"); var count = 0; testMap.Visit <string, string>((key, value) => { count++; }); testMap2.Visit <BigInteger, string>((key, value) => { count++; }); Assert.AreEqual(count, (int)testMap.Count() + testMap2.Count()); }
protected void onImportInProgress(string[] args) { if (args.Length != 1) { Console.WriteLine("File path needs to be given!"); } var InProgressTag = ".inprogress"; var storage = new KeyStoreStorage(_cli.Nexus.CreateKeyStoreAdapter("swaps")); var inProgressMap = new StorageMap(InProgressTag, storage); var filePath = args[0]; using (var reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); var hash = Hash.Parse(values[0]); if (!inProgressMap.ContainsKey <Hash>(hash)) { inProgressMap.Set <Hash, string>(hash, values[1]); } } } }
protected void onShowInProgress() { var InProgressTag = ".inprogress"; var storage = new KeyStoreStorage(_cli.Nexus.CreateKeyStoreAdapter("swaps")); var inProgressMap = new StorageMap(InProgressTag, storage); inProgressMap.Visit <Hash, string>((key, value) => { Console.WriteLine($"{key.ToString()},{value}"); }); }
public void TestDBChangeSetStorageMapClear() { var storage = new KeyStoreStorage(CreateKeyStoreAdapterTest("test2")); var changeSet = new StorageChangeSetContext(storage); var testMapKey = Encoding.UTF8.GetBytes($".test._valueMap"); var testMapKey2 = Encoding.UTF8.GetBytes($".test2._valueMap"); var testMap = new StorageMap(testMapKey, changeSet); var testMap2 = new StorageMap(testMapKey2, changeSet); testMap.Set("test1", "Value1"); testMap.Set("test2", "Value2"); testMap.Set("test3", "Value3"); testMap.Set("test4", "Value4"); Assert.IsTrue(testMap.Count() == 4); testMap2.Set <BigInteger, string>(new BigInteger(1), "Value21"); testMap2.Set <BigInteger, string>(new BigInteger(2), "Value22"); testMap2.Set <BigInteger, string>(new BigInteger(3), "Value23"); testMap2.Set <BigInteger, string>(new BigInteger(4), "Value24"); Assert.IsTrue(testMap2.Count() == 4); changeSet.Execute(); var count = 0; testMap.Visit <string, string>((key, value) => { count++; }); testMap2.Visit <BigInteger, string>((key, value) => { count++; }); Console.WriteLine($"visit: {count} count: {(int)testMap.Count() + testMap2.Count()}"); Assert.AreEqual(count, (int)testMap.Count() + testMap2.Count()); testMap.Clear(); testMap2.Clear(); Assert.IsTrue(testMap.Count() == 0); Assert.IsTrue(testMap2.Count() == 0); Assert.IsNull(testMap.Get <string, string>("test1")); Assert.IsNull(testMap.Get <string, string>("test2")); Assert.IsNull(testMap.Get <string, string>("test3")); Assert.IsNull(testMap.Get <string, string>("test4")); Assert.IsNull(testMap2.Get <BigInteger, string>(new BigInteger(1))); Assert.IsNull(testMap2.Get <BigInteger, string>(new BigInteger(2))); Assert.IsNull(testMap2.Get <BigInteger, string>(new BigInteger(3))); Assert.IsNull(testMap2.Get <BigInteger, string>(new BigInteger(4))); }
public void TestDBStorageMapClearEmpty() { var storage = new KeyStoreStorage(CreateKeyStoreAdapterTest("test2")); var testMapKey = Encoding.UTF8.GetBytes($".test._valueMap"); var testMap = new StorageMap(testMapKey, storage); Assert.IsTrue(testMap.Count() == 0); testMap.Clear(); Assert.IsTrue(testMap.Count() == 0); }
public void TestDBStorageAllValues() { var storage = new KeyStoreStorage(CreateKeyStoreAdapterTest("test4")); var testMapKey = Encoding.UTF8.GetBytes($".test._valueMap"); var testMapKey2 = Encoding.UTF8.GetBytes($".test2._valueMap"); var testMapKey3 = Encoding.UTF8.GetBytes($".test3._valueMap"); var testMap = new StorageMap(testMapKey, storage); var testMap2 = new StorageMap(testMapKey2, storage); var testMap3 = new StorageMap(testMapKey3, storage); testMap.Set("test1", "Value1"); testMap.Set("test2", "Value2"); testMap.Set("test3", "Value3"); testMap.Set("test4", "Value4"); var count = 0; foreach (var a in testMap.AllValues <string>()) { count++; var key = "test" + count; Assert.AreEqual(testMap.Get <string, string>(key), a); } testMap2.Set <BigInteger, string>(new BigInteger(1), "Value21"); testMap2.Set <BigInteger, string>(new BigInteger(2), "Value22"); testMap2.Set <BigInteger, string>(new BigInteger(3), "Value23"); testMap2.Set <BigInteger, string>(new BigInteger(4), "Value24"); count = 0; foreach (var a in testMap2.AllValues <string>()) { count++; var key = new BigInteger(count); Assert.AreEqual(testMap2.Get <BigInteger, string>(key), a); } testMap3.Set <BigInteger, BigInteger>(new BigInteger(1), new BigInteger(1)); testMap3.Set <BigInteger, BigInteger>(new BigInteger(2), new BigInteger(2)); testMap3.Set <BigInteger, BigInteger>(new BigInteger(3), new BigInteger(3)); testMap3.Set <BigInteger, BigInteger>(new BigInteger(4), new BigInteger(4)); count = 0; foreach (var a in testMap3.AllValues <BigInteger>()) { count++; var key = new BigInteger(count); Assert.AreEqual(testMap3.Get <BigInteger, BigInteger>(key), a); } }
public void TestDBStorageAllValuesEmpty() { var storage = new KeyStoreStorage(CreateKeyStoreAdapterTest("test3")); var testMapKey = Encoding.UTF8.GetBytes($".test._valueMap"); var testMap = new StorageMap(testMapKey, storage); Assert.IsTrue(testMap.AllValues <BigInteger>().Length == 0); testMap.Set(1, 1); testMap.Clear(); Assert.IsTrue(testMap.AllValues <BigInteger>().Length == 0); testMap.Set(1, 1); testMap.Set(2, 2); testMap.Set(3, 2); Assert.IsTrue(testMap.AllValues <BigInteger>().Length == 3); }
protected void onExportInProgress(string[] args) { if (args.Length != 1) { Console.WriteLine("File path needs to be given!"); } var filePath = args[0]; var InProgressTag = ".inprogress"; var storage = new KeyStoreStorage(_cli.Nexus.CreateKeyStoreAdapter("swaps")); var inProgressMap = new StorageMap(InProgressTag, storage); var csv = new StringBuilder(); inProgressMap.Visit <Hash, string>((key, value) => { var line = $"{key.ToString()},{value}"; csv.AppendLine(line); }); System.IO.File.WriteAllText(filePath, csv.ToString()); }
static void DumpBalances(string symbol, int decimals) { var prefix = BalanceSheet.MakePrefix(symbol); var storage = new KeyStoreStorage(store); var stakeMapKey = GetKeyForField <StakeContract>("_stakes"); var stakeMap = new StorageMap(stakeMapKey, storage); var stakeCount = stakeMap.Count(); var addresses = new HashSet <Address>(); //Console.WriteLine($"************ BALANCE LIST FOR {symbol} ************"); decimal total = 0; store.Visit((key, value) => { if (HasPrefix(prefix, key)) { var bytes = new byte[key.Length - prefix.Length]; ByteArrayUtils.CopyBytes(key, prefix.Length, bytes, 0, bytes.Length); var addr = Address.FromBytes(bytes); if (addr.IsSystem || addr.Text == "P2KFNXEbt65rQiWqogAzqkVGMqFirPmqPw8mQyxvRKsrXV8") { return; } BigInteger amount; if (value.Length > 0) { amount = BigInteger.FromSignedArray(value); } else { amount = 0; } /*if (symbol == DomainSettings.StakingTokenSymbol && stakeMap.ContainsKey<Address>(addr)) * { * var temp = stakeMap.Get<Address, EnergyStake>(addr); * amount += temp.stakeAmount; * }*/ addresses.Add(addr); string s; if (PrettyPrint) { var dec = UnitConversion.ToDecimal(amount, decimals); total += dec; s = dec.ToString(); } else { s = amount.ToString(); } Console.WriteLine($"{addr.Text},{symbol},{s}"); } }); /* * if (symbol == DomainSettings.StakingTokenSymbol) * { * var masterListKey = GetKeyForField<StakeContract>("_mastersList"); * var masterList = new StorageList(masterListKey, storage); * var masterCount = masterList.Count(); * for (int i = 0; i < masterCount; i++) * { * var master = masterList.Get<EnergyMaster>(i); * if (addresses.Contains(master.address)) * { * continue; * } * * var temp = stakeMap.Get<Address, EnergyAction>(master.address); * * string s; * * if (PrettyPrint) * { * var stake = UnitConversion.ToDecimal(temp.totalAmount, decimals); * s = stake.ToString(); * total += stake; * } * else * { * s = temp.totalAmount.ToString(); * } * * Console.WriteLine($"{master.address.Text},{symbol},{s}"); * } * }*/ if (PrettyPrint) { Console.WriteLine($"Total,{symbol},{total}"); } }
protected void OnConvertCommand(string[] args) { // TODO, could actually run in a background thread, with updates written out to console. // TODO2, not necessary, it's a one time thing... // TODO ugly quickfix, add additional command handler to support commands with multiple args string fileStoragePath = null; string dbStoragePath = null; string verificationPath = null; int includeArchives = 0; if (args.Length == 2) { fileStoragePath = args[0]; dbStoragePath = args[1]; } else if (args.Length == 3) { fileStoragePath = args[0]; dbStoragePath = args[1]; verificationPath = args[2]; } else if (args.Length == 4) { fileStoragePath = args[0]; dbStoragePath = args[1]; verificationPath = args[2]; includeArchives = Int32.Parse(args[3]); } Func <string, IKeyValueStoreAdapter> fileStorageFactory = (name) => new BasicDiskStore(fileStoragePath); Func <string, IKeyValueStoreAdapter> dbStorageFactory = (name) => new DBPartition(Spook.Logger, dbStoragePath); Func <string, IKeyValueStoreAdapter> verificationStorageFactory = null; if (!string.IsNullOrEmpty(verificationPath)) { verificationStorageFactory = (name) => new BasicDiskStore(verificationPath); } KeyValueStore <Hash, Archive> fileStorageArchives = null; if (includeArchives > 0) { fileStorageArchives = new KeyValueStore <Hash, Archive>(fileStorageFactory("archives")); } KeyValueStore <Hash, byte[]> fileStorageContents = new KeyValueStore <Hash, byte[]>(fileStorageFactory("contents")); KeyStoreStorage fileStorageRoot = new KeyStoreStorage(fileStorageFactory("chain.main")); KeyValueStore <Hash, Archive> dbStorageArchives = new KeyValueStore <Hash, Archive>(dbStorageFactory("archives")); KeyValueStore <Hash, byte[]> dbStorageContents = new KeyValueStore <Hash, byte[]>(dbStorageFactory("contents")); KeyStoreStorage dbStorageRoot = new KeyStoreStorage(dbStorageFactory("chain.main")); KeyValueStore <Hash, Archive> fileStorageArchiveVerify = new KeyValueStore <Hash, Archive>(verificationStorageFactory("archives.verify")); KeyValueStore <Hash, byte[]> fileStorageContentVerify = new KeyValueStore <Hash, byte[]>(verificationStorageFactory("contents.verify")); KeyStoreStorage fileStorageRootVerify = new KeyStoreStorage(verificationStorageFactory("chain.main.verify")); int count = 0; if (includeArchives > 0) { Spook.Logger.Message("Starting copying archives..."); fileStorageArchives.Visit((key, value) => { count++; dbStorageArchives.Set(key, value); var val = dbStorageArchives.Get(key); if (!CompareArchive(val, value)) { Spook.Logger.Message($"Archives: NewValue: {value.Hash} and oldValue: {val.Hash} differ, fail now!"); Environment.Exit(-1); } }); Spook.Logger.Message($"Finished copying {count} archives..."); count = 0; } Spook.Logger.Message("Starting copying content items..."); fileStorageContents.Visit((key, value) => { count++; dbStorageContents.Set(key, value); var val = dbStorageContents.Get(key); Spook.Logger.Message("COUNT: " + count); if (!CompareBA(val, value)) { Spook.Logger.Message($"CONTENTS: NewValue: {Encoding.UTF8.GetString(val)} and oldValue: {Encoding.UTF8.GetString(value)} differ, fail now!"); Environment.Exit(-1); } }); Spook.Logger.Message("Starting copying root..."); fileStorageRoot.Visit((key, value) => { count++; StorageKey stKey = new StorageKey(key); dbStorageRoot.Put(stKey, value); Spook.Logger.Message("COUNT: " + count); var val = dbStorageRoot.Get(stKey); if (!CompareBA(val, value)) { Spook.Logger.Message($"ROOT: NewValue: {Encoding.UTF8.GetString(val)} and oldValue: {Encoding.UTF8.GetString(value)} differ, fail now!"); Environment.Exit(-1); } }); Spook.Logger.Message($"Finished copying {count} root items..."); count = 0; if (!string.IsNullOrEmpty(verificationPath)) { Spook.Logger.Message($"Create verification stores"); if (includeArchives > 0) { Spook.Logger.Message("Start writing verify archives..."); dbStorageArchives.Visit((key, value) => { count++; // very ugly and might not always work, but should be ok for now byte[] bytes = value.Size.ToUnsignedByteArray(); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } int size = BitConverter.ToInt32(bytes, 0); var ms = new MemoryStream(new byte[size]); var bw = new BinaryWriter(ms); value.SerializeData(bw); fileStorageContentVerify.Set(key, ms.ToArray()); }); Spook.Logger.Message($"Finished writing {count} archives..."); count = 0; } Spook.Logger.Message("Start writing content items..."); dbStorageContents.Visit((key, value) => { count++; Spook.Logger.Message($"Content: {count}"); fileStorageContentVerify.Set(key, value); }); Spook.Logger.Message($"Finished writing {count} content items..."); count = 0; Spook.Logger.Message("Starting writing root..."); dbStorageRoot.Visit((key, value) => { count++; StorageKey stKey = new StorageKey(key); fileStorageRootVerify.Put(stKey, value); Spook.Logger.Message($"Wrote: {count}"); }); Spook.Logger.Message($"Finished writing {count} root items..."); } }