use of co.rsk.peg.bitcoin.CoinbaseInformation in project rskj by rsksmart.
the class BridgeStorageProviderTest method getCoinBaseInformation_before_RSKIP143.
@Test
public void getCoinBaseInformation_before_RSKIP143() {
Repository repository = mock(Repository.class);
Sha256Hash hash = Sha256Hash.ZERO_HASH;
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsBeforeFork);
CoinbaseInformation result = provider.getCoinbaseInformation(hash);
assertNull(result);
verify(repository, never()).getStorageBytes(PrecompiledContracts.BRIDGE_ADDR, DataWord.fromLongString("coinbaseInformation-" + hash.toString()));
}
use of co.rsk.peg.bitcoin.CoinbaseInformation in project rskj by rsksmart.
the class BridgeStorageProviderTest method getCoinBaseInformation_after_RSKIP143.
@Test
public void getCoinBaseInformation_after_RSKIP143() {
Repository repository = mock(Repository.class);
Sha256Hash hash = Sha256Hash.ZERO_HASH;
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(Sha256Hash.ZERO_HASH);
when(repository.getStorageBytes(PrecompiledContracts.BRIDGE_ADDR, DataWord.fromLongString("coinbaseInformation-" + hash.toString()))).thenReturn(BridgeSerializationUtils.serializeCoinbaseInformation(coinbaseInformation));
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
CoinbaseInformation result = provider.getCoinbaseInformation(hash);
assertEquals(coinbaseInformation.getWitnessMerkleRoot(), result.getWitnessMerkleRoot());
}
use of co.rsk.peg.bitcoin.CoinbaseInformation in project rskj by rsksmart.
the class BridgeSupportTest method isBlockMerkleRootValid_coinbase_information_not_null_and_unequal_mroots_after_rskip_143.
@Test
public void isBlockMerkleRootValid_coinbase_information_not_null_and_unequal_mroots_after_rskip_143() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(PegTestUtils.createHash(1));
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getCoinbaseInformation(Sha256Hash.ZERO_HASH)).thenReturn(coinbaseInformation);
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, mock(Repository.class), mock(BtcLockSenderProvider.class), mock(PeginInstructionsProvider.class), mock(Block.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
BtcBlock btcBlock = mock(BtcBlock.class);
when(btcBlock.getMerkleRoot()).thenReturn(Sha256Hash.ZERO_HASH);
when(btcBlock.getHash()).thenReturn(Sha256Hash.ZERO_HASH);
Assert.assertFalse(bridgeSupport.isBlockMerkleRootValid(PegTestUtils.createHash(2), btcBlock));
}
use of co.rsk.peg.bitcoin.CoinbaseInformation in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_sending_segwit_tx_twice_locks_just_once.
@Test
public void registerBtcTransaction_sending_segwit_tx_twice_locks_just_once() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock mockedActivations = mock(ActivationConfig.ForBlock.class);
when(mockedActivations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
BtcTransaction txWithWitness = new BtcTransaction(btcParams);
// first input spends P2PKH
BtcECKey srcKey1 = new BtcECKey();
txWithWitness.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
// second input spends P2SH-P2PWKH (actually, just has a witness doesn't matter if it truly spends a witness for the test's sake)
txWithWitness.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
txWithWitness.setWitness(0, txWit);
List<BtcECKey> fedKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
Federation fed = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(fedKeys), Instant.ofEpochMilli(1000L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
txWithWitness.addOutput(Coin.COIN.multiply(5), fed.getAddress());
// Create the pmt without witness and calculate the block merkle root
byte[] bits = new byte[1];
bits[0] = 0x3f;
PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash()), 1);
Sha256Hash merkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
PartialMerkleTree pmtWithWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash(true)), 1);
Sha256Hash witnessMerkleRoot = pmtWithWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getNewFederation()).thenReturn(fed);
when(provider.getCoinbaseInformation(registerHeader.getHash())).thenReturn(new CoinbaseInformation(witnessMerkleRoot));
when(provider.getLockWhitelist()).thenReturn(new LockWhitelist(new HashMap<>(), 0));
when(provider.getLockingCap()).thenReturn(Coin.FIFTY_COINS);
// mock an actual store for the processed txs
HashMap<Sha256Hash, Long> processedTxs = new HashMap<>();
doAnswer(a -> {
processedTxs.put(a.getArgument(0), a.getArgument(1));
return null;
}).when(provider).setHeightBtcTxhashAlreadyProcessed(any(), anyLong());
doAnswer(a -> Optional.ofNullable(processedTxs.get(a.getArgument(0)))).when(provider).getHeightIfBtcTxhashIsAlreadyProcessed(any());
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
Block executionBlock = mock(Block.class);
when(executionBlock.getNumber()).thenReturn(666L);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(provider).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(mockFactory).withActivations(mockedActivations).build();
int height = 30;
mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
// Tx is locked
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithWitness.bitcoinSerialize(), height, pmtWithWitness.bitcoinSerialize());
verify(provider, never()).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(true), executionBlock.getNumber());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(false), executionBlock.getNumber());
BtcTransaction txWithoutWitness = new BtcTransaction(btcParams, txWithWitness.bitcoinSerialize());
txWithoutWitness.setWitness(0, null);
assertFalse(txWithoutWitness.hasWitness());
// Tx is NOT locked again!
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithoutWitness.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithoutWitness.getHash(), executionBlock.getNumber());
assertNotEquals(txWithWitness.getHash(true), txWithoutWitness.getHash());
}
use of co.rsk.peg.bitcoin.CoinbaseInformation in project rskj by rsksmart.
the class BridgeSupportTest method hasBtcCoinbaseTransaction_before_rskip_143_activation.
@Test
public void hasBtcCoinbaseTransaction_before_rskip_143_activation() throws AddressFormatException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(false);
Repository repository = createRepository();
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, mock(BtcLockSenderProvider.class), mock(PeginInstructionsProvider.class), mock(Block.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(Sha256Hash.ZERO_HASH);
provider.setCoinbaseInformation(Sha256Hash.ZERO_HASH, coinbaseInformation);
Assert.assertFalse(bridgeSupport.hasBtcBlockCoinbaseTransactionInformation(Sha256Hash.ZERO_HASH));
}
Aggregations