Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor rlp #2175

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Refactor rlp #2175

wants to merge 12 commits into from

Conversation

ilanolkies
Copy link
Contributor

@ilanolkies ilanolkies commented Nov 14, 2023

Description

This pull request is a refactor and it adds minor bug fixing. It shouldn't change any existing functionality, modifying test cases was avoided.

Remove unused methods

Remove methods from RLP class that are not used like decode set or full traverse

Remove type detection

encode(Object input) was removed in favor of methods like encodeRskAddress that express the type of the element to be encoded. The overhead of detecting the type of the element, produced for example by creating Value class, is unnecessary since the developer needs to understand the protocol to know what element is decoded.

(See also https://github.com/rsksmart/rskj/security/code-scanning/2392 triggered in this PR)

Similarly, decode2 is removed in favor of decodeList. Note that the overhead produce for detecting if the element was a string or a list is visible calls like (RLPList) RLP.decode2(rlpEncoded).get(0);, two lists are created for decoding a single list.

The methods were moved to test utilities to prevent modifying the tests (RLPTestUtil)

Replace for constants

Constants hardcoded in the code are replaced for the respective static final constant. Also, duplicated constants were removed.

Remove decodeInt and fix byteArrayToInt

The method decodeInt is bugged. The data might not fit in the int. The method is removed in favor of ByteUtil.byteArrayToInt in FrameCoded that is used in other places.

The method decodeInt had the same bug, but is fixed by comparing to Integer.MAX_VALUE with intermediate BigInteger conversion. The conversion was already in the code so it doesn’t add significant overhead.

Fix toGas and byteArrayToLong

It follows from the considerations mentioned above. byteArrayToLong now favors only naturals big endian. toGas will return Long.MAX_VALUE on overflowing values, being gas limit natural number.

Motivation and Context

Researching on RLP now 👍

How Has This Been Tested?

Almost no new implementation was written, existent unit tests stand as source of trust. Test was added for fixed bug.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:
    fed:refactor-rlp

@ilanolkies ilanolkies requested review from a team as code owners November 14, 2023 16:03
rskj-core/src/test/java/org/ethereum/util/RLPTest.java Dismissed Show dismissed Hide dismissed
rskj-core/src/test/java/org/ethereum/util/RLPTest.java Dismissed Show dismissed Hide dismissed
rskj-core/src/test/java/org/ethereum/util/RLPTest.java Dismissed Show dismissed Hide dismissed
rskj-core/src/test/java/org/ethereum/util/RLPTest.java Dismissed Show dismissed Hide dismissed
rskj-core/src/test/java/org/ethereum/util/ValueTest.java Dismissed Show dismissed Hide dismissed
rskj-core/src/test/java/org/ethereum/util/RLPTestUtil.java Dismissed Show dismissed Hide dismissed
Copy link

sonarcloud bot commented Nov 22, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

96.3% 96.3% Coverage
0.0% 0.0% Duplication

throw new RuntimeException("Unsupported type: Only accepting String, Integer and BigInteger for now");
}

private static final int OFFSET_SHORT_ITEM = 0x80;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a NIT, I suggest you move these constants declaration to the beginning of the class.

rmoreliovlabs
rmoreliovlabs previously approved these changes Jan 11, 2024
Copy link
Contributor

@rmoreliovlabs rmoreliovlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Vovchyk
Copy link
Contributor

Vovchyk commented Jan 15, 2024

pipeline:run

@Vovchyk
Copy link
Contributor

Vovchyk commented Jan 15, 2024

@ilanolkies could you please rebase your PR? The pipeline fails and that could be due to a bit outdated version of master the pr is based on..

@Vovchyk
Copy link
Contributor

Vovchyk commented Jan 18, 2024

pipeline:run

@Vovchyk
Copy link
Contributor

Vovchyk commented Jan 18, 2024

@ilanolkies, seems the pipeline fails with on the powpeg jar building phase:

> Task :rskj:rskj-core:generateResources
> Task :rskj:rskj-core:jar

> Task :compileJava FAILED
/home/jenkins/workspace/Pipeline/powpeg-node/src/main/java/co/rsk/federate/Proof.java:31: error: cannot find symbol
        RLPList rlpList = (RLPList) RLP.decode2(rlpData).get(0);
                                       ^
  symbol:   method decode2(byte[])
  location: class org.ethereum.util.RLP
/home/jenkins/workspace/Pipeline/powpeg-node/src/main/java/co/rsk/federate/Proof.java:71: error: cannot find symbol
        RLPList rlpList = (RLPList)RLP.decode2(rlpData).get(0);

it looks like the powpeg code should be refactored as well according to your changes

@nagarev
Copy link
Contributor

nagarev commented Jan 24, 2024

pipeline:run

Copy link

sonarcloud bot commented Jan 24, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

3 New issues
0 Security Hotspots
95.7% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

@nagarev
Copy link
Contributor

nagarev commented Jan 25, 2024

pipeline:run

Copy link
Contributor

@nagarev nagarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants