Search in sources :

Example 31 with LogInfo

use of org.ethereum.vm.LogInfo in project rskj by rsksmart.

the class BridgeSupportReleaseBtcTest method handmade_release_after_rskip_146_185_rejected_lowAmount.

@Test
public void handmade_release_after_rskip_146_185_rejected_lowAmount() throws IOException {
    when(activationMock.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
    when(activationMock.isActive(ConsensusRule.RSKIP185)).thenReturn(true);
    List<LogInfo> logInfo = new ArrayList<>();
    BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo));
    bridgeSupport = initBridgeSupport(eventLogger, activationMock);
    releaseTx = buildReleaseRskTx(Coin.ZERO);
    bridgeSupport.releaseBtc(releaseTx);
    Transaction rskTx = buildUpdateTx();
    rskTx.sign(SENDER.getPrivKeyBytes());
    bridgeSupport.updateCollections(rskTx);
    verify(repository, times(1)).transfer(argThat((a) -> a.equals(PrecompiledContracts.BRIDGE_ADDR)), argThat((a) -> a.equals(new RskAddress(SENDER.getAddress()))), argThat((a) -> a.equals(co.rsk.core.Coin.fromBitcoin(Coin.ZERO))));
    assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    assertEquals(2, logInfo.size());
    verify(eventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());
    verify(eventLogger, times(1)).logReleaseBtcRequestRejected(any(), any(), any());
    verify(eventLogger, times(1)).logUpdateCollections(any());
}
Also used : BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) RejectedPegoutReason(co.rsk.peg.utils.RejectedPegoutReason) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) Hex(org.bouncycastle.util.encoders.Hex) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) ArgumentCaptor(org.mockito.ArgumentCaptor) BridgeConstants(co.rsk.config.BridgeConstants) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) Before(org.junit.Before) LogInfo(org.ethereum.vm.LogInfo) HashUtil(org.ethereum.crypto.HashUtil) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test) IOException(java.io.IOException) MutableTrieImpl(co.rsk.db.MutableTrieImpl) Instant(java.time.Instant) Repository(org.ethereum.core.Repository) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) Program(org.ethereum.vm.program.Program) Mockito(org.mockito.Mockito) List(java.util.List) InternalTransaction(org.ethereum.vm.program.InternalTransaction) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) Transaction(org.ethereum.core.Transaction) 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) LogInfo(org.ethereum.vm.LogInfo) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) ArrayList(java.util.ArrayList) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 32 with LogInfo

use of org.ethereum.vm.LogInfo in project rskj by rsksmart.

the class BridgeSupportReleaseBtcTest method release_before_rskip_219.

@Test
public void release_before_rskip_219() throws IOException {
    when(activationMock.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
    when(activationMock.isActive(ConsensusRule.RSKIP185)).thenReturn(true);
    when(activationMock.isActive(ConsensusRule.RSKIP219)).thenReturn(false);
    List<LogInfo> logInfo = new ArrayList<>();
    BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo));
    bridgeSupport = initBridgeSupport(eventLogger, activationMock);
    // Get a value between old and new minimum pegout values
    Coin middle = bridgeConstants.getLegacyMinimumPegoutTxValueInSatoshis().subtract(bridgeConstants.getMinimumPegoutTxValueInSatoshis()).div(2);
    Coin value = bridgeConstants.getMinimumPegoutTxValueInSatoshis().add(middle);
    assertTrue(value.isLessThan(bridgeConstants.getLegacyMinimumPegoutTxValueInSatoshis()));
    assertTrue(value.isGreaterThan(bridgeConstants.getMinimumPegoutTxValueInSatoshis()));
    bridgeSupport.releaseBtc(buildReleaseRskTx(value));
    Transaction rskTx = buildUpdateTx();
    rskTx.sign(SENDER.getPrivKeyBytes());
    assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    assertEquals(1, logInfo.size());
    verify(eventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());
    verify(eventLogger, times(1)).logReleaseBtcRequestRejected(any(), any(), any());
}
Also used : BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) LogInfo(org.ethereum.vm.LogInfo) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) ArrayList(java.util.ArrayList) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 33 with LogInfo

use of org.ethereum.vm.LogInfo in project rskj by rsksmart.

the class BridgeSupportTest method addSignatureFromValidFederator.

