Search in sources :

Example 1 with MerkleBranch

use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method getBtcTransactionConfirmations_merkleBranchDoesNotProve.

@Test
public void getBtcTransactionConfirmations_merkleBranchDoesNotProve() throws BlockStoreException, IOException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    Sha256Hash blockHash = Sha256Hash.of(Hex.decode("aabbcc"));
    Sha256Hash merkleRoot = Sha256Hash.of(Hex.decode("ddeeff"));
    BtcBlock blockHeader = mock(BtcBlock.class);
    when(blockHeader.getHash()).thenReturn(blockHash);
    when(blockHeader.getMerkleRoot()).thenReturn(merkleRoot);
    int height = 50;
    StoredBlock block = new StoredBlock(blockHeader, new BigInteger("0"), height);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    when(btcBlockStore.getFromCache(blockHash)).thenReturn(block);
    StoredBlock chainHead = new StoredBlock(blockHeader, new BigInteger("0"), 132);
    when(btcBlockStore.getChainHead()).thenReturn(chainHead);
    when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(track, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    Sha256Hash btcTransactionHash = Sha256Hash.of(Hex.decode("112233"));
    BridgeSupport bridgeSupport = getBridgeSupport(provider, track, mockFactory, activations);
    MerkleBranch merkleBranch = mock(MerkleBranch.class);
    when(merkleBranch.reduceFrom(btcTransactionHash)).thenReturn(Sha256Hash.ZERO_HASH);
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(btcTransactionHash, blockHash, merkleBranch);
    Assert.assertEquals(BridgeSupport.BTC_TRANSACTION_CONFIRMATION_INVALID_MERKLE_BRANCH_ERROR_CODE.intValue(), confirmations);
}
Also used : MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with MerkleBranch

use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.

the class BridgeSupportTest method getBtcTransactionConfirmations_rejects_tx_with_witness_before_rskip_143.

@Test
public void getBtcTransactionConfirmations_rejects_tx_with_witness_before_rskip_143() throws Exception {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(false);
    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<>());
    int height = 50;
    StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    when(btcBlockStore.getFromCache(registerHeader.getHash())).thenReturn(block);
    when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
    StoredBlock chainHead = new StoredBlock(registerHeader, new BigInteger("0"), 132);
    when(btcBlockStore.getChainHead()).thenReturn(chainHead);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
    BridgeSupport bridgeSupport = getBridgeSupport(provider, mock(Repository.class), mockFactory);
    MerkleBranch merkleBranch = mock(MerkleBranch.class);
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(true), registerHeader.getHash(), merkleBranch);
    Assert.assertEquals(-5, confirmations);
}
Also used : MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) Script(co.rsk.bitcoinj.script.Script) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 3 with MerkleBranch

use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.

the class BridgeSupportTest method getBtcTransactionConfirmations_accepts_tx_without_witness_after_rskip_143.

@Test
public void getBtcTransactionConfirmations_accepts_tx_without_witness_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);
    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<>());
    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())).thenReturn(blockMerkleRoot);
    BtcBlock btcBlock = mock(BtcBlock.class);
    when(btcBlock.getMerkleRoot()).thenReturn(blockMerkleRoot);
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(), registerHeader.getHash(), merkleBranch);
    Assert.assertEquals(chainHead.getHeight() - block.getHeight() + 1, confirmations);
}
Also used : MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 4 with MerkleBranch

use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.

the class BridgeSupportTest method getBtcTransactionConfirmations_tx_without_witness_unequal_roots_after_rskip_143.

@Test
public void getBtcTransactionConfirmations_tx_without_witness_unequal_roots_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);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<>());
    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())).thenReturn(PegTestUtils.createHash(5));
    CoinbaseInformation coinbaseInformation = new CoinbaseInformation(Sha256Hash.ZERO_HASH);
    doReturn(coinbaseInformation).when(provider).getCoinbaseInformation(registerHeader.getHash());
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(), registerHeader.getHash(), merkleBranch);
    Assert.assertEquals(-5, confirmations);
}
Also used : PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 5 with MerkleBranch

use of co.rsk.peg.bitcoin.MerkleBranch in project rskj by rsksmart.

the class BridgeSupportTest method getBtcTransactionConfirmations_accepts_tx_with_witness_after_rskip_143.

@Test
public void getBtcTransactionConfirmations_accepts_tx_with_witness_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(witnessMerkleRoot);
    provider.setCoinbaseInformation(registerHeader.getHash(), coinbaseInformation);
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(tx1.getHash(true), registerHeader.getHash(), merkleBranch);
    Assert.assertEquals(chainHead.getHeight() - block.getHeight() + 1, confirmations);
}
Also used : MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

MerkleBranch (co.rsk.peg.bitcoin.MerkleBranch)11 BigInteger (java.math.BigInteger)11 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)11 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)10 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)10 MutableRepository (org.ethereum.db.MutableRepository)10 Test (org.junit.Test)10 Repository (org.ethereum.core.Repository)7 Block (org.ethereum.core.Block)6 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)5 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)5 Script (co.rsk.bitcoinj.script.Script)4 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)4 BridgeConstants (co.rsk.config.BridgeConstants)4 RskAddress (co.rsk.core.RskAddress)4 Keccak256 (co.rsk.crypto.Keccak256)4 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)4 IOException (java.io.IOException)4 Instant (java.time.Instant)4 Constants (org.ethereum.config.Constants)4