Search in sources :

Example 6 with MerkleBranch

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 }));
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) CoreMatchers.is(org.hamcrest.CoreMatchers.is) VMException(org.ethereum.vm.exception.VMException) HashMapDB(org.ethereum.datasource.HashMapDB) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskAddress(co.rsk.core.RskAddress) Utils.uint32ToByteStreamLE(co.rsk.bitcoinj.core.Utils.uint32ToByteStreamLE) Keccak256(co.rsk.crypto.Keccak256) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) EVMAssembler(co.rsk.asm.EVMAssembler) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) StandardCharsets(java.nio.charset.StandardCharsets) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) Modifier(java.lang.reflect.Modifier) RskSystemProperties(co.rsk.config.RskSystemProperties) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Hex(org.bouncycastle.util.encoders.Hex) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) OutputStream(java.io.OutputStream) TypeConverter(org.ethereum.rpc.TypeConverter) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) IOException(java.io.IOException) Field(java.lang.reflect.Field) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) World(co.rsk.test.World) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ByteUtil(org.ethereum.util.ByteUtil) Paths(java.nio.file.Paths) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) VM(org.ethereum.vm.VM) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BigInteger(java.math.BigInteger) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with MerkleBranch

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());
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) CoreMatchers.is(org.hamcrest.CoreMatchers.is) VMException(org.ethereum.vm.exception.VMException) HashMapDB(org.ethereum.datasource.HashMapDB) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskAddress(co.rsk.core.RskAddress) Utils.uint32ToByteStreamLE(co.rsk.bitcoinj.core.Utils.uint32ToByteStreamLE) Keccak256(co.rsk.crypto.Keccak256) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) EVMAssembler(co.rsk.asm.EVMAssembler) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) StandardCharsets(java.nio.charset.StandardCharsets) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) Modifier(java.lang.reflect.Modifier) RskSystemProperties(co.rsk.config.RskSystemProperties) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Hex(org.bouncycastle.util.encoders.Hex) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) OutputStream(java.io.OutputStream) TypeConverter(org.ethereum.rpc.TypeConverter) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) IOException(java.io.IOException) Field(java.lang.reflect.Field) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) World(co.rsk.test.World) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ByteUtil(org.ethereum.util.ByteUtil) Paths(java.nio.file.Paths) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) VM(org.ethereum.vm.VM) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BigInteger(java.math.BigInteger) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with MerkleBranch

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"));
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) CoreMatchers.is(org.hamcrest.CoreMatchers.is) VMException(org.ethereum.vm.exception.VMException) HashMapDB(org.ethereum.datasource.HashMapDB) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskAddress(co.rsk.core.RskAddress) Utils.uint32ToByteStreamLE(co.rsk.bitcoinj.core.Utils.uint32ToByteStreamLE) Keccak256(co.rsk.crypto.Keccak256) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) EVMAssembler(co.rsk.asm.EVMAssembler) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) StandardCharsets(java.nio.charset.StandardCharsets) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) Modifier(java.lang.reflect.Modifier) RskSystemProperties(co.rsk.config.RskSystemProperties) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Hex(org.bouncycastle.util.encoders.Hex) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) OutputStream(java.io.OutputStream) TypeConverter(org.ethereum.rpc.TypeConverter) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) IOException(java.io.IOException) Field(java.lang.reflect.Field) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) World(co.rsk.test.World) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ByteUtil(org.ethereum.util.ByteUtil) Paths(java.nio.file.Paths) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) VM(org.ethereum.vm.VM) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BigInteger(java.math.BigInteger) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with MerkleBranch

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);
}
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)

Example 10 with MerkleBranch

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);
}
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) 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