/**
 * Helper method to test addSignature() with a valid federatorPublicKey parameter and both valid/invalid signatures
 *
 * @param privateKeysToSignWith keys used to sign the tx. Federator key when we want to produce a valid signature, a random key when we want to produce an invalid signature
 * @param numberOfInputsToSign  There is just 1 input. 1 when testing the happy case, other values to test attacks/bugs.
 * @param signatureCanonical    Signature should be canonical. true when testing the happy case, false to test attacks/bugs.
 * @param signTwice             Sign again with the same key
 * @param expectedResult        "InvalidParameters", "PartiallySigned" or "FullySigned"
 */
private void addSignatureFromValidFederator(List<BtcECKey> privateKeysToSignWith, int numberOfInputsToSign, boolean signatureCanonical, boolean signTwice, String expectedResult) throws Exception {
    // Federation is the genesis federation ATM
    Federation federation = bridgeConstants.getGenesisFederation();
    Repository repository = createRepository();
    final Keccak256 keccak256 = PegTestUtils.createHash3();
    Repository track = repository.startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    BtcTransaction prevTx = new BtcTransaction(btcParams);
    TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, federation.getAddress());
    prevTx.addOutput(prevOut);
    BtcTransaction t = new BtcTransaction(btcParams);
    TransactionOutput output = new TransactionOutput(btcParams, t, Coin.COIN, new BtcECKey().toAddress(btcParams));
    t.addOutput(output);
    t.addInput(prevOut).setScriptSig(createBaseInputScriptThatSpendsFromTheFederation(federation));
    provider.getRskTxsWaitingForSignatures().put(keccak256, t);
    provider.save();
    track.commit();
    track = repository.startTracking();
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    List<LogInfo> logs = new ArrayList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(bridgeConstants, activations, logs);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, new BridgeStorageProvider(track, contractAddress, bridgeConstants, activationsAfterForks), track, eventLogger, mock(Block.class), null);
    Script inputScript = t.getInputs().get(0).getScriptSig();
    List<ScriptChunk> chunks = inputScript.getChunks();
    byte[] program = chunks.get(chunks.size() - 1).data;
    Script redeemScript = new Script(program);
    Sha256Hash sighash = t.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    BtcECKey.ECDSASignature sig = privateKeysToSignWith.get(0).sign(sighash);
    if (!signatureCanonical) {
        sig = new BtcECKey.ECDSASignature(sig.r, BtcECKey.CURVE.getN().subtract(sig.s));
    }
    byte[] derEncodedSig = sig.encodeToDER();
    List derEncodedSigs = new ArrayList();
    for (int i = 0; i < numberOfInputsToSign; i++) {
        derEncodedSigs.add(derEncodedSig);
    }
    bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeysToSignWith.get(0)), derEncodedSigs, keccak256.getBytes());
    if (signTwice) {
        // Create another valid signature with the same private key
        ECDSASigner signer = new ECDSASigner();
        X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1");
        ECDomainParameters CURVE = new ECDomainParameters(CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH());
        ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeysToSignWith.get(0).getPrivKey(), CURVE);
        signer.init(true, privKey);
        BigInteger[] components = signer.generateSignature(sighash.getBytes());
        BtcECKey.ECDSASignature sig2 = new BtcECKey.ECDSASignature(components[0], components[1]).toCanonicalised();
        bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeysToSignWith.get(0)), Lists.newArrayList(sig2.encodeToDER()), keccak256.getBytes());
    }
    if (privateKeysToSignWith.size() > 1) {
        BtcECKey.ECDSASignature sig2 = privateKeysToSignWith.get(1).sign(sighash);
        byte[] derEncodedSig2 = sig2.encodeToDER();
        List derEncodedSigs2 = new ArrayList();
        for (int i = 0; i < numberOfInputsToSign; i++) {
            derEncodedSigs2.add(derEncodedSig2);
        }
        bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeysToSignWith.get(1)), derEncodedSigs2, keccak256.getBytes());
    }
    bridgeSupport.save();
    track.commit();
    provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    if ("FullySigned".equals(expectedResult)) {
        Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
        Assert.assertThat(logs, is(not(empty())));
        Assert.assertThat(logs, hasSize(3));
        LogInfo releaseTxEvent = logs.get(2);
        Assert.assertThat(releaseTxEvent.getTopics(), hasSize(1));
        Assert.assertThat(releaseTxEvent.getTopics(), hasItem(Bridge.RELEASE_BTC_TOPIC));
        BtcTransaction releaseTx = new BtcTransaction(btcParams, ((RLPList) RLP.decode2(releaseTxEvent.getData()).get(0)).get(1).getRLPData());
        Script retrievedScriptSig = releaseTx.getInput(0).getScriptSig();
        Assert.assertEquals(4, retrievedScriptSig.getChunks().size());
        Assert.assertEquals(true, retrievedScriptSig.getChunks().get(1).data.length > 0);
        Assert.assertEquals(true, retrievedScriptSig.getChunks().get(2).data.length > 0);
    } else {
        Script retrievedScriptSig = provider.getRskTxsWaitingForSignatures().get(keccak256).getInput(0).getScriptSig();
        Assert.assertEquals(4, retrievedScriptSig.getChunks().size());
        // for "InvalidParameters"
        boolean expectSignatureToBePersisted = false;
        if ("PartiallySigned".equals(expectedResult)) {
            expectSignatureToBePersisted = true;
        }
        Assert.assertEquals(expectSignatureToBePersisted, retrievedScriptSig.getChunks().get(1).data.length > 0);
        Assert.assertEquals(false, retrievedScriptSig.getChunks().get(2).data.length > 0);
    }
}
Also used : ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) RLPList(org.ethereum.util.RLPList) Script(co.rsk.bitcoinj.script.Script) LogInfo(org.ethereum.vm.LogInfo) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) RLPList(org.ethereum.util.RLPList) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) Block(org.ethereum.core.Block) BigInteger(java.math.BigInteger)

