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