c# pow implementation work
This commit is contained in:
parent
843f1c1a8e
commit
02cdbc75ce
@ -12,6 +12,8 @@ namespace onionrpow_cli
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
|
||||||
|
int secondsSinceEpoch = (int)t.TotalSeconds;
|
||||||
using (Stream stdin = Console.OpenStandardInput())
|
using (Stream stdin = Console.OpenStandardInput())
|
||||||
{
|
{
|
||||||
var data = new List<byte>();
|
var data = new List<byte>();
|
||||||
@ -23,6 +25,8 @@ namespace onionrpow_cli
|
|||||||
}
|
}
|
||||||
onionrpow.OnionrPow.compute(data.ToArray(), 2);
|
onionrpow.OnionrPow.compute(data.ToArray(), 2);
|
||||||
}
|
}
|
||||||
|
TimeSpan t2 = DateTime.UtcNow - new DateTime(1970, 1, 1);
|
||||||
|
Console.WriteLine((int)t2.TotalSeconds - secondsSinceEpoch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using SHA3;
|
using SHA3;
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ namespace onionrpow
|
|||||||
{
|
{
|
||||||
public static void compute(byte[] data, int difficulty){
|
public static void compute(byte[] data, int difficulty){
|
||||||
using (var shaAlg = SHA3.Net.Sha3.Sha3256())
|
using (var shaAlg = SHA3.Net.Sha3.Sha3256())
|
||||||
|
//using (SHA256 shaAlg = SHA256.Create())
|
||||||
{
|
{
|
||||||
string stringData = Encoding.UTF8.GetString(data);
|
string stringData = Encoding.UTF8.GetString(data);
|
||||||
bool found = false;
|
bool found = false;
|
||||||
@ -54,41 +56,66 @@ namespace onionrpow
|
|||||||
block.n = new Random().Next(10000);
|
block.n = new Random().Next(10000);
|
||||||
block.c = 0;
|
block.c = 0;
|
||||||
|
|
||||||
|
metadataJson.Clear();
|
||||||
|
metadataJson.AddRange(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(block)));
|
||||||
|
int location = Encoding.UTF8.GetString(metadataJson.ToArray()).IndexOf("\"c\":");
|
||||||
|
|
||||||
|
var metadata1 = new List<byte>();
|
||||||
|
var metadata2 = new List<byte>();
|
||||||
|
var countKey = new List<byte>();
|
||||||
|
countKey.AddRange(Encoding.UTF8.GetBytes("\"c\":"));
|
||||||
|
|
||||||
|
bool afterNum = false;
|
||||||
|
for (int i = location + 4; i < metadataJson.Count; i++){
|
||||||
|
if (!afterNum && ((char) metadataJson[i]).Equals(',')){
|
||||||
|
afterNum = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (afterNum){
|
||||||
|
metadata2.Add(metadataJson[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < location; i++){
|
||||||
|
metadata1.Add(metadataJson[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var preCompiled = new List<byte>();
|
||||||
|
preCompiled.AddRange(metadata1);
|
||||||
|
preCompiled.AddRange(countKey);
|
||||||
|
int powCounter = 0;
|
||||||
|
|
||||||
var justDataArray = justData.ToArray();
|
var justDataArray = justData.ToArray();
|
||||||
justData.Clear();
|
justData.Clear();
|
||||||
var encoded = new List<byte>();
|
int difficultyCounter = 0;
|
||||||
int calculatedDifficulty = 0;
|
|
||||||
|
|
||||||
var nl = Encoding.UTF8.GetBytes("\n")[0];
|
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
encoded.Clear();
|
var compiled = preCompiled.ToList();
|
||||||
encoded.AddRange(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(block)));
|
compiled.AddRange(metadata1);
|
||||||
// TODO keep nl and dataarray in
|
compiled.AddRange(Encoding.UTF8.GetBytes(powCounter.ToString()));
|
||||||
encoded.Add(nl);
|
compiled.AddRange(metadata2);
|
||||||
encoded.AddRange(justDataArray);
|
compiled.AddRange(justDataArray);
|
||||||
var encodedArray = encoded.ToArray();
|
var hash = shaAlg.ComputeHash(compiled.ToArray());
|
||||||
|
foreach (byte b in hash){
|
||||||
calculatedDifficulty = 0;
|
if (b == 0){
|
||||||
|
difficultyCounter += 1;
|
||||||
foreach(char c in shaAlg.ComputeHash(encodedArray)){
|
if (difficultyCounter == difficulty){
|
||||||
if (c == 0){
|
Console.WriteLine(powCounter);
|
||||||
calculatedDifficulty += 1;
|
Console.WriteLine(BitConverter.ToString(hash));
|
||||||
if (calculatedDifficulty == difficulty){
|
|
||||||
Console.WriteLine(counter);
|
|
||||||
Console.WriteLine(Encoding.UTF8.GetString(encodedArray));
|
|
||||||
Console.WriteLine(BitConverter.ToString(shaAlg.ComputeHash(encodedArray)));
|
|
||||||
|
|
||||||
goto powDone;
|
goto powDone;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else{
|
difficultyCounter = 0;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//Console.WriteLine(powCounter);
|
||||||
block.c += 1;
|
powCounter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Console.WriteLine(location);
|
||||||
|
//Console.WriteLine(Encoding.UTF8.GetString(metadataJson.ToArray()));
|
||||||
}
|
}
|
||||||
powDone:;
|
powDone:;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user