c# pow implementation work
This commit is contained in:
		
							parent
							
								
									e9dbc9fa09
								
							
						
					
					
						commit
						60d6aa0510
					
				
					 3 changed files with 45 additions and 7 deletions
				
			
		|  | @ -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); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -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<byte> data; | ||||
| 
 | ||||
|         public void setData(List<byte> blockData){ | ||||
|             this.data = blockData; | ||||
|         } | ||||
|         //public List<byte> data { get; set; } | ||||
| 
 | ||||
|     } | ||||
|     public class OnionrPow | ||||
|  | @ -52,9 +51,46 @@ namespace onionrpow | |||
|                     counter += 1; | ||||
|                 } | ||||
|                 Block block = JsonConvert.DeserializeObject<Block>(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<byte>(); | ||||
|                 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' | ||||
|     } | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue