Skip to content

Commit

Permalink
Merge pull request #2256 from rsksmart/fix-snapshot-unit-tests
Browse files Browse the repository at this point in the history
Fix snapshot sonarcloud and unit tests
  • Loading branch information
Vovchyk authored Mar 8, 2024
2 parents 59db1eb + 49ad04f commit 40a23f6
Show file tree
Hide file tree
Showing 19 changed files with 774 additions and 50 deletions.
1 change: 0 additions & 1 deletion rskj-core/src/main/java/co/rsk/net/NodeMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import co.rsk.util.MaxSizeHashMap;
import co.rsk.util.TraceUtils;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.core.BlockIdentifier;
import org.ethereum.crypto.HashUtil;
import org.ethereum.net.server.ChannelManager;
import org.slf4j.Logger;
Expand Down
1 change: 0 additions & 1 deletion rskj-core/src/main/java/co/rsk/net/SnapshotProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ private void generateChunkRequestTasks() {
chunkTasks.add(task);
from += chunkSize * 1024L;
}
logger.debug("Generated: {} chunk request tasks.", chunkTasks.size());
}

private void startRequestingChunks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.ethereum.util.RLP;

import java.math.BigInteger;

public class SnapStatusRequestMessage extends Message {

public SnapStatusRequestMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand Down
1 change: 0 additions & 1 deletion rskj-core/src/main/java/co/rsk/net/sync/SnapSyncState.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.List;

public class SnapSyncState extends BaseSyncState {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.db.TransactionInfo;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,7 +33,6 @@ public class RskModuleImpl implements RskModule {
private final ReceiptStore receiptStore;
private final Web3InformationRetriever web3InformationRetriever;
private final Flusher flusher;

private final NodeStopper nodeStopper;

private final RskSystemProperties rskSystemProperties;
Expand Down
43 changes: 31 additions & 12 deletions rskj-core/src/main/java/co/rsk/trie/TrieDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import co.rsk.util.HexUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.Keccak256Helper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -300,9 +299,18 @@ public byte[] getValue() {
}

public boolean isTerminal() {
return (!this.leftNodePresent && !this.rightNodePresent) ||
!((this.leftNodePresent && !this.leftNodeEmbedded) ||
(this.rightNodePresent && !this.rightNodeEmbedded));
// old impl:
// return (!this.leftNodePresent && !this.rightNodePresent) ||
// !((this.leftNodePresent && !this.leftNodeEmbedded) ||
// (this.rightNodePresent && !this.rightNodeEmbedded));
if (!this.leftNodePresent && !this.rightNodePresent) {
return true;
}

boolean isLeftTerminal = !this.leftNodePresent || this.leftNodeEmbedded;
boolean isRightTerminal = !this.rightNodePresent || this.rightNodeEmbedded;

return isLeftTerminal && isRightTerminal;
}

public byte[] getLeft() {
Expand Down Expand Up @@ -401,14 +409,25 @@ public String toDescription() {
* Based on {@link Trie:toMessage()}
*/
public byte[] toMessage() {
ByteBuffer buffer = ByteBuffer.allocate(
1 + // flags
(this.sharedPrefixPresent ? SharedPathSerializer.calculateVarIntSize(this.pathLength) + this.path.length : 0) +
serializedLength(leftNodePresent, leftNodeEmbedded, left) +
serializedLength(rightNodePresent, rightNodeEmbedded, right) +
((leftNodePresent || rightNodePresent) ? childrenSize.getSizeInBytes() : 0) +
(hasLongVal ? Keccak256Helper.DEFAULT_SIZE_BYTES + Uint24.BYTES : value.length)
);
// ByteBuffer buffer = ByteBuffer.allocate(
// 1 + // flags
// (this.sharedPrefixPresent ? SharedPathSerializer.calculateVarIntSize(this.pathLength) + this.path.length : 0) +
// serializedLength(leftNodePresent, leftNodeEmbedded, left) +
// serializedLength(rightNodePresent, rightNodeEmbedded, right) +
// ((leftNodePresent || rightNodePresent) ? childrenSize.getSizeInBytes() : 0) +
// (hasLongVal ? Keccak256Helper.DEFAULT_SIZE_BYTES + Uint24.BYTES : value.length)
// );

int sharedPrefixSize = this.sharedPrefixPresent ? SharedPathSerializer.calculateVarIntSize(this.pathLength) + this.path.length : 0;
int leftNodeSize = serializedLength(leftNodePresent, leftNodeEmbedded, left);
int rightNodeSize = serializedLength(rightNodePresent, rightNodeEmbedded, right);
int childrenSizeBytes = (leftNodePresent || rightNodePresent) ? childrenSize.getSizeInBytes() : 0;
int valueSize = hasLongVal ? Keccak256Helper.DEFAULT_SIZE_BYTES + Uint24.BYTES : value.length;

int totalSize = 1 + sharedPrefixSize + leftNodeSize + rightNodeSize + childrenSizeBytes + valueSize;

ByteBuffer buffer = ByteBuffer.allocate(totalSize);

buffer.put(flags);
if (this.sharedPrefixPresent) {
SharedPathSerializer.serializeBytes(buffer, this.pathLength, this.path);
Expand Down
19 changes: 13 additions & 6 deletions rskj-core/src/main/java/co/rsk/trie/TrieDTOInOrderIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ private TrieDTO findByChildrenSize(long offset, TrieDTO nodeDTO, Deque<TrieDTO>

if (nodeDTO.isLeftNodePresent() && !nodeDTO.isLeftNodeEmbedded()) {
TrieDTO left = getNode(nodeDTO.getLeftHash());

if (left == null) {
throw new NullPointerException("Left node is null");
}

long maxLeftSize = left.getTotalSize();

if (offset <= maxLeftSize) {
visiting.push(nodeDTO);
return findByChildrenSize(offset, left, visiting);
Expand All @@ -64,25 +70,26 @@ private TrieDTO findByChildrenSize(long offset, TrieDTO nodeDTO, Deque<TrieDTO>
if (offset <= maxLeftSize) {
return pushAndReturn(nodeDTO, visiting, (offset - left.getTotalSize()));
}
} else if (nodeDTO.isLeftNodePresent() && nodeDTO.isLeftNodeEmbedded()) {
if (offset <= nodeDTO.getLeftSize()) {
} else if (nodeDTO.isLeftNodePresent() && nodeDTO.isLeftNodeEmbedded() && (offset <= nodeDTO.getLeftSize())) {
return pushAndReturn(nodeDTO, visiting, offset);
}
}

if (nodeDTO.isRightNodePresent() && !nodeDTO.isRightNodeEmbedded()) {
TrieDTO right = getNode(nodeDTO.getRightHash());
if (right == null) {
throw new NullPointerException("Right node is null.");
}

long maxParentSize = nodeDTO.getTotalSize() - right.getTotalSize();
long maxRightSize = nodeDTO.getTotalSize();

if (maxParentSize < offset && offset <= maxRightSize) {
preRootNodes.add(nodeDTO);
return findByChildrenSize(offset - maxParentSize, right, visiting);
}
} else if (nodeDTO.isRightNodeEmbedded()) {
if (offset <= nodeDTO.getTotalSize()) {
} else if (nodeDTO.isRightNodeEmbedded() && (offset <= nodeDTO.getTotalSize())) {
long leftAndParentSize = nodeDTO.getTotalSize() - nodeDTO.getRightSize();
return pushAndReturn(nodeDTO, visiting, offset - leftAndParentSize);
}
}
}
if (nodeDTO.getTotalSize() >= offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
public class TrieDTOInOrderRecoverer {

private static final Logger logger = LoggerFactory.getLogger(TrieDTOInOrderRecoverer.class);
private TrieDTOInOrderRecoverer() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static Optional<TrieDTO> recoverTrie(TrieDTO[] trieCollection, Consumer<? super TrieDTO> processTrieDTO) {
Optional<TrieDTO> result = recoverSubtree(trieCollection, 0, trieCollection.length - 1, processTrieDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.ethereum.core.Block;
import org.ethereum.core.BlockIdentifier;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.HashUtil;
import org.ethereum.net.message.ReasonCode;
import org.ethereum.sync.SyncPool;
import org.slf4j.Logger;
Expand Down
4 changes: 2 additions & 2 deletions rskj-core/src/main/java/org/ethereum/net/server/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public double score(MessageType type) {
}

private double priority(MessageType type) {

switch (type) {
case TRANSACTIONS:
return 2;
Expand Down Expand Up @@ -152,8 +151,9 @@ private double priority(MessageType type) {
case BLOCK_HEADERS_RESPONSE_MESSAGE:
return 5;
// TODO (pato) add priority for Snap sync messages.
default:
return 0.0;
}
return 0.0;
}
public synchronized void imported(boolean best) {
if (best) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.net.messages;

import co.rsk.blockchain.utils.BlockGenerator;
import org.ethereum.core.Block;
import org.ethereum.util.RLP;
import org.junit.jupiter.api.Test;

import java.math.BigInteger;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

class SnapBlocksRequestMessageTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: SnapBlocksRequestMessageTest is not referenced within this codebase. If not used as an external API it should be removed.

private final Block block4Test = new BlockGenerator().getBlock(1);
private final SnapBlocksRequestMessage underTest = new SnapBlocksRequestMessage(block4Test.getNumber());


@Test
void getMessageType_returnCorrectMessageType() {
//given-when
MessageType messageType = underTest.getMessageType();

//then
assertEquals(MessageType.SNAP_BLOCKS_REQUEST_MESSAGE, messageType);
}

@Test
void getEncodedMessage_returnExpectedByteArray() {
//given default block 4 test

//when
byte[] encodedMessage = underTest.getEncodedMessage();

//then
assertThat(encodedMessage, equalTo(RLP.encodeList(RLP.encodeBigInteger(BigInteger.ONE))));
}

@Test
void getBlockNumber_returnTheExpectedValue() {
//given default block 4 test

//when
long blockNumber = underTest.getBlockNumber();

//then
assertThat(blockNumber, equalTo(block4Test.getNumber()));
}

@Test
void givenAcceptIsCalled_messageVisitorIsAppliedFormessage() {
//given
Block block = new BlockGenerator().getBlock(1);
SnapBlocksRequestMessage message = new SnapBlocksRequestMessage(block.getNumber());
MessageVisitor visitor = mock(MessageVisitor.class);

//when
message.accept(visitor);

//then
verify(visitor, times(1)).apply(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.net.messages;

import co.rsk.blockchain.utils.BlockGenerator;
import co.rsk.config.TestSystemProperties;
import co.rsk.core.BlockDifficulty;
import co.rsk.db.HashMapBlocksIndex;
import org.ethereum.core.Block;
import org.ethereum.core.BlockFactory;
import org.ethereum.datasource.HashMapDB;
import org.ethereum.db.BlockStore;
import org.ethereum.db.IndexedBlockStore;
import org.ethereum.util.RLP;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

class SnapBlocksResponseMessageTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: SnapBlocksResponseMessageTest is not referenced within this codebase. If not used as an external API it should be removed.

private final TestSystemProperties config = new TestSystemProperties();
private final BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
private final BlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
private final Block block4Test = new BlockGenerator().getBlock(1);
private final List<Block> blockList = Collections.singletonList(new BlockGenerator().getBlock(1));
private final List<BlockDifficulty> blockDifficulties = Collections.singletonList(indexedBlockStore.getTotalDifficultyForHash(block4Test.getHash().getBytes()));
private final SnapBlocksResponseMessage underTest = new SnapBlocksResponseMessage(blockList, blockDifficulties);


@Test
void getMessageType_returnCorrectMessageType() {
//given-when
MessageType messageType = underTest.getMessageType();

//then
assertEquals(MessageType.SNAP_BLOCKS_RESPONSE_MESSAGE, messageType);
}

@Test
void getEncodedMessage_returnExpectedByteArray() {
//given default block 4 test
byte[] expectedEncodedMessage = RLP.encodeList(
RLP.encodeList(RLP.encode(block4Test.getEncoded())),
RLP.encodeList(RLP.encode(blockDifficulties.get(0).getBytes())));
//when
byte[] encodedMessage = underTest.getEncodedMessage();

//then
assertThat(encodedMessage, equalTo(expectedEncodedMessage));
}

@Test
void getDifficulties_returnTheExpectedValue() {
//given default block 4 test

//when
List<BlockDifficulty> difficultiesReturned = underTest.getDifficulties();
//then
assertThat(difficultiesReturned, equalTo(blockDifficulties));
}

@Test
void getBlocks_returnTheExpectedValue() {
//given default block 4 test

//when
List<Block> blocksReturned = underTest.getBlocks();
//then
assertThat(blocksReturned, equalTo(blockList));
}

@Test
void givenAcceptIsCalled_messageVisitorIsAppliedForMessage() {
//given
SnapBlocksResponseMessage message = new SnapBlocksResponseMessage(blockList, blockDifficulties);
MessageVisitor visitor = mock(MessageVisitor.class);

//when
message.accept(visitor);

//then
verify(visitor, times(1)).apply(message);
}

}
Loading

0 comments on commit 40a23f6

Please sign in to comment.