Skip to content

Commit

Permalink
Refactor claim transaction validation class and implement some sugges…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
rmoreliovlabs committed May 21, 2024
1 parent 4c74307 commit 6baeb88
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@

package co.rsk;

import co.rsk.core.bc.ClaimTransactionValidator;
import co.rsk.util.CommandLineFixture;
import co.rsk.util.EthSwapUtil;
import co.rsk.util.HexUtils;
import co.rsk.util.OkHttpClientTestFixture;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.Response;
import org.ethereum.config.Constants;
import org.ethereum.core.BlockTxSignatureCache;
import org.ethereum.core.CallTransaction;
import org.ethereum.core.ReceivedTxSignatureCache;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.junit.jupiter.api.Assertions;
Expand All @@ -44,7 +46,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class EtherSwapTest {
public class ClaimTransactionTest {
private static final int LOCAL_PORT = 4444;

private static final CallTransaction.Function CALL_LOCK_FUNCTION = CallTransaction.Function.fromSignature(
Expand All @@ -68,6 +70,8 @@ public class EtherSwapTest {
private String strBaseArgs;
private String baseJavaCmd;

private ClaimTransactionValidator claimTransactionValidator;

@BeforeEach
public void setup() throws IOException {
String projectPath = System.getProperty("user.dir");
Expand All @@ -85,6 +89,8 @@ public void setup() throws IOException {
baseArgs = new String[]{"--regtest"};
strBaseArgs = String.join(" ", baseArgs);
baseJavaCmd = String.format("java %s", String.format("-Drsk.conf.file=%s", rskConfFile));

claimTransactionValidator = new ClaimTransactionValidator( new BlockTxSignatureCache(new ReceivedTxSignatureCache()), Constants.regtest());
}

private Response lockTxRequest(String refundAddress, byte[] lockData, BigInteger amount) throws IOException {
Expand Down Expand Up @@ -155,7 +161,7 @@ void whenClaimTxIsSend_shouldShouldFailDueToLowFundsInContract() throws Exceptio
String refundAddress = "0x7986b3df570230288501eea3d890bd66948c9b79";
String claimAddress = "0x8486054b907b0d79569723c761b7113736d32c5a";
byte[] preimage = "preimage".getBytes(StandardCharsets.UTF_8);
byte[] preimageHash = HashUtil.sha256(EthSwapUtil.encodePacked(preimage));
byte[] preimageHash = HashUtil.sha256(claimTransactionValidator.encodePacked(preimage));
BigInteger amount = BigInteger.valueOf(5000);

byte[] lockData = CALL_LOCK_FUNCTION.encode(
Expand Down Expand Up @@ -207,7 +213,7 @@ void whenClaimTxIsSend_shouldExecuteEvenIfSenderHasNoFunds() throws Exception {
String refundAddress = "0x7986b3df570230288501eea3d890bd66948c9b79";
String claimAddress = "0x8486054b907b0d79569723c761b7113736d32c5a";
byte[] preimage = "preimage".getBytes(StandardCharsets.UTF_8);
byte[] preimageHash = HashUtil.sha256(EthSwapUtil.encodePacked(preimage));
byte[] preimageHash = HashUtil.sha256(claimTransactionValidator.encodePacked(preimage));
BigInteger amount = BigInteger.valueOf(500000);

byte[] lockData = CALL_LOCK_FUNCTION.encode(
Expand Down Expand Up @@ -262,11 +268,11 @@ void whenClaimTxIsSend_shouldExecuteEvenIfSenderHasNoFunds() throws Exception {
}

@Test
void whenClaimTxIsSentTwice_secondClaimTxShoulNotBeIncludedInMempool() throws Exception {
void whenClaimTxIsSentTwice_secondClaimTxShouldNotBeIncludedInMempool() throws Exception {
String refundAddress = "0x7986b3df570230288501eea3d890bd66948c9b79";
String claimAddress = "0x8486054b907b0d79569723c761b7113736d32c5a";
byte[] preimage = "preimage".getBytes(StandardCharsets.UTF_8);
byte[] preimageHash = HashUtil.sha256(EthSwapUtil.encodePacked(preimage));
byte[] preimageHash = HashUtil.sha256(claimTransactionValidator.encodePacked(preimage));
BigInteger amount = BigInteger.valueOf(500000);

byte[] lockData = CALL_LOCK_FUNCTION.encode(
Expand Down
7 changes: 4 additions & 3 deletions rskj-core/src/main/java/co/rsk/core/bc/BlockExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import co.rsk.metrics.profilers.Metric;
import co.rsk.metrics.profilers.Profiler;
import co.rsk.metrics.profilers.ProfilerFactory;
import co.rsk.util.EthSwapUtil;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.config.Constants;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
Expand Down Expand Up @@ -66,6 +65,7 @@ public class BlockExecutor {

private final Constants constants;
private final SignatureCache signatureCache;
private final ClaimTransactionValidator claimTransactionValidator;

public BlockExecutor(
ActivationConfig activationConfig,
Expand All @@ -78,6 +78,7 @@ public BlockExecutor(
this.activationConfig = activationConfig;
this.constants = constants;
this.signatureCache = signatureCache;
this.claimTransactionValidator = new ClaimTransactionValidator(signatureCache, constants);
}

/**
Expand Down Expand Up @@ -291,8 +292,8 @@ private BlockResult executeInternal(
for (Transaction tx : block.getTransactionsList()) {
logger.trace("apply block: [{}] tx: [{}] ", block.getNumber(), i);

if (EthSwapUtil.isClaimTx(tx, constants)
&& !EthSwapUtil.hasLockedFunds(tx,signatureCache, track)) {
if (claimTransactionValidator.isClaimTx(tx)
&& !claimTransactionValidator.hasLockedFunds(tx, track)) {
logger.warn("block: [{}] discarded claim tx: [{}], because the funds it tries to claim no longer exist in contract",
block.getNumber(),
tx.getHash());
Expand Down
113 changes: 0 additions & 113 deletions rskj-core/src/main/java/co/rsk/core/bc/ClaimTransactionInfoHolder.java

This file was deleted.

Loading

0 comments on commit 6baeb88

Please sign in to comment.