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

Refactor rlp #2175

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ blocksminer

# Jacoco Reports
jacocoHtml/
htmlreport/

# CheckStyle Reports
config/checkstyle/reports/
Expand Down
4 changes: 1 addition & 3 deletions rskj-core/src/main/java/co/rsk/db/RepositoryLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@

import java.util.Optional;

import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;

public class RepositoryLocator {
// all zeroed, default hash for empty nodes
private static final Keccak256 EMPTY_HASH = new Keccak256(Keccak256Helper.keccak256(RLP.encodeElement(EMPTY_BYTE_ARRAY)));
private static final Keccak256 EMPTY_HASH = new Keccak256(Keccak256Helper.keccak256(RLP.encodeElement(null)));

private final TrieStore trieStore;
private final StateRootHandler stateRootHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static byte[] encode(BlocksBloom blocksBloom) {
}

public static BlocksBloom decode(byte[] data) {
RLPList list = (RLPList) RLP.decode2(data).get(0);
RLPList list = RLP.decodeList(data);

long from = decodeLong(list.get(0).getRLPData());
long to = decodeLong(list.get(1).getRLPData());
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void setExtraData(byte[] clientExtraData) {
- rlpClientExtraDataEncodingOverhead;
byte[] clientExtraDataResized = Arrays.copyOf(clientExtraData, Math.min(clientExtraData.length, clientExtraDataSize));

this.extraData = RLP.encodeList(version, RLP.encode(identity), RLP.encodeElement(clientExtraDataResized));
this.extraData = RLP.encodeList(version, RLP.encodeElement(identity), RLP.encodeElement(clientExtraDataResized));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static FindNodePeerMessage create(byte[] nodeId, String check, ECKey priv

@Override
protected final void parse(byte[] data) {
RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0);
RLPList dataList = RLP.decodeList(data);
if (dataList.size() < 2) {
throw new PeerDiscoveryException(MORE_DATA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static NeighborsPeerMessage create(List<Node> nodes, String check, ECKey

@Override
protected final void parse(byte[] data) {
RLPList list = (RLPList) RLP.decode2OneItem(data, 0);
RLPList list = RLP.decodeList(data);

if (list.size() < 2) {
throw new PeerDiscoveryException(MORE_DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static PingPeerMessage create(String host, int port, String check, ECKey

@Override
protected final void parse(byte[] data) {
RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0);
RLPList dataList = RLP.decodeList(data);

if (dataList.size() < 3) {
throw new PeerDiscoveryException(MORE_DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public int getPort() {

@Override
protected final void parse(byte[] data) {
RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0);
RLPList dataList = RLP.decodeList(data);
if (dataList.size() < 3) {
throw new PeerDiscoveryException(MORE_DATA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MessageType getMessageType() {

@Override
public byte[] getEncodedMessage() {
byte[] block = RLP.encode(this.block.getEncoded());
byte[] block = RLP.encodeElement(this.block.getEncoded());

return RLP.encodeList(block);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public MessageType getMessageType() {

@Override
public byte[] getEncodedMessageWithoutId() {
byte[] rlpBlock = RLP.encode(this.block.getEncoded());
byte[] rlpBlock = RLP.encodeElement(this.block.getEncoded());

return RLP.encodeList(rlpBlock);
}
Expand Down
4 changes: 2 additions & 2 deletions rskj-core/src/main/java/co/rsk/net/messages/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final byte[] getEncoded() {

@VisibleForTesting
static Message create(BlockFactory blockFactory, byte[] encoded) {
return create(blockFactory, (RLPList) RLP.decode2(encoded).get(0));
return create(blockFactory, RLP.decodeList(encoded));
}

public static Message create(BlockFactory blockFactory, RLPList paramsList) {
Expand All @@ -51,7 +51,7 @@ public static Message create(BlockFactory blockFactory, RLPList paramsList) {
if (body != null) {
int type = paramsList.get(0).getRLPData()[0];
MessageType messageType = MessageType.valueOfType(type);
RLPList list = (RLPList) RLP.decode2(body).get(0);
RLPList list = RLP.decodeList(body);
return messageType.createMessage(blockFactory, list);
}

Expand Down
37 changes: 23 additions & 14 deletions rskj-core/src/main/java/co/rsk/net/messages/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@

import static org.ethereum.util.ByteUtil.byteArrayToInt;

class DecodeMessageUtil {
private DecodeMessageUtil () {
}

public static RLPList getMessageFromList(RLPList list) {
return RLP.decodeList(list.get(1).getRLPData());
}
}

/**
* Created by mario on 16/02/17.
*/
Expand Down Expand Up @@ -97,7 +106,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BLOCK_HASH_REQUEST_MESSAGE(8) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
byte[] rlpHeight = message.get(0).getRLPData();
Expand All @@ -109,7 +118,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BLOCK_HASH_RESPONSE_MESSAGE(18) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
byte[] hash = message.get(0).getRLPData();
Expand All @@ -120,7 +129,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BLOCK_HEADERS_REQUEST_MESSAGE(9) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list){
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
byte[] hash = message.get(0).getRLPData();
byte[] rlpCount = message.get(1).getRLPData();
Expand All @@ -134,9 +143,9 @@ public Message createMessage(BlockFactory blockFactory, RLPList list){
BLOCK_HEADERS_RESPONSE_MESSAGE(10) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
RLPList rlpHeaders = (RLPList)RLP.decode2(message.get(0).getRLPData()).get(0);
RLPList rlpHeaders = RLP.decodeList(message.get(0).getRLPData());
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();

List<BlockHeader> headers = new ArrayList<>();
Expand All @@ -153,7 +162,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BLOCK_REQUEST_MESSAGE(11) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
byte[] hash = message.get(0).getRLPData();
Expand All @@ -163,7 +172,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BLOCK_RESPONSE_MESSAGE(12) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
byte[] rlpBlock = message.get(0).getRLPData();

Expand All @@ -176,11 +185,11 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
SKELETON_RESPONSE_MESSAGE(13) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();

RLPList paramsList = (RLPList)RLP.decode2(message.get(0).getRLPData()).get(0);
RLPList paramsList = RLP.decodeList(message.get(0).getRLPData());

List<BlockIdentifier> blockIdentifiers = new ArrayList<>();

Expand All @@ -196,7 +205,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BODY_REQUEST_MESSAGE(14) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
byte[] hash = message.get(0).getRLPData();

Expand All @@ -207,11 +216,11 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
BODY_RESPONSE_MESSAGE(15) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList) RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
RLPList rlpTransactions = (RLPList) RLP.decode2(message.get(0).getRLPData()).get(0);
RLPList rlpUncles = (RLPList) RLP.decode2(message.get(1).getRLPData()).get(0);
RLPList rlpTransactions = RLP.decodeList(message.get(0).getRLPData());
RLPList rlpUncles = RLP.decodeList(message.get(1).getRLPData());

List<Transaction> transactions = new ArrayList<>();
for (int k = 0; k < rlpTransactions.size(); k++) {
Expand All @@ -238,7 +247,7 @@ public Message createMessage(BlockFactory blockFactory, RLPList list) {
SKELETON_REQUEST_MESSAGE(16) {
@Override
public Message createMessage(BlockFactory blockFactory, RLPList list) {
RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
RLPList message = DecodeMessageUtil.getMessageFromList(list);
byte[] rlpId = list.get(0).getRLPData();
long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
byte[] rlpStartNumber = message.get(0).getRLPData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public NewBlockHashesMessage(List<BlockIdentifier> blockIdentifiers) {
}

private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
RLPList paramsList = RLP.decodeList(encoded);

blockIdentifiers = new ArrayList<>();

Expand Down
Loading