|
20 | 20 | import static org.mockito.Mockito.verify; |
21 | 21 | import static org.mockito.Mockito.verifyNoInteractions; |
22 | 22 | import static org.mockito.Mockito.when; |
| 23 | +import static org.mockito.Mockito.spy; |
23 | 24 |
|
24 | 25 | import co.rsk.bitcoinj.core.BtcECKey; |
25 | 26 | import co.rsk.bitcoinj.core.BtcTransaction; |
|
32 | 33 | import co.rsk.bitcoinj.script.Script; |
33 | 34 | import co.rsk.bitcoinj.script.ScriptBuilder; |
34 | 35 | import co.rsk.bitcoinj.script.ScriptChunk; |
| 36 | +import co.rsk.federate.bitcoin.BitcoinTestUtils; |
| 37 | +import co.rsk.peg.bitcoin.BitcoinUtils; |
35 | 38 | import co.rsk.peg.bitcoin.FlyoverRedeemScriptBuilderImpl; |
36 | 39 | import co.rsk.peg.constants.BridgeConstants; |
37 | 40 | import co.rsk.crypto.Keccak256; |
@@ -765,6 +768,129 @@ void onBestBlock_whenOnlySvpSpendTxWaitingForSignaturesIsAvailable_shouldAddSign |
765 | 768 | ); |
766 | 769 | } |
767 | 770 |
|
| 771 | + @Test |
| 772 | + void onBestBlock_whenSvpSpendTxWaitingForSignaturesIsAvailableWithSignatureFromAnotherFederationMember_shouldSendAddSignature() throws Exception { |
| 773 | + // Arrange |
| 774 | + List<BtcECKey> federationKeys = TestUtils.getFederationPrivateKeys(9); |
| 775 | + Federation proposedFederation = TestUtils.createFederation(bridgeConstants.getBtcParams(), federationKeys); |
| 776 | + Script scriptSig = proposedFederation.getP2SHScript().createEmptyInputScript(null, proposedFederation.getRedeemScript()); |
| 777 | + |
| 778 | + BtcTransaction svpSpendTx = new BtcTransaction(params); |
| 779 | + svpSpendTx.addInput(BitcoinTestUtils.createHash(1), 0, scriptSig); |
| 780 | + svpSpendTx.addInput(BitcoinTestUtils.createHash(2), 0, scriptSig); |
| 781 | + svpSpendTx.addOutput(Coin.COIN, new BtcECKey().toAddress(params)); |
| 782 | + Sha256Hash svpSpendTxHashBeforeSigning = svpSpendTx.getHash(); |
| 783 | + |
| 784 | + // Sign the svp spend tx |
| 785 | + List<TransactionInput> inputs = svpSpendTx.getInputs(); |
| 786 | + for (TransactionInput input : inputs) { |
| 787 | + BitcoinTestUtils.signTransactionInputFromP2shMultiSig( |
| 788 | + svpSpendTx, |
| 789 | + inputs.indexOf(input), |
| 790 | + List.of(federationKeys.get(0)) |
| 791 | + ); |
| 792 | + } |
| 793 | + |
| 794 | + Keccak256 svpSpendCreationRskTxHash = createHash(0); |
| 795 | + Map.Entry<Keccak256, BtcTransaction> svpSpendTxWFS = new AbstractMap.SimpleEntry<>(svpSpendCreationRskTxHash, svpSpendTx); |
| 796 | + StateForProposedFederator stateForProposedFederator = new StateForProposedFederator(svpSpendTxWFS); |
| 797 | + |
| 798 | + Ethereum ethereum = mock(Ethereum.class); |
| 799 | + AtomicReference<EthereumListener> ethereumListener = new AtomicReference<>(); |
| 800 | + doAnswer((InvocationOnMock invocation) -> { |
| 801 | + ethereumListener.set((EthereumListener) invocation.getArguments()[0]); |
| 802 | + return null; |
| 803 | + }).when(ethereum).addListener(any(EthereumListener.class)); |
| 804 | + |
| 805 | + FederatorSupport federatorSupport = mock(FederatorSupport.class); |
| 806 | + FederationMember federationMember = proposedFederation.getMembers().get(0); |
| 807 | + doReturn(federationMember).when(federatorSupport).getFederationMember(); |
| 808 | + // return svp spend tx waiting for signatures |
| 809 | + doReturn(Optional.of(stateForProposedFederator)).when(federatorSupport).getStateForProposedFederator(); |
| 810 | + // returns zero pegouts waiting for signatures |
| 811 | + doReturn(mock(StateForFederator.class)).when(federatorSupport).getStateForFederator(); |
| 812 | + |
| 813 | + ECKey ecKey = new ECKey(); |
| 814 | + ECPublicKey signerPublicKey = new ECPublicKey(federationMember.getBtcPublicKey().getPubKey()); |
| 815 | + |
| 816 | + ECDSASigner signer = mock(ECDSASigner.class); |
| 817 | + doReturn(signerPublicKey).when(signer).getPublicKey(BTC.getKeyId()); |
| 818 | + doReturn(1).when(signer).getVersionForKeyId(ArgumentMatchers.any(KeyId.class)); |
| 819 | + doReturn(ecKey.doSign(new byte[]{})).when(signer).sign(any(KeyId.class), any(SignerMessage.class)); |
| 820 | + |
| 821 | + PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class); |
| 822 | + doReturn(Constants.mainnet()).when(powpegNodeSystemProperties).getNetworkConstants(); |
| 823 | + doReturn(true).when(powpegNodeSystemProperties).isPegoutEnabled(); |
| 824 | + when(powpegNodeSystemProperties.getPegoutSignedCacheTtl()) |
| 825 | + .thenReturn(PEGOUT_SIGNED_CACHE_TTL); |
| 826 | + |
| 827 | + SignerMessageBuilderFactory signerMessageBuilderFactory = new SignerMessageBuilderFactory( |
| 828 | + mock(ReceiptStore.class) |
| 829 | + ); |
| 830 | + |
| 831 | + Keccak256 blockHash = createHash(2); |
| 832 | + Long blockNumber = 0L; |
| 833 | + Block block = mock(Block.class); |
| 834 | + TransactionReceipt txReceipt = mock(TransactionReceipt.class); |
| 835 | + TransactionInfo txInfo = mock(TransactionInfo.class); |
| 836 | + when(block.getHash()).thenReturn(blockHash); |
| 837 | + when(block.getNumber()).thenReturn(blockNumber); |
| 838 | + when(blockStore.getBlockByHash(blockHash.getBytes())).thenReturn(block); |
| 839 | + when(txInfo.getReceipt()).thenReturn(txReceipt); |
| 840 | + when(txInfo.getBlockHash()).thenReturn(blockHash.getBytes()); |
| 841 | + when(receiptStore.getInMainChain(svpSpendCreationRskTxHash.getBytes(), blockStore)).thenReturn(Optional.of(txInfo)); |
| 842 | + |
| 843 | + ReleaseCreationInformationGetter releaseCreationInformationGetter = |
| 844 | + spy(new ReleaseCreationInformationGetter( |
| 845 | + receiptStore, blockStore |
| 846 | + )); |
| 847 | + |
| 848 | + BtcReleaseClientStorageSynchronizer storageSynchronizer = |
| 849 | + mock(BtcReleaseClientStorageSynchronizer.class); |
| 850 | + when(storageSynchronizer.isSynced()).thenReturn(true); |
| 851 | + |
| 852 | + BtcReleaseClient btcReleaseClient = new BtcReleaseClient( |
| 853 | + ethereum, |
| 854 | + federatorSupport, |
| 855 | + powpegNodeSystemProperties, |
| 856 | + mock(NodeBlockProcessor.class) |
| 857 | + ); |
| 858 | + |
| 859 | + ActivationConfig activationConfig = mock(ActivationConfig.class); |
| 860 | + when(activationConfig.isActive(ConsensusRule.RSKIP419, bestBlock.getNumber())).thenReturn(true); |
| 861 | + |
| 862 | + btcReleaseClient.setup( |
| 863 | + signer, |
| 864 | + activationConfig, |
| 865 | + signerMessageBuilderFactory, |
| 866 | + releaseCreationInformationGetter, |
| 867 | + mock(ReleaseRequirementsEnforcer.class), |
| 868 | + mock(BtcReleaseClientStorageAccessor.class), |
| 869 | + storageSynchronizer |
| 870 | + ); |
| 871 | + |
| 872 | + btcReleaseClient.start(proposedFederation); |
| 873 | + |
| 874 | + // Act |
| 875 | + ethereumListener.get().onBestBlock(bestBlock, Collections.emptyList()); |
| 876 | + |
| 877 | + // Assert |
| 878 | + |
| 879 | + // We should have searched by the expected svp spend tx hash |
| 880 | + BitcoinUtils.removeSignaturesFromTransactionWithP2shMultiSigInputs(svpSpendTx); |
| 881 | + assertEquals(svpSpendTxHashBeforeSigning, svpSpendTx.getHash()); |
| 882 | + verify(releaseCreationInformationGetter).getTxInfoToSign( |
| 883 | + anyInt(), |
| 884 | + eq(svpSpendCreationRskTxHash), |
| 885 | + eq(svpSpendTx)); |
| 886 | + |
| 887 | + // We should have added a signature for the svp spend tx |
| 888 | + verify(federatorSupport).addSignature( |
| 889 | + anyList(), |
| 890 | + any(byte[].class) |
| 891 | + ); |
| 892 | + } |
| 893 | + |
768 | 894 | @Test |
769 | 895 | void onBestBlock_whenBothPegoutAndSvpSpendTxWaitingForSignaturesAreAvailableAndFederatorIsOnlyPartOfProposedFederation_shouldOnlyAddOneSignature() throws Exception { |
770 | 896 | // Arrange |
|
0 commit comments