-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to 32-byte chainwork to ThinConverter class #354
Closed
nathanieliov
wants to merge
3
commits into
32-byte-chainwork-support-integration
from
use-serialize-and-deserialize-V2
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,15 +4,57 @@ | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
import static org.junit.jupiter.api.Assertions.assertNull; | ||||||
|
||||||
import co.rsk.peg.constants.BridgeRegTestConstants; | ||||||
import co.rsk.bitcoinj.core.NetworkParameters; | ||||||
import co.rsk.peg.constants.BridgeConstants; | ||||||
import co.rsk.peg.constants.BridgeMainNetConstants; | ||||||
import java.math.BigInteger; | ||||||
import java.util.ArrayList; | ||||||
import java.util.stream.Stream; | ||||||
import org.bitcoinj.core.ECKey; | ||||||
import org.junit.jupiter.api.Assertions; | ||||||
import org.junit.jupiter.api.Test; | ||||||
import org.junit.jupiter.params.ParameterizedTest; | ||||||
import org.junit.jupiter.params.provider.Arguments; | ||||||
import org.junit.jupiter.params.provider.MethodSource; | ||||||
|
||||||
class ThinConverterTest { | ||||||
|
||||||
private static final BigInteger NEGATIVE_CHAIN_WORK = BigInteger.valueOf(-1); | ||||||
private static final BigInteger BELOW_MAX_WORK_V1 = new BigInteger("ffffffffffffffff", 16); // 8 bytes | ||||||
private static final BigInteger MAX_WORK_V1 = new BigInteger(/* 12 bytes */ "ffffffffffffffffffffffff", 16); | ||||||
private static final BigInteger MAX_WORK_V2 = new BigInteger(/* 32 bytes */ | ||||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); | ||||||
private static final BigInteger TOO_LARGE_WORK_V1 = new BigInteger(/* 13 bytes */ "ffffffffffffffffffffffffff", 16); | ||||||
private static final BigInteger TOO_LARGE_WORK_V2 = new BigInteger(/* 33 bytes */ | ||||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); | ||||||
|
||||||
private static final int height = 200; | ||||||
private static int nhash = 0; | ||||||
|
||||||
private static final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); | ||||||
org.bitcoinj.core.NetworkParameters bitcoinCoreParams = org.bitcoinj.params.MainNetParams.get(); | ||||||
co.rsk.bitcoinj.core.NetworkParameters bitcoinjThinParams = co.rsk.bitcoinj.params.MainNetParams.get(); | ||||||
|
||||||
private static final ECKey userKey = ECKey.fromPrivate(BigInteger.valueOf(100)); | ||||||
|
||||||
public static Stream<Arguments> validChainWorkArgsProvider() { | ||||||
return Stream.of( | ||||||
Arguments.of(BigInteger.ZERO), | ||||||
Arguments.of(BigInteger.ONE), | ||||||
Arguments.of(BELOW_MAX_WORK_V1), | ||||||
Arguments.of(MAX_WORK_V1), | ||||||
Arguments.of(TOO_LARGE_WORK_V1), | ||||||
Arguments.of(MAX_WORK_V2) | ||||||
); | ||||||
} | ||||||
|
||||||
public static Stream<Arguments> invalidChainWorkArgsProvider() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Including both format |
||||||
return Stream.of( | ||||||
Arguments.of(NEGATIVE_CHAIN_WORK), | ||||||
Arguments.of(TOO_LARGE_WORK_V2) | ||||||
); | ||||||
} | ||||||
|
||||||
private org.bitcoinj.core.Sha256Hash createOriginalHash() { | ||||||
byte[] bytes = new byte[32]; | ||||||
bytes[0] = (byte) nhash++; | ||||||
|
@@ -25,61 +67,95 @@ private co.rsk.bitcoinj.core.Sha256Hash createThinHash() { | |||||
return co.rsk.bitcoinj.core.Sha256Hash.wrap(bytes); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void toThinInstanceStoredBlock() { | ||||||
org.bitcoinj.core.NetworkParameters params = org.bitcoinj.params.RegTestParams.get(); | ||||||
BigInteger chainWork = BigInteger.TEN; | ||||||
int height = 200; | ||||||
org.bitcoinj.core.Block originalBlock = new org.bitcoinj.core.Block(params, 1, createOriginalHash(), createOriginalHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
@ParameterizedTest | ||||||
@MethodSource("validChainWorkArgsProvider") | ||||||
void toThinInstanceStoredBlock(BigInteger chainWork) { | ||||||
// arrange | ||||||
org.bitcoinj.core.Block originalBlock = new org.bitcoinj.core.Block(bitcoinCoreParams, 1, createOriginalHash(), createOriginalHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
org.bitcoinj.core.StoredBlock originalStoredBlock = new org.bitcoinj.core.StoredBlock(originalBlock, chainWork, height); | ||||||
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = ThinConverter.toThinInstance(originalStoredBlock, new BridgeRegTestConstants()); | ||||||
|
||||||
// act | ||||||
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = ThinConverter.toThinInstance(originalStoredBlock, bridgeConstants); | ||||||
|
||||||
// assert | ||||||
assertEquals(chainWork, thinStoredBlock.getChainWork()); | ||||||
assertEquals(height, thinStoredBlock.getHeight()); | ||||||
assertArrayEquals(originalBlock.bitcoinSerialize(), thinStoredBlock.getHeader().bitcoinSerialize()); | ||||||
} | ||||||
|
||||||
assertNull(ThinConverter.toThinInstance(null, new BridgeRegTestConstants())); | ||||||
@ParameterizedTest | ||||||
@MethodSource("invalidChainWorkArgsProvider") | ||||||
void toThinInstanceStored_whenInvalidChainWork_shouldFail(BigInteger chainWork) { | ||||||
// arrange | ||||||
org.bitcoinj.core.Block originalBlock = new org.bitcoinj.core.Block(bitcoinCoreParams, 1, createOriginalHash(), createOriginalHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
org.bitcoinj.core.StoredBlock originalStoredBlock = new org.bitcoinj.core.StoredBlock(originalBlock, chainWork, height); | ||||||
|
||||||
// act and assert | ||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> ThinConverter.toThinInstance(originalStoredBlock, bridgeConstants)); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void toOriginalInstanceStoredBlock() { | ||||||
co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get(); | ||||||
BigInteger chainWork = BigInteger.TEN; | ||||||
int height = 200; | ||||||
co.rsk.bitcoinj.core.BtcBlock thinBlock = new co.rsk.bitcoinj.core.BtcBlock(params, 1, createThinHash(), createThinHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
void toThinInstanceStoredBlock_whenPassingNull_shouldReturnNull() { | ||||||
assertNull(ThinConverter.toThinInstance(null, bridgeConstants)); | ||||||
} | ||||||
|
||||||
@ParameterizedTest | ||||||
@MethodSource("validChainWorkArgsProvider") | ||||||
void toOriginalInstanceStoredBlock(BigInteger chainWork) { | ||||||
// arrange | ||||||
co.rsk.bitcoinj.core.BtcBlock thinBlock = new co.rsk.bitcoinj.core.BtcBlock(bitcoinjThinParams, 1, createThinHash(), createThinHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = new co.rsk.bitcoinj.core.StoredBlock(thinBlock, chainWork, height); | ||||||
org.bitcoinj.core.StoredBlock originalStoredBlock = ThinConverter.toOriginalInstance(thinStoredBlock, new BridgeRegTestConstants()); | ||||||
|
||||||
// act | ||||||
org.bitcoinj.core.StoredBlock originalStoredBlock = ThinConverter.toOriginalInstance(thinStoredBlock, bridgeConstants); | ||||||
|
||||||
// assert | ||||||
assertEquals(chainWork, originalStoredBlock.getChainWork()); | ||||||
assertEquals(height, originalStoredBlock.getHeight()); | ||||||
assertArrayEquals(thinBlock.bitcoinSerialize(), originalStoredBlock.getHeader().bitcoinSerialize()); | ||||||
} | ||||||
|
||||||
assertNull(ThinConverter.toOriginalInstance(null, new BridgeRegTestConstants())); | ||||||
@ParameterizedTest | ||||||
@MethodSource("invalidChainWorkArgsProvider") | ||||||
void toOriginalInstanceStoredBlock_whenInvalidChainWork_shouldFail(BigInteger chainWork) { | ||||||
// arrange | ||||||
co.rsk.bitcoinj.core.BtcBlock thinBlock = new co.rsk.bitcoinj.core.BtcBlock(bitcoinjThinParams, 1, createThinHash(), createThinHash(), 1000, 2000, 3000, new ArrayList<>()); | ||||||
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = new co.rsk.bitcoinj.core.StoredBlock(thinBlock, chainWork, height); | ||||||
|
||||||
// act and assert | ||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> ThinConverter.toOriginalInstance(thinStoredBlock, bridgeConstants)); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void toOriginalInstance_whenPassingNull_shouldReturnNull() { | ||||||
assertNull(ThinConverter.toOriginalInstance(null, bridgeConstants)); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void toOriginalInstanceNetworkParameters() { | ||||||
org.bitcoinj.core.NetworkParameters originalParams = ThinConverter.toOriginalInstance(co.rsk.bitcoinj.core.NetworkParameters.ID_REGTEST); | ||||||
assertEquals(co.rsk.bitcoinj.core.NetworkParameters.ID_REGTEST, originalParams.getId()); | ||||||
org.bitcoinj.core.NetworkParameters originalParams = ThinConverter.toOriginalInstance( | ||||||
NetworkParameters.ID_MAINNET); | ||||||
assertEquals(co.rsk.bitcoinj.core.NetworkParameters.ID_MAINNET, originalParams.getId()); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void toOriginalInstanceTransaction() { | ||||||
co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get(); | ||||||
co.rsk.bitcoinj.core.BtcTransaction thinTx = new co.rsk.bitcoinj.core.BtcTransaction(params); | ||||||
co.rsk.bitcoinj.core.BtcTransaction thinTx = new co.rsk.bitcoinj.core.BtcTransaction(bitcoinjThinParams); | ||||||
co.rsk.bitcoinj.script.Script script = new co.rsk.bitcoinj.script.Script(new byte[]{0}); | ||||||
thinTx.addInput(createThinHash(), 1, script); | ||||||
thinTx.addOutput(co.rsk.bitcoinj.core.Coin.CENT, co.rsk.bitcoinj.core.Address.fromBase58(params, "mhxk5q8QdGFoaP4SJ3DPtXjrbxAgxjNm3C")); | ||||||
org.bitcoinj.core.Transaction originalTx = ThinConverter.toOriginalInstance(params.getId(), thinTx); | ||||||
thinTx.addOutput(co.rsk.bitcoinj.core.Coin.CENT, co.rsk.bitcoinj.core.Address.fromP2SHHash(bitcoinjThinParams, userKey.getPubKeyHash())); | ||||||
org.bitcoinj.core.Transaction originalTx = ThinConverter.toOriginalInstance(bitcoinjThinParams.getId(), thinTx); | ||||||
assertEquals(thinTx.getHash().toString(), originalTx.getTxId().toString()); | ||||||
co.rsk.bitcoinj.core.BtcTransaction thinnTx2 = ThinConverter.toThinInstance(params, originalTx); | ||||||
co.rsk.bitcoinj.core.BtcTransaction thinnTx2 = ThinConverter.toThinInstance(bitcoinjThinParams, originalTx); | ||||||
assertEquals(thinTx.getHash(), thinnTx2.getHash()); | ||||||
} | ||||||
|
||||||
|
||||||
@Test | ||||||
void toOriginalInstanceAddress() { | ||||||
co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get(); | ||||||
co.rsk.bitcoinj.core.Address thinAddress = co.rsk.bitcoinj.core.Address.fromBase58(params, "mhxk5q8QdGFoaP4SJ3DPtXjrbxAgxjNm3C"); | ||||||
org.bitcoinj.core.Address originalAddress = ThinConverter.toOriginalInstance(ThinConverter.toOriginalInstance(co.rsk.bitcoinj.core.NetworkParameters.ID_REGTEST), thinAddress); | ||||||
co.rsk.bitcoinj.core.Address thinAddress = co.rsk.bitcoinj.core.Address.fromP2SHHash(bitcoinjThinParams, userKey.getPubKeyHash()); | ||||||
org.bitcoinj.core.Address originalAddress = ThinConverter.toOriginalInstance(ThinConverter.toOriginalInstance( | ||||||
bitcoinjThinParams.getId()), thinAddress); | ||||||
assertEquals(thinAddress.toString(), originalAddress.toString()); | ||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This includes both formats of chainwork.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is the too large work v1 valid in v1?