mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-11 17:55:53 +00:00
Correct AuxPoW index calculation to match unsigned integer maths used by reference client.
Add unit tests for AuxPoW index calculation.
This commit is contained in:
parent
95060faa20
commit
1dcdff9f6e
@ -355,7 +355,10 @@ public class AuxPoW extends ChildMessage {
|
||||
|
||||
if (getChainMerkleBranch().getIndex() != getExpectedIndex(nonce, ((AuxPoWNetworkParameters) params).getChainID(), getChainMerkleBranch().size())) {
|
||||
if (throwException) {
|
||||
throw new VerificationException("Aux POW wrong index");
|
||||
throw new VerificationException("Aux POW wrong index in chain merkle branch for chain ID "
|
||||
+ ((AuxPoWNetworkParameters) params).getChainID() + ". Was "
|
||||
+ getChainMerkleBranch().getIndex() + ", expected "
|
||||
+ getExpectedIndex(nonce, ((AuxPoWNetworkParameters) params).getChainID(), getChainMerkleBranch().size()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -402,12 +405,20 @@ public class AuxPoW extends ChildMessage {
|
||||
// Choose a pseudo-random slot in the chain merkle tree
|
||||
// but have it be fixed for a size/nonce/chain combination.
|
||||
|
||||
long rand = nonce;
|
||||
// We do most of the maths with a signed 32 bit integer, as the operation is
|
||||
// the same as the 32 unsigned integer that the reference version uses
|
||||
int rand = (int) nonce;
|
||||
rand = rand * 1103515245 + 12345;
|
||||
rand += chainId;
|
||||
rand = rand * 1103515245 + 12345;
|
||||
|
||||
return (int) (rand % (1L << merkleHeight));
|
||||
// At this point, we need to flip the value to its positive version,
|
||||
// so we switch to a 64 bit signed integer for the last calculations
|
||||
long longRand = rand & 0xffffffffl;
|
||||
|
||||
longRand %= (1 << merkleHeight);
|
||||
|
||||
return (int) longRand;
|
||||
}
|
||||
|
||||
public Transaction getTransaction() {
|
||||
|
@ -122,6 +122,31 @@ public class DogecoinBlockTest {
|
||||
assertTrue(auxpow.checkProofOfWork(block.getHash(), block.getDifficultyTargetAsInteger(), false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm parsing of block with a nonce value above Integer.MAX_VALUE.
|
||||
* See https://github.com/rnicoll/libdohj/pull/7
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void shouldParseBlock748634() throws IOException {
|
||||
byte[] payload = Util.getBytes(getClass().getResourceAsStream("dogecoin_block748634.bin"));
|
||||
AltcoinSerializer serializer = (AltcoinSerializer)params.getDefaultSerializer();
|
||||
final AltcoinBlock block = (AltcoinBlock)serializer.makeBlock(payload);
|
||||
assertEquals("bd98a06391115285265c04984e8505229739f6ffa5d498929a91fbe7c281ea7b", block.getHashAsString());
|
||||
assertEquals(0, block.getNonce());
|
||||
|
||||
// Check block version values
|
||||
assertEquals(2, block.getVersion());
|
||||
assertEquals(98, block.getChainID());
|
||||
assertTrue(block.getVersionFlags().get(0));
|
||||
|
||||
final AuxPoW auxpow = block.getAuxPoW();
|
||||
assertNotNull(auxpow);
|
||||
|
||||
assertTrue(auxpow.checkProofOfWork(block.getHash(), block.getDifficultyTargetAsInteger(), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm parsing of block with a nonce value above Integer.MAX_VALUE.
|
||||
* See https://github.com/rnicoll/libdohj/issues/5
|
||||
|
File diff suppressed because one or more lines are too long
@ -1 +0,0 @@
|
||||
020162000d6f03470d329026cd1fc720c0609cd378ca8691a117bd1aa46f01fb09b1a8468a15bf6f0b0e83f2e5036684169eafb9406468d4f075c999fb5b2a78fbb827ee41fb11548441361b0000000001000000010000000000000000000000000000000000000000000000000000000000000000ffffffff380345bf09fabe6d6d980ba42120410de0554d42a5b5ee58167bcd86bf7591f429005f24da45fb51cf0800000000000000cdb1f1ff0e000000ffffffff01800c0c2a010000001976a914aa3750aa18b8a0f3f0590731e1fab934856680cf88ac00000000b3e64e02fff596209c498f1b18f798d62f216f11c8462bf3922319000000000003a979a636db2450363972d211aee67b71387a3daaa3051be0fd260c5acd4739cd52a418d29d8a0e56c8714c95a0dc24e1c9624480ec497fe2441941f3fee8f9481a3370c334178415c83d1d0c2deeec727c2330617a47691fc5e79203669312d100000000036fa40307b3a439538195245b0de56a2c1db6ba3a64f8bdd2071d00bc48c841b5e77b98e5c7d6f06f92dec5cf6d61277ecb9a0342406f49f34c51ee8ce4abd678038129485de14238bd1ca12cd2de12ff0e383aee542d90437cd664ce139446a00000000002000000d2ec7dfeb7e8f43fe77aba3368df95ac2088034420402730ee0492a2084217083411b3fc91033bfdeea339bc11b9efc986e161c703e07a9045338c165673f09940fb11548b54021b58cc9ae50601000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0d0389aa050101062f503253482fffffffff010066f33caf050000232102b73438165461b826b30a46078f211aa005d1e7e430b1e0ed461678a5fe516c73ac000000000100000001ef2e86aa5f027e13d7fc1f0bd4a1fc677d698e42850680634ccd1834668ff320010000006b483045022100fcf5dc43afa85978a71e76a9f4c11cd6bf2a7d5677212f9001ad085d420a5d3a022068982e1e53e94fc6007cf8b60ff3919bcaf7f0b70fefb79112cb840777d8c7cf0121022b050b740dd02c1b4e1e7cdbffe6d836d987c9db4c4db734b58526f08942193bffffffff02004e7253000000001976a91435cb1f77e88e96fb3094d84e8d3b7789a092636d88ac00d4b7e8b00700001976a9146ca1f634daa4efc7871abab945c7cefd282b481f88ac0000000001000000010a6c24bbc92fd0ec32bb5b0a051c44eba0c1325f0b24d9523c109f8bb1281f49000000006a4730440220608577619fb3a0b826f09df5663ffbf121c8e0164f43b73d9affe2f9e4576bd0022040782c9a7df0a20afe1a7e3578bf27e1331c862253af21ced4fde5ef1b44b787012103e4f91ad831a87cc532249944bc7138a355f7d0aac25dc4737a8701181ce680a5ffffffff010019813f0d0000001976a91481db1aa49ebc6a71cad96949eb28e22af85eb0bd88ac0000000001000000017b82db0f644ecff378217d9b8dc0de8817eaf85ceefacab23bf344e2e495dca5010000006b483045022100f07ced6bfdbd6cdeb8b2c8fc92b9803f5798754b5b6c454c8f084198bea303f402205616f84d7ec882af9c34a3fd2457ca3fb81ec5a463a963a6e684edee427d4525012102c056b10494520dbd7b37e2e6bb8f72f98d73a609a926901221bfb114fa1d5a80ffffffff02f0501a22000000001976a914ca63ded8b23d0252158a3bdc816747ef89fb438988ac80b65ea1350700001976a914fb26a7c16ace531a8e7bbd925e46c67c3150c1c888ac000000000100000001c9bdba900e1579ebf4e44415fe8b9abec57a763f8c70a30604bea7fbe7c55d42000000006a47304402204ccbeeace0630e72102fdaf0836e41f8f6dcdde6a178f0fbc2d96a4d17a1df8f02207e4a91203a2abd87fdddee96510482ef96535741b6c17a1acae93c977ad248e5012103e0747583a342b76a5de9c21db138b9640d49b4f3b67a306d3b3f217416d49b55ffffffff020058850c020000001976a9144417c63a91208a02a5f46a0f7a2b806adc7d19a788ac0042dc06030000001976a9147b61c5adef0d559e5acf2901c2989294624b651988ac0000000001000000017c1423b198dfc3da37ae9a5fc11a3720e4343b3049d3b289b8285eb04595c04b000000006b483045022100b0c1cb9608bf644d7a8916bf61f36ced95bd045e97612804ca774f60e05e7bde022017c12255eecc474c8d8b05d0910013b2df8703af68212cf0962b6b8ee0e101ee01210341e154088c23b8ea943bca94c1d4f65361668a242b168522f00199365414b46affffffff01019891ad000000001976a91481db1aa49ebc6a71cad96949eb28e22af85eb0bd88ac00000000
|
BIN
src/test/resources/org/bitcoinj/core/dogecoin_block748634.bin
Normal file
BIN
src/test/resources/org/bitcoinj/core/dogecoin_block748634.bin
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user