/
BlockHeader.java
371 lines (300 loc) · 13.5 KB
/
BlockHeader.java
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.norswap.nanoeth.blocks;
import com.norswap.nanoeth.Config;
import com.norswap.nanoeth.annotations.Nullable;
import com.norswap.nanoeth.receipts.BloomFilter;
import com.norswap.nanoeth.data.Address;
import com.norswap.nanoeth.data.Hash;
import com.norswap.nanoeth.data.MerkleRoot;
import com.norswap.nanoeth.data.Natural;
import com.norswap.nanoeth.rlp.RLP;
import com.norswap.nanoeth.rlp.RLPLayoutable;
import com.norswap.nanoeth.rlp.RLPParsingException;
import com.norswap.nanoeth.utils.Hashing;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
import static com.norswap.nanoeth.blocks.BlockValidityStatus.*;
public final class BlockHeader implements RLPLayoutable {
// ---------------------------------------------------------------------------------------------
/**
* The {@link #hash} of the parent block.
* <p>Yellowpaper notation: Hp
*/
public final Hash parentHash;
// ---------------------------------------------------------------------------------------------
/**
* Hash of the the uncles (aka "ommers") list of the block.
* <p>Yellowpaper notation: Ho
*/
public final Hash uncleHash;
// ---------------------------------------------------------------------------------------------
/**
* Address that will receive the block reward (aka "beneficiary", aka "miner's address").
* <p>Yellowpaper notation: Hc
*/
public final Address coinbase;
// ---------------------------------------------------------------------------------------------
/**
* The Merkle root of the state tree.
* <p>Yellowpaper notation: Hr
*/
public final MerkleRoot stateRoot;
// ---------------------------------------------------------------------------------------------
/**
* The Merkle root of the transactions list of the block.
* <p>Yellowpaper notation: Ht
*/
public final MerkleRoot transactionsRoot;
// ---------------------------------------------------------------------------------------------
/**
* The Merkle root of the receipts list of the block.
* <p>Yellowpaper notation: He
*/
public final MerkleRoot receiptsRoot;
// ---------------------------------------------------------------------------------------------
/**
* The Bloom filter composed from indexable information (logger address and log topics)
* contained in each log entry from the receipt of each transaction in the transactions list.
* <p>Yellowpaper notation: Hb
*/
public final BloomFilter logsBloom;
// ---------------------------------------------------------------------------------------------
/**
* The difficult level of the block, calculated from the previous' block difficulty level and
* the {@link #timestamp} (cf. {@link Difficulty}).
*/
public final Natural difficulty;
// ---------------------------------------------------------------------------------------------
/**
* The number of ancestor blocks (aka "height"). The genesis block has number 0.
* <p>Yellowpaper notation: Hi
*/
public final Natural number;
// ---------------------------------------------------------------------------------------------
/**
* Current maximum amount of gas usable per block.
* <p>Yellowpaper notation: Hl
* <p>A block's miner can choose to update the gas limit incrementally (up or down) within the
* limit set by the protocol (cf. {@link #validate(BlockHeader)}).
*/
public final Natural gasLimit;
// ---------------------------------------------------------------------------------------------
/**
* The amount of gas used in this block.
* <p>Yellowpaper notation: Hg</p>
*/
public final Natural gasUsed;
// ---------------------------------------------------------------------------------------------
/**
* A value equal to the reasonable output of Unix’s time() at this block’s inception.
* <p>Yellowpaper notation: Hs
* <p>The yellowpaper puts a 256 bytes limit on this, but it doesn't seem to affect serialization.
*/
public final Natural timestamp;
// ---------------------------------------------------------------------------------------------
/**
* An arbitrary byte array containing data relevant to this block.
* This must be 32 bytes or fewer.
* <p>Yellowpaper notaion: Hx
*/
public final byte[] extraData;
// ---------------------------------------------------------------------------------------------
/**
* A hash which, combined with the {@link #nonce}, proves that a sufficient amount of
* computation has been carried out on this block.
* <p>Yellowpaper notation: Hm
*/
public final Hash mixHash;
// ---------------------------------------------------------------------------------------------
/**
* A 64-bit scalar value which, combined with the {@link #mixHash}, proves that a sufficient
* amount of computation has been carried out on this block.
*
* <p>Miners iterate the nonce value until they can find a {@link #mixHash} that satisfies
* the {@link #difficulty} requirement.
*
* <p>Yellowpaper notation: Hn
*
* <p>Note that unlike other scalar values, by like addresses, hashes, ... the nonce gets
* serialized in full, even if it has leading zeroes.
*/
public final long nonce;
// ---------------------------------------------------------------------------------------------
/**
* For hash caching, access via {@link #hash()}.
*/
private Hash hash;
// ---------------------------------------------------------------------------------------------
/** Constructs a header from header data, and computes the header hash from this data. */
public BlockHeader(
Hash parentHash,
Hash uncleHash,
Address coinbase,
MerkleRoot stateRoot,
MerkleRoot transactionsRoot,
MerkleRoot receiptsRoot,
BloomFilter logsBloom,
Natural difficulty,
Natural number,
Natural gasLimit,
Natural gasUsed,
Natural timestamp,
byte[] extraData,
Hash mixHash,
long nonce) {
this.parentHash = parentHash;
this.uncleHash = uncleHash;
this.coinbase = coinbase;
this.stateRoot = stateRoot;
this.transactionsRoot = transactionsRoot;
this.receiptsRoot = receiptsRoot;
this.logsBloom = logsBloom;
this.difficulty = difficulty;
this.number = number;
this.gasLimit = gasLimit;
this.gasUsed = gasUsed;
this.timestamp = timestamp;
this.extraData = extraData;
this.mixHash = mixHash;
this.nonce = nonce;
}
// ---------------------------------------------------------------------------------------------
/**
* Hash of all the other data in the header. This is not serialized in the block (but will
* be serialized in the children as {@link #parentHash}). This is computed lazily and cached.
*/
public Hash hash() {
return hash != null
? hash
: (hash = Hashing.keccak(rlpEncode()));
}
// ---------------------------------------------------------------------------------------------
/**
* Parses a RLP sequence into a block header.
*
* @throws com.norswap.nanoeth.rlp.RLPParsingException
* if the RLP sequence does not properly parse to a block header
*/
public static BlockHeader from (RLP rlp) throws RLPParsingException {
return BlockParser.parseHeader(rlp);
}
// ---------------------------------------------------------------------------------------------
/**
* Validates the block header against its parent (automatically retrieved from {@link Blocks#DB}).
*
* @return {@link BlockValidityStatus#VAL_VALID} if the header is valid, or a {@link BlockValidityStatus}
* value that indicates the reason for the failure.
*
* @see Block#validate() for full block validation, including running the transactions.
*/
public BlockValidityStatus validate() {
return validate(Blocks.DB.getHeader(parentHash));
}
// ---------------------------------------------------------------------------------------------
/**
* Validates the block header against its parent. The parent is expected to have been
* retrieve from this header's {@link #parentHash} (can be null for the genesis block).
*
* @return {@link BlockValidityStatus#VAL_VALID} if the header is valid, or a {@link BlockValidityStatus}
* value that indicates the reason for the failure.
*
* @see Block#validate() for full block validation, including running the transactions.
*/
public BlockValidityStatus validate (@Nullable BlockHeader parent) {
if (parent == null)
return this.equals(Config.GENESIS.header)
? VAL_VALID
: VAL_UNKNOWN_PARENT;
// We assume that this is correct, as we use the hash in the header to retrieve the parent.
assert parentHash.equals(parent.hash());
if (timestamp.compareTo(parent.timestamp) <= 0)
return VAL_OUTDATED_TIMESTAMP;
if (!number.equals(parent.number.add(1)))
return VAL_BAD_NUMBER;
if (extraData.length > 32)
return VAL_EXTRA_DATA_TOO_LONG;
var gasLimitIncrement = parent.gasLimit.divide(1024);
if (gasLimit.compareTo(parent.gasLimit.add(gasLimitIncrement)) > 0)
return VAL_GAS_LIMIT_TOO_HIGH;
if (gasLimit.compareTo(parent.gasLimit.subtract(gasLimitIncrement)) < 0)
return VAL_GAS_LIMIT_TOO_LOW;
if (gasLimit.lower(5000)) // minimum gas limit
return VAL_GAS_LIMIT_TOO_LOW;
if (gasUsed.compareTo(gasLimit) > 0)
return VAL_GAS_USED_TOO_HIGH;
if (Config.VALIDATE_POW) {
if (!difficulty.equals(Difficulty.computeDifficulty(timestamp, parent)))
return VAL_BAD_DIFFICULTY;
var maxNonce = BigInteger.TWO.pow(256).divide(difficulty);
if (new Natural(nonce).compareTo(maxNonce) > 0)
return VAL_NONCE_TOO_HIGH;
if (Config.VALIDATE_POW && !ProofOfWork.verifyPoW(this))
return VAL_INVALID_POW;
}
return VAL_VALID;
}
// ---------------------------------------------------------------------------------------------
/** Return true iff the {@link #uncleHash} is not the hash of the empty sequence. */
public boolean hasUncles() {
return !uncleHash.equals(Hash.EMPTY_SEQ_HASH);
}
// ---------------------------------------------------------------------------------------------
/**
* Returns the RLP representation of this block header, which is stored in the block (itself
* RLP-encoded) when circulated over the network. Uncle block headers are similarly encoded.
*/
@Override public RLP rlpLayout() {
return RLP.sequence(
parentHash, uncleHash, coinbase, stateRoot, transactionsRoot, receiptsRoot, logsBloom,
difficulty, number, gasLimit, gasUsed, timestamp, extraData, mixHash, nonce);
}
// ---------------------------------------------------------------------------------------------
@Override public boolean equals (Object o) {
if (this == o) return true;
if (!(o instanceof BlockHeader)) return false;
BlockHeader that = (BlockHeader) o;
return nonce == that.nonce
&& parentHash.equals(that.parentHash)
&& uncleHash.equals(that.uncleHash)
&& coinbase.equals(that.coinbase)
&& stateRoot.equals(that.stateRoot)
&& transactionsRoot.equals(that.transactionsRoot)
&& receiptsRoot.equals(that.receiptsRoot)
&& logsBloom.equals(that.logsBloom)
&& difficulty.equals(that.difficulty)
&& number.equals(that.number)
&& gasLimit.equals(that.gasLimit)
&& gasUsed.equals(that.gasUsed)
&& timestamp.equals(that.timestamp)
&& Arrays.equals(extraData,that.extraData)
&& mixHash.equals(that.mixHash);
}
@Override public int hashCode () {
int result = Objects.hash(parentHash, uncleHash, coinbase, stateRoot, transactionsRoot,
receiptsRoot, logsBloom, difficulty, number, gasLimit, gasUsed, timestamp, mixHash,
nonce);
return 31 * result + Arrays.hashCode(extraData);
}
// ---------------------------------------------------------------------------------------------
@Override public String toString () {
return "BlockHeader{" +
"parentHash=" + parentHash +
", uncleHash=" + uncleHash +
", coinbase=" + coinbase +
", stateRoot=" + stateRoot +
", transactionsRoot=" + transactionsRoot +
", receiptsRoot=" + receiptsRoot +
", logsBloom=" + logsBloom +
", difficulty=" + difficulty +
", number=" + number +
", gasLimit=" + gasLimit +
", gasUsed=" + gasUsed +
", timestamp=" + timestamp +
", extraData=" + Arrays.toString(extraData) +
", mixHash=" + mixHash +
", nonce=" + nonce +
", hash=" + hash() +
'}';
}
// ---------------------------------------------------------------------------------------------
}