Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change logReleaseBtcRequestRejected to receive co.rsk.core.Coin #2707

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions rskj-core/src/main/java/co/rsk/peg/BridgeEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public enum BridgeEvents {
}
),
RELEASE_REQUEST_REJECTED("release_request_rejected",
new CallTransaction.Param[]{
new CallTransaction.Param(true, Fields.SENDER, SolidityType.getType(SolidityType.ADDRESS)),
new CallTransaction.Param(false, Fields.AMOUNT, SolidityType.getType(SolidityType.UINT256)),
new CallTransaction.Param(false, Fields.REASON, SolidityType.getType(SolidityType.INT256))
}
new CallTransaction.Param[]{
new CallTransaction.Param(true, Fields.SENDER, SolidityType.getType(SolidityType.ADDRESS)),
new CallTransaction.Param(false, Fields.AMOUNT, SolidityType.getType(SolidityType.UINT256)),
new CallTransaction.Param(false, Fields.REASON, SolidityType.getType(SolidityType.INT256))
}
),
BATCH_PEGOUT_CREATED("batch_pegout_created",
new CallTransaction.Param[]{
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ private void refundAndEmitRejectEvent(Coin value, RskAddress senderAddress, Reje
}

private void emitRejectEvent(Coin value, RskAddress senderAddress, RejectedPegoutReason reason) {
eventLogger.logReleaseBtcRequestRejected(senderAddress.toHexString(), value, reason);
eventLogger.logReleaseBtcRequestRejected(senderAddress, value, reason);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ default void logReleaseBtcRequestReceived(String sender, Address btcDestinationA
throw new UnsupportedOperationException();
}

default void logReleaseBtcRequestRejected(String sender, Coin amount, RejectedPegoutReason reason) {
default void logReleaseBtcRequestRejected(RskAddress sender, co.rsk.core.Coin amount, RejectedPegoutReason reason) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,24 @@
package co.rsk.peg.utils;

import co.rsk.bitcoinj.core.*;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.peg.BridgeEvents;
import co.rsk.peg.bitcoin.UtxoUtils;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.peg.federation.Federation;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.constants.FederationConstants;
import co.rsk.peg.pegin.RejectedPeginReason;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ConsensusRule;
import org.ethereum.core.Block;
import org.ethereum.core.CallTransaction;
import org.ethereum.core.SignatureCache;
import org.ethereum.core.Transaction;
import org.ethereum.core.*;
import org.ethereum.crypto.ECKey;
import org.ethereum.util.ByteUtil;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo;
import org.ethereum.vm.PrecompiledContracts;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.ethereum.vm.*;

/**
* Responsible for logging events triggered by BridgeContract.
Expand All @@ -57,7 +51,12 @@ public class BridgeEventLoggerImpl implements BridgeEventLogger {
private final List<LogInfo> logs;
private final ActivationConfig.ForBlock activations;

public BridgeEventLoggerImpl(BridgeConstants bridgeConstants, ActivationConfig.ForBlock activations, List<LogInfo> logs, SignatureCache signatureCache) {
public BridgeEventLoggerImpl(
BridgeConstants bridgeConstants,
ActivationConfig.ForBlock activations,
List<LogInfo> logs,
SignatureCache signatureCache) {

this.activations = activations;
this.bridgeConstants = bridgeConstants;
this.signatureCache = signatureCache;
Expand Down Expand Up @@ -219,11 +218,13 @@ private void logReleaseBtcRequestReceived(String sender, String btcDestinationAd
}

@Override
public void logReleaseBtcRequestRejected(String sender, Coin amount, RejectedPegoutReason reason) {
public void logReleaseBtcRequestRejected(RskAddress sender, co.rsk.core.Coin amount, RejectedPegoutReason reason) {
CallTransaction.Function event = BridgeEvents.RELEASE_REQUEST_REJECTED.getEvent();
byte[][] encodedTopicsInBytes = event.encodeEventTopics(sender);
byte[][] encodedTopicsInBytes = event.encodeEventTopics(sender.toHexString());
List<DataWord> encodedTopics = LogInfo.byteArrayToList(encodedTopicsInBytes);
byte[] encodedData = event.encodeEventData(amount.getValue(), reason.getValue());
byte[] encodedData = activations.isActive(ConsensusRule.RSKIP427) ?
event.encodeEventData(amount.asBigInteger(), reason.getValue()) :
event.encodeEventData(amount.toBitcoin().getValue(), reason.getValue());

this.logs.add(new LogInfo(BRIDGE_CONTRACT_ADDRESS, encodedTopics, encodedData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@
import static org.mockito.Mockito.*;

class BridgeSupportReleaseBtcTest {

private static final String TO_ADDRESS = "0000000000000000000000000000000000000006";
private static final BigInteger DUST_AMOUNT = new BigInteger("1");
private static final BigInteger NONCE = new BigInteger("0");
private static final BigInteger GAS_PRICE = new BigInteger("100");
private static final BigInteger GAS_LIMIT = new BigInteger("1000");
private static final String DATA = "80af2871";
private static final ECKey SENDER = new ECKey();
private static final RskAddress BRIDGE_ADDRESS = PrecompiledContracts.BRIDGE_ADDR;

private BridgeConstants bridgeConstants;
private FederationConstants federationConstants;
Expand All @@ -101,7 +101,7 @@ class BridgeSupportReleaseBtcTest {
private FeePerKbSupport feePerKbSupport;

@BeforeEach
void setUpOnEachTest() throws IOException {
void setUpOnEachTest() {
signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
bridgeConstants = new BridgeRegTestConstants();
federationConstants = bridgeConstants.getFederationConstants();
Expand Down Expand Up @@ -231,8 +231,13 @@ void handmade_release_after_rskip_146() throws IOException {
when(activationMock.isActive(ConsensusRule.RSKIP185)).thenReturn(false);

List<LogInfo> logInfo = new ArrayList<>();
BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo, signatureCache));
bridgeSupport = initBridgeSupport(eventLogger, activationMock);
BridgeEventLoggerImpl bridgeEventLogger = spy(new BridgeEventLoggerImpl(
bridgeConstants,
activationMock,
logInfo,
signatureCache
));
bridgeSupport = initBridgeSupport(bridgeEventLogger, activationMock);

bridgeSupport.releaseBtc(releaseTx);

Expand All @@ -243,7 +248,7 @@ void handmade_release_after_rskip_146() throws IOException {
verify(repository, never()).transfer(any(), any(), any());
assertEquals(1, provider.getPegoutsWaitingForConfirmations().getEntries().size());
assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
verify(eventLogger, times(1)).logReleaseBtcRequested(
verify(bridgeEventLogger, times(1)).logReleaseBtcRequested(
any(byte[].class),
any(BtcTransaction.class),
any(Coin.class)
Expand All @@ -257,8 +262,13 @@ void handmade_release_after_rskip_146_185() throws IOException {
when(activationMock.isActive(ConsensusRule.RSKIP326)).thenReturn(false);

List<LogInfo> logInfo = new ArrayList<>();
BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo, signatureCache));
bridgeSupport = initBridgeSupport(eventLogger, activationMock);
BridgeEventLoggerImpl bridgeEventLogger = spy(new BridgeEventLoggerImpl(
bridgeConstants,
activationMock,
logInfo,
signatureCache
));
bridgeSupport = initBridgeSupport(bridgeEventLogger, activationMock);

bridgeSupport.releaseBtc(releaseTx);

Expand All @@ -272,13 +282,13 @@ void handmade_release_after_rskip_146_185() throws IOException {
assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());

assertEquals(3, logInfo.size());
verify(eventLogger, times(1)).logReleaseBtcRequested(
verify(bridgeEventLogger, times(1)).logReleaseBtcRequested(
any(byte[].class),
any(BtcTransaction.class),
any(Coin.class)
);
verify(eventLogger, times(1)).logReleaseBtcRequestReceived(any(), any(), any());
verify(eventLogger, times(1)).logUpdateCollections(any());
verify(bridgeEventLogger, times(1)).logReleaseBtcRequestReceived(any(), any(), any());
verify(bridgeEventLogger, times(1)).logUpdateCollections(any());

LogInfo logInfo1 = logInfo.get(0);
CallTransaction.Function event = BridgeEvents.RELEASE_REQUEST_RECEIVED_LEGACY.getEvent();
Expand Down Expand Up @@ -1204,20 +1214,23 @@ private void testPegoutMinimumWithFeeVerificationPass(Coin feePerKB, Coin value)
verify(eventLogger, never()).logReleaseBtcRequestRejected(any(), any(), any());
}

private void testPegoutMinimumWithFeeVerificationRejectedByLowAmount(Coin feePerKB, Coin value)
throws IOException {
when(activationMock.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activationMock.isActive(ConsensusRule.RSKIP185)).thenReturn(true);
when(activationMock.isActive(ConsensusRule.RSKIP219)).thenReturn(true);

RskAddress bridgeAddress = PrecompiledContracts.BRIDGE_ADDR;
private void testPegoutMinimumWithFeeVerificationRejectedByLowAmount(Coin feePerKB, Coin value) throws IOException {
ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0);

List<LogInfo> logInfo = new ArrayList<>();
BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo, signatureCache));
bridgeSupport = initBridgeSupport(eventLogger, activationMock);
BridgeEventLoggerImpl bridgeEventLogger = spy(new BridgeEventLoggerImpl(
bridgeConstants,
irisActivations,
logInfo,
signatureCache
));
bridgeSupport = initBridgeSupport(bridgeEventLogger, irisActivations);
when(feePerKbSupport.getFeePerKb()).thenReturn(feePerKB);

int pegoutSize = BridgeUtils.getRegularPegoutTxSize(activationMock, federationStorageProvider.getNewFederation(federationConstants, activationMock));
int pegoutSize = BridgeUtils.getRegularPegoutTxSize(
irisActivations,
federationStorageProvider.getNewFederation(federationConstants, irisActivations)
);
Coin minValueAccordingToFee = feePerKbSupport.getFeePerKb().div(1000).times(pegoutSize);
Coin minValueWithGapAboveFee = minValueAccordingToFee.add(minValueAccordingToFee.times(bridgeConstants.getMinimumPegoutValuePercentageToReceiveAfterFee()).div(100));

Expand All @@ -1232,33 +1245,39 @@ private void testPegoutMinimumWithFeeVerificationRejectedByLowAmount(Coin feePer

co.rsk.core.Coin coin = co.rsk.core.Coin.fromBitcoin(value);

verify(repository, times(1)).transfer(bridgeAddress, senderAddress, coin);
verify(repository, times(1)).transfer(BRIDGE_ADDRESS, senderAddress, coin);

assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());

assertEquals(1, logInfo.size());

verify(eventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());
verify(bridgeEventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());

verify(eventLogger, times(1))
.logReleaseBtcRequestRejected(senderAddress.toHexString(), value, RejectedPegoutReason.LOW_AMOUNT);
verify(bridgeEventLogger, times(1)).logReleaseBtcRequestRejected(
senderAddress,
value,
RejectedPegoutReason.LOW_AMOUNT
);

}

private void testPegoutMinimumWithFeeVerificationRejectedByFeeAboveValue(Coin feePerKB, Coin value)
throws IOException {
when(activationMock.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activationMock.isActive(ConsensusRule.RSKIP185)).thenReturn(true);
when(activationMock.isActive(ConsensusRule.RSKIP219)).thenReturn(true);

RskAddress bridgeAddress = PrecompiledContracts.BRIDGE_ADDR;
private void testPegoutMinimumWithFeeVerificationRejectedByFeeAboveValue(Coin feePerKB, Coin value) throws IOException {
ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0);

List<LogInfo> logInfo = new ArrayList<>();
BridgeEventLoggerImpl eventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activationMock, logInfo, signatureCache));
bridgeSupport = initBridgeSupport(eventLogger, activationMock);
BridgeEventLoggerImpl bridgeEventLogger = spy(new BridgeEventLoggerImpl(
bridgeConstants,
irisActivations,
logInfo,
signatureCache
));
bridgeSupport = initBridgeSupport(bridgeEventLogger, irisActivations);
when(feePerKbSupport.getFeePerKb()).thenReturn(feePerKB);

int pegoutSize = BridgeUtils.getRegularPegoutTxSize(activationMock, federationStorageProvider.getNewFederation(federationConstants, activationMock));
int pegoutSize = BridgeUtils.getRegularPegoutTxSize(
irisActivations,
federationStorageProvider.getNewFederation(federationConstants, irisActivations)
);
Coin minValueAccordingToFee = feePerKbSupport.getFeePerKb().div(1000).times(pegoutSize);
Coin minValueWithGapAboveFee = minValueAccordingToFee.add(minValueAccordingToFee.times(bridgeConstants.getMinimumPegoutValuePercentageToReceiveAfterFee()).div(100));

Expand All @@ -1273,16 +1292,16 @@ private void testPegoutMinimumWithFeeVerificationRejectedByFeeAboveValue(Coin fe

co.rsk.core.Coin coin = co.rsk.core.Coin.fromBitcoin(value);

verify(repository, times(1)).transfer(bridgeAddress, senderAddress, coin);
verify(repository, times(1)).transfer(BRIDGE_ADDRESS, senderAddress, coin);

assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());

assertEquals(1, logInfo.size());

verify(eventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());
verify(bridgeEventLogger, never()).logReleaseBtcRequestReceived(any(), any(), any());

verify(eventLogger, times(1)).logReleaseBtcRequestRejected(
senderAddress.toHexString(),
verify(bridgeEventLogger, times(1)).logReleaseBtcRequestRejected(
senderAddress,
value,
RejectedPegoutReason.FEE_ABOVE_VALUE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BridgeEventLoggerImplTest {
private static final FederationConstants FEDERATION_CONSTANTS = BRIDGE_CONSTANTS.getFederationConstants();
private static final NetworkParameters NETWORK_PARAMETERS = BRIDGE_CONSTANTS.getBtcParams();
private static final BtcTransaction BTC_TRANSACTION = new BtcTransaction(NETWORK_PARAMETERS);
private static final RskAddress RSK_ADDRESS = new RskAddress("0x0000000000000000000000000000000000000000");
private static final RskAddress RSK_ADDRESS = new RskAddress("0x0000000000000000000000000000000000000101");
private static final Keccak256 RSK_TX_HASH = RskTestUtils.createHash(1);

private List<LogInfo> eventLogs;
Expand Down Expand Up @@ -458,11 +458,32 @@ void testLogReleaseBtcRequestReceivedAfterRSKIP326HardFork() {
}

@Test
void testLogReleaseBtcRequestRejected() {
Coin amount = Coin.COIN;
void testLogReleaseBtcRequestRejected_preRSKIP427_logAmountAsSatoshis() {
ActivationConfig.ForBlock arrowheadActivations = ActivationConfigsForTest.arrowhead631().forBlock(0);
eventLogger = new BridgeEventLoggerImpl(BRIDGE_CONSTANTS, arrowheadActivations, eventLogs, signatureCache);

co.rsk.core.Coin amount = co.rsk.core.Coin.valueOf(100_000_000_000_000_000L);
RejectedPegoutReason reason = RejectedPegoutReason.LOW_AMOUNT;

eventLogger.logReleaseBtcRequestRejected(RSK_ADDRESS, amount, reason);

commonAssertLogs(eventLogs);
assertTopics(2, eventLogs);
assertEvent(
eventLogs,
0,
BridgeEvents.RELEASE_REQUEST_REJECTED.getEvent(),
new Object[]{RSK_ADDRESS.toString()},
new Object[]{amount.toBitcoin().getValue(), reason.getValue()}
);
}

@Test
void testLogReleaseBtcRequestRejected_postRSKIP427_logAmountAsWeis() {
co.rsk.core.Coin amount = co.rsk.core.Coin.valueOf(100_000_000_000_000_000L);
RejectedPegoutReason reason = RejectedPegoutReason.LOW_AMOUNT;

eventLogger.logReleaseBtcRequestRejected(RSK_ADDRESS.toString(), amount, reason);
eventLogger.logReleaseBtcRequestRejected(RSK_ADDRESS, amount, reason);

commonAssertLogs(eventLogs);
assertTopics(2, eventLogs);
Expand All @@ -471,7 +492,7 @@ void testLogReleaseBtcRequestRejected() {
0,
BridgeEvents.RELEASE_REQUEST_REJECTED.getEvent(),
new Object[]{RSK_ADDRESS.toString()},
new Object[]{amount.value, reason.getValue()}
new Object[]{amount.asBigInteger(), reason.getValue()}
);
}

Expand Down
Loading
Loading