-
Notifications
You must be signed in to change notification settings - Fork 280
Fix: Allow appending to struct at the end of storage layout #1136 #1147
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
28e7838
Enhance StorageLayoutComparator to safely allow appending fields to s…
ginzahatemi f1ac5fd
Add unit tests for struct appending in storage layout validation. Inc…
ginzahatemi 27c92d8
Remove outdated comments regarding struct field appending in StorageL…
ginzahatemi 118adb3
Refactor comments in StorageLayoutComparator for clarity and consiste…
ginzahatemi f0bf093
Add StructAppend contract with versioning for struct modifications. I…
ginzahatemi a59deba
Fix testcases
ericglau 30874ce
Enhance StorageLayoutComparator to safely allow appending fields to s…
ginzahatemi 59fcbf7
Add unit tests for struct appending in storage layout validation. Inc…
ginzahatemi 00b0d43
Remove outdated comments regarding struct field appending in StorageL…
ginzahatemi cf214a8
Refactor comments in StorageLayoutComparator for clarity and consiste…
ginzahatemi 7fb9ba9
Add StructAppend contract with versioning for struct modifications. I…
ginzahatemi 59f7477
Merge branch 'OpenZeppelin:master' into struct-append-1136
ginzahatemi a53fcf5
Merge branch 'struct-append-1136' of https://github.com/ginzahatemi/o…
ginzahatemi d7a3aa9
Refactor StorageLayoutComparator to improve struct append validation …
ginzahatemi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract StructAppendV1 { | ||
struct MyStruct { | ||
uint256 x; | ||
uint256 y; | ||
} | ||
|
||
MyStruct public myStruct; | ||
} | ||
|
||
contract StructAppendV2_Ok { | ||
struct MyStruct { | ||
uint256 x; | ||
uint256 y; | ||
uint256 z; // appended field | ||
} | ||
|
||
MyStruct public myStruct; | ||
} | ||
|
||
contract StructAppendBeforeStorageEndV1 { | ||
struct MyStruct { | ||
uint256 x; | ||
uint256 y; | ||
} | ||
|
||
MyStruct public myStruct; | ||
uint256 public zz; | ||
} | ||
|
||
contract StructAppendBeforeStorageEndV2_Bad { | ||
struct MyStruct { | ||
uint256 x; | ||
uint256 y; | ||
uint256 z; // appended field | ||
} | ||
|
||
MyStruct public myStruct; | ||
uint256 public zz; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,7 +176,32 @@ export class StorageLayoutComparator { | |
// Gap shrink or gap replacement that finishes on the same slot is safe | ||
return false; | ||
} else if (o.kind === 'append' && allowAppend) { | ||
// Allow append operation if the allowAppend flag is set | ||
// This applies to both direct storage appends and appends within structs | ||
return false; | ||
} else if (o.kind === 'typechange' && o.change?.kind === 'struct members' && o.change.allowAppend) { | ||
const structOps = o.change.ops; | ||
const hasNonAppendOps = structOps.some(op => op.kind !== 'append'); | ||
if (hasNonAppendOps) { | ||
return true; | ||
} | ||
|
||
const structEnd = storageFieldEnd(o.original); | ||
if (structEnd === undefined) { | ||
return true; | ||
} | ||
|
||
const hasFieldsAfter = original | ||
.filter(field => !isGap(field)) | ||
.some(field => { | ||
const fieldStart = storageFieldBegin(field); | ||
if (field === o.original || fieldStart === undefined) { | ||
return false; | ||
} | ||
return fieldStart > structEnd; | ||
}); | ||
|
||
return hasFieldsAfter; | ||
Comment on lines
+189
to
+204
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear what this section of code achieves. There should be testcases and changing/removing any of this code should cause those testcases to fail. |
||
} | ||
return true; | ||
}); | ||
|
@@ -324,6 +349,7 @@ export class StorageLayoutComparator { | |
} | ||
} | ||
assert(isStructMembers(originalMembers) && isStructMembers(updatedMembers)); | ||
|
||
const ops = this.layoutLevenshtein(originalMembers, updatedMembers, { allowAppend }); | ||
if (ops.length > 0) { | ||
return { kind: 'struct members', ops, original, updated, allowAppend }; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import _test, { TestFn } from 'ava'; | ||
import { ContractDefinition } from 'solidity-ast'; | ||
import { findAll, astDereferencer } from 'solidity-ast/utils'; | ||
import { artifacts } from 'hardhat'; | ||
|
||
import { SolcOutput } from '../solc-api'; | ||
import { getStorageUpgradeErrors } from '../storage'; | ||
import { StorageLayout } from './layout'; | ||
import { extractStorageLayout } from './extract'; | ||
|
||
interface Context { | ||
extractStorageLayout: (contract: string) => StorageLayout; | ||
} | ||
|
||
const test = _test as TestFn<Context>; | ||
|
||
test.before(async t => { | ||
const buildInfo = await artifacts.getBuildInfo('contracts/test/StructAppend.sol:StructAppendV1'); | ||
if (buildInfo === undefined) { | ||
throw new Error('Build info not found'); | ||
} | ||
const solcOutput: SolcOutput = buildInfo.output; | ||
const contracts: Record<string, ContractDefinition> = {}; | ||
const storageLayouts: Record<string, StorageLayout> = {}; | ||
for (const def of findAll('ContractDefinition', solcOutput.sources['contracts/test/StructAppend.sol'].ast)) { | ||
contracts[def.name] = def; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
storageLayouts[def.name] = solcOutput.contracts['contracts/test/StructAppend.sol'][def.name].storageLayout!; | ||
} | ||
const deref = astDereferencer(solcOutput); | ||
const dummyDecodeSrc = () => 'file.sol:1'; | ||
t.context.extractStorageLayout = name => | ||
extractStorageLayout(contracts[name], dummyDecodeSrc, deref, storageLayouts[name]); | ||
}); | ||
|
||
test('struct append at end of storage - ok', t => { | ||
const v1 = t.context.extractStorageLayout('StructAppendV1'); | ||
const v2 = t.context.extractStorageLayout('StructAppendV2_Ok'); | ||
const comparison = getStorageUpgradeErrors(v1, v2); | ||
t.deepEqual(comparison, [], JSON.stringify(comparison, null, 2)); | ||
}); | ||
|
||
test('struct append not at end of storage - bad', t => { | ||
const v1 = t.context.extractStorageLayout('StructAppendBeforeStorageEndV1'); | ||
const v2 = t.context.extractStorageLayout('StructAppendBeforeStorageEndV2_Bad'); | ||
const comparison = getStorageUpgradeErrors(v1, v2); | ||
t.like(comparison, { | ||
length: 2, | ||
0: { | ||
kind: 'typechange', | ||
change: { | ||
kind: 'struct members', | ||
ops: { | ||
length: 1, | ||
0: { kind: 'append' }, | ||
}, | ||
}, | ||
original: { label: 'myStruct' }, | ||
updated: { label: 'myStruct' }, | ||
}, | ||
1: { | ||
kind: 'layoutchange', | ||
change: { | ||
slot: { | ||
from: '2', | ||
to: '3', | ||
}, | ||
}, | ||
original: { label: 'zz' }, | ||
updated: { label: 'zz' }, | ||
}, | ||
}); | ||
}); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing test cases for namespaced structs as described in #1136
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added test cases for namespaced structs as requested in the latest commit. Let me know if there are any more suggestions