Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Allow use of large chain ids #1289

Merged
merged 29 commits into from
Apr 24, 2019
Merged

Allow use of large chain ids #1289

merged 29 commits into from
Apr 24, 2019

Conversation

jframe
Copy link
Contributor

@jframe jframe commented Apr 16, 2019

PR description

This updates chainId to be a BigInteger so that large values for a chainId can be used. Decided on using BigInteger as Geth currently using BigInt.

Fixed Issue(s)

@jframe jframe requested review from CjHare and rain-on April 16, 2019 04:58
@jframe jframe marked this pull request as ready for review April 16, 2019 04:59
@jframe jframe requested a review from ajsutton April 16, 2019 05:33
recId = v.subtract(REPLAY_UNPROTECTED_V_BASE).byteValue();
} else if (v.compareTo(REPLAY_PROTECTED_V_MIN) > 0) {
chainId = v.subtract(REPLAY_PROTECTED_V_BASE).divide(TWO);
recId = v.subtract(TWO.multiply(chainId).add(REPLAY_PROTECTED_V_BASE)).byteValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should use byteValueExact instead of byteValue here and line 96.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

} else if (v > REPLAY_PROTECTED_V_MIN) {
chainId = (v - REPLAY_PROTECTED_V_BASE) / 2;
recId = (byte) (v - (2 * chainId + REPLAY_PROTECTED_V_BASE));
BigInteger chainId = BigInteger.valueOf(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably shouldn't be using -1 as a sentinel value now that we're not using a primitive. Probably should be Optional<BigInteger> chainId = Optional.empty(); and that can flow through to Builder.chainId.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -426,9 +429,9 @@ public String toString() {

protected Address sender;

protected int chainId = -1;
protected BigInteger chainId = BigInteger.valueOf(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
protected BigInteger chainId = BigInteger.valueOf(-1);
protected Optional<BigInteger> chainId = Optional.empty();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


public Builder chainId(final int chainId) {
public Builder chainId(final BigInteger chainId) {
this.chainId = chainId;
Copy link
Contributor

Choose a reason for hiding this comment

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

Two possible approaches here, either public Builder chainId(final Optional<BigInteger> chainId) which is just directly assigned or public Builder chainId(final BigInteger chainId) which is wrapped with Optional.of - if you don't have a chain ID don't call chainId on the builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. used approach of defaulting to Optional.empty only calling chainId on builder if you have a chainId

final OptionalInt optionalChainId =
chainId > 0 ? OptionalInt.of(chainId) : OptionalInt.empty();
final Optional<BigInteger> optionalChainId =
chainId.signum() == 1 ? Optional.of(chainId) : Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

With the changes above you then don't need this and can just use chainId directly since it's already an optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -37,7 +38,7 @@
*/
public class MainnetTransactionValidator implements TransactionValidator {

public static final int NO_CHAIN_ID = -1;
public static final BigInteger NO_CHAIN_ID = BigInteger.valueOf(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't be using a sentinel value here - I suspect this constant can just go away and Optional.empty() used in its place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

this.gasCalculator = gasCalculator;
this.disallowSignatureMalleability = checkSignatureMalleability;
this.chainId = chainId > 0 ? OptionalInt.of(chainId) : OptionalInt.empty();
this.chainId = chainId.signum() == 1 ? Optional.of(chainId) : Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just pass in Optional<BigInteger> and avoid the sentinel check here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

throw new ParameterException(
this.commandLine,
"No networkId specified and chainId in "
+ "genesis file is too large to be used as a networkId");
Copy link
Contributor

Choose a reason for hiding this comment

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

ugh, this is quite unfortunate but probably better to bail out with an error than to default to something else silently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah it's not nice, but I think it's the best thing to do

@@ -22,4 +23,15 @@ public static OptionalLong getOptionalLong(final JsonObject jsonObject, final St
? OptionalLong.of(jsonObject.getLong(key))
: OptionalLong.empty();
}

public static BigInteger getBigInteger(final JsonObject jsonObject, final String key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional<BigInteger> for consistency with the other method in this class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

this.chainId = chainId;
return this;
}

public Builder chainId(final BigInteger chainId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

did you end up with 2 'chainId' setters on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just removed actually. don't think it's necessary afterall

@jframe
Copy link
Contributor Author

jframe commented Apr 18, 2019

Updated so make chainId optional in ProtocolSchedule as well. This allows having no chainId when using FixedDifficultyProtocolSchedule without the need to special -1 chainId value being used.

Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@CjHare CjHare left a comment

Choose a reason for hiding this comment

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

LGTM

@rain-on rain-on merged commit ac53e3d into PegaSysEng:master Apr 24, 2019
notlesh pushed a commit to notlesh/pantheon that referenced this pull request Apr 24, 2019
rain-on pushed a commit to rain-on/pantheon that referenced this pull request Apr 28, 2019
rain-on added a commit that referenced this pull request Apr 28, 2019
notlesh pushed a commit to notlesh/pantheon that referenced this pull request May 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants