From 60d6aa0510fdd9127da8db00444d17741df43aa2 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 23 Sep 2020 00:36:08 +0000 Subject: [PATCH] c# pow implementation work --- pow-csharp/onionrpow-cli/Program.cs | 2 +- pow-csharp/onionrpow/OnionrPow.cs | 48 +++++++++++++++++++++++++---- src/onionrproofs/subprocesspow.py | 2 ++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/pow-csharp/onionrpow-cli/Program.cs b/pow-csharp/onionrpow-cli/Program.cs index 9529fdda..cea68c97 100644 --- a/pow-csharp/onionrpow-cli/Program.cs +++ b/pow-csharp/onionrpow-cli/Program.cs @@ -21,7 +21,7 @@ namespace onionrpow_cli //stdout.Write(buffer, 0, bytes); data.AddRange(buffer); } - onionrpow.OnionrPow.compute(data.ToArray(), 1); + onionrpow.OnionrPow.compute(data.ToArray(), 2); } } } diff --git a/pow-csharp/onionrpow/OnionrPow.cs b/pow-csharp/onionrpow/OnionrPow.cs index 4dd6b8a2..fb992495 100644 --- a/pow-csharp/onionrpow/OnionrPow.cs +++ b/pow-csharp/onionrpow/OnionrPow.cs @@ -17,13 +17,12 @@ namespace onionrpow public string meta { get; set; } public string sig { get; set; } public string signer { get; set; } + + public int n; + public int c; public int time; - private List data; - - public void setData(List blockData){ - this.data = blockData; - } + //public List data { get; set; } } public class OnionrPow @@ -52,9 +51,46 @@ namespace onionrpow counter += 1; } Block block = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(metadataJson.ToArray())); - block.setData(justData); + block.n = new Random().Next(10000); + block.c = 0; + var justDataArray = justData.ToArray(); + justData.Clear(); + var encoded = new List(); + int calculatedDifficulty = 0; + + var nl = Encoding.UTF8.GetBytes("\n")[0]; + + while(true){ + encoded.Clear(); + encoded.AddRange(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(block))); + // TODO keep nl and dataarray in + encoded.Add(nl); + encoded.AddRange(justDataArray); + var encodedArray = encoded.ToArray(); + + calculatedDifficulty = 0; + + foreach(char c in shaAlg.ComputeHash(encodedArray)){ + if (c == 0){ + calculatedDifficulty += 1; + if (calculatedDifficulty == difficulty){ + Console.WriteLine(counter); + Console.WriteLine(Encoding.UTF8.GetString(encodedArray)); + Console.WriteLine(BitConverter.ToString(shaAlg.ComputeHash(encodedArray))); + + goto powDone; + } + } + else{ + break; + } + } + + block.c += 1; + } } + powDone:; } //b'{"meta":"{\\"ch\\":\\"global\\",\\"type\\":\\"brd\\"}","sig":"pR4qmKGGCdnyNyZRlhGfF9GC7bONCsEnY04lTfiVuTHexPJypOqmxe9iyDQQqdR+PB2gwWuNqGMs5O8\\/S\\/hsCA==","signer":"UO74AP5LGQFI7EJTN6NAVINIPU2XO2KA7CAS6KSWGWAY5XIB5SUA====","time":1600542238,"pow":300182}\nxcvxcvvxcxcv' } diff --git a/src/onionrproofs/subprocesspow.py b/src/onionrproofs/subprocesspow.py index 901f0ef4..8726bf1d 100755 --- a/src/onionrproofs/subprocesspow.py +++ b/src/onionrproofs/subprocesspow.py @@ -116,6 +116,7 @@ class SubprocessPOW: metadata['n'] = secrets.randbits(16) puzzle = self.puzzle difficulty = self.difficulty + start = time.time() while True: # Break if shutdown received @@ -135,5 +136,6 @@ class SubprocessPOW: token = bytesconverter.bytes_to_str(token) if puzzle == token[0:difficulty]: pipe.send(payload) + print(metadata['pow'], time.time() - start) break nonce += 1