-
Notifications
You must be signed in to change notification settings - Fork 8
/
blocks.js
165 lines (145 loc) Β· 5.56 KB
/
blocks.js
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
/*
*
* Blocks (Updated)
*
*/
// Import Required Modules
const bignum = require('bignum');
const util = require('./util.js');
// Import Required Modules
const Merkle = require('./merkle.js');
const Transactions = require('./transactions.js');
const algorithms = require('./algorithms.js');
const versionRolling = require('./stratum/version_rolling');
// BlockTemplate Main Function
const BlockTemplate = function (jobId, rpcData, extraNoncePlaceholder, options) {
// Establish Block Variables
this.rpcData = rpcData;
this.jobId = jobId;
this.submits = [];
// Calculate Block Target/Difficulty
this.target = rpcData.target ? bignum(rpcData.target, 16) : util.bignumFromBitsHex(rpcData.bits);
const { diff1 } = algorithms[options.coin.algorithm];
this.difficulty = parseFloat((diff1 / this.target.toNumber()).toFixed(9));
// Function to get Transaction Buffers
function getTransactionBuffers(txs) {
const txHashes = txs.map((tx) => {
if (tx.txid !== undefined) {
return util.uint256BufferFromHash(tx.txid);
}
return util.uint256BufferFromHash(tx.hash);
});
return [null].concat(txHashes);
}
// Create Generation Transaction
function createGeneration(rpcDataArg, extraNoncePlaceholderArg, optionsArg) {
const transactions = new Transactions();
switch (options.coin.algorithm) {
default:
return transactions.bitcoin(rpcDataArg, extraNoncePlaceholderArg, optionsArg);
}
}
// Establish Block Historical Hashes
this.prevHashReversed = util.reverseByteOrder(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');
if (rpcData.finalsaplingroothash) {
this.hashReserved = util.reverseBuffer(Buffer.from(rpcData.finalsaplingroothash, 'hex')).toString('hex');
} else {
this.hashReserved = '0000000000000000000000000000000000000000000000000000000000000000';
}
// Push Submissions to Array
this.registerSubmit = function (extraNonce1, extraNonce2, nTime, nonce, versionRollingBits) {
const submission = extraNonce1 + extraNonce2 + nTime + nonce + versionRollingBits;
if (this.submits.indexOf(submission) === -1) {
this.submits.push(submission);
return true;
}
return false;
};
// Establish Merkle Variables
this.merkleBranch = new Merkle(getTransactionBuffers(rpcData.transactions)).branch;
this.merkleBranchHex = this.merkleBranch.map((step) => step.toString('hex'));
// Structure Block Transaction Data
this.generation = createGeneration(rpcData, extraNoncePlaceholder, options);
this.transactions = Buffer.concat(rpcData.transactions.map((tx) => Buffer.from(tx.data, 'hex')));
// Serialize Block Coinbase
this.serializeCoinbase = function (extraNonce1, extraNonce2, optionsArg) {
switch (optionsArg.coin.algorithm) {
default:
return Buffer.concat([
this.generation[0],
extraNonce1,
extraNonce2,
this.generation[1],
]);
}
};
// Serialize Block Headers
this.serializeHeader = function (merkleRoot, nTime, nonce, version, optionsArg) {
const headerBuf = Buffer.alloc(80);
let position = 0;
switch (optionsArg.coin.algorithm) {
default:
headerBuf.write(nonce, position, 4, 'hex');
headerBuf.write(this.rpcData.bits, position += 4, 4, 'hex');
headerBuf.write(nTime, position += 4, 4, 'hex');
headerBuf.write(merkleRoot, position += 4, 32, 'hex');
headerBuf.write(this.rpcData.previousblockhash, position += 32, 32, 'hex');
headerBuf.writeUInt32BE(version, position + 32);
return util.reverseBuffer(headerBuf);
}
};
// Serialize Entire Block
this.serializeBlock = function (header, coinbase, optionsArg) {
switch (optionsArg.coin.algorithm) {
default:
return Buffer.concat([
header,
util.varIntBuffer(this.rpcData.transactions.length + 1),
coinbase,
this.transactions,
Buffer.from([]),
]);
}
};
// Serialize and return the crafted block header, and also return
// a continuation function for constructing the full solution to submit if desired
this.startSolution = function (coinbaseBuffer, merkleRoot, nTime, nonce, versionRollingBits, optionsArg) {
const version = (this.rpcData.version & ~versionRolling.maxMaskBits) | versionRollingBits;
const headerBuffer = this.serializeHeader(merkleRoot, nTime, nonce, version, optionsArg);
const finishSolution = function () {
return this.serializeBlock(headerBuffer, coinbaseBuffer, optionsArg).toString('hex');
}.bind(this);
return [headerBuffer, finishSolution];
};
// Get Current Job Parameters
this.getJobParams = function (optionsArg) {
switch (optionsArg.coin.algorithm) {
default:
if (!this.jobParams) {
this.jobParams = [
this.jobId,
this.prevHashReversed,
this.generation[0].toString('hex'),
this.generation[1].toString('hex'),
this.merkleBranchHex,
util.packInt32BE(this.rpcData.version & ~versionRolling.maxMaskBits).toString('hex'),
this.rpcData.bits,
util.packUInt32BE(this.rpcData.curtime).toString('hex'),
true,
];
}
return this.jobParams;
}
};
this.hasSameParent = function (other) {
return this.rpcData.previousblockhash === other.previousblockhash;
};
this.hasSameDifficulty = function (other) {
return this.rpcData.bits === other.bits;
};
this.isMoreRecent = function (other) {
return this.rpcData.height > other.height;
};
};
// Export BlockTemplate
module.exports = BlockTemplate;