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

feat: address account nonce discrepancies mono #11045

Merged
merged 27 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b52de40
chore: increment nonce flow
natanasow Jan 18, 2024
30de72a
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 18, 2024
1123f75
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 19, 2024
a55c6c8
chore: edint cotnract create transition logic
natanasow Jan 19, 2024
62de76c
chore: refactor evm tx processor
natanasow Jan 19, 2024
573a9a0
chore: add nonce suite
natanasow Jan 20, 2024
de2e563
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 20, 2024
5e407d3
chore: add nonce suite to concurrent suites
natanasow Jan 20, 2024
21896cd
chore: add more tests
natanasow Jan 21, 2024
578ff13
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 23, 2024
a09470a
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 25, 2024
c0a063c
chore: edit record file
natanasow Jan 25, 2024
3affb1f
chore: fix unit tests
natanasow Jan 26, 2024
446c731
chore: add v8 evm fn result test files
natanasow Jan 29, 2024
b386250
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 30, 2024
1f15bf6
fix: unit test mocks
Ivo-Yankov Jan 30, 2024
dd96689
Merge remote-tracking branch 'origin/9728-address-account-nonce-discr…
natanasow Jan 30, 2024
7f09a67
chore: fix unit tests
natanasow Jan 30, 2024
482d510
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 30, 2024
334aa6a
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Jan 31, 2024
b6fdb64
chore: revert
natanasow Jan 31, 2024
8504f7d
chore: fix missing signer nonce for ethereum contract creation record…
natanasow Feb 2, 2024
a15e96b
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Feb 5, 2024
8611fd2
chore: add ethereum contract creation tests
natanasow Feb 5, 2024
773113f
chore: add eth contract creation tests
natanasow Feb 5, 2024
11f6393
chore: add tests
natanasow Feb 6, 2024
dbb8b4e
Merge branch 'develop' into 9728-address-account-nonce-discrepancies-…
natanasow Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected TransactionProcessingResult execute(

super.setupFields(payload, contractCreation);

final var chargingResult = chargeForGas(
final var chargingResult = chargeForGasAndIncrementEthereumNonce(
gasCost,
upfrontCost,
value,
Expand Down Expand Up @@ -247,7 +247,7 @@ private void sendToCoinbase(
mutableCoinbase.incrementBalance(Wei.of(amount * gasPrice));
}

private ChargingResult chargeForGas(
private ChargingResult chargeForGasAndIncrementEthereumNonce(
final Wei gasCost,
final Wei upfrontCost,
final long value,
Expand Down Expand Up @@ -308,7 +308,14 @@ private ChargingResult chargeForGas(
final var senderCanAffordValue = senderAccount.getBalance().compareTo(Wei.of(value)) >= 0;
validateTrue(senderCanAffordValue, INSUFFICIENT_PAYER_BALANCE);
}

// increment sender's ethereum nonce right after all checks
// and before entering the evm for non-static calls
if (relayer != null) {
senderAccount.incrementNonce();
}
}

return new ChargingResult(senderAccount, mutableRelayer, allowanceCharged);
}

Expand All @@ -325,7 +332,7 @@ private void handleResourceLimitExceeded(
final Wei upfrontCost) {
// Consume all gas on resource exhaustion, using a clean updater
final var feesOnlyUpdater = (HederaWorldState.Updater) worldState.updater();
chargeForGas(
chargeForGasAndIncrementEthereumNonce(
gasCost,
upfrontCost,
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class TransactionProcessingResult extends HederaEvmTransactionProcessingR

private List<ContractID> createdContracts = Collections.emptyList();
private Map<ContractID, Long> contractNonces = new TreeMap<>();
private Long signerNonce;

public static TransactionProcessingResult failed(
final long gasUsed,
Expand Down Expand Up @@ -143,6 +144,14 @@ public void setActions(final List<SolidityAction> actions) {
this.actions = actions;
}

public Long getSignerNonce() {
return signerNonce;
}

public void setSignerNonce(Long signerNonce) {
this.signerNonce = signerNonce;
}

/**
* Converts the {@link TransactionProcessingResult} into {@link ContractFunctionResult} gRPC
* model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.hedera.node.app.service.mono.legacy.core.jproto.JKey;
import com.hedera.node.app.service.mono.legacy.core.jproto.JKeySerializer;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.AugmentedDataInputStream;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand All @@ -41,6 +42,16 @@ public static void writeNullableString(@Nullable final String msg, final Seriali
writeNullable(msg, out, (msgVal, outVal) -> outVal.writeNormalisedString(msgVal));
}

@Nullable
public static Long readNullableLong(final SerializableDataInputStream in) throws IOException {
return readNullable(in, AugmentedDataInputStream::readLong);
}

public static void writeNullableLong(@Nullable final Long num, final SerializableDataOutputStream out)
throws IOException {
writeNullable(num, out, (numVal, outVal) -> outVal.writeLong(numVal));
}

@Nullable
public static <T> T readNullable(final SerializableDataInputStream in, final IoReadingFunction<T> reader)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.hedera.node.app.service.mono.state.submerkle;

import static com.hedera.node.app.service.evm.accounts.HederaEvmContractAliases.isMirror;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.readNullableLong;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.readNullableSerializable;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.readNullableString;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.writeNullableLong;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.writeNullableSerializable;
import static com.hedera.node.app.service.mono.state.serdes.IoUtils.writeNullableString;
import static com.swirlds.common.utility.CommonUtils.hex;
Expand All @@ -27,6 +29,7 @@
import com.google.common.base.MoreObjects;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.Int64Value;
import com.hedera.node.app.hapi.utils.ethereum.EthTxData;
import com.hedera.node.app.service.mono.contracts.execution.TransactionProcessingResult;
import com.hederahashgraph.api.proto.java.ContractFunctionResult;
Expand Down Expand Up @@ -54,7 +57,8 @@
static final int RELEASE_0260_VERSION = 5;
static final int RELEASE_0290_VERSION = 6;
static final int RELEASE_0400_VERSION = 7;
static final int CURRENT_VERSION = RELEASE_0400_VERSION;
static final int RELEASE_0470_VERSION = 8;
static final int CURRENT_VERSION = RELEASE_0470_VERSION;

static final long RUNTIME_CONSTRUCTABLE_ID = 0x2055c5c03ff84eb4L;

Expand All @@ -79,6 +83,7 @@
private long amount;
private byte[] functionParameters = EMPTY;
private EntityId senderId;
private Long signerNonce;

public EvmFnResult() {
// RuntimeConstructable
Expand All @@ -105,12 +110,13 @@
recipient,
serializableIdsFrom(result.getCreatedContracts()),
serializableContractNoncesFrom(result.getContractNonces()),
evmAddress);
evmAddress,
result.getSignerNonce());
} else {
final var error = result.getRevertReason()
.map(Object::toString)
.orElse(result.getHaltReason().map(Object::toString).orElse(null));
return failure(result.getGasUsed(), error);
return failure(result.getGasUsed(), error, result.getSignerNonce());
}
}

Expand All @@ -127,7 +133,8 @@
final long gas,
final long amount,
final byte[] functionParameters,
final EntityId senderId) {
final EntityId senderId,
final Long signerNonce) {
this.contractId = contractId;
this.result = result;
this.error = error;
Expand All @@ -141,6 +148,7 @@
this.amount = amount;
this.functionParameters = functionParameters;
this.senderId = senderId;
this.signerNonce = signerNonce;
}

/* --- SelfSerializable --- */
Expand Down Expand Up @@ -197,6 +205,9 @@
if (version >= RELEASE_0400_VERSION) {
contractNonces = in.readSerializableList(MAX_CREATED_CONTRACT_NONCES, true, ContractNonceInfo::new);
}
if (version >= RELEASE_0470_VERSION) {
signerNonce = readNullableLong(in);
}
}

@Override
Expand All @@ -214,6 +225,7 @@
out.writeByteArray(functionParameters);
writeNullableSerializable(senderId, out);
out.writeSerializableList(contractNonces, true, true);
writeNullableLong(signerNonce, out);
}

/* --- Object --- */
Expand All @@ -238,7 +250,8 @@
&& gas == that.gas
&& amount == that.amount
&& Arrays.equals(functionParameters, that.functionParameters)
&& Objects.equals(senderId, that.senderId);
&& Objects.equals(senderId, that.senderId)
&& Objects.equals(signerNonce, that.signerNonce);
}

@Override
Expand All @@ -265,6 +278,7 @@
.add("amount", amount)
.add("functionParameters", hex(functionParameters))
.add("senderId", senderId)
.add("signerNonce", signerNonce)
.toString();
}

Expand Down Expand Up @@ -325,6 +339,10 @@
return senderId;
}

public Long getSignerNonce() {
return signerNonce;
}

public void setEvmAddress(final byte[] evmAddress) {
this.evmAddress = evmAddress;
}
Expand All @@ -345,6 +363,10 @@
this.senderId = senderId;
}

public void setSignerNonce(Long signerNonce) {
this.signerNonce = signerNonce;
}

public void setContractNonces(List<ContractNonceInfo> contractNonces) {
this.contractNonces = contractNonces;
}
Expand Down Expand Up @@ -391,6 +413,9 @@
if (senderId != null) {
grpc.setSenderId(senderId.toGrpcAccountId());
}
if (signerNonce != null) {
grpc.setSignerNonce(Int64Value.newBuilder().setValue(signerNonce).build());

Check warning on line 417 in hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/state/submerkle/EvmFnResult.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/state/submerkle/EvmFnResult.java#L417

Added line #L417 was not covered by tests
}
return grpc.build();
}

