Search in sources :

Example 56 with LogInfo

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

the class BridgeSupportTest method addSignatureMultipleInputsPartiallyValid.

@Test
public void addSignatureMultipleInputsPartiallyValid() throws Exception {
    // Federation is the genesis federation ATM
    Federation federation = bridgeConstants.getGenesisFederation();
    Repository repository = createRepository();
    final Keccak256 keccak256 = PegTestUtils.createHash3(1);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    BtcTransaction prevTx1 = new BtcTransaction(btcParams);
    TransactionOutput prevOut1 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx1.addOutput(prevOut1);
    BtcTransaction prevTx2 = new BtcTransaction(btcParams);
    TransactionOutput prevOut2 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx2.addOutput(prevOut2);
    BtcTransaction prevTx3 = new BtcTransaction(btcParams);
    TransactionOutput prevOut3 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx3.addOutput(prevOut3);
    BtcTransaction t = new BtcTransaction(btcParams);
    TransactionOutput output = new TransactionOutput(btcParams, t, Coin.COIN, new BtcECKey().toAddress(btcParams));
    t.addOutput(output);
    t.addInput(prevOut1).setScriptSig(createBaseInputScriptThatSpendsFromTheFederation(federation));
    t.addInput(prevOut2).setScriptSig(createBaseInputScriptThatSpendsFromTheFederation(federation));
    t.addInput(prevOut3).setScriptSig(createBaseInputScriptThatSpendsFromTheFederation(federation));
    provider.getRskTxsWaitingForSignatures().put(keccak256, t);
    provider.save();
    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(repository, contractAddress, bridgeConstants, activationsAfterForks), repository, eventLogger, mock(Block.class), null);
    // Generate valid signatures for inputs
    List<byte[]> derEncodedSigsFirstFed = new ArrayList<>();
    List<byte[]> derEncodedSigsSecondFed = new ArrayList<>();
    BtcECKey privateKeyOfFirstFed = BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS.get(0);
    BtcECKey privateKeyOfSecondFed = BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS.get(1);
    BtcECKey.ECDSASignature lastSig = null;
    for (int i = 0; i < 3; i++) {
        Script inputScript = t.getInput(i).getScriptSig();
        List<ScriptChunk> chunks = inputScript.getChunks();
        byte[] program = chunks.get(chunks.size() - 1).data;
        Script redeemScript = new Script(program);
        Sha256Hash sighash = t.hashForSignature(i, redeemScript, BtcTransaction.SigHash.ALL, false);
        // Sign the last input with a random key
        // but keep the good signature for a subsequent call
        BtcECKey.ECDSASignature sig = privateKeyOfFirstFed.sign(sighash);
        if (i == 2) {
            lastSig = sig;
            sig = new BtcECKey().sign(sighash);
        }
        derEncodedSigsFirstFed.add(sig.encodeToDER());
        derEncodedSigsSecondFed.add(privateKeyOfSecondFed.sign(sighash).encodeToDER());
    }
    // Sign with two valid signatures and one invalid signature
    bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    // Sign with two valid signatures and one malformed signature
    byte[] malformedSignature = new byte[lastSig.encodeToDER().length];
    for (int i = 0; i < malformedSignature.length; i++) {
        malformedSignature[i] = (byte) i;
    }
    derEncodedSigsFirstFed.set(2, malformedSignature);
    bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    // Sign with fully valid signatures for same federator
    derEncodedSigsFirstFed.set(2, lastSig.encodeToDER());
    bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    // Sign with second federation
    bridgeSupport.addSignature(findPublicKeySignedBy(federation.getBtcPublicKeys(), privateKeyOfSecondFed), derEncodedSigsSecondFed, keccak256.getBytes());
    bridgeSupport.save();
    provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertThat(logs, is(not(empty())));
    Assert.assertThat(logs, hasSize(5));
    LogInfo releaseTxEvent = logs.get(4);
    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());
    // Verify all inputs fully signed
    for (int i = 0; i < releaseTx.getInputs().size(); i++) {
        Script retrievedScriptSig = releaseTx.getInput(i).getScriptSig();
        Assert.assertEquals(4, retrievedScriptSig.getChunks().size());
        assertTrue(Objects.requireNonNull(retrievedScriptSig.getChunks().get(1).data).length > 0);
        assertTrue(Objects.requireNonNull(retrievedScriptSig.getChunks().get(2).data).length > 0);
    }
}
Also used : PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Script(co.rsk.bitcoinj.script.Script) LogInfo(org.ethereum.vm.LogInfo) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) RLPList(org.ethereum.util.RLPList) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 57 with LogInfo

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

the class LogsNotificationEmitterTest method logInfo.

private LogInfo logInfo(final RskAddress logSource, byte... data) {
    LogInfo logInfo = mock(LogInfo.class);
    when(logInfo.getAddress()).thenReturn(logSource.getBytes());
    when(logInfo.getData()).thenReturn(data);
    return logInfo;
}
Also used : LogInfo(org.ethereum.vm.LogInfo)

