Skip to content

Commit

Permalink
Resolve deduplication issue and unecessary imports
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioDemianLerner authored and Vovchyk committed Jun 15, 2022
1 parent 45f4b31 commit 2077e07
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
18 changes: 13 additions & 5 deletions rskj-core/src/main/java/co/rsk/trie/SharedPathSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static void serializeInto(TrieKeySlice sharedPath,ByteBuffer buffer) {
buffer.put(sharedPath.encode());
}

// Returns the size of the path prefix when path needs encoding.
// The prefix indicates the length of the shared path in bits.
// TO DO: Rename to something like getEncodedPathBitlengthSize()
private static int lsharedSize(TrieKeySlice sharedPath) {
if (!isPresent(sharedPath)) {
return 0;
Expand All @@ -88,11 +91,7 @@ private int lsharedSize() {
return lsharedSize(this.sharedPath);
}

public static TrieKeySlice deserialize(ByteBuffer message, boolean sharedPrefixPresent) {
if (!sharedPrefixPresent) {
return TrieKeySlice.empty();
}

public static int getPathBitsLength(ByteBuffer message) {
int lshared; // this is a bit length

// upgrade to int so we can compare positive values
Expand All @@ -106,12 +105,21 @@ public static TrieKeySlice deserialize(ByteBuffer message, boolean sharedPrefixP
} else {
lshared = (int) readVarInt(message).value;
}
return lshared;
}
public static TrieKeySlice deserialize(ByteBuffer message, boolean sharedPrefixPresent) {
if (!sharedPrefixPresent) {
return TrieKeySlice.empty();
}

int lshared = getPathBitsLength(message);

int lencoded = PathEncoder.calculateEncodedLength(lshared);
byte[] encodedKey = new byte[lencoded];
message.get(encodedKey);
return TrieKeySlice.fromEncoded(encodedKey, 0, lshared, lencoded);
}

private static VarInt readVarInt(ByteBuffer message) {
// read without touching the buffer position so when we read into bytes it contains the header
int first = Byte.toUnsignedInt(message.get(message.position()));
Expand Down
2 changes: 0 additions & 2 deletions rskj-core/src/main/java/co/rsk/trie/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package co.rsk.trie;

import co.rsk.bitcoinj.core.VarInt;
import co.rsk.core.RskAddress;
import co.rsk.core.types.ints.Uint16;
import co.rsk.core.types.ints.Uint24;
import co.rsk.core.types.ints.Uint8;
Expand All @@ -30,7 +29,6 @@
import co.rsk.util.NodeStopper;
import org.ethereum.crypto.Keccak256Helper;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.TrieKeyMapper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.slf4j.Logger;
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/trie/TrieKeySlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TrieKeySlice {
private final int offset;
private final int limit;

private TrieKeySlice(byte[] expandedKey, int offset, int limit) {
public TrieKeySlice(byte[] expandedKey, int offset, int limit) {
this.expandedKey = expandedKey;
this.offset = offset;
this.limit = limit;
Expand Down
10 changes: 10 additions & 0 deletions rskj-core/src/test/java/co/rsk/trie/TrieIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public static Collection primeNumbers() {

}

@Test
public void testIterationElement() {
byte[] expandedKey = new byte[]{0,1,1,0,1,1,0,0,0,1};

IterationElement ie = new IterationElement(
new TrieKeySlice(expandedKey,1,7),new Trie());
String dump = ie.toString();
Assert.assertEquals("110110",dump);
}

@Test
public void testIterator() {
int nodeCount = 0;
Expand Down

0 comments on commit 2077e07

Please sign in to comment.