Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7de4695

Browse files
jeffsmale90MicaiahReiddavidmurdoch
authored
fix: evm_revert fails in some cases (#4136)
Co-authored-by: Micaiah Reid <[email protected]> Co-authored-by: David Murdoch <[email protected]>
1 parent a3ae75f commit 7de4695

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

src/chains/ethereum/ethereum/src/forking/trie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ export class ForkTrie extends GanacheTrie {
8989
endBlockNumber: Quantity
9090
) {
9191
const db = this.metadataDB;
92-
const stream = db.createReadStream({
92+
const stream = db.createKeyStream({
9393
gte: lexico.encode([startBlockNumber.toBuffer()]),
9494
lt: lexico.encode([
9595
Quantity.from(endBlockNumber.toBigInt() + 1n).toBuffer()
9696
])
9797
});
9898
const batch = db.batch();
99-
for await (const [key] of stream) {
99+
for await (const key of stream) {
100100
batch.del(key);
101101
}
102102
await batch.write();

src/chains/ethereum/ethereum/tests/forking/forking.test.ts

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -825,31 +825,52 @@ describe("forking", function () {
825825
); //sanity check
826826
}
827827
}
828+
829+
/**
830+
* - Initializes `localProvider` as a fork of `remoteProvider`.
831+
* - Sets `value1` to `localInitialValue` (if it is not null).
832+
* - Creates a snapshot .
833+
* - Iterates `snapshotValues`, setting `value1` to each of those values.
834+
* - Reverts.
835+
* - Ensures that `value1` has reverted to either `localInitialValue` or
836+
* `remoteInitialValue` (if the `localInitialValue` was not provided).
837+
*/
828838
async function initializeSnapshotSetRevertThenTest(
829-
initialValue: number,
839+
remoteInitialValue: number,
840+
localInitialValue: number | null,
830841
snapshotValues: number[]
831842
) {
843+
const expectedValueAfterRevert =
844+
localInitialValue == null ? remoteInitialValue : localInitialValue;
845+
832846
const { localProvider } = await startLocalChain(PORT, {
833847
disableCache: true
834848
});
835849
const subId = await localProvider.send("eth_subscribe", ["newHeads"]);
836850

837-
// set value1 to {initialValue} (delete it)
838-
await set(localProvider, 1, initialValue);
839-
const message = await localProvider.once("message");
840-
const initialBlockNumber = parseInt(
841-
(message.data.result as any).number,
842-
16
843-
);
844-
assert.strictEqual(
845-
Quantity.from(
846-
await get(localProvider, "value1", initialBlockNumber)
847-
).toNumber(),
848-
initialValue
849-
); // sanity check
851+
if (localInitialValue !== null) {
852+
// set value1 to {initialValue} (note: if the value is `0` it actually deletes it from the state)
853+
await set(localProvider, 1, localInitialValue);
854+
await localProvider.once("message");
855+
856+
assert.strictEqual(
857+
+(await get(
858+
localProvider,
859+
"value1",
860+
await getBlockNumber(localProvider)
861+
)),
862+
localInitialValue
863+
); // sanity check
864+
}
865+
866+
const initialBlockNumber = await getBlockNumber(localProvider);
850867

851868
const snapId = await localProvider.send("evm_snapshot");
852-
await testPermutations(localProvider, initialValue, snapshotValues);
869+
await testPermutations(
870+
localProvider,
871+
expectedValueAfterRevert,
872+
snapshotValues
873+
);
853874
await localProvider.send("evm_revert", [snapId]);
854875

855876
assert.strictEqual(
@@ -858,21 +879,23 @@ describe("forking", function () {
858879
); // sanity check
859880

860881
assert.strictEqual(
861-
Quantity.from(
862-
await get(localProvider, "value1", initialBlockNumber)
863-
).toNumber(),
864-
initialValue,
882+
+(await get(localProvider, "value1", initialBlockNumber)),
883+
expectedValueAfterRevert,
865884
"value was not reverted to `initialValue` after evm_revert"
866885
);
867886

868887
// Finally, check all permutations outside of the snapshot/revert to
869888
// make sure deleted state was properly reverted
870-
await testPermutations(localProvider, initialValue, snapshotValues);
889+
await testPermutations(
890+
localProvider,
891+
expectedValueAfterRevert,
892+
snapshotValues
893+
);
871894

872895
await localProvider.send("eth_unsubscribe", [subId]);
873896
}
874897

875-
const initialValues = [0, 1];
898+
const initialValues = [null, 0, 1]; // null means to _not_ set an initial value
876899
// test all permutations of values: 0, 1, 2
877900
const permutations = [
878901
[0],
@@ -900,20 +923,30 @@ describe("forking", function () {
900923
const subId = await remoteProvider.send("eth_subscribe", [
901924
"newHeads"
902925
]);
903-
// set the remoteProvider's value1 initialValue to {remoteInitialValue}
904-
await set(remoteProvider, 1, remoteInitialValue);
905-
const message = await remoteProvider.once("message");
906-
await remoteProvider.send("eth_unsubscribe", [subId]);
907-
const blockNumber = parseInt(
908-
(message.data.result as any).number,
909-
16
926+
// set the remoteProvider's value1 initialValue to {remoteInitialValue} (only if not null)
927+
if (remoteInitialValue !== null) {
928+
await set(remoteProvider, 1, remoteInitialValue);
929+
await remoteProvider.once("message");
930+
await remoteProvider.send("eth_unsubscribe", [subId]);
931+
const blockNumber = await getBlockNumber(remoteProvider);
932+
assert.strictEqual(
933+
parseInt(
934+
await get(remoteProvider, "value1", blockNumber),
935+
16
936+
),
937+
remoteInitialValue
938+
); // sanity check to make sure our initial conditions are correct
939+
}
940+
941+
const blockNumber = await getBlockNumber(remoteProvider);
942+
const startValue = await get(
943+
remoteProvider,
944+
"value1",
945+
blockNumber
910946
);
911-
assert.strictEqual(
912-
parseInt(await get(remoteProvider, "value1", blockNumber), 16),
913-
remoteInitialValue
914-
); // sanity check to make sure our initial conditions are correct
915947

916948
await initializeSnapshotSetRevertThenTest(
949+
+startValue,
917950
initialValue,
918951
permutation
919952
);

0 commit comments

Comments
 (0)