Example 34 with LogInfo

use of org.ethereum.vm.LogInfo in project rskj by rsksmart.

the class TransactionTest method encodeReceiptTest.

@Ignore
@Test
public void encodeReceiptTest() {
    String data = "f90244a0f5ff3fbd159773816a7c707a9b8cb6bb778b934a8f6466c7830ed970498f4b688301e848b902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dbda94cd2a3d9f938e13cd947ec05abc7fe734df8dd826c083a1a1a1";
    byte[] stateRoot = Hex.decode("f5ff3fbd159773816a7c707a9b8cb6bb778b934a8f6466c7830ed970498f4b68");
    byte[] gasUsed = Hex.decode("01E848");
    Bloom bloom = new Bloom(Hex.decode("0000000000000000800000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
    LogInfo logInfo1 = new LogInfo(Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"), null, Hex.decode("a1a1a1"));
    List<LogInfo> logs = new ArrayList<>();
    logs.add(logInfo1);
    // TODO calculate cumulative gas
    TransactionReceipt receipt = new TransactionReceipt(stateRoot, gasUsed, gasUsed, bloom, logs, TransactionReceipt.SUCCESS_STATUS);
    assertEquals(data, ByteUtil.toHexString(receipt.getEncoded()));
}
Also used : LogInfo(org.ethereum.vm.LogInfo) ArrayList(java.util.ArrayList) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 35 with LogInfo

use of org.ethereum.vm.LogInfo in project rskj by rsksmart.

the class LogInfoTest method test_2.

// rlp decode
@Test
public void test_2() {
    LogInfo log = new LogInfo(Hex.decode("d5ccd26ba09ce1d85148b5081fa3ed77949417be"), null, null);
    assertEquals("d794d5ccd26ba09ce1d85148b5081fa3ed77949417bec080", ByteUtil.toHexString(log.getEncoded()));
    logger.info("{}", log);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) Test(org.junit.Test)

Aggregations

LogInfo (org.ethereum.vm.LogInfo)74 DataWord (org.ethereum.vm.DataWord)36 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)25 CallTransaction (org.ethereum.core.CallTransaction)23 Transaction (org.ethereum.core.Transaction)17 RskAddress (co.rsk.core.RskAddress)13 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)12 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)11 InternalTransaction (org.ethereum.vm.program.InternalTransaction)11 Block (org.ethereum.core.Block)9 Repository (org.ethereum.core.Repository)9 RepositoryImpl (co.rsk.db.RepositoryImpl)8 Keccak256 (co.rsk.crypto.Keccak256)7 BigInteger (java.math.BigInteger)7 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)7 RLPList (org.ethereum.util.RLPList)6 LinkedList (java.util.LinkedList)4 RLPElement (org.ethereum.util.RLPElement)4 Coin (co.rsk.core.Coin)3