// Command methods and proccess public static void GetInput() { while (true) { bool argumentFound = false; string argument = Console.ReadLine(); if (argument == "getbcinfo") { PrintChainInfo(); argumentFound = true; } if (argument == "setsecure") { string s = argument.Replace("setsecure", ""); s = s.Replace(" ", ""); if (s == "1") { SECURE_MODE = true; Console.WriteLine("Secure mode set to true."); } if (s == "0") { SECURE_MODE = true; Console.WriteLine("Secure mode set to false."); } argumentFound = true; } if (argument.Contains("gettidal")) { uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Console.WriteLine("Water height from harmonics at St-Nazaire : " + Tidal.GetTidalAtSpecificTime(unixTimestamp) + "meters"); argumentFound = true; } if (argument == "getcominfo") { arduino.GetPortReady(); argumentFound = true; } if (argument.Contains("setarduino")) { string s = argument.Replace("setarduino", ""); s = s.Replace(" ", ""); arduino.Initialize(s); argumentFound = true; } if (argument.Contains("setseapow")) { string s = argument.Replace("setseapow", ""); s = s.Replace(" ", ""); uint val = 0; if (uint.TryParse(s, out val)) { if (ValidYesOrNo("[Warning] If you change SEA_FORCE parameters, you should do it on all the network nodes...")) { float old_val = SEA_FORCE; SEA_FORCE = val; Console.WriteLine("Sea force successfully changed from " + old_val + " to " + SEA_FORCE); } } else { Print("bad arguments "); } argumentFound = true; } if (argument.Contains("setport")) { string s = argument.Replace("setport", ""); s = s.Replace(" ", ""); int port = 37; if (int.TryParse(s, out port)) { ACTUAL_PORT = port; Console.WriteLine("new port for data request is " + ACTUAL_PORT); argumentFound = true; } else { Print("bad arguments "); } argumentFound = true; } if (argument.Contains("setbrcparam")) { string maxArgs = GetStringAfterArgs(argument, "max:"); string fbcArgs = GetStringAfterArgs(argument, "fbc:"); uint max = 0; uint fbc = 0; if (uint.TryParse(maxArgs, out max)) { BROADCAST_BLOCKS_LIMIT = max; Console.WriteLine("new broadcast blocks max limit : " + BROADCAST_BLOCKS_LIMIT); } if (uint.TryParse(fbcArgs, out fbc)) { BROADCAST_FULL_BLOCKCHAIN_CLOCK = fbc; Console.WriteLine("full blockchain will be broadcast every " + BROADCAST_FULL_BLOCKCHAIN_CLOCK + " minutes. "); } argumentFound = true; } if (argument == "netconnect") { uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMinutes; LATEST_FBROADCAST_TICK = unixTimestamp; NT = new network(); NT.Initialize(); argumentFound = true; } if (argument == "nettest 1") { if (NT != null) { NT.BroadcastFile(GetLatestBlockChainFilePath(), 1); } else { Console.WriteLine("You're offline. Please use netconnect to get online. "); } argumentFound = true; } if (argument.Contains("nettest 2")) { if (NT != null) { string s = argument.Replace("nettest 2", ""); s = s.Replace(" ", ""); string[] args = s.Split(':'); if (args.Length == 2) { uint start = 0; uint end = 0; if (uint.TryParse(args[0], out start) && uint.TryParse(args[1], out end)) { Console.WriteLine("sending from " + start + " to " + end); NT.BroadcastBlockchain(start, end); } } else { Console.WriteLine("wrong args"); } } else { Console.WriteLine("You're offline. Please use netconnect to get online. "); } argumentFound = true; } if (argument == "nettest 3") { if (NT != null) { NT.BroadcastFile(_folderPath + "ptx", 2); } else { Console.WriteLine("You're offline. Please use netconnect to get online. "); } argumentFound = true; } if (argument.Contains("readblockascii")) { string s = argument.Replace("readblockascii", ""); s = s.Replace(" ", ""); uint index = 0; bool success = uint.TryParse(s, out index); if (success) { Block b = GetBlockAtIndex(index); if (b == null) { Console.WriteLine("Block can't be found."); } else { Console.WriteLine("[ block data ]"); PrintBlockInASCII(b); } argumentFound = true; } else { Console.WriteLine("invalid argument"); } argumentFound = true; } if (argument.Contains("getblockinfo")) { string s = argument.Replace("getblockinfo", ""); s = s.Replace(" ", ""); uint index = 0; bool success = uint.TryParse(s, out index); if (success) { Block b = GetBlockAtIndex(index); if (b == null) { Console.WriteLine("Block can't be found."); } else { Console.WriteLine("[ block data ]"); PrintBlockData(b); } argumentFound = true; } else { Console.WriteLine("invalid argument"); } argumentFound = true; } if (argument == "createwallet") { GenerateNewPairKey(); argumentFound = true; } if (argument.Contains("getutxo")) { string s = argument.Replace("getutxo", ""); s = s.Replace(" ", ""); uint index = 0; bool success = uint.TryParse(s, out index); if (success) { UTXO utxo = GetOfficialUTXOAtPointer(index); if (utxo == null) { Console.WriteLine("UTXO of this public Key not existing in UTXO Set "); } else { Console.WriteLine("UTXO Hash :" + SHAToHex(utxo.HashKey, false)); Console.WriteLine("UTXO Sold :" + utxo.Sold); Console.WriteLine("UTXO Token :" + utxo.TokenOfUniqueness); } argumentFound = true; } } if (argument.Contains("getutxop")) { string path = argument.Replace("location", AppDomain.CurrentDomain.BaseDirectory.Remove(AppDomain.CurrentDomain.BaseDirectory.Length - 1)); path = getfilePath(path.ToCharArray()); if (path.Length == 0) { Console.WriteLine("invalid argument. Please set path of your public key file in quote. "); } else { Console.WriteLine(path); if (File.Exists(path)) { byte[] pkeyHASH = ComputeSHA256(File.ReadAllBytes(path)); Console.WriteLine("result for hash key : " + SHAToHex(pkeyHASH, false)); uint myUTXOP = GetUTXOPointer(pkeyHASH); UTXO myUTXO = GetOfficialUTXOAtPointer(myUTXOP); if (myUTXO != null) { Console.WriteLine("UTXO Hash :" + SHAToHex(pkeyHASH, false)); Console.WriteLine("UTXO Pointer :" + myUTXOP); Console.WriteLine("UTXO Sold :" + myUTXO.Sold); Console.WriteLine("UTXO Token :" + myUTXO.TokenOfUniqueness); } else { Console.WriteLine("UTXO of this public Key not existing in UTXO Set "); } } else { Console.WriteLine("Path : " + path + " does not exist. Please set path of your public key file. "); } argumentFound = true; } } if (argument == "verifychain") { if (ValidYesOrNo("[WARNING] Verify blockchain can take a lot of time.")) { Console.WriteLine("Verifying Blockchain..."); if (isBlockChainValid()) { Console.WriteLine("Blockchain is Valid! "); } else { Console.WriteLine("Blockchain is not valid! "); } } argumentFound = true; } if (argument == "buildutxos") { if (ValidYesOrNo("[WARNING] It will delete current UTXO Set, then rebuilt it can take a lot of time.")) { Console.WriteLine("Please wait during UTXO Set writting..."); BuildUTXOSet(); Console.WriteLine("UTXO Set writting finished!"); } argumentFound = true; } if (argument == "initchain") { if (ValidYesOrNo("[WARNING] It will delete UTXO Set, blockchain files, PTX file and forks. You will be disconnected! ")) { NT = null; // we should close nt ClearAllFiles(); CheckFilesAtRoot(); PrintChainInfo(); } argumentFound = true; } if (argument.Contains("mine")) { //mine pkey:[pkeypath] utxop:[utxop] minlock:[] ntime:[] // find pkey argumentFound = true; string pkeyArgs = GetStringAfterArgs(argument, "pkey:", '\"'); string utxopArgs = GetStringAfterArgs(argument, "utxop:"); string minlockArgs = GetStringAfterArgs(argument, "minlock:"); string ntimeArgs = GetStringAfterArgs(argument, "ntime:"); string path = ""; uint utxopointer = 0; if (pkeyArgs.Length != 0) { path = pkeyArgs.Replace("location", AppDomain.CurrentDomain.BaseDirectory.Remove(AppDomain.CurrentDomain.BaseDirectory.Length - 1)); path = getfilePath(path.ToCharArray()); } else { // automatically detect if there is a key in folder string[] files = Directory.GetFiles(_folderPath, "publicKey", SearchOption.AllDirectories); if (files.Length > 0) { if (files.Length > 1) { Console.WriteLine("Multiple public keys have been found! "); if (ValidYesOrNo("This will automatically use " + files[0] + "...")) { path = files[0]; } } else { Console.WriteLine("Will use key " + files[0]); path = files[0]; } } else { Console.WriteLine("No public key found in directories and subdirectories. Please set a path in argument."); } } if (File.Exists(path)) { byte[] pkeyHASH = ComputeSHA256(File.ReadAllBytes(path)); bool Continue = true; if (utxopArgs.Length == 0) { if (!ValidYesOrNo("[WARNING] You didn't set an UTXO pointer.")) { Continue = false; } } else { uint parsePointer = 0; if (!uint.TryParse(utxopArgs, out parsePointer) && Continue) { if (!ValidYesOrNo("[WARNING] Incorrect UTXO pointer") && Continue) { Continue = false; } } else { UTXO utxo = GetOfficialUTXOAtPointer(parsePointer); if (utxo == null && Continue) { if (!ValidYesOrNo("[WARNING] Incorrect UTXO pointer")) { Continue = false; } } else { if (!utxo.HashKey.SequenceEqual(pkeyHASH) && Continue) { if (!ValidYesOrNo("[WARNING] Incorrect UTXO pointer")) { Continue = false; } } } if (parsePointer == 0 && Continue) { if (!ValidYesOrNo("[WARNING] Incorrect UTXO pointer")) { Continue = false; } } utxopointer = parsePointer; } } if (Continue) { uint mnlock = 5000; uint nTime = 0; if (minlockArgs.Length != 0) { uint.TryParse(minlockArgs, out mnlock); } if (ntimeArgs.Length != 0) { uint.TryParse(ntimeArgs, out nTime); } MYMINERPKEY = pkeyHASH; MYUTXOPOINTER = utxopointer; MAXLOCKTIMESETTING = mnlock; NTIMES = nTime; MININGENABLED = true; argumentFound = true; } } else { Console.WriteLine("Path : " + path + " does not exist. Please set path of your public key file. "); } } if (argument.Contains("newtx")) { //newtx sprkey: spukey: sutxop: amount: rpukey: rutxop: fee: lock: ( offset in sec since now we want to create the tx) while (argument.Contains("location")) { argument = argument.Replace("location", AppDomain.CurrentDomain.BaseDirectory.Remove(AppDomain.CurrentDomain.BaseDirectory.Length - 1)); } //string path = pkeyArgs.Replace("location", AppDomain.CurrentDomain.BaseDirectory); //path = getfilePath(path.ToCharArray()); string sprkeyArgs = getfilePath(GetStringAfterArgs(argument, "sprkey:", '\"').ToCharArray()); //< i use get path string spukeyArgs = getfilePath(GetStringAfterArgs(argument, "spukey:", '\"').ToCharArray()); string sutxopArgs = GetStringAfterArgs(argument, "sutxop:"); //< string amountArgs = GetStringAfterArgs(argument, "amount:"); string rpukeyArgs = getfilePath(GetStringAfterArgs(argument, "rpukey:", '\"').ToCharArray()); string feeArgs = GetStringAfterArgs(argument, "fee:"); string locktimeArgs = GetStringAfterArgs(argument, "lock:"); if (sprkeyArgs.Length == 0 || spukeyArgs.Length == 0 || sutxopArgs.Length == 0 || amountArgs.Length == 0 || rpukeyArgs.Length == 0 || feeArgs.Length == 0 || locktimeArgs.Length == 0) { Console.WriteLine("missing argument. see getcmdinfo. "); } else { uint sutxop = 0; uint amount = 0; uint rutxop = 0; uint fee = 0; uint locktime = 0; if (!File.Exists(sprkeyArgs) || !File.Exists(spukeyArgs) || !uint.TryParse(sutxopArgs, out sutxop) || !uint.TryParse(amountArgs, out amount) || !File.Exists(rpukeyArgs) || !uint.TryParse(feeArgs, out fee) || !uint.TryParse(locktimeArgs, out locktime)) { Console.WriteLine("invalid argument. see getcmdinfo. "); } else { SetUpTx(sprkeyArgs, spukeyArgs, sutxop, amount, rpukeyArgs, rutxop, fee, locktime); argumentFound = true; } } } if (argument.Contains("hideapoeminatransaction")) { //newtx sprkey: spukey: sutxop: amount: rpukey: rutxop: fee: lock: while (argument.Contains("location")) { argument = argument.Replace("location", AppDomain.CurrentDomain.BaseDirectory.Remove(AppDomain.CurrentDomain.BaseDirectory.Length - 1)); } //string path = pkeyArgs.Replace("location", AppDomain.CurrentDomain.BaseDirectory); //path = getfilePath(path.ToCharArray()); string sprkeyArgs = getfilePath(GetStringAfterArgs(argument, "sprkey:", '\"').ToCharArray()); //< i use get path string spukeyArgs = getfilePath(GetStringAfterArgs(argument, "spukey:", '\"').ToCharArray()); string sutxopArgs = GetStringAfterArgs(argument, "sutxop:"); //< string poem = getfilePath(GetStringAfterArgs(argument, "poem:", '\"').ToCharArray()); //string rpukeyArgs = getfilePath(GetStringAfterArgs(argument, "rpukey:", '\"').ToCharArray()); // string rutxopArgs = GetStringAfterArgs(argument, "rutxop:"); string feeArgs = GetStringAfterArgs(argument, "fee:"); string locktimeArgs = GetStringAfterArgs(argument, "lock:"); if (sprkeyArgs.Length == 0 || spukeyArgs.Length == 0 || sutxopArgs.Length == 0 || feeArgs.Length == 0 || locktimeArgs.Length == 0 || poem.Length == 0) { Console.WriteLine("missing argument. see getcmdinfo. "); } else { uint sutxop = 0; uint rutxop = 0; uint fee = 0; uint locktime = 0; if (!File.Exists(sprkeyArgs) || !File.Exists(spukeyArgs) || !uint.TryParse(sutxopArgs, out sutxop) || !uint.TryParse(feeArgs, out fee) || !uint.TryParse(locktimeArgs, out locktime)) { Console.WriteLine("invalid argument. see getcmdinfo. "); } else { //SetUpTx(sprkeyArgs, spukeyArgs, sutxop, amount, rpukeyArgs, rutxop, fee, locktime); Console.WriteLine("poem is " + poem); XYPoem.BuildandHidePoemsInPublicKeyHash(sprkeyArgs, spukeyArgs, sutxop, 0, poem, rutxop, fee, locktime); argumentFound = true; } } } if (argument.Contains("reqtx")) { argument = argument.Replace("reqtx", ""); argument = argument.Replace("location", AppDomain.CurrentDomain.BaseDirectory.Remove(AppDomain.CurrentDomain.BaseDirectory.Length - 1)); // enlever le dernier / ici ! string txPath = getfilePath(argument.ToCharArray()); if (File.Exists(txPath)) { PendingPTXFiles.Add(new Tuple <bool, string>(true, txPath)); argumentFound = true; } else { Console.WriteLine("Path : " + txPath + " does not exist. Please set path of your public key file. "); } } if (!argumentFound) { Console.WriteLine("invalid input. type getcmdinfo for command information. "); } if (argument.Contains("getcmdinfo")) { PrintArgumentInfo(); } Console.WriteLine(""); } }
// Main Thread of our blockchain static void Main(string[] args) { CheckFilesAtRoot(); VerifyFiles(); PrintArgumentInfo(); if (ValidYesOrNo("BimWatch configuration will start.")) { BIMWATCH.ConfigurePort(); } if (ValidYesOrNo("XY Poem configuration will start.")) { XYPoem.ConfigurePort(); } new Thread(() => { Thread.CurrentThread.IsBackground = true; GetInput(); }).Start(); /* * new Thread(() => * { * Thread.CurrentThread.IsBackground = true; * MagicKey(); * }).Start(); */ while (true) { try { // ------------- PART 1 for (int i = PendingDLBlocks.Count - 1; i >= 0; i--) { Print("will concatenate dl at " + i.ToString() + PendingDLBlocks[i]); PendingBlockFiles.Add(new Tuple <bool, string>(false, ConcatenateDL(PendingDLBlocks[i]))); Print(" we added " + PendingBlockFiles[i].Item2); PendingDLBlocks.RemoveAt(i); } for (int i = PendingDLTXs.Count - 1; i >= 0; i--) { PendingPTXFiles.Add(new Tuple <bool, string>(false, ConcatenateDL(PendingDLTXs[i]))); PendingDLTXs.RemoveAt(i); } // ------------- PART 2 for (int i = PendingBlockFiles.Count - 1; i >= 0; i--) // un bloc est relancé deux fois ... { ProccessTempBlocks(PendingBlockFiles[i].Item2, PendingBlockFiles[i].Item1); PendingBlockFiles.RemoveAt(i); // } for (int i = PendingPTXFiles.Count - 1; i >= 0; i--) { ProccessTempTXforPending(PendingPTXFiles[i].Item2, PendingPTXFiles[i].Item1); PendingPTXFiles.RemoveAt(i); } if (PendingBlockFiles.Count == 0 && PendingPTXFiles.Count == 0 && BroadcastQueue.Count > 0) { // clean files containing pID in dll folder... foreach (string s in dlRemovalQueue) { string[] files = Directory.GetFiles(Program._folderPath + "net"); foreach (string f in files) { if (f.Contains(s.ToString())) { File.Delete(f); } } Print("will delete files with " + s.ToString()); } dlRemovalQueue = new List <string>(); if (NT != null) { // first remove the extendedpeer if need to be cleaned ... for (int n = peerRemovalQueue.Count - 1; n >= 0; n--) { string ip = peerRemovalQueue[n]; for (int i = NT.mPeers.Count - 1; i >= 0; i--) { if (NT.mPeers[i].IP == ip) { NT.mPeers[i].Peer.Close(); string _ip = NT.mPeers[i].IP; Int32 Port = 13000; //.... NT.mPeers.RemoveAt(i); new Thread(() => { Thread.CurrentThread.IsBackground = true; NT.Connect(ip, Port); }).Start(); Print("peers " + _ip + " removed due to inactivity!"); peerRemovalQueue.RemoveAt(n); break; } } } bool _cS = false; foreach (network.ExtendedPeer ex in NT.mPeers) // just send to everyone normally ... { if (ex.currentlySending) { _cS = true; break; } } if (!_cS) { BroadCast(BroadcastQueue[0]); } else { //Console.WriteLine("currently sending"); } } else { PendingBlockFiles = new List <Tuple <bool, string> >(); PendingPTXFiles = new List <Tuple <bool, string> >(); BroadcastQueue = new List <BroadcastInfo>(); } } if (PendingBlockFiles.Count == 0 && BroadcastQueue.Count == 0 && MININGENABLED && NT != null) // only mine when there is no pendingblock. { if (NTIMES == 0) { Console.WriteLine("mining in finito"); // wait that pendingblockfiles are empty and broadcast is empty if (STOP_MINING) { STOP_MINING = false; MININGENABLED = false; } // thread this ... . string winblockPath = StartMining(MYMINERPKEY, MYUTXOPOINTER, MAXLOCKTIMESETTING, 1); // need to thread this stuff ... if (winblockPath.Length != 0) { PendingBlockFiles.Add(new Tuple <bool, string>(true, winblockPath)); } else { Console.WriteLine("le chemin d'acces au bloc miné a été vide... "); } } else { Console.WriteLine("start mining " + NTIMES + " times "); string winblockPath = StartMining(MYMINERPKEY, MYUTXOPOINTER, MAXLOCKTIMESETTING, NTIMES); if (winblockPath.Length != 0) { PendingBlockFiles.Add(new Tuple <bool, string>(true, winblockPath)); } else { Console.WriteLine("le chemin d'acces au bloc miné a été vide... "); } MININGENABLED = false; } } } catch (Exception e) { if (e is NullReferenceException || e is ArgumentOutOfRangeException || e is ArgumentNullException || e is FileNotFoundException) { if (SECURE_MODE) { RemoveAllConcatenateDL(); PendingDLBlocks = new List <string>(); PendingDLTXs = new List <string>(); PendingBlockFiles = new List <Tuple <bool, string> >(); PendingPTXFiles = new List <Tuple <bool, string> >(); BroadcastQueue = new List <BroadcastInfo>(); peerRemovalQueue = new List <string>(); dlRemovalQueue = new List <string>(); Console.WriteLine(e.ToString()); } // remove net temp .... } else { Print(e.ToString()); return; } } } }