Expand Down Expand Up @@ -433,7 +458,8 @@
final Address recipient,
final List<EntityId> createdContractIds,
final List<ContractNonceInfo> contractNonces,
final byte[] evmAddress) {
final byte[] evmAddress,
final Long signerNonce) {
return new EvmFnResult(
isMirror(recipient.toArray()) ? EntityId.fromAddress(recipient) : null,
output.toArrayUnsafe(),
Expand All @@ -447,10 +473,11 @@
0L,
0L,
EMPTY,
null);
null,
signerNonce);
}

private static EvmFnResult failure(final long gasUsed, final String error) {
private static EvmFnResult failure(final long gasUsed, final String error, final Long signerNonce) {
return new EvmFnResult(
null,
EMPTY,
Expand All @@ -464,6 +491,7 @@
0L,
0L,
EMPTY,
null);
null,
signerNonce);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public class ExpirableTxnRecord implements FastCopyable, SerializableHashable {
static final int RELEASE_0280_VERSION = 11;
// Debatable -- 0.33/0.34?
static final int RELEASE_0340_VERSION = 12;
static final int CURRENT_VERSION = RELEASE_0340_VERSION;
static final int RELEASE_0470_VERSION = 13;
static final int CURRENT_VERSION = RELEASE_0470_VERSION;
static final long RUNTIME_CONSTRUCTABLE_ID = 0x8b9ede7ca8d8db93L;

static final int MAX_MEMO_BYTES = 32 * 1_024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public static void addContractCallResultToRecord(
traceabilityOn ? messageFrame.getRemainingGas() : 0L,
traceabilityOn ? messageFrame.getValue().toLong() : 0L,
traceabilityOn ? messageFrame.getInputData().toArrayUnsafe() : EvmFnResult.EMPTY,
EntityId.fromAddress(senderAddress));
EntityId.fromAddress(senderAddress),
null);
childRecord.setContractCallResult(evmFnResult);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ public void doStateTransitionOperation(
result = evmTxProcessor.execute(
sender, receiver.canonicalAddress(), op.getGas(), op.getAmount(), callData, txnCtx.consensusTime());
} else {
sender.incrementEthereumNonce();
accountStore.commitAccount(sender);

result = evmTxProcessor.executeEth(
sender,
receiver.canonicalAddress(),
Expand All @@ -171,6 +168,7 @@ public void doStateTransitionOperation(
offeredGasPrice,
accountStore.loadAccount(relayerId),
maxGasAllowanceInTinybars);
result.setSignerNonce(worldState.get(senderId.asEvmAddress()).getNonce());
}

/* --- Externalise result --- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ public void doStateTransitionOperation(
codeWithConstructorArgs,
consensusTime);
} else {
sender.incrementEthereumNonce();
accountStore.commitAccount(sender);

result = evmTxProcessor.executeEth(
tinker-michaelj marked this conversation as resolved.
Show resolved Hide resolved
sender,
newContractAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
} catch (InvalidTransactionException e) {
var result = TransactionProcessingResult.failed(
0, 0, 0, Optional.of(e.messageBytes()), Optional.empty(), Collections.emptyMap(), List.of());
result.setSignerNonce((Long) accountsLedger.get(callerNum.toGrpcAccountId(), ETHEREUM_NONCE));

Check warning on line 137 in hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/txns/ethereum/EthereumTransitionLogic.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/txns/ethereum/EthereumTransitionLogic.java#L137

Added line #L137 was not covered by tests
recordService.externaliseEvmCallTransaction(result);
throw e;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected EvmFnResult getExpectedObject(
// Always empty before 0.40
seeded.setContractNonces(Collections.emptyList());
}
if (version < EvmFnResult.RELEASE_0470_VERSION) {
seeded.setSignerNonce(null);
}
return seeded;
}

Expand Down
Loading
Loading