use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.
the class BridgeTestPowerMock method getBtcTransactionConfirmationsAfterWasabi_ok.
@Test
public void getBtcTransactionConfirmationsAfterWasabi_ok() throws Exception {
Transaction txMock = mock(Transaction.class);
BridgeSupportFactory bridgeSupportFactoryMock = mock(BridgeSupportFactory.class);
Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactoryMock);
bridge.init(txMock, getGenesisBlock(), createRepository().startTracking(), null, null, null);
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
when(bridgeSupportFactoryMock.newInstance(any(), any(), any(), any())).thenReturn(bridgeSupportMock);
byte[] btcTxHash = Sha256Hash.of(Hex.decode("aabbcc")).getBytes();
byte[] btcBlockHash = Sha256Hash.of(Hex.decode("ddeeff")).getBytes();
byte[][] merkleBranchHashes = new byte[][] { Sha256Hash.of(Hex.decode("11")).getBytes(), Sha256Hash.of(Hex.decode("22")).getBytes(), Sha256Hash.of(Hex.decode("33")).getBytes() };
BigInteger merkleBranchBits = BigInteger.valueOf(123);
MerkleBranch merkleBranch = mock(MerkleBranch.class);
PowerMockito.whenNew(MerkleBranch.class).withArguments(any(List.class), any(Integer.class)).then((Answer<MerkleBranch>) invocation -> {
List<Sha256Hash> hashes = invocation.getArgument(0);
Assert.assertArrayEquals(merkleBranchHashes[0], hashes.get(0).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[1], hashes.get(1).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[2], hashes.get(2).getBytes());
Integer bits = invocation.getArgument(1);
Assert.assertEquals(123, bits.intValue());
return merkleBranch;
});
when(bridgeSupportMock.getBtcTransactionConfirmations(any(Sha256Hash.class), any(Sha256Hash.class), any(MerkleBranch.class))).then((Answer<Integer>) invocation -> {
Sha256Hash txHash = invocation.getArgument(0);
Assert.assertArrayEquals(btcTxHash, txHash.getBytes());
Sha256Hash blockHash = invocation.getArgument(1);
Assert.assertArrayEquals(btcBlockHash, blockHash.getBytes());
MerkleBranch merkleBranchArg = invocation.getArgument(2);
Assert.assertEquals(merkleBranch, merkleBranchArg);
return 78;
});
Assert.assertEquals(78, bridge.getBtcTransactionConfirmations(new Object[] { btcTxHash, btcBlockHash, merkleBranchBits, merkleBranchHashes }));
}
use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.
the class BridgeTestPowerMock method getBtcTransactionConfirmationsAfterWasabi_merkleBranchConstructionError.
@Test
public void getBtcTransactionConfirmationsAfterWasabi_merkleBranchConstructionError() throws Exception {
Transaction txMock = mock(Transaction.class);
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
Bridge bridge = PowerMockito.spy(new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory));
bridge.init(txMock, getGenesisBlock(), createRepository().startTracking(), null, null, null);
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
byte[] btcTxHash = Sha256Hash.of(Hex.decode("aabbcc")).getBytes();
byte[] btcBlockHash = Sha256Hash.of(Hex.decode("ddeeff")).getBytes();
byte[][] merkleBranchHashes = new byte[][] { Sha256Hash.of(Hex.decode("11")).getBytes(), Sha256Hash.of(Hex.decode("22")).getBytes(), Sha256Hash.of(Hex.decode("33")).getBytes() };
BigInteger merkleBranchBits = BigInteger.valueOf(123);
MerkleBranch merkleBranch = mock(MerkleBranch.class);
PowerMockito.whenNew(MerkleBranch.class).withArguments(any(List.class), any(Integer.class)).then((Answer<MerkleBranch>) invocation -> {
List<Sha256Hash> hashes = invocation.getArgument(0);
Assert.assertArrayEquals(merkleBranchHashes[0], hashes.get(0).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[1], hashes.get(1).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[2], hashes.get(2).getBytes());
Integer bits = invocation.getArgument(1);
Assert.assertEquals(123, bits.intValue());
throw new IllegalArgumentException("blabla");
});
try {
bridge.getBtcTransactionConfirmations(new Object[] { btcTxHash, btcBlockHash, merkleBranchBits, merkleBranchHashes });
Assert.fail();
} catch (VMException e) {
Assert.assertTrue(e.getMessage().contains("in getBtcTransactionConfirmations"));
Assert.assertEquals(IllegalArgumentException.class, e.getCause().getClass());
verify(bridgeSupportMock, never()).getBtcTransactionConfirmations(any(), any(), any());
}
}
use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.
the class BridgeTestPowerMock method getBtcTransactionConfirmationsAfterWasabi_errorInBridgeSupport.
@Test
public void getBtcTransactionConfirmationsAfterWasabi_errorInBridgeSupport() throws Exception {
Transaction txMock = mock(Transaction.class);
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
Bridge bridge = PowerMockito.spy(new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory));
bridge.init(txMock, getGenesisBlock(), createRepository().startTracking(), null, null, null);
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
byte[] btcTxHash = Sha256Hash.of(Hex.decode("aabbcc")).getBytes();
byte[] btcBlockHash = Sha256Hash.of(Hex.decode("ddeeff")).getBytes();
byte[][] merkleBranchHashes = new byte[][] { Sha256Hash.of(Hex.decode("11")).getBytes(), Sha256Hash.of(Hex.decode("22")).getBytes(), Sha256Hash.of(Hex.decode("33")).getBytes() };
BigInteger merkleBranchBits = BigInteger.valueOf(123);
MerkleBranch merkleBranch = mock(MerkleBranch.class);
PowerMockito.whenNew(MerkleBranch.class).withArguments(any(List.class), any(Integer.class)).then((Answer<MerkleBranch>) invocation -> {
List<Sha256Hash> hashes = invocation.getArgument(0);
Assert.assertArrayEquals(merkleBranchHashes[0], hashes.get(0).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[1], hashes.get(1).getBytes());
Assert.assertArrayEquals(merkleBranchHashes[2], hashes.get(2).getBytes());
Integer bits = invocation.getArgument(1);
Assert.assertEquals(123, bits.intValue());
return merkleBranch;
});
when(bridgeSupportMock.getBtcTransactionConfirmations(any(Sha256Hash.class), any(Sha256Hash.class), any(MerkleBranch.class))).then((Answer<Integer>) invocation -> {
Sha256Hash txHash = invocation.getArgument(0);
Assert.assertArrayEquals(btcTxHash, txHash.getBytes());
Sha256Hash blockHash = invocation.getArgument(1);
Assert.assertArrayEquals(btcBlockHash, blockHash.getBytes());
MerkleBranch merkleBranchArg = invocation.getArgument(2);
Assert.assertEquals(merkleBranch, merkleBranchArg);
throw new VMException("bla bla bla");
});
try {
bridge.getBtcTransactionConfirmations(new Object[] { btcTxHash, btcBlockHash, merkleBranchBits, merkleBranchHashes });
Assert.fail();
} catch (VMException e) {
Assert.assertTrue(e.getMessage().contains("in getBtcTransactionConfirmations"));
Assert.assertEquals(VMException.class, e.getCause().getClass());
Assert.assertTrue(e.getCause().getMessage().contains("bla bla bla"));
}
}
use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.
the class BridgeSupportTest method getBtcTransactionConfirmations_registered_coinbase_unequal_witnessroot_after_rskip_143.
@Test
public void getBtcTransactionConfirmations_registered_coinbase_unequal_witnessroot_after_rskip_143() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
Repository repository = createRepository();
BtcTransaction tx1 = new BtcTransaction(btcParams);
tx1.addOutput(Coin.COIN.multiply(10), Address.fromBase58(btcParams, "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou"));
tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
tx1.setWitness(0, txWit);
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx1.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash blockMerkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
List<Sha256Hash> hashes2 = new ArrayList<>();
hashes2.add(tx1.getHash(true));
PartialMerkleTree pmt2 = new PartialMerkleTree(btcParams, bits, hashes2, 1);
List<Sha256Hash> hashlist2 = new ArrayList<>();
Sha256Hash witnessMerkleRoot = pmt2.getTxnHashAndMerkleRoot(hashlist2);
int height = 50;
StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStore.getFromCache(registerHeader.getHash())).thenReturn(block);
StoredBlock chainHead = new StoredBlock(registerHeader, new BigInteger("0"), 132);
when(btcBlockStore.getChainHead()).thenReturn(chainHead);
when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
BridgeStorageProvider provider = spy(new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations));
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, mock(BtcLockSenderProvider.class), mock(PeginInstructionsProvider.class), mock(Block.class), mockFactory, activations);
MerkleBranch merkleBranch = mock(MerkleBranch.class);
when(merkleBranch.reduceFrom(tx1.getHash(true))).thenReturn(witnessMerkleRoot);
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(Sha256Hash.ZERO_HASH);
provider.setCoinbaseInformation(registerHeader.getHash(), coinbaseInformation);
int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(true), registerHeader.getHash(), merkleBranch);
Assert.assertEquals(-5, confirmations);
}
use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.
the class BridgeSupportTest method getBtcTransactionConfirmations_unregistered_coinbase_after_rskip_143.
@Test
public void getBtcTransactionConfirmations_unregistered_coinbase_after_rskip_143() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
Repository repository = createRepository();
BtcTransaction tx1 = new BtcTransaction(btcParams);
tx1.addOutput(Coin.COIN.multiply(10), Address.fromBase58(btcParams, "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou"));
tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
tx1.setWitness(0, txWit);
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx1.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash blockMerkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
List<Sha256Hash> hashes2 = new ArrayList<>();
hashes2.add(tx1.getHash(true));
PartialMerkleTree pmt2 = new PartialMerkleTree(btcParams, bits, hashes2, 1);
List<Sha256Hash> hashlist2 = new ArrayList<>();
Sha256Hash witnessMerkleRoot = pmt2.getTxnHashAndMerkleRoot(hashlist2);
int height = 50;
StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStore.getFromCache(registerHeader.getHash())).thenReturn(block);
StoredBlock chainHead = new StoredBlock(registerHeader, new BigInteger("0"), 132);
when(btcBlockStore.getChainHead()).thenReturn(chainHead);
when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
BridgeStorageProvider provider = spy(new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations));
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, mock(BtcLockSenderProvider.class), mock(PeginInstructionsProvider.class), mock(Block.class), mockFactory, activations);
MerkleBranch merkleBranch = mock(MerkleBranch.class);
when(merkleBranch.reduceFrom(tx1.getHash(true))).thenReturn(witnessMerkleRoot);
int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(true), registerHeader.getHash(), merkleBranch);
Assert.assertEquals(-5, confirmations);
}
Aggregations