-
Notifications
You must be signed in to change notification settings - Fork 497
/
Share.cs
172 lines (150 loc) · 7.16 KB
/
Share.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#region License
//
// CoiniumServ - Crypto Currency Mining Pool Server Software
// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org
// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For the terms of this license, see licenses/gpl_v3.txt.
//
// Alternatively, you can license this software under a commercial
// license or white-label it as set out in licenses/commercial.txt.
//
#endregion
using System;
using CoiniumServ.Algorithms;
using CoiniumServ.Coin.Coinbase;
using CoiniumServ.Cryptology;
using CoiniumServ.Daemon.Responses;
using CoiniumServ.Jobs;
using CoiniumServ.Mining;
using CoiniumServ.Server.Mining.Stratum;
using CoiniumServ.Utils.Extensions;
using CoiniumServ.Utils.Helpers;
using CoiniumServ.Utils.Numerics;
namespace CoiniumServ.Shares
{
public class Share : IShare
{
public bool IsValid { get { return Error == ShareError.None; } }
public bool IsBlockCandidate { get; private set; }
public Block Block { get; private set; }
public Transaction GenerationTransaction { get; private set; }
public bool IsBlockAccepted { get { return Block != null; } }
public IMiner Miner { get; private set; }
public ShareError Error { get; private set; }
public UInt64 JobId { get; private set; }
public IJob Job { get; private set; }
public int Height { get; private set; }
public UInt32 NTime { get; private set; }
public UInt32 Nonce { get; private set; }
public UInt32 ExtraNonce1 { get; private set; }
public UInt32 ExtraNonce2 { get; private set; }
public byte[] CoinbaseBuffer { get; private set; }
public Hash CoinbaseHash { get; private set; }
public byte[] MerkleRoot { get; private set; }
public byte[] HeaderBuffer { get; private set; }
public byte[] HeaderHash { get; private set; }
public BigInteger HeaderValue { get; private set; }
public Double Difficulty { get; private set; }
public double BlockDiffAdjusted { get; private set; }
public byte[] BlockHex { get; private set; }
public byte[] BlockHash { get; private set; }
public Share(IStratumMiner miner, UInt64 jobId, IJob job, string extraNonce2, string nTimeString, string nonceString)
{
Miner = miner;
JobId = jobId;
Job = job;
Error = ShareError.None;
var submitTime = TimeHelpers.NowInUnixTimestamp(); // time we recieved the share from miner.
if (Job == null)
{
Error = ShareError.JobNotFound;
return;
}
// check size of miner supplied extraNonce2
if (extraNonce2.Length/2 != ExtraNonce.ExpectedExtraNonce2Size)
{
Error = ShareError.IncorrectExtraNonce2Size;
return;
}
ExtraNonce2 = Convert.ToUInt32(extraNonce2, 16); // set extraNonce2 for the share.
// check size of miner supplied nTime.
if (nTimeString.Length != 8)
{
Error = ShareError.IncorrectNTimeSize;
return;
}
NTime = Convert.ToUInt32(nTimeString, 16); // read ntime for the share
// make sure NTime is within range.
if (NTime < job.BlockTemplate.CurTime || NTime > submitTime + 7200)
{
Error = ShareError.NTimeOutOfRange;
return;
}
// check size of miner supplied nonce.
if (nonceString.Length != 8)
{
Error = ShareError.IncorrectNonceSize;
return;
}
Nonce = Convert.ToUInt32(nonceString, 16); // nonce supplied by the miner for the share.
// set job supplied parameters.
Height = job.BlockTemplate.Height; // associated job's block height.
ExtraNonce1 = miner.ExtraNonce; // extra nonce1 assigned to miner.
// check for duplicate shares.
if (!Job.RegisterShare(this)) // try to register share with the job and see if it's duplicated or not.
{
Error = ShareError.DuplicateShare;
return;
}
// construct the coinbase.
CoinbaseBuffer = Serializers.SerializeCoinbase(Job, ExtraNonce1, ExtraNonce2);
CoinbaseHash = Coin.Coinbase.Utils.HashCoinbase(CoinbaseBuffer);
// create the merkle root.
MerkleRoot = Job.MerkleTree.WithFirst(CoinbaseHash).ReverseBuffer();
// create the block headers
HeaderBuffer = Serializers.SerializeHeader(Job, MerkleRoot, NTime, Nonce);
HeaderHash = Job.HashAlgorithm.Hash(HeaderBuffer);
HeaderValue = new BigInteger(HeaderHash);
// calculate the share difficulty
Difficulty = ((double)new BigRational(AlgorithmManager.Diff1, HeaderValue)) * Job.HashAlgorithm.Multiplier;
// calculate the block difficulty
BlockDiffAdjusted = Job.Difficulty * Job.HashAlgorithm.Multiplier;
// check if block candicate
if (Job.Target >= HeaderValue)
{
IsBlockCandidate = true;
BlockHex = Serializers.SerializeBlock(Job, HeaderBuffer, CoinbaseBuffer, miner.Pool.Config.Coin.Options.IsProofOfStakeHybrid);
BlockHash = HeaderBuffer.DoubleDigest().ReverseBuffer();
}
else
{
IsBlockCandidate = false;
BlockHash = HeaderBuffer.DoubleDigest().ReverseBuffer();
// Check if share difficulty reaches miner difficulty.
var lowDifficulty = Difficulty/miner.Difficulty < 0.99; // share difficulty should be equal or more then miner's target difficulty.
if (!lowDifficulty) // if share difficulty is high enough to match miner's current difficulty.
return; // just accept the share.
if (Difficulty >= miner.PreviousDifficulty) // if the difficulty matches miner's previous difficulty before the last vardiff triggered difficulty change
return; // still accept the share.
// if the share difficulty can't match miner's current difficulty or previous difficulty
Error = ShareError.LowDifficultyShare; // then just reject the share with low difficult share error.
}
}
public void SetFoundBlock(Block block, Transaction genTx)
{
Block = block;
GenerationTransaction = genTx;
}
}
}