diff --git a/.gitignore b/.gitignore index 80b59516973..c68b42715d9 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ blocksminer # Jacoco Reports jacocoHtml/ +htmlreport/ # CheckStyle Reports config/checkstyle/reports/ diff --git a/rskj-core/src/main/java/co/rsk/db/RepositoryLocator.java b/rskj-core/src/main/java/co/rsk/db/RepositoryLocator.java index 3a3eef66fec..b365235eed0 100644 --- a/rskj-core/src/main/java/co/rsk/db/RepositoryLocator.java +++ b/rskj-core/src/main/java/co/rsk/db/RepositoryLocator.java @@ -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; diff --git a/rskj-core/src/main/java/co/rsk/logfilter/BlocksBloomEncoder.java b/rskj-core/src/main/java/co/rsk/logfilter/BlocksBloomEncoder.java index f74a7b7f8be..0b53c01578d 100644 --- a/rskj-core/src/main/java/co/rsk/logfilter/BlocksBloomEncoder.java +++ b/rskj-core/src/main/java/co/rsk/logfilter/BlocksBloomEncoder.java @@ -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()); diff --git a/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java b/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java index 01f9cb68075..940c4008ae3 100644 --- a/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java +++ b/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java @@ -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 diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/message/FindNodePeerMessage.java b/rskj-core/src/main/java/co/rsk/net/discovery/message/FindNodePeerMessage.java index 6d1040a9b6d..cfb21b9c506 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/message/FindNodePeerMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/message/FindNodePeerMessage.java @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/message/NeighborsPeerMessage.java b/rskj-core/src/main/java/co/rsk/net/discovery/message/NeighborsPeerMessage.java index 6cc59097c67..371d5d2ca58 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/message/NeighborsPeerMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/message/NeighborsPeerMessage.java @@ -87,7 +87,7 @@ public static NeighborsPeerMessage create(List 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); diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/message/PingPeerMessage.java b/rskj-core/src/main/java/co/rsk/net/discovery/message/PingPeerMessage.java index b319be58762..e670ed704ed 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/message/PingPeerMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/message/PingPeerMessage.java @@ -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); diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/message/PongPeerMessage.java b/rskj-core/src/main/java/co/rsk/net/discovery/message/PongPeerMessage.java index 05dae5c5118..12fb7fc2de6 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/message/PongPeerMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/message/PongPeerMessage.java @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/net/messages/BlockMessage.java b/rskj-core/src/main/java/co/rsk/net/messages/BlockMessage.java index 72a9d073e6e..57010f36a55 100644 --- a/rskj-core/src/main/java/co/rsk/net/messages/BlockMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/messages/BlockMessage.java @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/net/messages/BlockResponseMessage.java b/rskj-core/src/main/java/co/rsk/net/messages/BlockResponseMessage.java index 804a451199a..f5de02874cf 100644 --- a/rskj-core/src/main/java/co/rsk/net/messages/BlockResponseMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/messages/BlockResponseMessage.java @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/net/messages/Message.java b/rskj-core/src/main/java/co/rsk/net/messages/Message.java index 7b16eb2130d..a7542db5058 100644 --- a/rskj-core/src/main/java/co/rsk/net/messages/Message.java +++ b/rskj-core/src/main/java/co/rsk/net/messages/Message.java @@ -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) { @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/net/messages/MessageType.java b/rskj-core/src/main/java/co/rsk/net/messages/MessageType.java index a7e9ad21fb5..8593f48e13e 100644 --- a/rskj-core/src/main/java/co/rsk/net/messages/MessageType.java +++ b/rskj-core/src/main/java/co/rsk/net/messages/MessageType.java @@ -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. */ @@ -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(); @@ -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(); @@ -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(); @@ -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 headers = new ArrayList<>(); @@ -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(); @@ -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(); @@ -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 blockIdentifiers = new ArrayList<>(); @@ -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(); @@ -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 transactions = new ArrayList<>(); for (int k = 0; k < rlpTransactions.size(); k++) { @@ -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(); diff --git a/rskj-core/src/main/java/co/rsk/net/messages/NewBlockHashesMessage.java b/rskj-core/src/main/java/co/rsk/net/messages/NewBlockHashesMessage.java index 4e7952005e3..3969e039815 100644 --- a/rskj-core/src/main/java/co/rsk/net/messages/NewBlockHashesMessage.java +++ b/rskj-core/src/main/java/co/rsk/net/messages/NewBlockHashesMessage.java @@ -55,7 +55,7 @@ public NewBlockHashesMessage(List blockIdentifiers) { } private void parse() { - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); blockIdentifiers = new ArrayList<>(); diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index b9cdac52c4c..292cbe733db 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -85,7 +85,7 @@ public static SortedMap deserializeMap(byte[] data, N return map; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); int ntxs = rlpList.size() / 2; @@ -128,7 +128,7 @@ public static List deserializeUTXOList(byte[] data) throws IOException { return list; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); int nutxos = rlpList.size(); @@ -162,7 +162,7 @@ public static SortedSet deserializeSet(byte[] data) { return set; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); int nhashes = rlpList.size(); @@ -196,7 +196,7 @@ public static Map deserializeMapOfHashesToLong(byte[] data) { return map; } - RLPList rlpList = (RLPList) RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); // List size must be even - key, value pairs expected in sequence if (rlpList.size() % 2 != 0) { @@ -251,7 +251,7 @@ private static StandardMultisigFederation deserializeStandardMultisigFederationW NetworkParameters networkParameters, FederationMemberDesserializer federationMemberDesserializer) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != FEDERATION_RLP_LIST_SIZE) { throw new RuntimeException(String.format("Invalid serialized Federation. Expected %d elements but got %d", FEDERATION_RLP_LIST_SIZE, rlpList.size())); @@ -378,7 +378,7 @@ public static byte[] serializeFederationMember(FederationMember federationMember // For the serialization format, see BridgeSerializationUtils::serializeFederationMember private static FederationMember deserializeFederationMember(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != FEDERATION_RLP_LIST_SIZE) { throw new RuntimeException(String.format("Invalid serialized FederationMember. Expected %d elements but got %d", FEDERATION_MEMBER_LIST_SIZE, rlpList.size())); @@ -428,7 +428,7 @@ public static byte[] serializePendingFederation(PendingFederation pendingFederat // For the serialization format, see BridgeSerializationUtils::serializePendingFederation public static PendingFederation deserializePendingFederation(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); List members = new ArrayList<>(); @@ -466,7 +466,7 @@ public static ABICallElection deserializeElection(byte[] data, AddressBasedAutho return new ABICallElection(authorizer); } - RLPList rlpList = (RLPList) RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); // List size must be even - key, value pairs expected in sequence if (rlpList.size() % 2 != 0) { @@ -523,7 +523,7 @@ public static Pair, Integer> deserializeO if (data == null || data.length == 0) { return null; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); int serializedAddressesSize = rlpList.size() - 1; // serialized addresses size must be even - key, value pairs expected in sequence @@ -547,7 +547,7 @@ public static Map deserializeUnlimitedLockWhit return new HashMap<>(); } - RLPList unlimitedWhitelistEntriesRlpList = (RLPList)RLP.decode2(data).get(0); + RLPList unlimitedWhitelistEntriesRlpList = RLP.decodeList(data); int unlimitedWhitelistEntriesSerializedAddressesSize = unlimitedWhitelistEntriesRlpList.size(); Map entries = new HashMap<>(unlimitedWhitelistEntriesSerializedAddressesSize); @@ -623,7 +623,7 @@ public static List deserializeReleaseRequestQueue(byt } int elementsMultipleCount = hasTxHash ? 3 : 2; - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); // Must have an even number of items if (rlpList.size() % elementsMultipleCount != 0) { @@ -715,7 +715,7 @@ public static PegoutsWaitingForConfirmations deserializePegoutsWaitingForConfirm } int elementsMultipleCount = hasTxHash ? 3 : 2; - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); // Must have an even number of items if (rlpList.size() % elementsMultipleCount != 0) { @@ -780,7 +780,7 @@ public static CoinbaseInformation deserializeCoinbaseInformation(byte[] data) { if (data == null) { return null; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != 1) { throw new RuntimeException(String.format("Invalid serialized coinbase information, expected 1 value but got %d", rlpList.size())); @@ -822,7 +822,7 @@ public static Script deserializeScript(byte[] data) { return null; } - RLPList rlpList = (RLPList) RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != 1) { throw new RuntimeException(String.format("Invalid serialized script. Expected 1 element, but got %d", rlpList.size())); } @@ -835,7 +835,7 @@ public static FlyoverFederationInformation deserializeFlyoverFederationInformati return null; } - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != 2) { throw new RuntimeException(String.format("Invalid serialized Fast Bridge Federation: expected 2 value but got %d", rlpList.size())); @@ -872,7 +872,7 @@ private static byte[] serializeABICallSpec(ABICallSpec spec) { // For the serialization format, see BridgeSerializationUtils::serializeABICallSpec private static ABICallSpec deserializeABICallSpec(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); if (rlpList.size() != 2) { throw new RuntimeException(String.format("Invalid serialized ABICallSpec. Expected 2 elements, but got %d", rlpList.size())); @@ -903,7 +903,7 @@ private static byte[] serializeBtcPublicKeys(List keys) { // For the serialization format, see BridgeSerializationUtils::serializePublicKeys private static List deserializeBtcPublicKeys(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); List keys = new ArrayList<>(); @@ -929,7 +929,7 @@ private static byte[] serializeVoters(List voters) { // For the serialization format, see BridgeSerializationUtils::serializeVoters private static List deserializeVoters(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); List addresses = new ArrayList<>(); diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeState.java b/rskj-core/src/main/java/co/rsk/peg/BridgeState.java index 355d9b34435..69f75d7b16a 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeState.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeState.java @@ -135,7 +135,7 @@ public byte[] getEncoded() throws IOException { } public static BridgeState create(BridgeConstants bridgeConstants, byte[] data, @Nullable ActivationConfig.ForBlock activations) throws IOException { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); byte[] btcBlockchainBestChainHeightBytes = rlpList.get(0).getRLPData(); int btcBlockchainBestChainHeight = btcBlockchainBestChainHeightBytes == null ? 0 : (new BigInteger(1, btcBlockchainBestChainHeightBytes)).intValue(); diff --git a/rskj-core/src/main/java/co/rsk/peg/StateForFederator.java b/rskj-core/src/main/java/co/rsk/peg/StateForFederator.java index 50088469803..f2b33484de9 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StateForFederator.java +++ b/rskj-core/src/main/java/co/rsk/peg/StateForFederator.java @@ -37,7 +37,7 @@ public StateForFederator(SortedMap rskTxsWaitingForSi } public StateForFederator(byte[] rlpData, NetworkParameters parameters) { - RLPList rlpList = (RLPList) RLP.decode2(rlpData).get(0); + RLPList rlpList = RLP.decodeList(rlpData); byte[] encodedWaitingForSign = rlpList.get(0).getRLPData(); this.rskTxsWaitingForSignatures = BridgeSerializationUtils.deserializeMap(encodedWaitingForSign, parameters, false); diff --git a/rskj-core/src/main/java/co/rsk/remasc/RemascState.java b/rskj-core/src/main/java/co/rsk/remasc/RemascState.java index 28ea3dfa86f..dd944ca63eb 100644 --- a/rskj-core/src/main/java/co/rsk/remasc/RemascState.java +++ b/rskj-core/src/main/java/co/rsk/remasc/RemascState.java @@ -64,11 +64,11 @@ public byte[] getEncoded() { } // we add an empty list because Remasc state expects to have an empty siblings list after 0.5.0 activation - return RLP.encodeList(rlpRewardBalance, rlpBurnedBalance, RLP.encodedEmptyList(), rlpBrokenSelectionRule); + return RLP.encodeList(rlpRewardBalance, rlpBurnedBalance, RLP.encodeList(), rlpBrokenSelectionRule); } public static RemascState create(byte[] data) { - RLPList rlpList = (RLPList)RLP.decode2(data).get(0); + RLPList rlpList = RLP.decodeList(data); Coin rlpRewardBalance = RLP.parseCoin(rlpList.get(0).getRLPData()); Coin rlpBurnedBalance = RLP.parseCoin(rlpList.get(1).getRLPData()); diff --git a/rskj-core/src/main/java/co/rsk/remasc/RemascStorageProvider.java b/rskj-core/src/main/java/co/rsk/remasc/RemascStorageProvider.java index 4fa5d1c80a8..e396bdf9b2a 100644 --- a/rskj-core/src/main/java/co/rsk/remasc/RemascStorageProvider.java +++ b/rskj-core/src/main/java/co/rsk/remasc/RemascStorageProvider.java @@ -151,7 +151,7 @@ private void saveSiblings() { DataWord address = DataWord.fromString(SIBLINGS_KEY); // we add an empty list because Remasc state expects to have an empty siblings list after 0.5.0 activation - this.repository.addStorageBytes(this.contractAddress, address, RLP.encodedEmptyList()); + this.repository.addStorageBytes(this.contractAddress, address, RLP.encodeList()); } public Boolean getBrokenSelectionRule() { diff --git a/rskj-core/src/main/java/co/rsk/remasc/Sibling.java b/rskj-core/src/main/java/co/rsk/remasc/Sibling.java index c6ce79151cb..74138546f48 100644 --- a/rskj-core/src/main/java/co/rsk/remasc/Sibling.java +++ b/rskj-core/src/main/java/co/rsk/remasc/Sibling.java @@ -27,7 +27,6 @@ import org.bouncycastle.util.BigIntegers; import java.math.BigInteger; -import java.util.ArrayList; /** * Siblings are part of the remasc contract state @@ -103,8 +102,7 @@ public byte[] getEncoded() { } public static Sibling create(byte[] data) { - ArrayList params = RLP.decode2(data); - RLPList sibling = (RLPList) params.get(0); + RLPList sibling = RLP.decodeList(data); byte[] hash = sibling.get(0).getRLPData(); RskAddress coinbase = RLP.parseRskAddress(sibling.get(1).getRLPData()); diff --git a/rskj-core/src/main/java/co/rsk/validators/ProofOfWorkRule.java b/rskj-core/src/main/java/co/rsk/validators/ProofOfWorkRule.java index f3e09465be9..a959e27264c 100644 --- a/rskj-core/src/main/java/co/rsk/validators/ProofOfWorkRule.java +++ b/rskj-core/src/main/java/co/rsk/validators/ProofOfWorkRule.java @@ -38,14 +38,12 @@ import org.ethereum.crypto.signature.ECDSASignature; import org.ethereum.crypto.signature.Secp256k1; import org.ethereum.util.RLP; -import org.ethereum.util.RLPElement; import org.ethereum.util.RLPList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.math.BigInteger; import java.util.Arrays; -import java.util.List; /** * Checks proof value against its boundary for the block header. @@ -239,12 +237,7 @@ private static boolean validFallbackBlockSignature(Constants constants, BlockHea ECKey fallbackMiningPubKey = ECKey.fromPublicOnly(fallbackMiningPubKeyBytes); - List signatureRlpElements = RLP.decode2(signatureBytesRLP); - if (signatureRlpElements.size() != 1) { - return false; - } - - RLPList signatureRLP = (RLPList) signatureRlpElements.get(0); + RLPList signatureRLP = RLP.decodeList(signatureBytesRLP); if (signatureRLP.size() != 3) { return false; diff --git a/rskj-core/src/main/java/org/ethereum/core/AccountState.java b/rskj-core/src/main/java/org/ethereum/core/AccountState.java index 359b16e2a3d..4a66157bdaa 100644 --- a/rskj-core/src/main/java/org/ethereum/core/AccountState.java +++ b/rskj-core/src/main/java/org/ethereum/core/AccountState.java @@ -57,7 +57,7 @@ public AccountState(BigInteger nonce, Coin balance) { public AccountState(byte[] rlpData) { this.rlpEncoded = rlpData; - RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0); + RLPList items = RLP.decodeList(rlpEncoded); this.nonce = items.get(0).getRLPData() == null ? BigInteger.ZERO : new BigInteger(1, items.get(0).getRLPData()); this.balance = RLP.parseSignedCoinNonNullZero(items.get(1).getRLPData()); diff --git a/rskj-core/src/main/java/org/ethereum/core/Transaction.java b/rskj-core/src/main/java/org/ethereum/core/Transaction.java index dc30b1b5c76..78de30c50c9 100644 --- a/rskj-core/src/main/java/org/ethereum/core/Transaction.java +++ b/rskj-core/src/main/java/org/ethereum/core/Transaction.java @@ -112,7 +112,7 @@ protected Transaction(RLPList transaction) { this.gasPrice = RLP.parseCoinNonNullZero(transaction.get(1).getRLPData()); this.gasLimit = transaction.get(2).getRLPData(); this.receiveAddress = RLP.parseRskAddress(transaction.get(3).getRLPData()); - this.value = RLP.parseCoinNullZero(transaction.get(4).getRLPData()); + this.value = RLP.parseCoin(transaction.get(4).getRLPData()); this.data = transaction.get(5).getRLPData(); // only parse signature in case tx is signed byte[] vData = transaction.get(6).getRLPData(); @@ -147,7 +147,7 @@ protected Transaction(byte[] nonce, byte[] gasPriceRaw, byte[] gasLimit, byte[] RLP.parseCoinNonNullZero(ByteUtil.cloneBytes(gasPriceRaw)), gasLimit, RLP.parseRskAddress(ByteUtil.cloneBytes(receiveAddress)), - RLP.parseCoinNullZero(ByteUtil.cloneBytes(valueRaw)), + RLP.parseCoin(ByteUtil.cloneBytes(valueRaw)), data, chainId, false diff --git a/rskj-core/src/main/java/org/ethereum/core/TransactionBuilder.java b/rskj-core/src/main/java/org/ethereum/core/TransactionBuilder.java index 878c970abc5..d3d0eda9ba8 100644 --- a/rskj-core/src/main/java/org/ethereum/core/TransactionBuilder.java +++ b/rskj-core/src/main/java/org/ethereum/core/TransactionBuilder.java @@ -73,7 +73,7 @@ public TransactionBuilder value(Coin value) { } public TransactionBuilder value(byte[] value) { - this.value(RLP.parseCoinNullZero(ByteUtil.cloneBytes(value))); + this.value(RLP.parseCoin(ByteUtil.cloneBytes(value))); return this; } diff --git a/rskj-core/src/main/java/org/ethereum/core/TransactionReceipt.java b/rskj-core/src/main/java/org/ethereum/core/TransactionReceipt.java index 913c4481186..4a23a1d454a 100644 --- a/rskj-core/src/main/java/org/ethereum/core/TransactionReceipt.java +++ b/rskj-core/src/main/java/org/ethereum/core/TransactionReceipt.java @@ -60,8 +60,7 @@ public TransactionReceipt() { public TransactionReceipt(byte[] rlp) { - ArrayList params = RLP.decode2(rlp); - RLPList receipt = (RLPList) params.get(0); + RLPList receipt = RLP.decodeList(rlp); RLPItem postTxStateRLP = (RLPItem) receipt.get(0); RLPItem cumulativeGasRLP = (RLPItem) receipt.get(1); diff --git a/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImpl.java b/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImpl.java index 2f7e62c5e55..f2af911ea64 100644 --- a/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImpl.java +++ b/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImpl.java @@ -112,7 +112,7 @@ private List getAll(byte[] transactionHash) { } List txsInfo = new ArrayList<>(); - RLPList txsList = (RLPList) RLP.decode2(txsBytes).get(0); + RLPList txsList = RLP.decodeList(txsBytes); int txsListSize = txsList.size(); diff --git a/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImplV2.java b/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImplV2.java index ab25dd2af97..5295d8e0de3 100644 --- a/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImplV2.java +++ b/rskj-core/src/main/java/org/ethereum/db/ReceiptStoreImplV2.java @@ -47,7 +47,7 @@ public void add(byte[] blockHash, int transactionIndex, TransactionReceipt recei RLPList txList = null; int txListSize = 0; if (txInfoBytes != null && txInfoBytes.length > 0) { - txList = (RLPList) RLP.decode2(txInfoBytes).get(0); + txList = RLP.decodeList(txInfoBytes); txListSize = txList.size(); } @@ -129,7 +129,7 @@ private static List parseBlockHashList(byte[] txsBytes) { return Collections.emptyList(); } - RLPList txHashList = (RLPList) RLP.decode2(txsBytes).get(0); + RLPList txHashList = RLP.decodeList(txsBytes); if (txHashList.size() == 0 || txHashList.get(0) instanceof RLPList) { return Collections.emptyList(); } diff --git a/rskj-core/src/main/java/org/ethereum/db/TransactionInfo.java b/rskj-core/src/main/java/org/ethereum/db/TransactionInfo.java index f43d00274d8..8c23f74b31c 100644 --- a/rskj-core/src/main/java/org/ethereum/db/TransactionInfo.java +++ b/rskj-core/src/main/java/org/ethereum/db/TransactionInfo.java @@ -22,12 +22,10 @@ import org.ethereum.core.Transaction; import org.ethereum.core.TransactionReceipt; import org.ethereum.util.RLP; -import org.ethereum.util.RLPElement; import org.ethereum.util.RLPItem; import org.ethereum.util.RLPList; import org.bouncycastle.util.BigIntegers; -import java.util.ArrayList; /** * Created by Ruben on 8/1/2016. @@ -45,8 +43,7 @@ public TransactionInfo(TransactionReceipt receipt, byte[] blockHash, int index) } public TransactionInfo(byte[] rlp) { - ArrayList params = RLP.decode2(rlp); - RLPList txInfo = (RLPList) params.get(0); + RLPList txInfo = RLP.decodeList(rlp); RLPList receiptRLP = (RLPList) txInfo.get(0); RLPItem blockHashRLP = (RLPItem) txInfo.get(1); RLPItem indexRLP = (RLPItem) txInfo.get(2); diff --git a/rskj-core/src/main/java/org/ethereum/net/eth/message/Eth62MessageFactory.java b/rskj-core/src/main/java/org/ethereum/net/eth/message/Eth62MessageFactory.java index a594ed4e49c..6644644047d 100644 --- a/rskj-core/src/main/java/org/ethereum/net/eth/message/Eth62MessageFactory.java +++ b/rskj-core/src/main/java/org/ethereum/net/eth/message/Eth62MessageFactory.java @@ -46,7 +46,7 @@ public Message create(byte code, byte[] encoded) { case STATUS: return new StatusMessage(encoded); case RSK_MESSAGE: - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); return new RskMessage(co.rsk.net.messages.Message.create(blockFactory, (RLPList) paramsList.get(0))); default: throw new IllegalArgumentException("No such message"); diff --git a/rskj-core/src/main/java/org/ethereum/net/eth/message/StatusMessage.java b/rskj-core/src/main/java/org/ethereum/net/eth/message/StatusMessage.java index caafa569b30..102c7140e87 100644 --- a/rskj-core/src/main/java/org/ethereum/net/eth/message/StatusMessage.java +++ b/rskj-core/src/main/java/org/ethereum/net/eth/message/StatusMessage.java @@ -64,7 +64,7 @@ public StatusMessage(byte protocolVersion, int networkId, } protected void parse() { - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); this.protocolVersion = paramsList.get(0).getRLPData()[0]; byte[] networkIdBytes = paramsList.get(1).getRLPData(); diff --git a/rskj-core/src/main/java/org/ethereum/net/p2p/DisconnectMessage.java b/rskj-core/src/main/java/org/ethereum/net/p2p/DisconnectMessage.java index 18df591b3e0..fa603899a2b 100644 --- a/rskj-core/src/main/java/org/ethereum/net/p2p/DisconnectMessage.java +++ b/rskj-core/src/main/java/org/ethereum/net/p2p/DisconnectMessage.java @@ -44,7 +44,7 @@ public DisconnectMessage(ReasonCode reason) { } private void parse() { - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); byte[] reasonBytes = paramsList.get(0).getRLPData(); if (reasonBytes == null) { diff --git a/rskj-core/src/main/java/org/ethereum/net/p2p/HelloMessage.java b/rskj-core/src/main/java/org/ethereum/net/p2p/HelloMessage.java index f905ee2b9a0..cd2d7ae9326 100644 --- a/rskj-core/src/main/java/org/ethereum/net/p2p/HelloMessage.java +++ b/rskj-core/src/main/java/org/ethereum/net/p2p/HelloMessage.java @@ -26,7 +26,6 @@ import org.ethereum.util.RLPElement; import org.ethereum.util.RLPList; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -78,7 +77,7 @@ public HelloMessage(byte p2pVersion, String clientId, } private void parse() { - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); byte[] p2pVersionBytes = paramsList.get(0).getRLPData(); this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0; @@ -117,7 +116,7 @@ private void encode() { for (int i = 0; i < this.capabilities.size(); i++) { Capability capability = this.capabilities.get(i); capabilities[i] = RLP.encodeList( - RLP.encodeElement(capability.getName().getBytes(StandardCharsets.UTF_8)), + RLP.encodeString(capability.getName()), RLP.encodeInt(capability.getVersion())); } byte[] capabilityList = RLP.encodeList(capabilities); diff --git a/rskj-core/src/main/java/org/ethereum/net/p2p/PeersMessage.java b/rskj-core/src/main/java/org/ethereum/net/p2p/PeersMessage.java index 9aee85e6931..ca7bb67fde2 100644 --- a/rskj-core/src/main/java/org/ethereum/net/p2p/PeersMessage.java +++ b/rskj-core/src/main/java/org/ethereum/net/p2p/PeersMessage.java @@ -51,7 +51,7 @@ public PeersMessage(Set peers) { } private void parse() { - RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); + RLPList paramsList = RLP.decodeList(encoded); peers = new LinkedHashSet<>(); for (int i = 1; i < paramsList.size(); ++i) { diff --git a/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthInitiateMessageV4.java b/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthInitiateMessageV4.java index 2b35c43e3eb..cdce22c01cb 100644 --- a/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthInitiateMessageV4.java +++ b/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthInitiateMessageV4.java @@ -48,7 +48,7 @@ public AuthInitiateMessageV4() { static AuthInitiateMessageV4 decode(byte[] wire) { AuthInitiateMessageV4 message = new AuthInitiateMessageV4(); - RLPList params = (RLPList) RLP.decode2OneItem(wire, 0); + RLPList params = RLP.decodeList(wire); byte[] signatureBytes = params.get(0).getRLPData(); int offset = 0; @@ -88,9 +88,9 @@ public byte[] encode() { byte[] publicKey = new byte[64]; System.arraycopy(this.publicKey.getEncoded(false), 1, publicKey, 0, publicKey.length); - byte[] sigBytes = RLP.encode(merge(rsigPad, ssigPad, new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())})); - byte[] publicBytes = RLP.encode(publicKey); - byte[] nonceBytes = RLP.encode(nonce); + byte[] sigBytes = RLP.encodeElement(merge(rsigPad, ssigPad, new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())})); + byte[] publicBytes = RLP.encodeElement(publicKey); + byte[] nonceBytes = RLP.encodeElement(nonce); byte[] versionBytes = RLP.encodeInt(version); return RLP.encodeList(sigBytes, publicBytes, nonceBytes, versionBytes); diff --git a/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthResponseMessageV4.java b/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthResponseMessageV4.java index da57466a9eb..cfa03c2af0e 100644 --- a/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthResponseMessageV4.java +++ b/rskj-core/src/main/java/org/ethereum/net/rlpx/AuthResponseMessageV4.java @@ -41,7 +41,7 @@ static AuthResponseMessageV4 decode(byte[] wire) { AuthResponseMessageV4 message = new AuthResponseMessageV4(); - RLPList params = (RLPList) RLP.decode2OneItem(wire, 0); + RLPList params = RLP.decodeList(wire); byte[] pubKeyBytes = params.get(0).getRLPData(); @@ -63,8 +63,8 @@ public byte[] encode() { byte[] publicKey = new byte[64]; System.arraycopy(ephemeralPublicKey.getEncoded(false), 1, publicKey, 0, publicKey.length); - byte[] publicBytes = RLP.encode(publicKey); - byte[] nonceBytes = RLP.encode(nonce); + byte[] publicBytes = RLP.encodeElement(publicKey); + byte[] nonceBytes = RLP.encodeElement(nonce); byte[] versionBytes = RLP.encodeInt(version); return RLP.encodeList(publicBytes, nonceBytes, versionBytes); diff --git a/rskj-core/src/main/java/org/ethereum/net/rlpx/FrameCodec.java b/rskj-core/src/main/java/org/ethereum/net/rlpx/FrameCodec.java index b37d7663109..5ed826260ca 100644 --- a/rskj-core/src/main/java/org/ethereum/net/rlpx/FrameCodec.java +++ b/rskj-core/src/main/java/org/ethereum/net/rlpx/FrameCodec.java @@ -22,21 +22,22 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufOutputStream; +import org.apache.commons.lang3.tuple.Pair; import org.bouncycastle.crypto.StreamCipher; import org.bouncycastle.crypto.digests.KeccakDigest; import org.bouncycastle.crypto.engines.AESEngine; import org.bouncycastle.crypto.modes.SICBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; +import org.ethereum.util.ByteUtil; import org.ethereum.util.RLP; +import org.ethereum.util.RLPElement; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static org.ethereum.util.RLP.decode2OneItem; - /** * Created by devrandom on 2015-04-11. */ @@ -191,7 +192,7 @@ public List readFrames(DataInput inp) throws IOException { return null; } - decode2OneItem(headBuffer, 3); + RLP.decodeFirstElement(headBuffer, 3); contextId = -1; totalFrameSize = -1; @@ -212,9 +213,12 @@ public List readFrames(DataInput inp) throws IOException { int frameSize = buffer.length - macSize; ingressMac.update(buffer, 0, frameSize); dec.processBytes(buffer, 0, frameSize, buffer, 0); + int pos = 0; - long type = RLP.decodeInt(buffer, pos); // FIXME long - pos = RLP.getNextElementIndex(buffer, pos); + Pair typeRead = RLP.decodeFirstElementReading(buffer, pos); + int type = ByteUtil.byteArrayToInt(typeRead.getLeft().getRLPData()); + pos = typeRead.getRight(); + InputStream payload = new ByteArrayInputStream(buffer, pos, totalBodySize - pos); int size = totalBodySize - pos; byte[] macBuffer = new byte[ingressMac.getDigestSize()]; diff --git a/rskj-core/src/main/java/org/ethereum/net/rlpx/HandshakeMessage.java b/rskj-core/src/main/java/org/ethereum/net/rlpx/HandshakeMessage.java index 3b7790e7d91..381b5659880 100644 --- a/rskj-core/src/main/java/org/ethereum/net/rlpx/HandshakeMessage.java +++ b/rskj-core/src/main/java/org/ethereum/net/rlpx/HandshakeMessage.java @@ -53,7 +53,7 @@ public HandshakeMessage(long version, String name, List caps, long l } static HandshakeMessage parse(byte[] wire) { - RLPList list = (RLPList) RLP.decode2(wire).get(0); + RLPList list = RLP.decodeList(wire); HandshakeMessage message = new HandshakeMessage(); message.version = ByteUtil.byteArrayToInt(list.get(0).getRLPData()); // FIXME long message.name = new String(list.get(1).getRLPData(), StandardCharsets.UTF_8); diff --git a/rskj-core/src/main/java/org/ethereum/net/rlpx/Node.java b/rskj-core/src/main/java/org/ethereum/net/rlpx/Node.java index 5d836592983..e913b08c2db 100644 --- a/rskj-core/src/main/java/org/ethereum/net/rlpx/Node.java +++ b/rskj-core/src/main/java/org/ethereum/net/rlpx/Node.java @@ -61,7 +61,7 @@ public Node(byte[] id, String host, int port) { } public Node(byte[] rlp) { - RLPList nodeRLP = (RLPList)RLP.decode2(rlp).get(0); + RLPList nodeRLP = RLP.decodeList(rlp); byte[] hostB = nodeRLP.get(0).getRLPData(); byte[] portB = nodeRLP.get(1).getRLPData(); diff --git a/rskj-core/src/main/java/org/ethereum/util/ByteUtil.java b/rskj-core/src/main/java/org/ethereum/util/ByteUtil.java index 12c22f62f9b..ad232c6867b 100644 --- a/rskj-core/src/main/java/org/ethereum/util/ByteUtil.java +++ b/rskj-core/src/main/java/org/ethereum/util/ByteUtil.java @@ -324,13 +324,20 @@ public static int byteArrayToInt(byte[] b) { if (b == null || b.length == 0) { return 0; } - return new BigInteger(1, b).intValue(); + + BigInteger bigInteger = new BigInteger(1, b); + + if (bigInteger.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) { + throw new RuntimeException("Cannot represent Integer " + bigInteger.toString(16)); + } + + return bigInteger.intValue(); } /** * Cast from byte[] to long * Limited to Long.MAX_VALUE: 2^63-1 (8 bytes) - * Will throw IlegalArgumentException if you pass + * Will throw RuntimeException if you pass * a byte array with more than 8 bytes. * * @param b array contains the values @@ -338,21 +345,19 @@ public static int byteArrayToInt(byte[] b) { */ public static long byteArrayToLong(byte[] b) { if (b == null || b.length == 0) { - return 0; - } - // avoids overflows in the result - if (b.length > 8) { - throw new IllegalArgumentException("byte array can't have more than 8 bytes if it is to be cast to long"); + return 0; } - long result = 0; - for (int i = 0; i < b.length; i++) { - result <<= 8; - result |= (b[i] & 0xFF); + + BigInteger bigInteger = new BigInteger(1, b); + + if (bigInteger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { + throw new RuntimeException("Cannot represent Integer " + bigInteger.toString(16)); } - return result; - } + return bigInteger.longValue(); + } + /** * Turn nibbles to a pretty looking output string * @@ -596,16 +601,6 @@ public static boolean isSingleZero(byte[] array){ return (array.length == 1 && array[0] == 0); } - public static boolean isAllZeroes(byte[] array) { - for (byte b : array) { - if (b != 0) { - return false; - } - } - - return true; - } - public static Set difference(Set setA, Set setB){ Set temp = new HashSet<>(setA); diff --git a/rskj-core/src/main/java/org/ethereum/util/RLP.java b/rskj-core/src/main/java/org/ethereum/util/RLP.java index 23b4ea69820..209160ed765 100644 --- a/rskj-core/src/main/java/org/ethereum/util/RLP.java +++ b/rskj-core/src/main/java/org/ethereum/util/RLP.java @@ -25,7 +25,6 @@ import co.rsk.util.RLPException; import org.apache.commons.lang3.tuple.Pair; import org.bouncycastle.util.BigIntegers; -import org.ethereum.db.ByteArrayWrapper; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -34,7 +33,6 @@ import java.nio.charset.StandardCharsets; import java.util.*; -import static org.bouncycastle.util.Arrays.concatenate; import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray; import static org.ethereum.util.ByteUtil.*; @@ -68,14 +66,6 @@ * @since 01.04.2014 */ public class RLP { - private static final int EMPTY_MARK = 128; - private static final int TINY_SIZE = 55; - - /** - * Allow for content up to size of 2^64 bytes * - */ - private static final double MAX_ITEM_LENGTH = Math.pow(256, 8); - /** * Reason for threshold according to Vitalik Buterin: * - 56 bytes maximizes the benefit of both options @@ -138,50 +128,13 @@ public class RLP { * DECODING * * ******************************************************/ - private static byte decodeOneByteItem(byte[] data, int index) { - // null item - if ((data[index] & 0xFF) == OFFSET_SHORT_ITEM) { - return (byte) (data[index] - OFFSET_SHORT_ITEM); - } - // single byte item - if ((data[index] & 0xFF) < OFFSET_SHORT_ITEM) { - return data[index]; - } - // single byte item - if ((data[index] & 0xFF) == OFFSET_SHORT_ITEM + 1) { - return data[index + 1]; - } - return 0; - } - - public static int decodeInt(byte[] data, int index) { - int value = 0; - // NOTE: there are two ways zero can be encoded - 0x00 and OFFSET_SHORT_ITEM - - if ((data[index] & 0xFF) < OFFSET_SHORT_ITEM) { - return data[index]; - } else if ((data[index] & 0xFF) >= OFFSET_SHORT_ITEM - && (data[index] & 0xFF) < OFFSET_LONG_ITEM) { - - byte length = (byte) (data[index] - OFFSET_SHORT_ITEM); - byte pow = (byte) (length - 1); - for (int i = 1; i <= length; ++i) { - value += (data[index + i] & 0xFF) << (8 * pow); - pow--; - } - } else { - throw new RuntimeException("wrong decode attempt"); - } - return value; - } - public static BigInteger decodeBigInteger(byte[] data, int index) { - RLPElement element = RLP.decodeFirstElement(data, index); - - if (element == null) { + if (data == null) { return null; } + RLPElement element = RLP.decodeElement(data, index).getLeft(); + byte[] bytes = element.getRLPData(); if (bytes == null || bytes.length == 0) { @@ -191,196 +144,6 @@ public static BigInteger decodeBigInteger(byte[] data, int index) { return BigIntegers.fromUnsignedByteArray(bytes); } - public static byte[] decodeIP4Bytes(byte[] data, int index) { - - int offset = 1; - - final byte[] result = new byte[4]; - for (int i = 0; i < 4; i++) { - result[i] = decodeOneByteItem(data, index + offset); - if ((data[index + offset] & 0xFF) > OFFSET_SHORT_ITEM) { - offset += 2; - } else { - offset += 1; - } - } - - // return IP address - return result; - } - - public static int getFirstListElement(byte[] payload, int pos) { - - if (pos >= payload.length) { - return -1; - } - - if ((payload[pos] & 0xFF) >= OFFSET_LONG_LIST) { - byte lengthOfLength = (byte) (payload[pos] - OFFSET_LONG_LIST); - return pos + lengthOfLength + 1; - } - if ((payload[pos] & 0xFF) >= OFFSET_SHORT_LIST - && (payload[pos] & 0xFF) < OFFSET_LONG_LIST) { - return pos + 1; - } - if ((payload[pos] & 0xFF) >= OFFSET_LONG_ITEM - && (payload[pos] & 0xFF) < OFFSET_SHORT_LIST) { - byte lengthOfLength = (byte) (payload[pos] - OFFSET_LONG_ITEM); - return pos + lengthOfLength + 1; - } - return -1; - } - - public static int getNextElementIndex(byte[] payload, int pos) { - - if (pos >= payload.length) { - return -1; - } - - if ((payload[pos] & 0xFF) >= OFFSET_LONG_LIST) { - byte lengthOfLength = (byte) (payload[pos] - OFFSET_LONG_LIST); - int length = calcLength(lengthOfLength, payload, pos); - return pos + lengthOfLength + length + 1; - } - if ((payload[pos] & 0xFF) >= OFFSET_SHORT_LIST - && (payload[pos] & 0xFF) < OFFSET_LONG_LIST) { - - byte length = (byte) ((payload[pos] & 0xFF) - OFFSET_SHORT_LIST); - return pos + 1 + length; - } - if ((payload[pos] & 0xFF) >= OFFSET_LONG_ITEM - && (payload[pos] & 0xFF) < OFFSET_SHORT_LIST) { - - byte lengthOfLength = (byte) (payload[pos] - OFFSET_LONG_ITEM); - int length = calcLength(lengthOfLength, payload, pos); - return pos + lengthOfLength + length + 1; - } - if ((payload[pos] & 0xFF) > OFFSET_SHORT_ITEM - && (payload[pos] & 0xFF) < OFFSET_LONG_ITEM) { - - byte length = (byte) ((payload[pos] & 0xFF) - OFFSET_SHORT_ITEM); - return pos + 1 + length; - } - if ((payload[pos] & 0xFF) == OFFSET_SHORT_ITEM) { - return pos + 1; - } - if ((payload[pos] & 0xFF) < OFFSET_SHORT_ITEM) { - return pos + 1; - } - return -1; - } - - /** - * Get exactly one message payload - */ - public static void fullTraverse(byte[] msgData, int level, int startPos, - int endPos, int levelToIndex, Queue index) { - - try { - - if (msgData == null || msgData.length == 0) { - return; - } - int pos = startPos; - - while (pos < endPos) { - - if (level == levelToIndex) { - index.add(pos); - } - - // It's a list with a payload more than 55 bytes - // data[0] - 0xF7 = how many next bytes allocated - // for the length of the list - if ((msgData[pos] & 0xFF) >= OFFSET_LONG_LIST) { - - byte lengthOfLength = (byte) (msgData[pos] - OFFSET_LONG_LIST); - int length = calcLength(lengthOfLength, msgData, pos); - - // now we can parse an item for data[1]..data[length] - System.out.println("-- level: [" + level - + "] Found big list length: " + length); - - fullTraverse(msgData, level + 1, pos + lengthOfLength + 1, - pos + lengthOfLength + length, levelToIndex, index); - - pos += lengthOfLength + length + 1; - continue; - } - // It's a list with a payload less than 55 bytes - if ((msgData[pos] & 0xFF) >= OFFSET_SHORT_LIST - && (msgData[pos] & 0xFF) < OFFSET_LONG_LIST) { - - byte length = (byte) ((msgData[pos] & 0xFF) - OFFSET_SHORT_LIST); - - System.out.println("-- level: [" + level - + "] Found small list length: " + length); - - fullTraverse(msgData, level + 1, pos + 1, pos + length + 1, - levelToIndex, index); - - pos += 1 + length; - continue; - } - // It's an item with a payload more than 55 bytes - // data[0] - 0xB7 = how much next bytes allocated for - // the length of the string - if ((msgData[pos] & 0xFF) >= OFFSET_LONG_ITEM - && (msgData[pos] & 0xFF) < OFFSET_SHORT_LIST) { - - byte lengthOfLength = (byte) (msgData[pos] - OFFSET_LONG_ITEM); - int length = calcLength(lengthOfLength, msgData, pos); - - // now we can parse an item for data[1]..data[length] - System.out.println("-- level: [" + level - + "] Found big item length: " + length); - pos += lengthOfLength + length + 1; - - continue; - } - // It's an item less than 55 bytes long, - // data[0] - 0x80 == length of the item - if ((msgData[pos] & 0xFF) > OFFSET_SHORT_ITEM - && (msgData[pos] & 0xFF) < OFFSET_LONG_ITEM) { - - byte length = (byte) ((msgData[pos] & 0xFF) - OFFSET_SHORT_ITEM); - - System.out.println("-- level: [" + level - + "] Found small item length: " + length); - pos += 1 + length; - continue; - } - // null item - if ((msgData[pos] & 0xFF) == OFFSET_SHORT_ITEM) { - System.out.println("-- level: [" + level - + "] Found null item: "); - pos += 1; - continue; - } - // single byte item - if ((msgData[pos] & 0xFF) < OFFSET_SHORT_ITEM) { - System.out.println("-- level: [" + level - + "] Found single item: "); - pos += 1; - continue; - } - } - } catch (Throwable th) { - throw new RuntimeException("RLP wrong encoding", - th.fillInStackTrace()); - } - } - - private static int calcLength(int lengthOfLength, byte[] msgData, int pos) { - byte pow = (byte) (lengthOfLength - 1); - int length = 0; - for (int i = 1; i <= lengthOfLength; ++i) { - length += (msgData[pos + i] & 0xFF) << (8 * pow); - pow--; - } - return length; - } - /** * Parse wire byte[] message into RLP elements * @@ -389,7 +152,7 @@ private static int calcLength(int lengthOfLength, byte[] msgData, int pos) { * - outcome of recursive RLP structure */ @Nonnull - public static ArrayList decode2(@CheckForNull byte[] msgData) { + public static ArrayList decodeListElements(@CheckForNull byte[] msgData) { ArrayList elements = new ArrayList<>(); if (msgData == null) { @@ -408,65 +171,30 @@ public static ArrayList decode2(@CheckForNull byte[] msgData) { return elements; } - public static RLPElement decodeFirstElement(@CheckForNull byte[] msgData, int position) { - if (msgData == null) { - return null; - } - - return decodeElement(msgData, position).getKey(); - } - - private static Pair decodeElement(byte[] msgData, int position) { // NOSONAR + private static Pair decodeElement(byte[] msgData, int position) { int b0 = msgData[position] & 0xff; - if (b0 >= 192) { - int length; - int offset; - - if (b0 <= 192 + TINY_SIZE) { - length = b0 - 192 + 1; - offset = 1; - } - else { - int nbytes = b0 - 247; - length = 1 + nbytes + bytesToLength(msgData, position + 1, nbytes); - offset = 1 + nbytes; - } - - if (Long.compareUnsigned(length, Integer.MAX_VALUE) > 0) { - throw new RLPException("The current implementation doesn't support lengths longer than Integer.MAX_VALUE because that is the largest number of elements an array can have"); - } - - if (position + length > msgData.length) { - throw new RLPException("The RLP byte array doesn't have enough space to hold an element with the specified length"); - } - - byte[] bytes = Arrays.copyOfRange(msgData, position, position + length); - RLPList list = new RLPList(bytes, offset); - - return Pair.of(list, position + length); + if (b0 < OFFSET_SHORT_ITEM) { + return Pair.of(new RLPItem(new byte[]{ msgData[position] }), position + 1); } - if (b0 == EMPTY_MARK) { + if (b0 == OFFSET_SHORT_ITEM) { return Pair.of(new RLPItem(ByteUtil.EMPTY_BYTE_ARRAY), position + 1); } - if (b0 < EMPTY_MARK) { - byte[] data = new byte[1]; - data[0] = msgData[position]; - return Pair.of(new RLPItem(data), position + 1); + if (b0 >= OFFSET_SHORT_LIST) { + return decodeListElement(msgData, position, b0); } int length; int offset; - if (b0 > (EMPTY_MARK + TINY_SIZE)) { - offset = b0 - (EMPTY_MARK + TINY_SIZE) + 1; - length = bytesToLength(msgData, position + 1, offset - 1); - } - else { - length = b0 & 0x7f; + if (b0 <= OFFSET_LONG_ITEM) { offset = 1; + length = b0 & 0x7f; + } else { + offset = b0 - OFFSET_LONG_ITEM + 1; + length = bytesToLength(msgData, position + 1, offset - 1); } if (Long.compareUnsigned(length, Integer.MAX_VALUE) > 0) { @@ -484,6 +212,32 @@ private static Pair decodeElement(byte[] msgData, int posit return Pair.of(new RLPItem(decoded), position + offset + length); } + private static Pair decodeListElement(byte[] msgData, int position, int b0) { + int length; + int offset; + + if (b0 <= OFFSET_LONG_LIST) { + offset = 1; + length = b0 - OFFSET_SHORT_LIST + 1; + } else { + offset = b0 - OFFSET_LONG_LIST + 1; + length = offset + bytesToLength(msgData, position + 1, offset - 1); + } + + if (Long.compareUnsigned(length, Integer.MAX_VALUE) > 0) { + throw new RLPException("The current implementation doesn't support lengths longer than Integer.MAX_VALUE because that is the largest number of elements an array can have"); + } + + if (position + length > msgData.length) { + throw new RLPException("The RLP byte array doesn't have enough space to hold an element with the specified length"); + } + + byte[] bytes = Arrays.copyOfRange(msgData, position, position + length); + RLPList list = new RLPList(bytes, offset); + + return Pair.of(list, position + length); + } + private static int bytesToLength(byte[] bytes, int position, int size) { if (position + size > bytes.length) { throw new RLPException("The length of the RLP item length can't possibly fit the data byte array"); @@ -507,26 +261,33 @@ private static int bytesToLength(byte[] bytes, int position, int size) { * Parse and verify that the passed data has just one list encoded as RLP */ public static RLPList decodeList(byte[] msgData) { - List decoded = RLP.decode2(msgData); - if (decoded.size() != 1) { - throw new IllegalArgumentException(String.format("Expected one RLP item but got %d", decoded.size())); + if (msgData == null) { + throw new IllegalArgumentException("The decoded element wasn't a list"); } - RLPElement element = decoded.get(0); - if (!(element instanceof RLPList)) { - throw new IllegalArgumentException("The decoded element wasn't a list"); + int b0 = msgData[0] & 0xff; + + if (b0 < OFFSET_SHORT_LIST) { + throw new RLPException("The decoded element wasn't a list"); } - return (RLPList) element; + return (RLPList) decodeListElement(msgData, 0, b0).getLeft(); } - @Nullable - public static RLPElement decode2OneItem(@CheckForNull byte[] msgData, int startPos) { + public static Pair decodeFirstElementReading(@CheckForNull byte[] msgData, int position) { + if (msgData == null) { + return null; + } + + return decodeElement(msgData, position); + } + + public static RLPElement decodeFirstElement(@CheckForNull byte[] msgData, int position) { if (msgData == null) { return null; } - return RLP.decodeFirstElement(msgData, startPos); + return decodeElement(msgData, position).getKey(); } @Nonnull @@ -540,7 +301,7 @@ public static RskAddress parseRskAddress(@Nullable byte[] bytes) { @Nonnull public static Coin parseCoin(@Nullable byte[] bytes) { - if (bytes == null || isAllZeroes(bytes)) { + if (bytes == null) { return Coin.ZERO; } else { return new Coin(bytes); @@ -565,14 +326,6 @@ public static Coin parseSignedCoinNonNullZero(byte[] bytes) { return new Coin(new BigInteger(bytes)); } - public static Coin parseCoinNullZero(@Nullable byte[] bytes) { - if (bytes == null) { - return Coin.ZERO; - } - - return new Coin(bytes); - } - /** * @param bytes the difficulty bytes, as expected by {@link BigInteger#BigInteger(byte[])}. */ @@ -589,58 +342,6 @@ public static BlockDifficulty parseBlockDifficulty(@Nullable byte[] bytes) { * ENCODING * * ******************************************************/ - /** - * Turn Object into its RLP encoded equivalent of a byte-array - * Support for String, Integer, BigInteger and Lists of any of these types. - * - * @param input as object or List of objects - * @return byte[] RLP encoded - */ - public static byte[] encode(Object input) { - Value val = new Value(input); - if (val.isList()) { - List inputArray = val.asList(); - if (inputArray.isEmpty()) { - return encodeLength(inputArray.size(), OFFSET_SHORT_LIST); - } - byte[] output = ByteUtil.EMPTY_BYTE_ARRAY; - for (Object object : inputArray) { - output = concatenate(output, encode(object)); - } - byte[] prefix = encodeLength(output.length, OFFSET_SHORT_LIST); - return concatenate(prefix, output); - } else { - byte[] inputAsBytes = toBytes(input); - if (inputAsBytes.length == 1 && (inputAsBytes[0] & 0xff) < 0x80) { - return inputAsBytes; - } else { - byte[] firstByte = encodeLength(inputAsBytes.length, OFFSET_SHORT_ITEM); - return concatenate(firstByte, inputAsBytes); - } - } - } - - /** - * Integer limitation goes up to 2^31-1 so length can never be bigger than MAX_ITEM_LENGTH - */ - public static byte[] encodeLength(int length, int offset) { - if (length < SIZE_THRESHOLD) { - byte firstByte = (byte) (length + offset); - return new byte[]{firstByte}; - } else if (length < MAX_ITEM_LENGTH) { - byte[] binaryLength; - if (length > 0xFF) { - binaryLength = intToBytesNoLeadZeroes(length); - } else { - binaryLength = new byte[]{(byte) length}; - } - byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1); - return concatenate(new byte[]{firstByte}, binaryLength); - } else { - throw new RuntimeException("Input too long"); - } - } - public static byte[] encodeByte(byte singleByte) { if ((singleByte & 0xFF) == 0) { return new byte[]{(byte) OFFSET_SHORT_ITEM}; @@ -786,71 +487,7 @@ public static byte[] encodeElement(@Nullable byte[] srcData) { } } - public static byte[] encodeListHeader(int size) { - - if (size == 0) { - return new byte[]{(byte) OFFSET_SHORT_LIST}; - } - - int totalLength = size; - - byte[] header; - if (totalLength < SIZE_THRESHOLD) { - - header = new byte[1]; - header[0] = (byte) (OFFSET_SHORT_LIST + totalLength); - } else { - // length of length = BX - // prefix = [BX, [length]] - int tmpLength = totalLength; - byte byteNum = 0; - while (tmpLength != 0) { - ++byteNum; - tmpLength = tmpLength >> 8; - } - tmpLength = totalLength; - - byte[] lenBytes = new byte[byteNum]; - for (int i = 0; i < byteNum; ++i) { - lenBytes[byteNum - 1 - i] = (byte) ((tmpLength >> (8 * i)) & 0xFF); - } - // first byte = F7 + bytes.length - header = new byte[1 + lenBytes.length]; - header[0] = (byte) (OFFSET_LONG_LIST + byteNum); - System.arraycopy(lenBytes, 0, header, 1, lenBytes.length); - - } - - return header; - } - - public static byte[] encodeSet(Set data) { - - int dataLength = 0; - Set encodedElements = new HashSet<>(); - for (ByteArrayWrapper element : data) { - - byte[] encodedElement = RLP.encodeElement(element.getData()); - dataLength += encodedElement.length; - encodedElements.add(encodedElement); - } - - byte[] listHeader = encodeListHeader(dataLength); - - byte[] output = new byte[listHeader.length + dataLength]; - - System.arraycopy(listHeader, 0, output, 0, listHeader.length); - - int cummStart = listHeader.length; - for (byte[] element : encodedElements) { - System.arraycopy(element, 0, output, cummStart, element.length); - cummStart += element.length; - } - - return output; - } - - public static byte[] encodeList(byte[]... elements) { + public static byte[]encodeList(byte[]... elements) { if (elements == null) { return new byte[]{(byte) OFFSET_SHORT_LIST}; @@ -895,43 +532,4 @@ public static byte[] encodeList(byte[]... elements) { } return data; } - - /* - * Utility function to convert Objects into byte arrays - */ - private static byte[] toBytes(Object input) { - if (input instanceof byte[]) { - return (byte[]) input; - } else if (input instanceof String) { - String inputString = (String) input; - return inputString.getBytes(StandardCharsets.UTF_8); - } else if (input instanceof Long) { - Long inputLong = (Long) input; - return (inputLong == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputLong)); - } else if (input instanceof Integer) { - Integer inputInt = (Integer) input; - return (inputInt == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputInt)); - } else if (input instanceof BigInteger) { - BigInteger inputBigInt = (BigInteger) input; - return (inputBigInt.equals(BigInteger.ZERO)) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(inputBigInt); - } else if (input instanceof Value) { - Value val = (Value) input; - return toBytes(val.asObj()); - } - throw new RuntimeException("Unsupported type: Only accepting String, Integer and BigInteger for now"); - } - - /** - * An encoded empty list - */ - public static byte[] encodedEmptyList() { - return new byte[] {(byte) OFFSET_SHORT_LIST}; - } - - /** - * An encoded empty byte array - */ - public static byte[] encodedEmptyByteArray() { - return new byte[] {(byte) OFFSET_SHORT_ITEM}; - } } \ No newline at end of file diff --git a/rskj-core/src/main/java/org/ethereum/util/RLPList.java b/rskj-core/src/main/java/org/ethereum/util/RLPList.java index c4a5f035f53..121310a8d56 100644 --- a/rskj-core/src/main/java/org/ethereum/util/RLPList.java +++ b/rskj-core/src/main/java/org/ethereum/util/RLPList.java @@ -55,6 +55,6 @@ private void checkElements() { byte[] bytes = this.getRLPData(); byte[] content = Arrays.copyOfRange(bytes, offset, bytes.length); - this.elements = RLP.decode2(content); + this.elements = RLP.decodeListElements(content); } } diff --git a/rskj-core/src/main/java/org/ethereum/vm/GasCost.java b/rskj-core/src/main/java/org/ethereum/vm/GasCost.java index 7501cf2cac2..b4d3b53923b 100644 --- a/rskj-core/src/main/java/org/ethereum/vm/GasCost.java +++ b/rskj-core/src/main/java/org/ethereum/vm/GasCost.java @@ -135,14 +135,11 @@ private GasCost() { } * is negative. */ public static long toGas(byte[] bytes) throws InvalidGasException { - if (bytes.length > 8) { + if (bytes.length > 8 || (bytes.length == 8 && (bytes[0] & 0xff) >= 0x80)) { return Long.MAX_VALUE; } - long result = ByteUtil.byteArrayToLong(bytes); - if (result < 0) { - throw new InvalidGasException(bytes); - } - return result; + + return ByteUtil.byteArrayToLong(bytes); } /** diff --git a/rskj-core/src/main/java/org/ethereum/vm/LogInfo.java b/rskj-core/src/main/java/org/ethereum/vm/LogInfo.java index f27ad982211..7e41fd8b80a 100644 --- a/rskj-core/src/main/java/org/ethereum/vm/LogInfo.java +++ b/rskj-core/src/main/java/org/ethereum/vm/LogInfo.java @@ -44,8 +44,7 @@ public class LogInfo { public LogInfo(byte[] rlp) { - ArrayList params = RLP.decode2(rlp); - RLPList logInfo = (RLPList) params.get(0); + RLPList logInfo = RLP.decodeList(rlp); RLPItem address = (RLPItem) logInfo.get(0); RLPList topics = (RLPList) logInfo.get(1); diff --git a/rskj-core/src/main/java/org/ethereum/vm/program/InternalTransaction.java b/rskj-core/src/main/java/org/ethereum/vm/program/InternalTransaction.java index b2a4fa2e614..c71107ec192 100644 --- a/rskj-core/src/main/java/org/ethereum/vm/program/InternalTransaction.java +++ b/rskj-core/src/main/java/org/ethereum/vm/program/InternalTransaction.java @@ -93,12 +93,12 @@ public byte[] getOriginHash() { public byte[] getEncoded() { byte[] nonce = getNonce(); if (isEmpty(nonce) || getLength(nonce) == 1 && nonce[0] == 0) { - nonce = RLP.encodeElement((byte[]) null); + nonce = RLP.encodeElement(null); } else { nonce = RLP.encodeElement(nonce); } - byte[] senderAddress = RLP.encodeElement(getSender(signatureCache).getBytes()); - byte[] receiveAddress = RLP.encodeElement(getReceiveAddress().getBytes()); + byte[] senderAddress = RLP.encodeRskAddress(getSender(signatureCache)); + byte[] receiveAddress = RLP.encodeRskAddress(getReceiveAddress()); byte[] value = RLP.encodeCoin(getValue()); byte[] gasPrice = RLP.encodeCoin(getGasPrice()); byte[] gasLimit = RLP.encodeElement(getGasLimit()); diff --git a/rskj-core/src/test/java/co/rsk/blockchain/utils/BlockGenerator.java b/rskj-core/src/test/java/co/rsk/blockchain/utils/BlockGenerator.java index 03360b9cfeb..7bc05fc586a 100644 --- a/rskj-core/src/test/java/co/rsk/blockchain/utils/BlockGenerator.java +++ b/rskj-core/src/test/java/co/rsk/blockchain/utils/BlockGenerator.java @@ -489,7 +489,7 @@ private static byte[] nullReplace(byte[] e) { } private static byte[] removeLastElement(byte[] rlpEncoded) { - ArrayList params = RLP.decode2(rlpEncoded); + ArrayList params = RLP.decodeListElements(rlpEncoded); RLPList block = (RLPList) params.get(0); RLPList header = (RLPList) block.get(0); if (header.size() < 20) { diff --git a/rskj-core/src/test/java/co/rsk/core/FreeBlock.java b/rskj-core/src/test/java/co/rsk/core/FreeBlock.java index d1d3dd1ae74..8fad13b3cfa 100644 --- a/rskj-core/src/test/java/co/rsk/core/FreeBlock.java +++ b/rskj-core/src/test/java/co/rsk/core/FreeBlock.java @@ -81,7 +81,7 @@ public FreeBlock(byte[] parentHash, byte[] unclesHash, byte[] coinbase, byte[] l private void parseRLP() { - ArrayList params = RLP.decode2(rlpEncoded); + ArrayList params = RLP.decodeListElements(rlpEncoded); RLPList block = (RLPList) params.get(0); // Parse Header diff --git a/rskj-core/src/test/java/co/rsk/core/FreeBlockHeader.java b/rskj-core/src/test/java/co/rsk/core/FreeBlockHeader.java index cd37afb037b..b99b4850c28 100644 --- a/rskj-core/src/test/java/co/rsk/core/FreeBlockHeader.java +++ b/rskj-core/src/test/java/co/rsk/core/FreeBlockHeader.java @@ -78,7 +78,7 @@ public class FreeBlockHeader { private byte[] uncleCount; public FreeBlockHeader(byte[] encoded) { - this((RLPList) RLP.decode2(encoded).get(0)); + this((RLPList) RLP.decodeListElements(encoded).get(0)); } public FreeBlockHeader(RLPList rlpHeader) { diff --git a/rskj-core/src/test/java/co/rsk/mine/MinerServerTest.java b/rskj-core/src/test/java/co/rsk/mine/MinerServerTest.java index cdca5ceea1b..a4a7de12473 100644 --- a/rskj-core/src/test/java/co/rsk/mine/MinerServerTest.java +++ b/rskj-core/src/test/java/co/rsk/mine/MinerServerTest.java @@ -50,6 +50,7 @@ import org.ethereum.util.RLP; import org.ethereum.util.RLPList; import org.ethereum.util.RskTestFactory; +import org.ethereum.util.RLPTestUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -607,7 +608,7 @@ void extraDataNotInitializedWithClientData() { byte[] firstItem = decodedExtraData.get(0).getRLPData(); assertNotNull(firstItem); - assertEquals(1, (RLP.decodeInt(firstItem,0))); + assertEquals(1, (RLPTestUtil.decodeInt(firstItem,0))); byte[] secondItem = decodedExtraData.get(1).getRLPData(); assertNotNull(secondItem); @@ -637,7 +638,7 @@ void extraDataWithClientData() { byte[] firstItem = decodedExtraData.get(0).getRLPData(); assertNotNull(firstItem); - assertEquals(1, (RLP.decodeInt(firstItem,0))); + assertEquals(1, (RLPTestUtil.decodeInt(firstItem,0))); byte[] secondItem = decodedExtraData.get(1).getRLPData(); assertNotNull(secondItem); @@ -672,7 +673,7 @@ void extraDataWithClientDataMoreThan32Bytes() { byte[] firstItem = decodedExtraData.get(0).getRLPData(); assertNotNull(firstItem); - assertEquals(1, (RLP.decodeInt(firstItem,0))); + assertEquals(1, (RLPTestUtil.decodeInt(firstItem,0))); byte[] secondItem = decodedExtraData.get(1).getRLPData(); assertNotNull(secondItem); diff --git a/rskj-core/src/test/java/co/rsk/net/discovery/PeerMessagesTest.java b/rskj-core/src/test/java/co/rsk/net/discovery/PeerMessagesTest.java index 6ac3c43c382..e7e6a414b9e 100644 --- a/rskj-core/src/test/java/co/rsk/net/discovery/PeerMessagesTest.java +++ b/rskj-core/src/test/java/co/rsk/net/discovery/PeerMessagesTest.java @@ -17,7 +17,7 @@ class PeerMessagesTest { private static byte[] wirePingPongPeerMessage = new byte[]{113,-5,-63,15,-4,21,-42,93,-10,19,-95,-37,-29,63,-92,-125,28,-27,-60,99,-85,-33,43,86,-12,-74,66,100,-31,-41,32,107,-69,20,-38,-122,-33,-110,-118,-126,-12,-85,20,91,44,44,-5,79,-101,-53,124,-57,-29,118,-81,-48,-89,-99,-29,-93,8,-17,113,-87,100,-123,-124,-77,-99,76,-51,54,4,-78,-41,116,84,-116,35,-35,-30,-12,16,-87,23,-32,-73,124,71,-26,-105,-28,-14,91,122,-114,1,1,-8,76,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-92,48,97,57,54,55,56,53,53,45,99,102,98,97,45,52,98,49,98,45,57,56,57,48,45,102,54,99,57,51,49,97,52,97,55,97,48,-124,-59,3,-58,-55}; private static byte[] mdcPingPongPeerMessage = new byte[]{113,-5,-63,15,-4,21,-42,93,-10,19,-95,-37,-29,63,-92,-125,28,-27,-60,99,-85,-33,43,86,-12,-74,66,100,-31,-41,32,107}; private static byte[] signaturePingPongPeerMessage = new byte[]{-69,20,-38,-122,-33,-110,-118,-126,-12,-85,20,91,44,44,-5,79,-101,-53,124,-57,-29,118,-81,-48,-89,-99,-29,-93,8,-17,113,-87,100,-123,-124,-77,-99,76,-51,54,4,-78,-41,116,84,-116,35,-35,-30,-12,16,-87,23,-32,-73,124,71,-26,-105,-28,-14,91,122,-114,1}; - private static byte[] dataWithNetworkIdPingPongPeerMessage = new byte[]{-8,76,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-92,48,97,57,54,55,56,53,53,45,99,102,98,97,45,52,98,49,98,45,57,56,57,48,45,102,54,99,57,51,49,97,52,97,55,97,48,-124,-59,3,-58,-55}; + private static byte[] dataWithNetworkIdPingPongPeerMessage = new byte[]{-8,76,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-92,48,97,57,54,55,56,53,53,45,99,102,98,97,45,52,98,49,98,45,57,56,57,48,45,102,54,99,57,51,49,97,52,97,55,97,48,-124,00,00,00,01}; private static byte[] dataWithoutNetworkIdPingPongPeerMessage = new byte[]{-8,71,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-48,-119,108,111,99,97,108,104,111,115,116,-126,31,-112,-126,31,-112,-92,55,52,56,49,99,49,51,57,45,98,99,102,99,45,52,53,52,50,45,97,55,55,48,45,53,99,97,52,57,97,52,101,97,97,55,97}; private static byte[] typePong = new byte[]{(byte) DiscoveryMessageType.PONG.getTypeValue()}; private static byte[] typePing = new byte[]{(byte) DiscoveryMessageType.PING.getTypeValue()}; @@ -27,7 +27,7 @@ class PeerMessagesTest { private static byte[] wireFindNodePeerMessage = new byte[]{-23,116,115,41,75,96,-74,89,-26,-64,-33,-84,-81,-65,-34,-32,-89,-112,125,25,48,94,-75,114,109,28,-58,-55,-83,-64,-46,69,-74,-122,61,12,-101,26,-14,-28,72,-93,26,-125,-12,-38,55,80,31,33,-105,110,54,-25,80,45,18,-44,-127,-22,-50,-88,-78,77,116,-66,-56,-54,126,-5,-118,-91,-88,-76,52,56,-103,121,59,1,72,109,94,-14,118,47,62,-76,41,-63,82,103,125,-100,13,-88,0,3,-16,-118,-2,-74,-97,-8,69,1,67,49,16,99,49,56,51,56,100,52,100,45,49,122,-92,99,52,55,45,52,49,98,50,45,57,99,54,55,45,52,57,51,97,57,56,55,101,98,48,48,99}; private static byte[] mdcFindNodePeerMessage = new byte[]{127,110,79,83,31,7,86,104,42,124,86,-57,76,-92,93,6,82,-37,97,-127,-54,72,86,-29,-81,-97,-10,94,-23,-102,-16,-82}; private static byte[] dataWithoutNetworkId = new byte[]{-16,-118,55,-9,-121,-84,69,-97,31,36,-115,111,-92,98,101,48,52,97,98,98,98,45,54,48,50,102,45,52,99,49,50,45,56,99,48,56,45,101,55,52,101,51,49,101,54,54,48,97,97}; - private static byte[] dataWithNetworkId = new byte[]{-11,-118,86,54,14,-30,58,118,14,-123,57,84,-92,54,53,98,52,54,57,99,54,45,98,97,54,57,45,52,50,51,56,45,98,102,53,97,45,97,52,56,53,99,48,50,52,101,51,99,101,-124,-112,-104,-118,19}; + private static byte[] dataWithNetworkId = new byte[]{-11,-118,86,54,14,-30,58,118,14,-123,57,84,-92,54,53,98,52,54,57,99,54,45,98,97,54,57,45,52,50,51,56,45,98,102,53,97,45,97,52,56,53,99,48,50,52,101,51,99,101,-124, 00, 00, 00, 01}; @Test @@ -44,7 +44,7 @@ void testParsePongPeerMessageWithoutNetworkId(){ void testParsePongPeerMessageWithNetworkId() { PongPeerMessage pongPeerMessage = PongPeerMessage.buildFromReceived(wirePingPongPeerMessage, mdcPingPongPeerMessage, signaturePingPongPeerMessage, typePong, dataWithNetworkIdPingPongPeerMessage); Assertions.assertTrue(pongPeerMessage.getNetworkId().isPresent()); - Assertions.assertEquals(-989608247,pongPeerMessage.getNetworkId().getAsInt()); + Assertions.assertEquals(1,pongPeerMessage.getNetworkId().getAsInt()); Assertions.assertEquals("0a967855-cfba-4b1b-9890-f6c931a4a7a0",pongPeerMessage.getMessageId()); Assertions.assertEquals(GOOD_HOST,pongPeerMessage.getHost()); Assertions.assertEquals(GOOD_PORT,pongPeerMessage.getPort()); @@ -65,7 +65,7 @@ void testParsePingPeerMessageWithoutNetworkId(){ void testParsePingPeerMessageWithNetworkId() { PingPeerMessage pingPeerMessage = PingPeerMessage.buildFromReceived(wirePingPongPeerMessage, mdcPingPongPeerMessage, signaturePingPongPeerMessage,typePing, dataWithNetworkIdPingPongPeerMessage); Assertions.assertTrue(pingPeerMessage.getNetworkId().isPresent()); - Assertions.assertEquals(-989608247,pingPeerMessage.getNetworkId().getAsInt()); + Assertions.assertEquals(1,pingPeerMessage.getNetworkId().getAsInt()); Assertions.assertEquals("0a967855-cfba-4b1b-9890-f6c931a4a7a0",pingPeerMessage.getMessageId()); Assertions.assertEquals(GOOD_HOST,pingPeerMessage.getHost()); Assertions.assertEquals(GOOD_PORT,pingPeerMessage.getPort()); @@ -87,7 +87,7 @@ void testParseFindNodePeerMessageWithoutNetworkId() { void testParseFindNodePeerMessage() { FindNodePeerMessage findNodePeerMessageExpected = FindNodePeerMessage.buildFromReceived(wireFindNodePeerMessage,mdcFindNodePeerMessage,signatureFindNodePeerMessage,typeFindNodePeerMessage,dataWithNetworkId); Assertions.assertTrue(findNodePeerMessageExpected.getNetworkId().isPresent()); - Assertions.assertEquals(-1869051373,findNodePeerMessageExpected.getNetworkId().getAsInt()); + Assertions.assertEquals(1,findNodePeerMessageExpected.getNetworkId().getAsInt()); Assertions.assertEquals("65b469c6-ba69-4238-bf5a-a485c024e3ce", findNodePeerMessageExpected.getMessageId()); Assertions.assertTrue(Arrays.equals(findNodePeerMessageExpected.getMdc(), mdcFindNodePeerMessage)); Assertions.assertTrue(Arrays.equals(findNodePeerMessageExpected.getPacket(), wireFindNodePeerMessage)); diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java index 35190f974fc..e1c2f2202ef 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java @@ -43,6 +43,7 @@ import org.ethereum.util.ByteUtil; import org.ethereum.util.RLP; import org.ethereum.util.RLPList; +import org.ethereum.util.RLPTestUtil; import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -308,7 +309,7 @@ void serializeFederation_serializedKeysAreCompressedAndThree() { byte[] serializedFederation = BridgeSerializationUtils.serializeFederation(testFederation); - RLPList serializedList = (RLPList) RLP.decode2(serializedFederation).get(0); + RLPList serializedList = (RLPList) RLP.decodeListElements(serializedFederation).get(0); Assertions.assertEquals(3, serializedList.size()); @@ -317,7 +318,7 @@ void serializeFederation_serializedKeysAreCompressedAndThree() { Assertions.assertEquals(NUM_MEMBERS, memberList.size()); for (int i = 0; i < NUM_MEMBERS; i++) { - RLPList memberKeys = (RLPList) RLP.decode2(memberList.get(i).getRLPData()).get(0); + RLPList memberKeys = (RLPList) RLP.decodeListElements(memberList.get(i).getRLPData()).get(0); Assertions.assertEquals(EXPECTED_NUM_KEYS, memberKeys.size()); for (int j = 0; j < EXPECTED_NUM_KEYS; j++) { Assertions.assertEquals(EXPECTED_PUBLICKEY_SIZE, memberKeys.get(j).getRLPData().length); @@ -384,12 +385,12 @@ void serializePendingFederation_serializedKeysAreCompressedAndThree() { byte[] serializedPendingFederation = BridgeSerializationUtils.serializePendingFederation(testPendingFederation); - RLPList memberList = (RLPList) RLP.decode2(serializedPendingFederation).get(0); + RLPList memberList = (RLPList) RLP.decodeListElements(serializedPendingFederation).get(0); Assertions.assertEquals(NUM_MEMBERS, memberList.size()); for (int i = 0; i < NUM_MEMBERS; i++) { - RLPList memberKeys = (RLPList) RLP.decode2(memberList.get(i).getRLPData()).get(0); + RLPList memberKeys = (RLPList) RLP.decodeListElements(memberList.get(i).getRLPData()).get(0); Assertions.assertEquals(EXPECTED_NUM_KEYS, memberKeys.size()); for (int j = 0; j < EXPECTED_NUM_KEYS; j++) { Assertions.assertEquals(EXPECTED_PUBLICKEY_SIZE, memberKeys.get(j).getRLPData().length); @@ -651,7 +652,7 @@ void deserializeElection_unevenOuterList() throws Exception { void deserializeElection_invalidCallSpec() throws Exception { AddressBasedAuthorizer authorizer = getTestingAddressBasedAuthorizer(); - byte[] rlpFirstSpec = RLP.encodeList(RLP.encode(Hex.decode("010203"))); // invalid spec + byte[] rlpFirstSpec = RLP.encodeList(RLPTestUtil.encode(Hex.decode("010203"))); // invalid spec byte[] rlpFirstVoters = RLP.encodeList(RLP.encodeElement(Hex.decode("03"))); // doesn't matter byte[] data = RLP.encodeList(rlpFirstSpec, rlpFirstVoters); @@ -1089,7 +1090,7 @@ void serializeScript() { byte[] actualData = BridgeSerializationUtils.serializeScript(expectedScript); - Assertions.assertEquals(expectedScript, new Script(((RLPList) RLP.decode2(actualData).get(0)).get(0).getRLPData())); + Assertions.assertEquals(expectedScript, new Script(((RLPList) RLP.decodeListElements(actualData).get(0)).get(0).getRLPData())); } @Test diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java index 5f4744eef9e..932b1e94359 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java @@ -532,7 +532,7 @@ void addSignatureMultipleInputsPartiallyValid() throws Exception { LogInfo releaseTxEvent = logs.get(4); assertThat(releaseTxEvent.getTopics(), hasSize(1)); assertThat(releaseTxEvent.getTopics(), hasItem(Bridge.RELEASE_BTC_TOPIC)); - BtcTransaction releaseTx = new BtcTransaction(btcRegTestParams, ((RLPList) RLP.decode2(releaseTxEvent.getData()).get(0)).get(1).getRLPData()); + BtcTransaction releaseTx = new BtcTransaction(btcRegTestParams, ((RLPList) RLP.decodeListElements(releaseTxEvent.getData()).get(0)).get(1).getRLPData()); // Verify all inputs fully signed for (int i = 0; i < releaseTx.getInputs().size(); i++) { Script retrievedScriptSig = releaseTx.getInput(i).getScriptSig(); @@ -643,7 +643,7 @@ private void addSignatureFromValidFederator(List privateKeysToSignWith LogInfo releaseTxEvent = logs.get(2); assertThat(releaseTxEvent.getTopics(), hasSize(1)); assertThat(releaseTxEvent.getTopics(), hasItem(Bridge.RELEASE_BTC_TOPIC)); - BtcTransaction releaseTx = new BtcTransaction(btcRegTestParams, ((RLPList) RLP.decode2(releaseTxEvent.getData()).get(0)).get(1).getRLPData()); + BtcTransaction releaseTx = new BtcTransaction(btcRegTestParams, ((RLPList) RLP.decodeListElements(releaseTxEvent.getData()).get(0)).get(1).getRLPData()); Script retrievedScriptSig = releaseTx.getInput(0).getScriptSig(); assertEquals(4, retrievedScriptSig.getChunks().size()); assertTrue(retrievedScriptSig.getChunks().get(1).data.length > 0); diff --git a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java index dfc52e60c63..44975a84826 100644 --- a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java @@ -134,7 +134,7 @@ void testLogAddSignatureBeforeRskip146() { // Assert log data Assertions.assertNotNull(logResult.getData()); - List rlpData = RLP.decode2(logResult.getData()); + List rlpData = RLP.decodeListElements(logResult.getData()); Assertions.assertEquals(1, rlpData.size()); RLPList dataList = (RLPList) rlpData.get(0); Assertions.assertEquals(3, dataList.size()); @@ -242,7 +242,7 @@ void testLogCommitFederationBeforeRskip146() { // Assert log data Assertions.assertNotNull(logResult.getData()); - List rlpData = RLP.decode2(logResult.getData()); + List rlpData = RLP.decodeListElements(logResult.getData()); Assertions.assertEquals(1, rlpData.size()); RLPList dataList = (RLPList) rlpData.get(0); Assertions.assertEquals(3, dataList.size()); diff --git a/rskj-core/src/test/java/co/rsk/remasc/RemascFeesPayerTest.java b/rskj-core/src/test/java/co/rsk/remasc/RemascFeesPayerTest.java index a3ff1f16c03..2833964cae7 100644 --- a/rskj-core/src/test/java/co/rsk/remasc/RemascFeesPayerTest.java +++ b/rskj-core/src/test/java/co/rsk/remasc/RemascFeesPayerTest.java @@ -79,7 +79,7 @@ void payMiningFees() { // Assert log data Assertions.assertNotNull(result.getData()); - List rlpData = RLP.decode2(result.getData()); + List rlpData = RLP.decodeListElements(result.getData()); Assertions.assertEquals(1 , rlpData.size()); RLPList dataList = (RLPList)rlpData.get(0); Assertions.assertEquals(2, dataList.size()); diff --git a/rskj-core/src/test/java/co/rsk/util/RLPTest.java b/rskj-core/src/test/java/co/rsk/util/RLPTest.java index a2bdf5e4b91..46abd435376 100644 --- a/rskj-core/src/test/java/co/rsk/util/RLPTest.java +++ b/rskj-core/src/test/java/co/rsk/util/RLPTest.java @@ -3,6 +3,7 @@ import org.ethereum.util.RLP; import org.ethereum.util.RLPElement; import org.ethereum.util.RLPList; +import org.ethereum.util.RLPTestUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -46,7 +47,7 @@ void encodeDecodeSingleBytes() { Assertions.assertNotNull(encoded); Assertions.assertEquals(1, encoded.length); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -69,7 +70,7 @@ void encodeDecodeSingleBytesWithHighValue() { Assertions.assertEquals(2, encoded.length); Assertions.assertEquals((byte)129, encoded[0]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -86,13 +87,13 @@ void encodeDecodeSingleBytesWithHighValueUsingEncode() { byte[] bytes = new byte[1]; bytes[0] = (byte)k; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(2, encoded.length); Assertions.assertEquals((byte)129, encoded[0]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -114,7 +115,7 @@ void encodeDecodeShortByteArrays() { Assertions.assertEquals(1 + k, encoded.length); Assertions.assertEquals((byte)(128 + k), encoded[0]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -130,13 +131,13 @@ void encodeDecodeShortByteArraysUsingEncode() { for (int k = 2; k < 56; k++) { byte[] bytes = new byte[k]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(1 + k, encoded.length); Assertions.assertEquals((byte)(128 + k), encoded[0]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -158,7 +159,7 @@ void encodeDecodeLongByteArrayWithOneByteLength() { Assertions.assertEquals((byte)(183 + 1), encoded[0]); Assertions.assertEquals((byte)56, encoded[1]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -172,14 +173,14 @@ void encodeDecodeLongByteArrayWithOneByteLength() { void encodeDecodeLongByteArrayWithOneByteLengthUsingEncode() { byte[] bytes = new byte[56]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(2 + 56, encoded.length); Assertions.assertEquals((byte)(183 + 1), encoded[0]); Assertions.assertEquals((byte)56, encoded[1]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -201,7 +202,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLength() { Assertions.assertEquals((byte)1, encoded[1]); Assertions.assertEquals((byte)0, encoded[2]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -215,7 +216,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLength() { void encodeDecodeLongByteArrayWithTwoBytesLengthUsingEncode() { byte[] bytes = new byte[256]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(3 + 256, encoded.length); @@ -223,7 +224,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLengthUsingEncode() { Assertions.assertEquals((byte)1, encoded[1]); Assertions.assertEquals((byte)0, encoded[2]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -245,7 +246,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCase() { Assertions.assertEquals((byte)0xff, encoded[1]); Assertions.assertEquals((byte)0xff, encoded[2]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -259,7 +260,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCase() { void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCaseUsingEncode() { byte[] bytes = new byte[256 * 256 - 1]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(3 + 256 * 256 - 1, encoded.length); @@ -267,7 +268,7 @@ void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCaseUsingEncode() { Assertions.assertEquals((byte)0xff, encoded[1]); Assertions.assertEquals((byte)0xff, encoded[2]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -290,7 +291,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLength() { Assertions.assertEquals((byte)0x00, encoded[2]); Assertions.assertEquals((byte)0x00, encoded[3]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -304,7 +305,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLength() { void encodeDecodeLongByteArrayWithThreeBytesLengthUsingEncode() { byte[] bytes = new byte[256 * 256]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(4 + 256 * 256, encoded.length); @@ -313,7 +314,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLengthUsingEncode() { Assertions.assertEquals((byte)0x00, encoded[2]); Assertions.assertEquals((byte)0x00, encoded[3]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -336,7 +337,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLengthBorderCase() { Assertions.assertEquals((byte)0xff, encoded[2]); Assertions.assertEquals((byte)0xff, encoded[3]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -350,7 +351,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLengthBorderCase() { void encodeDecodeLongByteArrayWithThreeBytesLengthBorderCaseUsingEncode() { byte[] bytes = new byte[256 * 256 * 256 - 1]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(4 + 256 * 256 * 256 - 1, encoded.length); @@ -359,7 +360,7 @@ void encodeDecodeLongByteArrayWithThreeBytesLengthBorderCaseUsingEncode() { Assertions.assertEquals((byte)0xff, encoded[2]); Assertions.assertEquals((byte)0xff, encoded[3]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -383,7 +384,7 @@ void encodeDecodeLongByteArrayWithFourBytesLength() { Assertions.assertEquals((byte)0x00, encoded[3]); Assertions.assertEquals((byte)0x00, encoded[4]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -397,7 +398,7 @@ void encodeDecodeLongByteArrayWithFourBytesLength() { void encodeDecodeLongByteArrayWithFourBytesLengthUsingEncode() { byte[] bytes = new byte[256 * 256 * 256]; - byte[] encoded = RLP.encode(bytes); + byte[] encoded = RLPTestUtil.encode(bytes); Assertions.assertNotNull(encoded); Assertions.assertEquals(5 + 256 * 256 * 256, encoded.length); @@ -407,7 +408,7 @@ void encodeDecodeLongByteArrayWithFourBytesLengthUsingEncode() { Assertions.assertEquals((byte)0x00, encoded[3]); Assertions.assertEquals((byte)0x00, encoded[4]); - RLPElement element = RLP.decode2OneItem(encoded, 0); + RLPElement element = RLP.decodeFirstElement(encoded, 0); Assertions.assertNotNull(element); @@ -436,7 +437,7 @@ void encodeDecodeEmptyList() { Assertions.assertEquals(1, encoded.length); Assertions.assertEquals((byte)192, encoded[0]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -459,7 +460,7 @@ void encodeDecodeShortListWithShortBytes() { Assertions.assertEquals(3, encoded.length); Assertions.assertEquals((byte)(192 + 2), encoded[0]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -484,7 +485,7 @@ void encodeDecodeShortListWithTwoByteArrays() { Assertions.assertEquals(1 + 3 + 3, encoded.length); Assertions.assertEquals((byte)(192 + 3 + 3), encoded[0]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -509,7 +510,7 @@ void encodeDecodeShortListWithTwoByteArraysWithElementsLength55() { Assertions.assertEquals(1 + 1 + 25 + 1 + 28, encoded.length); Assertions.assertEquals((byte)(192 + 55), encoded[0]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -535,7 +536,7 @@ void encodeDecodeShortListWithTwoByteArraysWithElementsLength56() { Assertions.assertEquals((byte)(247 + 1), encoded[0]); Assertions.assertEquals((byte)(56), encoded[1]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -561,7 +562,7 @@ void encodeDecodeShortListWithTwoByteArraysWithOneByteLength() { Assertions.assertEquals((byte)(247 + 1), encoded[0]); Assertions.assertEquals((byte)(255), encoded[1]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -588,7 +589,7 @@ void encodeDecodeShortListWithTwoByteArraysWithTwoBytesLength() { Assertions.assertEquals((byte)(1), encoded[1]); Assertions.assertEquals((byte)(0), encoded[2]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -615,7 +616,7 @@ void encodeDecodeShortListWithTwoByteArraysWithTwoBytesLengthBorderCase() { Assertions.assertEquals((byte)(255), encoded[1]); Assertions.assertEquals((byte)(255), encoded[2]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -643,7 +644,7 @@ void encodeDecodeShortListWithTwoByteArraysWithThreeBytesLength() { Assertions.assertEquals((byte)(0), encoded[2]); Assertions.assertEquals((byte)(0), encoded[3]); - ArrayList list = RLP.decode2(encoded); + ArrayList list = RLP.decodeListElements(encoded); Assertions.assertNotNull(list); Assertions.assertEquals(1, list.size()); @@ -661,7 +662,7 @@ void invalidLengthWithZeroByteLength() { byte[] encoded = new byte[] { (byte)0x81 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -674,7 +675,7 @@ void invalidLengthWithZeroByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)0x81 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -687,7 +688,7 @@ void invalidLengthWithOneByteLength() { byte[] encoded = new byte[] { (byte)(183 + 1), 0x01 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -700,7 +701,7 @@ void invalidLengthWithOneByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 1), 0x01 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -715,7 +716,7 @@ void invalidLengthWithOneByteLengthBorderCase() { encoded[1] = (byte)0xff; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -730,7 +731,7 @@ void invalidLengthWithOneByteLengthBorderCaseUsingDecode2() { encoded[1] = (byte)0xff; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -743,7 +744,7 @@ void invalidLengthWithTwoByteLength() { byte[] encoded = new byte[] { (byte)(183 + 2), 0x01, 0x00 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -756,7 +757,7 @@ void invalidLengthWithTwoByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 2), 0x01, 0x00 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -772,7 +773,7 @@ void invalidLengthWithTwoByteLengthBorderCase() { encoded[2] = (byte)0xff; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -788,7 +789,7 @@ void invalidLengthWithTwoByteLengthBorderCaseUsingDecode2() { encoded[2] = (byte)0xff; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -805,7 +806,7 @@ void invalidLengthWithThreeByteLengthBorderCase() { encoded[3] = (byte)255; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -822,7 +823,7 @@ void invalidLengthWithThreeByteLengthBorderCaseUsingDecode2() { encoded[3] = (byte)255; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -835,7 +836,7 @@ void invalidLengthWithFourByteLength() { byte[] encoded = new byte[] { (byte)(183 + 4), 0x01, 0x00, 0x00, 0x00 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -848,7 +849,7 @@ void invalidLengthWithFourByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 4), 0x01, 0x00, 0x00, 0x00 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -861,7 +862,7 @@ void invalidOneByteLength() { byte[] encoded = new byte[] { (byte)(183 + 1) }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -874,7 +875,7 @@ void invalidOneByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 1) }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -887,7 +888,7 @@ void invalidTwoByteLength() { byte[] encoded = new byte[] { (byte)(183 + 2), 0x01 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -900,7 +901,7 @@ void invalidTwoByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 2), 0x01 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -913,7 +914,7 @@ void invalidThreeByteLength() { byte[] encoded = new byte[] { (byte)(183 + 3), 0x01, 0x02 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -926,7 +927,7 @@ void invalidThreeByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 3), 0x01, 0x02 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -939,7 +940,7 @@ void invalidFourByteLength() { byte[] encoded = new byte[] { (byte)(183 + 4), 0x01, 0x02, 0x03 }; try { - RLP.decode2OneItem(encoded, 0); + RLP.decodeFirstElement(encoded, 0); Assertions.fail(); } catch (RLPException ex) { @@ -952,7 +953,7 @@ void invalidFourByteLengthUsingDecode2() { byte[] encoded = new byte[] { (byte)(183 + 4), 0x01, 0x02, 0x03 }; try { - RLP.decode2(encoded); + RLP.decodeListElements(encoded); Assertions.fail(); } catch (RLPException ex) { @@ -1002,13 +1003,13 @@ void lengthOfLengthLessThanZero() { @Test void encodeDecodeInteger() { for (int k = 0; k < 2048; k++) { - Assertions.assertEquals(k, RLP.decodeInt(RLP.encodeInt(k), 0)); + Assertions.assertEquals(k, RLPTestUtil.decodeInt(RLP.encodeInt(k), 0)); } } @Test void encodeDecodeInteger128() { - Assertions.assertEquals(128, RLP.decodeInt(RLP.encodeInt(128), 0)); + Assertions.assertEquals(128, RLPTestUtil.decodeInt(RLP.encodeInt(128), 0)); } @Test @@ -1016,8 +1017,8 @@ void encodeDecodeInteger128() { void encodeDecodeIntegerInList() { for (int k = 1; k < 2048; k++) { byte[] bytes = RLP.encodeList(RLP.encodeInt(k), new byte[0]); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); - Assertions.assertEquals(k, RLP.decodeInt(bytes2, 0)); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); + Assertions.assertEquals(k, RLPTestUtil.decodeInt(bytes2, 0)); } } @@ -1025,7 +1026,7 @@ void encodeDecodeIntegerInList() { void encodeDecodeIntegerInListUsingBigInteger() { for (int k = 1; k < 2048; k++) { byte[] bytes = RLP.encodeList(RLP.encodeInt(k), new byte[0]); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); Assertions.assertEquals(k, BigIntegers.fromUnsignedByteArray(bytes2).intValue()); } } @@ -1033,7 +1034,7 @@ void encodeDecodeIntegerInListUsingBigInteger() { @Test void encodeDecodeInteger0InList() { byte[] bytes = RLP.encodeList(RLP.encodeInt(0)); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); // known issue, the byte array is null Assertions.assertNull(bytes2); } @@ -1042,14 +1043,14 @@ void encodeDecodeInteger0InList() { @Disabled("Known issue, RLP.decodeInt should not be used in this case, to be reviewed") void encodeDecodeInteger128InList() { byte[] bytes = RLP.encodeList(RLP.encodeInt(128)); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); - Assertions.assertEquals(128, RLP.decodeInt(bytes2, 0)); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); + Assertions.assertEquals(128, RLPTestUtil.decodeInt(bytes2, 0)); } @Test void encodeDecodeInteger128InListUsingBigInteger() { byte[] bytes = RLP.encodeList(RLP.encodeInt(128)); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); Assertions.assertEquals(128, BigIntegers.fromUnsignedByteArray(bytes2).intValue()); } @@ -1057,14 +1058,14 @@ void encodeDecodeInteger128InListUsingBigInteger() { @Disabled("Known issue, RLP.decodeInt should not be used in this case, to be reviewed") void encodeDecodeInteger238InList() { byte[] bytes = RLP.encodeList(RLP.encodeInt(238)); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); - Assertions.assertEquals(238, RLP.decodeInt(bytes2, 0)); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); + Assertions.assertEquals(238, RLPTestUtil.decodeInt(bytes2, 0)); } @Test void encodeDecodeInteger238InListUsingBigInteger() { byte[] bytes = RLP.encodeList(RLP.encodeInt(238)); - byte[] bytes2 = ((RLPList)(RLP.decode2(bytes).get(0))).get(0).getRLPData(); + byte[] bytes2 = ((RLPList)(RLP.decodeListElements(bytes).get(0))).get(0).getRLPData(); Assertions.assertEquals(238, BigIntegers.fromUnsignedByteArray(bytes2).intValue()); } } diff --git a/rskj-core/src/test/java/org/ethereum/jsontestsuite/RLPTestCase.java b/rskj-core/src/test/java/org/ethereum/jsontestsuite/RLPTestCase.java index 3afc377166e..4313e7da451 100644 --- a/rskj-core/src/test/java/org/ethereum/jsontestsuite/RLPTestCase.java +++ b/rskj-core/src/test/java/org/ethereum/jsontestsuite/RLPTestCase.java @@ -21,12 +21,7 @@ import org.bouncycastle.util.encoders.Hex; -import org.ethereum.util.ByteUtil; -import org.ethereum.util.RLP; -import org.ethereum.util.RLPElement; -import org.ethereum.util.RLPList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.ethereum.util.*; import java.math.BigInteger; import java.nio.charset.StandardCharsets; @@ -35,8 +30,6 @@ import java.util.Vector; public class RLPTestCase { - private static Logger logger = LoggerFactory.getLogger("rlp"); - private Object in; private String out; @@ -77,7 +70,7 @@ public void doEncode() { public void doDecode() { String out = this.out.toLowerCase(); - ArrayList list = RLP.decode2(Hex.decode(out)); + ArrayList list = RLP.decodeListElements(Hex.decode(out)); checkRLPAgainstJson(list.get(0), in); } @@ -93,12 +86,12 @@ public byte[] buildRLP(Object in) { if (in instanceof String) { String s = in.toString(); if (s.contains("#")) { - return RLP.encode(new BigInteger(s.substring(1))); + return RLP.encodeBigInteger(new BigInteger(s.substring(1))); } } else if (in instanceof Integer) { return RLP.encodeInt(Integer.parseInt(in.toString())); } - return RLP.encode(in); + return RLPTestUtil.encode(in); } } diff --git a/rskj-core/src/test/java/org/ethereum/util/ByteUtilTest.java b/rskj-core/src/test/java/org/ethereum/util/ByteUtilTest.java index 9741a99ef15..9d83858fa53 100644 --- a/rskj-core/src/test/java/org/ethereum/util/ByteUtilTest.java +++ b/rskj-core/src/test/java/org/ethereum/util/ByteUtilTest.java @@ -97,13 +97,29 @@ void testByteArrayToLong() { @Test void testByteArrayToLongThrowsWhenOverflow() { - Assertions.assertThrows(IllegalArgumentException.class, () -> ByteUtil.byteArrayToLong(new byte[]{ + Assertions.assertThrows(RuntimeException.class, () -> ByteUtil.byteArrayToLong(new byte[]{ (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)123, } )); } + @Test + void testByteArrayToLongNegative() { + Assertions.assertThrows(RuntimeException.class, () -> ByteUtil.byteArrayToLong(new byte[]{ + (byte)-1, (byte)255, (byte)255, (byte)255, + (byte)255, (byte)255, (byte)255, (byte)255 + })); + } + + @Test + void testByteArrayToLongNegative2() { + Assertions.assertThrows(RuntimeException.class, () -> ByteUtil.byteArrayToLong(new byte[]{ + (byte) 0xa4, (byte) 0xbe, (byte) 0xfa, (byte) 0xd1, + (byte) 0x41, (byte) 0xd5, (byte) 0x1c, (byte) 0x4f + })); + } + @Test void testByteArrayToInt() { Assertions.assertEquals(0, ByteUtil.byteArrayToInt(null)); @@ -126,6 +142,28 @@ void testByteArrayToInt() { } + @Test + void byteArrayToIntTooLong() { + byte[] b = new byte[]{ 0x12, 0x12, 0x12, 0x12, 0x12 }; + + Assertions.assertThrows(RuntimeException.class, () -> ByteUtil.byteArrayToInt(b)); + + int prev = new BigInteger(1, b).intValue(); + + Assertions.assertNotEquals(BigInteger.valueOf(prev), new BigInteger(1, b)); + } + + @Test + void byteArrayToIntNegative() { + byte[] b = new byte[]{ (byte) 0xff, 0x12, 0x12, 0x12 }; + + Assertions.assertThrows(RuntimeException.class, () -> ByteUtil.byteArrayToInt(b)); + + int prev = new BigInteger(1, b).intValue(); + + Assertions.assertTrue(prev < 0); + } + @Test void testNumBytes() { String test1 = "0"; diff --git a/rskj-core/src/test/java/org/ethereum/util/RLPDump.java b/rskj-core/src/test/java/org/ethereum/util/RLPDump.java index 37d33afac8b..26c3154e7e7 100644 --- a/rskj-core/src/test/java/org/ethereum/util/RLPDump.java +++ b/rskj-core/src/test/java/org/ethereum/util/RLPDump.java @@ -34,13 +34,13 @@ public class RLPDump { void dumpTest() { System.out.println(ByteUtil.toHexString(new ECKey().getPubKey())); String hexRlp = "f872f870845609a1ba64c0b8660480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee8580"; - System.out.println(dump(RLP.decode2(Hex.decode(hexRlp)), 0)); + System.out.println(dump(RLP.decodeListElements(Hex.decode(hexRlp)), 0)); hexRlp = "f8d1f8cf845605846c3cc58479a94c49b8c0800b0b2d39d7c59778edb5166bfd0415c5e02417955ef4ef7f7d8c1dfc7f59a0141d97dd798bde6b972090390758b67457e93c2acb11ed4941d4443f87cedbc09c1b0476ca17f4f04da3d69cfb6470969f73d401ee7692293a00a2ff2d7f3fac87d43d85aed19c9e6ecbfe7e5f8268209477ffda58c7a481eec5c50abd313d10b6554e6e04a04fd93b9bf781d600f4ceb3060002ce1eddbbd51a9a902a970d9b41a9627141c0c52742b1179d83e17f1a273adf0a4a1d0346c68686a51428dd9a01"; - System.out.println(dump(RLP.decode2(Hex.decode(hexRlp)), 0)); + System.out.println(dump(RLP.decodeListElements(Hex.decode(hexRlp)), 0)); hexRlp = "dedd84560586f03cc58479a94c498e0c48656c6c6f205768697370657281bc"; - System.out.println(dump(RLP.decode2(Hex.decode(hexRlp)), 0)); + System.out.println(dump(RLP.decodeListElements(Hex.decode(hexRlp)), 0)); hexRlp = "dedd84560586f03cc58479a94c498e0c48656c6c6f205768697370657281bc"; - System.out.println(dump(RLP.decode2(Hex.decode(hexRlp)), 0)); + System.out.println(dump(RLP.decodeListElements(Hex.decode(hexRlp)), 0)); } public static String dump(ArrayList el, int indent) { diff --git a/rskj-core/src/test/java/org/ethereum/util/RLPTest.java b/rskj-core/src/test/java/org/ethereum/util/RLPTest.java index e35526741e3..a6859f83d90 100644 --- a/rskj-core/src/test/java/org/ethereum/util/RLPTest.java +++ b/rskj-core/src/test/java/org/ethereum/util/RLPTest.java @@ -22,11 +22,8 @@ import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.encoders.Hex; import org.ethereum.crypto.HashUtil; -import org.ethereum.db.ByteArrayWrapper; import org.ethereum.net.client.Capability; import org.ethereum.net.p2p.HelloMessage; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; @@ -34,19 +31,16 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.math.BigInteger; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.*; import static org.ethereum.util.ByteUtil.byteArrayToInt; -import static org.ethereum.util.ByteUtil.wrap; import static org.ethereum.util.RLP.*; import static org.ethereum.util.RlpTestData.*; import static org.junit.jupiter.api.Assertions.*; @SuppressWarnings("squid:S2699") class RLPTest { - +/* @Test void test1() throws UnknownHostException { @@ -133,7 +127,7 @@ void test3() throws UnknownHostException { nextIndex = getFirstListElement(payload, nextIndex); assertEquals(-1, nextIndex); } - +*/ @Test /** encode byte */ void test4() { @@ -187,55 +181,55 @@ void testEncodeInt() { byte[] expected = {(byte) 0x80}; byte[] data = encodeInt(0); assertArrayEquals(expected, data); - assertEquals(0, RLP.decodeInt(data, 0)); + assertEquals(0, RLPTestUtil.decodeInt(data, 0)); byte[] expected2 = {(byte) 0x78}; data = encodeInt(120); assertArrayEquals(expected2, data); - assertEquals(120, RLP.decodeInt(data, 0)); + assertEquals(120, RLPTestUtil.decodeInt(data, 0)); byte[] expected3 = {(byte) 0x7F}; data = encodeInt(127); assertArrayEquals(expected3, data); - assertEquals(127, RLP.decodeInt(data, 0)); + assertEquals(127, RLPTestUtil.decodeInt(data, 0)); - assertEquals(256, RLP.decodeInt(RLP.encodeInt(256), 0)); - assertEquals(255, RLP.decodeInt(RLP.encodeInt(255), 0)); - assertEquals(127, RLP.decodeInt(RLP.encodeInt(127), 0)); - assertEquals(128, RLP.decodeInt(RLP.encodeInt(128), 0)); + assertEquals(256, RLPTestUtil.decodeInt(RLP.encodeInt(256), 0)); + assertEquals(255, RLPTestUtil.decodeInt(RLP.encodeInt(255), 0)); + assertEquals(127, RLPTestUtil.decodeInt(RLP.encodeInt(127), 0)); + assertEquals(128, RLPTestUtil.decodeInt(RLP.encodeInt(128), 0)); data = RLP.encodeInt(1024); - assertEquals(1024, RLP.decodeInt(data, 0)); + assertEquals(1024, RLPTestUtil.decodeInt(data, 0)); byte[] expected4 = {(byte) 0x82, (byte) 0x76, (byte) 0x5F}; data = encodeInt(30303); assertArrayEquals(expected4, data); - assertEquals(30303, RLP.decodeInt(data, 0)); + assertEquals(30303, RLPTestUtil.decodeInt(data, 0)); byte[] expected5 = {(byte) 0x82, (byte) 0x4E, (byte) 0xEA}; data = encodeInt(20202); assertArrayEquals(expected5, data); - assertEquals(20202, RLP.decodeInt(data, 0)); + assertEquals(20202, RLPTestUtil.decodeInt(data, 0)); byte[] expected6 = {(byte) 0x83, 1, 0, 0}; data = encodeInt(65536); assertArrayEquals(expected6, data); - assertEquals(65536, RLP.decodeInt(data, 0)); + assertEquals(65536, RLPTestUtil.decodeInt(data, 0)); byte[] expected7 = {(byte) 0x84, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00}; data = encodeInt(Integer.MIN_VALUE); assertArrayEquals(expected7, data); - assertEquals(Integer.MIN_VALUE, RLP.decodeInt(data, 0)); + assertEquals(Integer.MIN_VALUE, RLPTestUtil.decodeInt(data, 0)); byte[] expected8 = {(byte) 0x84, (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; data = encodeInt(Integer.MAX_VALUE); assertArrayEquals(expected8, data); - assertEquals(Integer.MAX_VALUE, RLP.decodeInt(data, 0)); + assertEquals(Integer.MAX_VALUE, RLPTestUtil.decodeInt(data, 0)); byte[] expected9 = {(byte) 0x84, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; data = encodeInt(0xFFFFFFFF); assertArrayEquals(expected9, data); - assertEquals(0xFFFFFFFF, RLP.decodeInt(data, 0)); + assertEquals(0xFFFFFFFF, RLPTestUtil.decodeInt(data, 0)); } @@ -375,7 +369,7 @@ void test10() { assertEquals("f856a000000000000000000000000000000000000000000000000000000000000000001dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000", ByteUtil.toHexString(header)); } - +/* @Test void test11() { // 2240089100000070 @@ -387,14 +381,14 @@ void test11() { // TODO: assert lenght overflow while parsing list in RLP } - +*/ @Test void test12() { String tx = "F86E12F86B80881BC16D674EC8000094CD2A3D9F938E13CD947EC05ABC7FE734DF8DD8268609184E72A00064801BA0C52C114D4F5A3BA904A9B3036E5E118FE0DBB987FE3955DA20F2CD8F6C21AB9CA06BA4C2874299A55AD947DBC98A25EE895AABF6B625C26C435E84BFD70EDF2F69"; byte[] payload = Hex.decode(tx); - ArrayList rlpList = decode2(payload); + ArrayList rlpList = decodeListElements(payload); // TODO: add some asserts in place of just printing the rlpList } @@ -564,7 +558,7 @@ void test13() { "37 b1 38 d9 a5 f6 37 e6 72 ed b9 89 69 66 4c 4e 7f d1 c4 12 6d ef"; byte[] payload = Hex.decode(peers); - ArrayList rlpList = decode2(payload); + ArrayList rlpList = decodeListElements(payload); // TODO: add some asserts in place of just printing the rlpList } @@ -575,7 +569,7 @@ void test14() { String blocksMsg = "f91c1c13f90150f8c4a07df3d35d4df0a56fcf1d6344d5315cb56b9bf83bb96ad17c7b96a9cd14133c5da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a064afb6284fa35f26d7b2c5a26afaa5483072fbcb575221b34ce002a991b7a223a04a8abe6d802797dc80b497584f898c2d4fd561cc185828cfa1b92f6f38ee348e833fbfe484533f201c80a000000000000000000000000000000000000000000000000000cfccb5cfd4667cf887f8850380942d0aceee7e5ab874e22ccf8d1a649f59106d74e88609184e72a000822710a047617600000000000000000000000000000000000000000000000000000000001ca08691ab40e258de3c4f55c868c0c34e780e747158a1d96ca50186dfd3305abd78a042269c981d048a7b791aafc8f4e644232740c1a1cceb5b6d05568827a64c0664c0f8c8f8c4a0637c8a6cdb907fac6f752334ab79065bcc4e46cd4f4358dbc2a653544a20eb31a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a022a36c1a1e807e6afc22e6bb53a31111f56e7ee7dbb2ee571cefb152b514db4da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fcfd784533f1cf980a0000000000000000000000000000000000000000000000000e153d743fa040b18c0c0f8c8f8c4a07b2536237cbf114a043b0f9b27c76f84ac160ea5b87b53e42c7e76148964d450a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a07a3be0ee10ece4b03097bf74aabac628aa0fae617377d30ab1b97376ee31f41aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fbfe884533f1ce880a0000000000000000000000000000000000000000000000000f3deea84969b6e95c0c0f8c8f8c4a0d2ae3f5dd931926de428d99611980e7cdd7c1b838273e43fcad1b94da987cfb8a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a00b5b11fcf4ee12c6812f9d01cf0dff07c72cd7e02e48b35682f67c595407be14a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833faffd84533f1ce280a00000000000000000000000000000000000000000000000005fcbc97b2eb8ffb3c0c0f8c8f8c4a094d615d3cb4b306d20985028c78f0c8413d509a75e8bb84eda95f734debad2a0a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a04b8fd1b98cf5758bd303ff86393eb6d944c1058124bddce5d4e04b5395254d5ea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fbfec84533f1c7680a000000000000000000000000000000000000000000000000079fe3710116b32aac0c0f8c8f8c4a09424a07a0e4c05bb872906c40844a75b76f6517467b79c12fa9cc6d79ae09934a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a02dbe9ff9cbbc4c5a6ff26971f75b405891141f4e9bce3c2dc4200a305138e584a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fcfdf84533f1c3b80a0000000000000000000000000000000000000000000000000e0a6f8cf1d56031bc0c0f8c8f8c4a009aabea60cf7eaa9df4afdf4e1b5f3e684dab34fc9a9180a050085a4131ceedfa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0436da067f9683029e717edf92da46c3443e8c342974f47a563302a0678efe702a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdfd684533f1bfc80a00000000000000000000000000000000000000000000000005bc88c041662ffdac0c0f8c8f8c4a0f8b104093483b7c0182e1bba2ce3340d14469d3a3ee7646821223a676c680ac1a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0d482e71cde61190a33ca5aeb88b6b06276984e5a14253a98df232e8767167221a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefd184533f1bce80a00000000000000000000000000000000000000000000000004aeb31823f6a1950c0c0f8c8f8c4a0dd1f0aba02c2bb3b5a2b6cb1cc907ea70912bd46dc7a78577f2cae6cdbcbe5f3a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a058ab6df33d7cbeb6a735a7e4ccf4f28143e6a1742e45dda8f8cf48af43cb66c0a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fffd084533f1b9f80a0000000000000000000000000000000000000000000000000577042b0858b510bc0c0f8c8f8c4a0a287bb7da30f04344976abe569bd719f69c1cbea65533e5311ca5862e6eaa504a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a07e0537009c23cb1152caf84a52272431f74b6140866b15805622b7bcb607cd42a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934783400fd384533f1b6180a000000000000000000000000000000000000000000000000083d31378a0993e1ac0c0f8c8f8c4a063483cff8fbd489e6ce273326d8dc1d54a31c67f936ca84bf500e5419d3e9805a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a07737d08564819d51f8f834a6ee4278c23a0c2f29a3f485b21002c1f24f04d8e4a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fffd484533f1b5780a0000000000000000000000000000000000000000000000000bb586fe6de016e14c0c0f8c8f8c4a0975c8ed0c9197b7c018e02e1c95f315acf82b25e4a812140f4515e8bc827650ca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0ad51229abead59e93c80db5ba160f0939bc49dcee65d1c081f6eb040ff1f571fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefd984533f1b4e80a0000000000000000000000000000000000000000000000000548f02c6ceb26fd4c0c0f8c8f8c4a05844082e41f7c1f34485c7902afa0aa0979a6a849100fc553cd946f4a663929ca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a01bc726443437c4c062be18d052278e4ef735b8fe84387c8a4fc85fb70d5078e0a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fffd884533f1b1080a0000000000000000000000000000000000000000000000000cc1e528f54f22bdac0c0f8c8f8c4a0ba06ba81c93faaf98ea2d83cbdc0788958d938b29a9eb2a92ffbd4a628b3d52ea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a05053bfe1c0f1f0dd341c6df35e5a659989be041e8521027cc90f7605eb15fbb9a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefdd84533f1b0380a0000000000000000000000000000000000000000000000000bcf9df2fec615ecac0c0f8c8f8c4a083732d997db15109e90464c24b7c959a78881d827c55a0d668a66a2736be5d87a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a054f4012cba33a2b80b0dca9dd52f56b2c588133bd71700863f8cb95127176634a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fffdc84533f1a4680a00000000000000000000000000000000000000000000000006920a1dc9d915d0ec0c0f8c8f8c4a052e2fba761c2d0643170ef041c017391e781190fe715ae87cdae8eee1d45d95ba01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0ee2c82f77d7afd1f8dbe4f791df8477496c23e5504b9d66814172077f65f81f2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefe184533f1a3880a0000000000000000000000000000000000000000000000000ae86da9012398fc4c0c0f8c8f8c4a055703ba09544f386966b6d63bfc31033b761a4d1a6bb86b0cf49b4bb0526744ea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a01684c03a675b53786f0077d1651c3d169a009b13a6ee2b5047be6dbbe6d957ffa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdfea84533f1a2f80a00000000000000000000000000000000000000000000000003247320d0eb639dfc0c0f8c8f8c4a05109a79b33d81f4ee4814b550fb0002f03368d67570f6d4e6105fce2874d8b72a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0ae72e8c60a3dcfd53deecdb2790d18f0cc710f77cf2c1ed76e7da829bde619dca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fcff784533f1a1d80a000000000000000000000000000000000000000000000000040e0bc9bc9bcf295c0c0f8c8f8c4a03961e4bbba5c95fad3db0cffa3a16b9106f9ea3e8957993eab576b683c22f416a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0e9c6cf457bbe64d6bda67852a276cdbadb4f384a36d496e81801a496cfd9b7b5a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdfee84533f19df80a0000000000000000000000000000000000000000000000000dbb3fd6c816776d8c0c0f8c8f8c4a06b8265a357cb3ad744e19f04eb15377f660c10c900cc352d24f9b09073a363d6a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a07ba07e1bc6a20ffa44ae6080d30982b9faa148faf6b1ec15e32d89ac853ac291a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefe984533f198d80a00000000000000000000000000000000000000000000000005171325b6a2f17f1c0c0f8c8f8c4a0dcdc0347bb87ce72d49ac2e4e11f89069509b129a2536bf3d57c6bca30894032a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0ca24447aa0cedb4b561c7810461eef19b16a827c27352e5e01f914e9d7c78247a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fffe884533f194680a0000000000000000000000000000000000000000000000000da4714cfed9d8bbcc0c0f8c8f8c4a047f2dd6c15ea4082b3e11e5cf6b925b27e51d9de68051a093e52ef465cffbb8ca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a05a7206edddf50fcfeeaa97348a7112fc6edd0b5eacb44cf43d6a6c6b6609b459a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fefed84533f193e80a0000000000000000000000000000000000000000000000000ffafba4bf8dc944ec0c0f8c8f8c4a04d5ad6d860772145872f6660ecefcb0b0b2056e0aa3509a48bf4c175459e5121a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a00f4659d09bb2ced56e7fd9c4d3d90daca8b4f471307b7c4385fd34a41016b0b2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdff684533f192580a000000000000000000000000000000000000000000000000090620e5e59a39fe5c0c0f8c8f8c4a0c1725c58d1bf023af468e0088db3cf642ae097cf2c58c2ece2fc746980acc7e6a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0be19a182ea1584050deb0a79abdc11be896ce8d00a282bcfaf9ffcd65fd64d6aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833feff184533f189080a000000000000000000000000000000000000000000000000076f17f4199bccd12c0c0f8c8f8c4a0bd521a20a18ca6ca7115065065a1aa20067ee580fb11e2963d1e3a681e8302afa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a011be45633350e39475a1a07712ba72de4602d9eebf639ccd5422a389095ccaf1a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdffa84533f187b80a00000000000000000000000000000000000000000000000000c71b81c4a4cb82cc0c0f8c8f8c4a07c6d2d56e9c87f1553e4d06705af61a7c19a6046d2c39f8ed1417988783d3b1da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a012f5f0668063509e33a45a64eb6a072b2d84aa19f430f49f159be5008a786b2ea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fd00684533f186080a0000000000000000000000000000000000000000000000000b3f962892cfec9e6c0c0f8c8f8c4a07154f0f8ecc7f791d22eec06ec86d87a44b2704f015b3d2cff3571a3d01ae0f6a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a079536abf8e163cf8aa97f0d52866d04363902d591fd7c36aa35fc983d45fefd6a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fdffd84533f182f80a0000000000000000000000000000000000000000000000000736716e42499890fc0c0f8c8f8c4a0bf2fb1ee988ac4e17eae221a24176649774333fab25b6fc96c6081527fb6f121a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a041578daae7bcccd4976340aeb19e4132d2fe4193a0d92f87744c82bfe113502fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fd00984533f182b80a00000000000000000000000000000000000000000000000001c62fa76645942c6c0c0f8c8f8c4a07f84873e2679d40458b9dda9900478a78871044e08f6b47dad659b9b60ff8d48a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0597d3f4160770c0492333f90bad739dc05117d0e478a91f09573742e432904e8a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fe00184533f17f680a0000000000000000000000000000000000000000000000000e24d8b1140fb34d5c0c0f8c8f8c4a0fd77bd13a8cde1766537febe751a27a2a31310a04638a1afcd5e8ad3c5485453a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0473b2b248d91010ba9aec2696ffc93c11c415ed132832be0fd0578f184862e13a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833feffc84533f17ca80a0000000000000000000000000000000000000000000000000fb5b65bac3f0d947c0c0f8c8f8c4a0518916dfb79c390bd7bff75712174512c2f96bec42a3f573355507ad1588ce0ca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a08599d2ec9e95ec62f41a4975b655d8445d6767035f94eb235ed5ebea976fb9eaa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347833fe00484533f17b880a0000000000000000000000000000000000000000000000000bc27f4b8a201476bc0c0f90319f8c4a0ab6b9a5613970faa771b12d449b2e9bb925ab7a369f0a4b86b286e9d540099cfa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943854aaf203ba5f8d49b1ec221329c7aebcf050d3a0990dc3b5acbee04124361d958fe51acb582593613fc290683940a0769549d3eda09bfe4817d274ea3eb8672e9fe848c3885b53bbbd1d7c26e6039f90fb96b942b0833ff00084533f16b780a000000000000000000000000000000000000000000000000077377adff6c227dbf9024ff89d80809400000000000000000000000000000000000000008609184e72a000822710b3606956330c0d630000003359366000530a0d630000003359602060005301356000533557604060005301600054630000000c5884336069571ca07f6eb94576346488c6253197bde6a7e59ddc36f2773672c849402aa9c402c3c4a06d254e662bf7450dd8d835160cbb053463fed0b53f2cdd7f3ea8731919c8e8ccf9010501809400000000000000000000000000000000000000008609184e72a000822710b85336630000002e59606956330c0d63000000155933ff33560d63000000275960003356576000335700630000005358600035560d630000003a590033560d63000000485960003356573360003557600035335700b84a7f4e616d65526567000000000000000000000000000000000000000000000000003057307f4e616d655265670000000000000000000000000000000000000000000000000057336069571ba04af15a0ec494aeac5b243c8a2690833faa74c0f73db1f439d521c49c381513e9a05802e64939be5a1f9d4d614038fbd5479538c48795614ef9c551477ecbdb49d2f8a6028094ccdeac59d35627b7de09332e819d5159e7bb72508609184e72a000822710b84000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002d0aceee7e5ab874e22ccf8d1a649f59106d74e81ba0d05887574456c6de8f7a0d172342c2cbdd4cf7afe15d9dbb8b75b748ba6791c9a01e87172a861f6c37b5a9e3a5d0d7393152a7fbe41530e5bb8ac8f35433e5931bc0"; byte[] payload = Hex.decode(blocksMsg); - ArrayList rlpList = decode2(payload); + ArrayList rlpList = decodeListElements(payload); // TODO: add some asserts in place of just printing the rlpList } @@ -590,7 +584,7 @@ void test15() { "aa 8e 20 42 18 f4 d8 e7 32 82 5b d7 80 cf 94 ed 5c c3"; byte[] payload = Hex.decode(helloMsg); - ArrayList rlpList = decode2(payload); + ArrayList rlpList = decodeListElements(payload); // TODO: add some asserts in place of just printing the rlpList } @@ -600,19 +594,21 @@ void test15() { * * Using assertEquals(String, String) instead of assertArrayEquals to see the actual content when the test fails. */ + /* @Test void testEncodeNull() { - Assertions.assertThrows(RuntimeException.class, () -> encode(null)); + Assertions.assertThrows(RuntimeException.class, () -> RLPTestUtil.encode(null)); } + */ @Test void testEncodeEmptyString() { String test = ""; String expected = "80"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - String decodeResult = ByteUtil.toHexStringOrEmpty(decode2(encoderesult).get(0).getRLPData()); + String decodeResult = ByteUtil.toHexStringOrEmpty(decodeListElements(encoderesult).get(0).getRLPData()); assertEquals(test, decodeResult); } @@ -620,10 +616,10 @@ void testEncodeEmptyString() { void testEncodeShortString() { String test = "dog"; String expected = "83646f67"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); assertEquals(test, bytesToAscii(decodeResult)); } @@ -631,10 +627,10 @@ void testEncodeShortString() { void testEncodeSingleCharacter() { String test = "d"; String expected = "64"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); assertEquals(test, bytesToAscii(decodeResult)); } @@ -642,10 +638,10 @@ void testEncodeSingleCharacter() { void testEncodeLongString() { String test = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; // length = 56 String expected = "b8384c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c6974"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); assertEquals(test, bytesToAscii(decodeResult)); } @@ -653,10 +649,10 @@ void testEncodeLongString() { void testEncodeZero() { Integer test = 0; String expected = "80"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - String decodeResult = ByteUtil.toHexStringOrEmpty(decode2(encoderesult).get(0).getRLPData()); + String decodeResult = ByteUtil.toHexStringOrEmpty(decodeListElements(encoderesult).get(0).getRLPData()); assertEquals("", decodeResult); } @@ -664,10 +660,10 @@ void testEncodeZero() { void testEncodeSmallInteger() { Integer test = 15; String expected = "0f"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); int result = byteArrayToInt(decodeResult); assertEquals(test, Integer.valueOf(result)); } @@ -676,19 +672,19 @@ void testEncodeSmallInteger() { void testEncodeMediumInteger() { Integer test = 1000; String expected = "8203e8"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); int result = byteArrayToInt(decodeResult); assertEquals(test, Integer.valueOf(result)); test = 1024; expected = "820400"; - encoderesult = encode(test); + encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - decodeResult = decode2(encoderesult).get(0).getRLPData(); + decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); result = byteArrayToInt(decodeResult); assertEquals(test, Integer.valueOf(result)); } @@ -697,10 +693,10 @@ void testEncodeMediumInteger() { void testEncodeBigInteger() { BigInteger test = new BigInteger("100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 16); String expected = "a0100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - byte[] decodeResult = decode2(encoderesult).get(0).getRLPData(); + byte[] decodeResult = decodeListElements(encoderesult).get(0).getRLPData(); assertEquals(test, BigIntegers.fromUnsignedByteArray(decodeResult)); } @@ -708,10 +704,10 @@ void testEncodeBigInteger() { void TestEncodeEmptyList() { Object[] test = new Object[0]; String expected = "c0"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList)decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals(0, decodeResult.size()); } @@ -719,19 +715,19 @@ void TestEncodeEmptyList() { void testEncodeShortStringList() { String[] test = new String[]{"cat", "dog"}; String expected = "c88363617483646f67"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList) decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals("cat", bytesToAscii(decodeResult.get(0).getRLPData())); assertEquals("dog", bytesToAscii(decodeResult.get(1).getRLPData())); test = new String[]{"dog", "god", "cat"}; expected = "cc83646f6783676f6483636174"; - encoderesult = encode(test); + encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - decodeResult = (RLPList) decode2(encoderesult).get(0); + decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals("dog", bytesToAscii(decodeResult.get(0).getRLPData())); assertEquals("god", bytesToAscii(decodeResult.get(1).getRLPData())); assertEquals("cat", bytesToAscii(decodeResult.get(2).getRLPData())); @@ -743,10 +739,10 @@ void testEncodeLongStringList() { String element2 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; String[] test = new String[]{element1, element2}; String expected = "f83e83636174b8384c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c6974"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList) decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals(element1, bytesToAscii(decodeResult.get(0).getRLPData())); assertEquals(element2, bytesToAscii(decodeResult.get(1).getRLPData())); } @@ -760,10 +756,10 @@ void testEncodeLongStringList() { void testEncodeMultiList() { Object[] test = new Object[]{1, new Object[]{"cat"}, "dog", new Object[]{2}}; String expected = "cc01c48363617483646f67c102"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList) decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals(1, byteArrayToInt(decodeResult.get(0).getRLPData())); assertEquals("cat", bytesToAscii(((((RLPList)decodeResult.get(1)).get(0).getRLPData())))); assertEquals("dog", bytesToAscii(decodeResult.get(2).getRLPData())); @@ -771,10 +767,10 @@ void testEncodeMultiList() { test = new Object[]{new Object[]{"cat", "dog"}, new Object[]{1, 2}, new Object[]{}}; expected = "cdc88363617483646f67c20102c0"; - encoderesult = encode(test); + encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - decodeResult = (RLPList) decode2(encoderesult).get(0); + decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals("cat", bytesToAscii((((RLPList) decodeResult.get(0)).get(0).getRLPData()))); assertEquals("dog", bytesToAscii((((RLPList) decodeResult.get(0)).get(1).getRLPData()))); assertEquals(1, byteArrayToInt((((RLPList) decodeResult.get(1)).get(0).getRLPData()))); @@ -787,10 +783,10 @@ void testEncodeEmptyListOfList() { // list = [ [ [], [] ], [] ], Object[] test = new Object[]{new Object[]{new Object[]{}, new Object[]{}}, new Object[]{}}; String expected = "c4c2c0c0c0"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList) decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals(2, decodeResult.size()); assertEquals(2, ((RLPList) (decodeResult.get(0))).size()); assertEquals(0, ((RLPList) (decodeResult.get(1))).size()); @@ -804,10 +800,10 @@ void testEncodeRepOfTwoListOfList() { //list: [ [], [[]], [ [], [[]] ] ] Object[] test = new Object[]{new Object[]{}, new Object[]{new Object[]{}}, new Object[]{new Object[]{}, new Object[]{new Object[]{}}}}; String expected = "c7c0c1c0c3c0c1c0"; - byte[] encoderesult = encode(test); + byte[] encoderesult = RLPTestUtil.encode(test); assertEquals(expected, ByteUtil.toHexString(encoderesult)); - RLPList decodeResult = (RLPList) decode2(encoderesult).get(0); + RLPList decodeResult = (RLPList) decodeListElements(encoderesult).get(0); assertEquals(3, decodeResult.size()); assertEquals(0, ((RLPList) (decodeResult.get(0))).size()); assertEquals(1, ((RLPList) (decodeResult.get(1))).size()); @@ -821,22 +817,22 @@ void testEncodeRepOfTwoListOfList() { @Test void testRlpEncode() { - assertEquals(result01, ByteUtil.toHexString(encode(test01))); - assertEquals(result02, ByteUtil.toHexString(encode(test02))); - assertEquals(result03, ByteUtil.toHexString(encode(test03))); - assertEquals(result04, ByteUtil.toHexString(encode(test04))); - assertEquals(result05, ByteUtil.toHexString(encode(test05))); - assertEquals(result06, ByteUtil.toHexString(encode(test06))); - assertEquals(result07, ByteUtil.toHexString(encode(test07))); - assertEquals(result08, ByteUtil.toHexString(encode(test08))); - assertEquals(result09, ByteUtil.toHexString(encode(test09))); - assertEquals(result10, ByteUtil.toHexString(encode(test10))); - assertEquals(result11, ByteUtil.toHexString(encode(test11))); - assertEquals(result12, ByteUtil.toHexString(encode(test12))); - assertEquals(result13, ByteUtil.toHexString(encode(test13))); - assertEquals(result14, ByteUtil.toHexString(encode(test14))); - assertEquals(result15, ByteUtil.toHexString(encode(test15))); - assertEquals(result16, ByteUtil.toHexString(encode(test16))); + assertEquals(result01, ByteUtil.toHexString(RLPTestUtil.encode(test01))); + assertEquals(result02, ByteUtil.toHexString(RLPTestUtil.encode(test02))); + assertEquals(result03, ByteUtil.toHexString(RLPTestUtil.encode(test03))); + assertEquals(result04, ByteUtil.toHexString(RLPTestUtil.encode(test04))); + assertEquals(result05, ByteUtil.toHexString(RLPTestUtil.encode(test05))); + assertEquals(result06, ByteUtil.toHexString(RLPTestUtil.encode(test06))); + assertEquals(result07, ByteUtil.toHexString(RLPTestUtil.encode(test07))); + assertEquals(result08, ByteUtil.toHexString(RLPTestUtil.encode(test08))); + assertEquals(result09, ByteUtil.toHexString(RLPTestUtil.encode(test09))); + assertEquals(result10, ByteUtil.toHexString(RLPTestUtil.encode(test10))); + assertEquals(result11, ByteUtil.toHexString(RLPTestUtil.encode(test11))); + assertEquals(result12, ByteUtil.toHexString(RLPTestUtil.encode(test12))); + assertEquals(result13, ByteUtil.toHexString(RLPTestUtil.encode(test13))); + assertEquals(result14, ByteUtil.toHexString(RLPTestUtil.encode(test14))); + assertEquals(result15, ByteUtil.toHexString(RLPTestUtil.encode(test15))); + assertEquals(result16, ByteUtil.toHexString(RLPTestUtil.encode(test16))); } @Test @@ -847,65 +843,65 @@ void testRlpDecode() { byte[] decodedData; RLPList decodedList; - emptyString = ByteUtil.toHexStringOrEmpty(decode2(Hex.decode(result01)).get(0).getRLPData()); + emptyString = ByteUtil.toHexStringOrEmpty(decodeListElements(Hex.decode(result01)).get(0).getRLPData()); assertEquals("", emptyString); - emptyString = ByteUtil.toHexStringOrEmpty(decode2(Hex.decode(result02)).get(0).getRLPData()); + emptyString = ByteUtil.toHexStringOrEmpty(decodeListElements(Hex.decode(result02)).get(0).getRLPData()); assertEquals(test02, emptyString); - decodedData = decode2(Hex.decode(result03)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result03)).get(0).getRLPData(); assertEquals(test03, bytesToAscii(decodedData)); - decodedData = decode2(Hex.decode(result04)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result04)).get(0).getRLPData(); assertEquals(test04, bytesToAscii(decodedData)); - decodedData = decode2(Hex.decode(result05)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result05)).get(0).getRLPData(); assertEquals(test05, bytesToAscii(decodedData)); - decodedList = (RLPList) decode2(Hex.decode(result06)).get(0); + decodedList = (RLPList) decodeListElements(Hex.decode(result06)).get(0); assertEquals(test06[0], bytesToAscii(decodedList.get(0).getRLPData())); assertEquals(test06[1], bytesToAscii(decodedList.get(1).getRLPData())); - decodedList = (RLPList) decode2(Hex.decode(result07)).get(0); + decodedList = (RLPList) decodeListElements(Hex.decode(result07)).get(0); assertEquals(test07[0], bytesToAscii(decodedList.get(0).getRLPData())); assertEquals(test07[1], bytesToAscii(decodedList.get(1).getRLPData())); assertEquals(test07[2], bytesToAscii(decodedList.get(2).getRLPData())); // 1 - decodedData = decode2(Hex.decode(result08)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result08)).get(0).getRLPData(); assertEquals(test08, byteArrayToInt(decodedData)); // 10 - decodedData = decode2(Hex.decode(result09)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result09)).get(0).getRLPData(); assertEquals(test09, byteArrayToInt(decodedData)); // 100 - decodedData = decode2(Hex.decode(result10)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result10)).get(0).getRLPData(); assertEquals(test10, byteArrayToInt(decodedData)); // 1000 - decodedData = decode2(Hex.decode(result11)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result11)).get(0).getRLPData(); assertEquals(test11, byteArrayToInt(decodedData)); - decodedData = decode2(Hex.decode(result12)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result12)).get(0).getRLPData(); assertEquals(0, test12.compareTo(BigIntegers.fromUnsignedByteArray(decodedData))); - decodedData = decode2(Hex.decode(result13)).get(0).getRLPData(); + decodedData = decodeListElements(Hex.decode(result13)).get(0).getRLPData(); assertEquals(0, test13.compareTo(BigIntegers.fromUnsignedByteArray(decodedData))); // Need to test with different expected value, because decoding doesn't recognize types - RLPList testObject1 = (RLPList) decode2(Hex.decode(result14)).get(0); + RLPList testObject1 = (RLPList) decodeListElements(Hex.decode(result14)).get(0); assertArrayEquals((byte[]) expected14[0], testObject1.get(0).getRLPData()); assertArrayEquals((byte[]) expected14[1], testObject1.get(1).getRLPData()); assertEquals(0, ((RLPList) testObject1.get(2)).size()); - RLPList testObject2 = (RLPList) decode2(Hex.decode(result15)).get(0); + RLPList testObject2 = (RLPList) decodeListElements(Hex.decode(result15)).get(0); assertEquals(2, testObject2.size()); assertEquals(2, ((RLPList) testObject2.get(0)).size()); assertEquals(0, ((RLPList) testObject2.get(1)).size()); assertEquals(0, ((RLPList) ((RLPList) testObject2.get(0)).get(0)).size()); assertEquals(0, ((RLPList) ((RLPList) testObject2.get(0)).get(1)).size()); // Need to test with different expected value, because decoding doesn't recognize types - RLPList testObject3 = (RLPList)decode2(Hex.decode(result16)).get(0); + RLPList testObject3 = (RLPList) decodeListElements(Hex.decode(result16)).get(0); assertEquals(3, testObject3.size()); assertArrayEquals((byte[]) expected16[0], testObject3.get(0).getRLPData()); assertEquals(1, ((RLPList) testObject3.get(1)).size()); @@ -913,6 +909,7 @@ void testRlpDecode() { assertArrayEquals((byte[]) expected16[2], testObject3.get(2).getRLPData()); } + /** @Test void testEncodeLength() { @@ -952,6 +949,7 @@ void unsupportedLength() { } } + */ // Code from: http://stackoverflow.com/a/4785776/459349 private String bytesToAscii(byte[] b) { @@ -994,7 +992,7 @@ void encodeBigIntegerEdge_1() { byte[] encodedData = encodeBigInteger(integer); System.out.println(ByteUtil.toHexString(encodedData)); } - +/* @Test void testEncodeListHeader(){ @@ -1044,7 +1042,7 @@ void testEncodeSet_2(){ byte[] setEncoded = encodeSet(data); assertEquals("c0", ByteUtil.toHexString(setEncoded)); } - +*/ @Test void testEncodeInt_7f(){ String result = ByteUtil.toHexString(encodeInt(0x7f)); @@ -1062,7 +1060,7 @@ void testEncodeInt_80(){ @Test void testEncode_ED(){ - String result = ByteUtil.toHexString(encode(0xED)); + String result = ByteUtil.toHexString(RLPTestUtil.encode(0xED)); String expected = "81ed"; assertEquals(expected, result); } @@ -1107,15 +1105,15 @@ void testEncodeHelloMessageCap0(){ @Test void partialDataParseTest() { String hex = "000080c180000000000000000000000042699b1104e93abf0008be55f912c2ff"; - RLPList el = (RLPList) decode2OneItem(Hex.decode(hex), 3); + RLPList el = (RLPList) RLP.decodeFirstElement(Hex.decode(hex), 3); assertEquals(1, el.size()); } @Test void shortStringRightBoundTest() { String testString = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; //String of length 55 - byte[] rlpEncoded = encode(testString); - String res = new String(decode2(rlpEncoded).get(0).getRLPData()); + byte[] rlpEncoded = RLPTestUtil.encode(testString); + String res = new String(decodeListElements(rlpEncoded).get(0).getRLPData()); assertEquals(testString, res); } } diff --git a/rskj-core/src/test/java/org/ethereum/util/RLPTestUtil.java b/rskj-core/src/test/java/org/ethereum/util/RLPTestUtil.java new file mode 100644 index 00000000000..1cdfe50ef53 --- /dev/null +++ b/rskj-core/src/test/java/org/ethereum/util/RLPTestUtil.java @@ -0,0 +1,70 @@ +package org.ethereum.util; + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray; + +public class RLPTestUtil { + + private static final int OFFSET_SHORT_ITEM = 0x80; + private static final int OFFSET_LONG_ITEM = 0xb7; + + public static byte[] encode(Object input) { + Value val = new Value(input); + if (val.isList()) { + List l = new ArrayList<>(); + for (Object o:val.asList()) l.add(encode(o)); + return RLP.encodeList(l.toArray(new byte[][]{})); + } else { + return RLP.encodeElement(toBytes(input)); + } + } + + /* + * Utility function to convert Objects into byte arrays + */ + private static byte[] toBytes(Object input) { + if (input == null) return null; + if (input instanceof byte[]) { + return (byte[]) input; + } else if (input instanceof String) { + String inputString = (String) input; + return inputString.getBytes(StandardCharsets.UTF_8); + } else if (input instanceof Long) { + Long inputLong = (Long) input; + return (inputLong == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputLong)); + } else if (input instanceof Integer) { + Integer inputInt = (Integer) input; + return (inputInt == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputInt)); + } else if (input instanceof BigInteger) { + BigInteger inputBigInt = (BigInteger) input; + return (inputBigInt.equals(BigInteger.ZERO)) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(inputBigInt); + } else if (input instanceof Value) { + Value val = (Value) input; + return toBytes(val.asObj()); + } + throw new RuntimeException("Unsupported type: Only accepting String, Integer and BigInteger for now"); + } + + public static int decodeInt(byte[] data, int index) { + int b0 = data[index] & 0xFF; + + if (b0 < OFFSET_SHORT_ITEM) return data[index]; + + if (b0 < OFFSET_LONG_ITEM) { + int value = 0; + byte length = (byte) (data[index] - OFFSET_SHORT_ITEM); + byte pow = (byte) (length - 1); + for (int i = 1; i <= length; ++i) { + value += (data[index + i] & 0xFF) << (8 * pow); + pow--; + } + return value; + } + + throw new RuntimeException("wrong decode attempt"); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/util/Value.java b/rskj-core/src/test/java/org/ethereum/util/Value.java similarity index 76% rename from rskj-core/src/main/java/org/ethereum/util/Value.java rename to rskj-core/src/test/java/org/ethereum/util/Value.java index f917c449662..6a27cfc8edf 100644 --- a/rskj-core/src/main/java/org/ethereum/util/Value.java +++ b/rskj-core/src/test/java/org/ethereum/util/Value.java @@ -19,9 +19,6 @@ package org.ethereum.util; -import org.ethereum.crypto.HashUtil; - -import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; @@ -32,24 +29,6 @@ public class Value { private Object value; - private byte[] rlp; - private byte[] keccak256; - - public static Value fromRlpEncoded(byte[] data) { - if (data != null && data.length != 0) { - Value v = new Value(); - v.init(data); - return v; - } - return null; - } - - public Value() { - } - - public void init(byte[] rlp){ - this.rlp = rlp; - } public Value(Object obj) { if (obj == null) { @@ -76,28 +55,6 @@ public List asList() { return Arrays.asList(valueArray); } - public int asInt() { - if (isInt()) { - return (Integer) value; - } else if (isBytes()) { - return new BigInteger(1, asBytes()).intValue(); - } - return 0; - } - - public long asLong() { - if (isLong()) { - return (Long) value; - } else if (isBytes()) { - return new BigInteger(1, asBytes()).longValue(); - } - return 0; - } - - public BigInteger asBigInt() { - return (BigInteger) value; - } - public String asString() { if (isBytes()) { return new String((byte[]) value); @@ -116,19 +73,6 @@ public byte[] asBytes() { return ByteUtil.EMPTY_BYTE_ARRAY; } - public String getHex(){ - return ByteUtil.toHexString(this.encode()); - } - - public byte[] getData(){ - return this.encode(); - } - - - public int[] asSlice() { - return (int[]) value; - } - public Value get(int index) { if (isList()) { // Guard for OutOfBounds @@ -144,24 +88,6 @@ public Value get(int index) { return new Value(null); } - /* ***************** - * Utility - * *****************/ - - public byte[] encode() { - if (rlp == null) { - rlp = RLP.encode(value); - } - return rlp; - } - - public byte[] hash(){ - if (keccak256 == null) { - keccak256 = HashUtil.keccak256(encode()); - } - return keccak256; - } - /* ***************** * Checks * *****************/ @@ -182,10 +108,6 @@ public boolean isLong() { return value instanceof Long; } - public boolean isBigInt() { - return value instanceof BigInteger; - } - public boolean isBytes() { return value instanceof byte[]; } @@ -209,23 +131,6 @@ public boolean isReadableString() { return (double) readableChars / (double) data.length > 0.55; } - // it's only if the isBytes() = true - public boolean isHexString() { - - int hexChars = 0; - byte[] data = (byte[]) value; - - for (byte aData : data) { - - if ((aData >= 48 && aData <= 57) - || (aData >= 97 && aData <= 102)) { - ++hexChars; - } - } - - return (double) hexChars / (double) data.length > 0.9; - } - public boolean isHashCode() { return this.asBytes().length == 32; } diff --git a/rskj-core/src/test/java/org/ethereum/util/ValueTest.java b/rskj-core/src/test/java/org/ethereum/util/ValueTest.java index 634114b7e86..dda9e98356b 100644 --- a/rskj-core/src/test/java/org/ethereum/util/ValueTest.java +++ b/rskj-core/src/test/java/org/ethereum/util/ValueTest.java @@ -19,11 +19,9 @@ package org.ethereum.util; -import org.bouncycastle.util.encoders.Hex; +import jdk.nashorn.internal.ir.annotations.Ignore; import org.junit.jupiter.api.Test; -import java.math.BigInteger; -import java.util.Arrays; import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; @@ -47,40 +45,21 @@ void testCmp() { assertTrue(cmp(val3, val4), "Expected values to be equalBytes"); } - @Test - void testTypes() { - Value str = new Value("str"); - assertEquals("str", str.asString()); - - Value num = new Value(1); - assertEquals(1, num.asInt()); - - Value inter = new Value(new Object[]{1}); - Object[] interExp = new Object[]{1}; - assertTrue(cmp(new Value(inter.asObj()), new Value(interExp))); - - Value byt = new Value(new byte[]{1, 2, 3, 4}); - byte[] bytExp = new byte[]{1, 2, 3, 4}; - assertArrayEquals(byt.asBytes(), bytExp); - - Value bigInt = new Value(BigInteger.valueOf(10)); - BigInteger bigExp = BigInteger.valueOf(10); - assertEquals(bigInt.asBigInt(), bigExp); - } - - @Test + // this test make no sense, it is testing Hex.decode vs. ByteUtil.toHexString + // Value will set rlp = passed value, and return rlp in .encode() + @Ignore void longListRLPBug_1() { String testRlp = "f7808080d387206f72726563748a626574656c676575736580d387207870726573738a70726564696361626c658080808080808080808080"; - Value val = Value.fromRlpEncoded(Hex.decode(testRlp)); + // Value val = Value.fromRlpEncoded(Hex.decode(testRlp)); - assertEquals(testRlp, ByteUtil.toHexString(val.encode())); + // assertEquals(testRlp, ByteUtil.toHexString(val.encode())); } @Test void toString_Empty() { Value val = new Value(null); - String str = val.toString(); + String str = val.asString(); assertEquals("", str); } @@ -88,27 +67,11 @@ void toString_Empty() { @Test void toString_SameString() { Value val = new Value("hello"); - String str = val.toString(); + String str = val.asString(); assertEquals("hello", str); } - @Test - void toString_Array() { - Value val = new Value(new String[] {"hello", "world", "!"}); - String str = val.toString(); - - assertEquals(" ['hello', 'world', '!'] ", str); - } - - @Test - void toString_UnsupportedType() { - Value val = new Value('a'); - String str = val.toString(); - - assertEquals("Unexpected type", str); - } - @Test void isEmpty_Null() { Value val = new Value(null); diff --git a/rskj-core/src/test/java/org/ethereum/vm/GasCostTest.java b/rskj-core/src/test/java/org/ethereum/vm/GasCostTest.java index afd5d9228fa..920d5e1d9e2 100644 --- a/rskj-core/src/test/java/org/ethereum/vm/GasCostTest.java +++ b/rskj-core/src/test/java/org/ethereum/vm/GasCostTest.java @@ -53,14 +53,47 @@ void toGasOverflowsSlightly() { Assertions.assertEquals(Long.MAX_VALUE, GasCost.toGas(bytes)); } + @Test + void toGasInRange() throws GasCost.InvalidGasException { + Assertions.assertEquals(0L, GasCost.toGas(new byte[]{ + (byte)0 + })); + + + Assertions.assertEquals(0x000000000a141e28L, GasCost.toGas(new byte[]{ + (byte)10, (byte)20, (byte)30, (byte)40, + })); + + Assertions.assertEquals(0x7ffffffffffffffeL, GasCost.toGas(new byte[]{ + (byte)0x7f, (byte)255, (byte)255, (byte)255, + (byte)255, (byte)255, (byte)255, (byte)254, + })); + + Assertions.assertEquals(Long.MAX_VALUE, GasCost.toGas(new byte[]{ + (byte)0x7f, (byte)255, (byte)255, (byte)255, + (byte)255, (byte)255, (byte)255, (byte)255, + })); + } + + @Test + void toGasIsAlwaysPositive() throws GasCost.InvalidGasException { + Assertions.assertEquals(255, GasCost.toGas(new byte[]{ + (byte) (-1 & 0xff), + })); + + Assertions.assertEquals(Long.MAX_VALUE, GasCost.toGas(new byte[]{ + (byte) (-1 & 0xff), (byte)255, (byte)255, (byte)255, + (byte)255, (byte)255, (byte)255, (byte)255, + })); + } @Test - void toGasGivesNegativeValue() throws GasCost.InvalidGasException { + void toGasGivesMaxIntegerOnOverflow() throws GasCost.InvalidGasException { byte[] negativeBytes = new byte[]{ (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, }; - Assertions.assertThrows(GasCost.InvalidGasException.class, () -> GasCost.toGas(negativeBytes)); + Assertions.assertEquals(Long.MAX_VALUE, GasCost.toGas(negativeBytes)); } @Test diff --git a/rskj-core/src/test/resources/json/StateTests/stSystemOperationsTest.json b/rskj-core/src/test/resources/json/StateTests/stSystemOperationsTest.json index 51ab9780e31..a4a116f2f7c 100644 --- a/rskj-core/src/test/resources/json/StateTests/stSystemOperationsTest.json +++ b/rskj-core/src/test/resources/json/StateTests/stSystemOperationsTest.json @@ -7874,7 +7874,7 @@ "currentDifficulty" : "0x7fb7d889155ce8c6", "currentGasLimit" : "0x58272e28", "currentNumber" : "0xa7d8c0", - "currentTimestamp" : "0xa4befad141d51c4f", + "currentTimestamp" : "0x00", "previousHash" : "d30f77155de00f207ad60109897e790f73e9f3431be25717bf3651d91949f041" }, "logs" : [