Skip to content

Commit

Permalink
Added option to set signature fields to 0x0 for Remasc on RPC response
Browse files Browse the repository at this point in the history
  • Loading branch information
Iago Lluque committed Sep 15, 2022
1 parent b137b43 commit 2528180
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public long workSubmissionRateLimitInMills() {
public boolean updateWorkOnNewTransaction() {
return getBoolean("miner.server.updateWorkOnNewTransaction", false);
}

public long minerMinGasPrice() {
return configFromFiles.getLong("miner.minGasPrice");
}
Expand Down Expand Up @@ -414,4 +414,8 @@ public boolean fastBlockPropagation() {
public Integer getMessageQueueMaxSize() {
return configFromFiles.getInt("peer.messageQueue.maxSizePerPeer");
}

public boolean rpcZeroSignatureIfRemasc() {
return configFromFiles.getBoolean("rpc.zeroSignatureIfRemasc");
}
}
10 changes: 5 additions & 5 deletions rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public BlockInformationResult getBlockInformationResult(BlockInformation blockIn
}

public BlockResultDTO getBlockResult(Block b, boolean fullTx) {
return BlockResultDTO.fromBlock(b, fullTx, this.blockStore, config.skipRemasc());
return BlockResultDTO.fromBlock(b, fullTx, this.blockStore, config.skipRemasc(), config.rpcZeroSignatureIfRemasc());
}

public BlockInformationResult[] eth_getBlocksByNumber(String number) {
Expand Down Expand Up @@ -705,7 +705,7 @@ public TransactionResultDTO eth_getTransactionByHash(String transactionHash) {

for (Transaction tx : txs) {
if (tx.getHash().equals(txHash)) {
return s = new TransactionResultDTO(null, null, tx);
return s = new TransactionResultDTO(null, null, tx, config.rpcZeroSignatureIfRemasc());
}
}
} else {
Expand All @@ -722,7 +722,7 @@ public TransactionResultDTO eth_getTransactionByHash(String transactionHash) {
return null;
}

return s = new TransactionResultDTO(block, txInfo.getIndex(), txInfo.getReceipt().getTransaction());
return s = new TransactionResultDTO(block, txInfo.getIndex(), txInfo.getReceipt().getTransaction(), config.rpcZeroSignatureIfRemasc());
} finally {
logger.debug("eth_getTransactionByHash({}): {}", transactionHash, s);
}
Expand All @@ -746,7 +746,7 @@ public TransactionResultDTO eth_getTransactionByBlockHashAndIndex(String blockHa

Transaction tx = b.getTransactionsList().get(idx);

return s = new TransactionResultDTO(b, idx, tx);
return s = new TransactionResultDTO(b, idx, tx, config.rpcZeroSignatureIfRemasc());
} finally {
if (logger.isDebugEnabled()) {
logger.debug("eth_getTransactionByBlockHashAndIndex({}, {}): {}", blockHash, index, s);
Expand All @@ -769,7 +769,7 @@ public TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrI
return null;
}

s = new TransactionResultDTO(block.get(), idx, txs.get(idx));
s = new TransactionResultDTO(block.get(), idx, txs.get(idx), config.rpcZeroSignatureIfRemasc());
return s;
} finally {
if (logger.isDebugEnabled()) {
Expand Down
33 changes: 16 additions & 17 deletions rskj-core/src/main/java/org/ethereum/rpc/dto/BlockResultDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@

package org.ethereum.rpc.dto;

import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import co.rsk.core.BlockDifficulty;
import co.rsk.core.Coin;
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.util.HexUtils;
import org.ethereum.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.ethereum.db.BlockStore;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -27,16 +35,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.ethereum.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.ethereum.db.BlockStore;

import co.rsk.core.BlockDifficulty;
import co.rsk.core.Coin;
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.util.HexUtils;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;


public class BlockResultDTO {
Expand Down Expand Up @@ -123,7 +122,7 @@ private BlockResultDTO(
this.paidFees = paidFees != null ? HexUtils.toQuantityJsonHex(paidFees.getBytes()) : null;
}

public static BlockResultDTO fromBlock(Block b, boolean fullTx, BlockStore blockStore, boolean skipRemasc) {
public static BlockResultDTO fromBlock(Block b, boolean fullTx, BlockStore blockStore, boolean skipRemasc, boolean zeroSignatureIfRemasc) {
if (b == null) {
return null;
}
Expand All @@ -136,7 +135,7 @@ public static BlockResultDTO fromBlock(Block b, boolean fullTx, BlockStore block
List<Transaction> blockTransactions = b.getTransactionsList();
// For full tx will present as TransactionResultDTO otherwise just as transaction hash
List<Object> transactions = IntStream.range(0, blockTransactions.size())
.mapToObj(txIndex -> toTransactionResult(txIndex, b, fullTx, skipRemasc))
.mapToObj(txIndex -> toTransactionResult(txIndex, b, fullTx, skipRemasc, zeroSignatureIfRemasc))
.filter(Objects::nonNull)
.collect(Collectors.toList());

Expand Down Expand Up @@ -182,15 +181,15 @@ public static BlockResultDTO fromBlock(Block b, boolean fullTx, BlockStore block
);
}

private static Object toTransactionResult(int transactionIndex, Block block, boolean fullTx, boolean skipRemasc) {
private static Object toTransactionResult(int transactionIndex, Block block, boolean fullTx, boolean skipRemasc, boolean zeroSignatureIfRemasc) {
Transaction transaction = block.getTransactionsList().get(transactionIndex);

if(skipRemasc && transaction.isRemascTransaction(transactionIndex, block.getTransactionsList().size())) {
return null;
}

if(fullTx) {
return new TransactionResultDTO(block, transactionIndex, transaction);
return new TransactionResultDTO(block, transactionIndex, transaction, zeroSignatureIfRemasc);
}

return transaction.getHash().toJsonString();
Expand Down Expand Up @@ -291,4 +290,4 @@ public String getHashForMergedMining() {
public String getPaidFees() {
return paidFees;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@

package org.ethereum.rpc.dto;

import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.signature.ECDSASignature;

import co.rsk.core.Coin;
import co.rsk.remasc.RemascTransaction;
import co.rsk.util.HexUtils;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.signature.ECDSASignature;

/**
* Created by Ruben on 8/1/2016.
*/
public class TransactionResultDTO {

private static final String HEX_ZERO = "0x0";

private String hash;
private String nonce;
private String blockHash;
Expand All @@ -46,7 +47,7 @@ public class TransactionResultDTO {
private String r;
private String s;

public TransactionResultDTO(Block b, Integer index, Transaction tx) {
public TransactionResultDTO(Block b, Integer index, Transaction tx, boolean zeroSignatureIfRemasc) {
hash = tx.getHash().toJsonString();

nonce = HexUtils.toQuantityJsonHex(tx.getNonce());
Expand All @@ -69,13 +70,18 @@ public TransactionResultDTO(Block b, Integer index, Transaction tx) {

input = HexUtils.toUnformattedJsonHex(tx.getData());

if (!(tx instanceof RemascTransaction)) {
boolean isRemasc = tx instanceof RemascTransaction;
if (!isRemasc) {
ECDSASignature signature = tx.getSignature();

v = String.format("0x%02x", tx.getEncodedV());

r = HexUtils.toQuantityJsonHex(signature.getR());
s = HexUtils.toQuantityJsonHex(signature.getS());
} else if (zeroSignatureIfRemasc) {
v = HEX_ZERO;
r = HEX_ZERO;
s = HEX_ZERO;
}
}

Expand Down Expand Up @@ -135,4 +141,4 @@ public String getS() {
return s;
}

}
}
1 change: 1 addition & 0 deletions rskj-core/src/main/resources/expected.conf
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ rpc = {
}
]
skipRemasc: <enabled>
zeroSignatureIfRemasc = <enabled>
gasEstimationCap = <gas>
}
wire = {
Expand Down
6 changes: 4 additions & 2 deletions rskj-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ miner {
# the number of milliseconds that should pass from a previous submission of a mining solution before next one is allowed
# set this value to zero or any negative number to disable this limit
workSubmissionRateLimitInMills = 0
# Toggles the behaviour of the miner server to prepare a new work for the miners when the node gets a new pending transaction

# Toggles the behaviour of the miner server to prepare a new work for the miners when the node gets a new pending transaction
updateWorkOnNewTransaction = false
}

Expand Down Expand Up @@ -413,6 +413,8 @@ rpc {
}
]
skipRemasc: false
# set signature to zero (instead of null) for Remasc transaction on RPC DTOs
zeroSignatureIfRemasc = true
gasEstimationCap = 6800000 # block gasLimit, rpc DoS protection for gas estimation
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@

package org.ethereum.rpc.dto;

import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import co.rsk.core.BlockDifficulty;
import co.rsk.core.genesis.TestGenesisLoader;
import co.rsk.remasc.RemascTransaction;
import co.rsk.test.builders.BlockBuilder;
import co.rsk.test.builders.TransactionBuilder;
import co.rsk.util.HexUtils;
import org.ethereum.core.Block;
import org.ethereum.core.Blockchain;
import org.ethereum.core.Transaction;
Expand All @@ -39,14 +34,21 @@
import org.junit.Before;
import org.junit.Test;

import co.rsk.core.BlockDifficulty;
import co.rsk.core.genesis.TestGenesisLoader;
import co.rsk.remasc.RemascTransaction;
import co.rsk.test.builders.BlockBuilder;
import co.rsk.test.builders.TransactionBuilder;
import co.rsk.util.HexUtils;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BlockResultDTOTest {

private static final String HEX_ZERO = "0x0";

private Block block;
private BlockStore blockStore;
public static final Transaction TRANSACTION = new TransactionBuilder().buildRandomTransaction();
Expand All @@ -63,7 +65,7 @@ public void setup() {

@Test
public void getBlockResultDTOWithRemascAndTransactionHashes() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, false);
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, false, false);
List<String> transactionHashes = transactionHashesByBlock(blockResultDTO);

Assert.assertNotNull(blockResultDTO);
Expand All @@ -74,7 +76,7 @@ public void getBlockResultDTOWithRemascAndTransactionHashes() {

@Test
public void getBlockResultDTOWithoutRemascAndTransactionHashes() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, true);
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, true, false);
List<String> transactionHashes = transactionHashesByBlock(blockResultDTO);

Assert.assertNotNull(blockResultDTO);
Expand All @@ -86,7 +88,7 @@ public void getBlockResultDTOWithoutRemascAndTransactionHashes() {
@Test
public void getBlockResultDTOWithoutRemasc_emptyTransactions() {
Block block = buildBlockWithTransactions(Arrays.asList(REMASC_TRANSACTION));
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, true);
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, false, blockStore, true, false);

Assert.assertEquals(HexUtils.toUnformattedJsonHex(EMPTY_TRIE_HASH), blockResultDTO.getTransactionsRoot());

Expand All @@ -96,20 +98,41 @@ public void getBlockResultDTOWithoutRemasc_emptyTransactions() {


@Test
public void getBlockResultDTOWithRemascAndFullTransactions() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, true, blockStore, false);
public void getBlockResultDTOWithNullSignatureRemascAndFullTransactions() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, true, blockStore, false, false);
Assert.assertNotNull(blockResultDTO);
Assert.assertEquals(2, blockResultDTO.getTransactions().size());

List<String> transactionResultsHashes = transactionResultsByBlock(blockResultDTO).stream().map(e -> e.getHash()).collect(Collectors.toList());
TransactionResultDTO regularTransaction = (TransactionResultDTO) blockResultDTO.getTransactions().get(0);
Assert.assertNotNull(regularTransaction);

TransactionResultDTO remascTransaction = (TransactionResultDTO) blockResultDTO.getTransactions().get(1);
Assert.assertNotNull(remascTransaction);
Assert.assertNull(remascTransaction.getV());
Assert.assertNull(remascTransaction.getR());
Assert.assertNull(remascTransaction.getS());
}


@Test
public void getBlockResultDTOWithWithZeroSignatureRemascAndFullTransactions() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, true, blockStore, false, true);
Assert.assertNotNull(blockResultDTO);
Assert.assertEquals(2, blockResultDTO.getTransactions().size());
Assert.assertTrue(transactionResultsHashes.contains(TRANSACTION.getHash().toJsonString()));
Assert.assertTrue(transactionResultsHashes.contains(REMASC_TRANSACTION.getHash().toJsonString()));

TransactionResultDTO regularTransaction = (TransactionResultDTO) blockResultDTO.getTransactions().get(0);
Assert.assertNotNull(regularTransaction);

TransactionResultDTO remascTransaction = (TransactionResultDTO) blockResultDTO.getTransactions().get(1);
Assert.assertNotNull(remascTransaction);
Assert.assertEquals(HEX_ZERO, remascTransaction.getV());
Assert.assertEquals(HEX_ZERO, remascTransaction.getR());
Assert.assertEquals(HEX_ZERO, remascTransaction.getS());
}

@Test
public void getBlockResultDTOWithoutRemascAndFullTransactions() {
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, true, blockStore, true);
BlockResultDTO blockResultDTO = BlockResultDTO.fromBlock(block, true, blockStore, true, false);

List<String> transactionResultsHashes = transactionResultsByBlock(blockResultDTO).stream().map(e -> e.getHash()).collect(Collectors.toList());

Expand Down
Loading

0 comments on commit 2528180

Please sign in to comment.