Search in sources :

Example 6 with CoinbaseInformation

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()));
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with CoinbaseInformation

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());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with CoinbaseInformation

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));
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) Block(org.ethereum.core.Block) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 9 with CoinbaseInformation

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());
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Script(co.rsk.bitcoinj.script.Script) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 10 with CoinbaseInformation

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));
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) Block(org.ethereum.core.Block) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

CoinbaseInformation (co.rsk.peg.bitcoin.CoinbaseInformation)22 Test (org.junit.Test)18 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)17 Repository (org.ethereum.core.Repository)16 MutableRepository (org.ethereum.db.MutableRepository)16 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)11 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)11 Block (org.ethereum.core.Block)11 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)9 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 BigInteger (java.math.BigInteger)5 MerkleBranch (co.rsk.peg.bitcoin.MerkleBranch)3 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)3 Transaction (org.ethereum.core.Transaction)3 InternalTransaction (org.ethereum.vm.program.InternalTransaction)3 RskAddress (co.rsk.core.RskAddress)2 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)2 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)2