Example 58 with LogInfo

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

the class BridgeEventLoggerImplTest method logPeginBtc.

@Test
public void logPeginBtc() {
    // Setup event logger
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    List<LogInfo> eventLogs = new LinkedList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(null, activations, eventLogs);
    RskAddress rskAddress = mock(RskAddress.class);
    when(rskAddress.toString()).thenReturn("0x00000000000000000000000000000000000000");
    // Mock btc transaction
    BtcTransaction mockedTx = mock(BtcTransaction.class);
    when(mockedTx.getHash()).thenReturn(PegTestUtils.createHash(0));
    Coin amount = Coin.SATOSHI;
    int protocolVersion = 1;
    // Act
    eventLogger.logPeginBtc(rskAddress, mockedTx, amount, protocolVersion);
    // Assert log size
    Assert.assertEquals(1, eventLogs.size());
    LogInfo logResult = eventLogs.get(0);
    CallTransaction.Function event = BridgeEvents.PEGIN_BTC.getEvent();
    // Assert address that made the log
    Assert.assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(logResult.getAddress()));
    // Assert log topics
    Assert.assertEquals(3, logResult.getTopics().size());
    byte[][] topics = event.encodeEventTopics(rskAddress.toString(), mockedTx.getHash().getBytes());
    for (int i = 0; i < topics.length; i++) {
        Assert.assertArrayEquals(topics[i], logResult.getTopics().get(i).getData());
    }
    // Assert log data
    byte[] encodedData = event.encodeEventData(amount.getValue(), protocolVersion);
    Assert.assertArrayEquals(encodedData, logResult.getData());
}
Also used : LogInfo(org.ethereum.vm.LogInfo) LinkedList(java.util.LinkedList) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) RskAddress(co.rsk.core.RskAddress) CallTransaction(org.ethereum.core.CallTransaction) Test(org.junit.Test)

Example 59 with LogInfo

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

the class BridgeEventLoggerImplTest method logReleaseBtcBeforeRskip146.

@Test
public void logReleaseBtcBeforeRskip146() {
    when(activations.isActive(ConsensusRule.RSKIP146)).thenReturn(false);
    // Setup Btc transaction
    Keccak256 rskTxHash = PegTestUtils.createHash3(1);
    // Act
    eventLogger.logReleaseBtc(btcTx, rskTxHash.getBytes());
    commonAssertLogs(eventLogs);
    assertTopics(1, eventLogs);
    LogInfo logResult = eventLogs.get(0);
    // Assert address that made the log
    Assert.assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(logResult.getAddress()));
    // Assert log topics
    Assert.assertEquals(1, logResult.getTopics().size());
    List<DataWord> topics = Collections.singletonList(Bridge.RELEASE_BTC_TOPIC);
    for (int i = 0; i < topics.size(); i++) {
        Assert.assertEquals(topics.get(i), logResult.getTopics().get(i));
    }
    // Assert log data
    byte[] encodedData = RLP.encodeList(RLP.encodeString(btcTx.getHashAsString()), RLP.encodeElement(btcTx.bitcoinSerialize()));
    Assert.assertArrayEquals(encodedData, logResult.getData());
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 60 with LogInfo

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

the class BridgeEventLoggerImplTest method logRejectedPegin.

@Test
public void logRejectedPegin() {
    // Setup event logger
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    List<LogInfo> eventLogs = new LinkedList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(null, activations, eventLogs);
    BtcTransaction btcTx = new BtcTransaction(BridgeRegTestConstants.getInstance().getBtcParams());
    eventLogger.logRejectedPegin(btcTx, RejectedPeginReason.PEGIN_CAP_SURPASSED);
    Assert.assertEquals(1, eventLogs.size());
    LogInfo entry = eventLogs.get(0);
    Assert.assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(entry.getAddress()));
    // Assert address that made the log
    LogInfo result = eventLogs.get(0);
    Assert.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress());
    // Assert log topics
    Assert.assertEquals(2, result.getTopics().size());
    CallTransaction.Function event = BridgeEvents.REJECTED_PEGIN.getEvent();
    byte[][] topics = event.encodeEventTopics(btcTx.getHash().getBytes());
    for (int i = 0; i < topics.length; i++) {
        Assert.assertArrayEquals(topics[i], result.getTopics().get(i).getData());
    }
    // Assert log data
    Assert.assertArrayEquals(event.encodeEventData(RejectedPeginReason.PEGIN_CAP_SURPASSED.getValue()), result.getData());
}
Also used : LogInfo(org.ethereum.vm.LogInfo) LinkedList(java.util.LinkedList) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) RskAddress(co.rsk.core.RskAddress) CallTransaction(org.ethereum.core.CallTransaction) 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