-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit test with namespaced layout for conflicts
- Loading branch information
Showing
11 changed files
with
226 additions
and
14 deletions.
There are no files selected for viewing
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
packages/core/contracts/test/NamespacedConflictsWithFunctions.sol
This file contains 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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
contract DuplicateNamespace { | ||
function foo() public pure returns (uint256) { | ||
return 0; | ||
} | ||
|
||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting1 { | ||
uint256 b; | ||
} | ||
|
||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting2 { | ||
uint256 c; | ||
} | ||
|
||
function foo2() public pure returns (uint256) { | ||
return 0; | ||
} | ||
} | ||
|
||
contract ConflictsWithParent is DuplicateNamespace { | ||
function foo3() public pure returns (uint256) { | ||
return 0; | ||
} | ||
|
||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting { | ||
uint256 a; | ||
} | ||
|
||
function foo4() public pure returns (uint256) { | ||
return 0; | ||
} | ||
} | ||
|
||
contract ConflictsInBothParents is DuplicateNamespace, ConflictsWithParent { | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/core/contracts/test/NamespacedConflictsWithVariables.sol
This file contains 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,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
contract DuplicateNamespace { | ||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting1 { | ||
uint256 b; | ||
} Conflicting1 $Conflicting1; | ||
|
||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting2 { | ||
uint256 c; | ||
} Conflicting2 $Conflicting2; | ||
} | ||
|
||
contract ConflictsWithParent is DuplicateNamespace { | ||
/// @custom:storage-location erc7201:conflicting | ||
struct Conflicting { | ||
uint256 a; | ||
} Conflicting $Conflicting; | ||
} | ||
|
||
contract ConflictsInBothParents is DuplicateNamespace, ConflictsWithParent { | ||
} |
This file contains 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
95 changes: 95 additions & 0 deletions
95
packages/core/src/storage-namespaced-conflicts-with-layout.test.ts
This file contains 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,95 @@ | ||
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 { StorageLayout } from './storage/layout'; | ||
import { extractStorageLayout } from './storage/extract'; | ||
import { solcInputOutputDecoder } from './src-decoder'; | ||
|
||
interface Context { | ||
extractStorageLayout: (contract: string) => ReturnType<typeof extractStorageLayout>; | ||
} | ||
|
||
const test = _test as TestFn<Context>; | ||
|
||
test.before(async t => { | ||
const origBuildInfo = await artifacts.getBuildInfo( | ||
'contracts/test/NamespacedConflictsWithFunctions.sol:DuplicateNamespace', | ||
); | ||
const namespacedBuildInfo = await artifacts.getBuildInfo( | ||
'contracts/test/NamespacedConflictsWithVariables.sol:DuplicateNamespace', | ||
); | ||
|
||
if (origBuildInfo === undefined || namespacedBuildInfo === undefined) { | ||
throw new Error('Build info not found'); | ||
} | ||
|
||
const origSolcOutput: SolcOutput = origBuildInfo.output; | ||
const origContracts: Record<string, ContractDefinition> = {}; | ||
const origStorageLayouts: Record<string, StorageLayout> = {}; | ||
|
||
const namespacedSolcOutput: SolcOutput = namespacedBuildInfo.output; | ||
const namespacedContracts: Record<string, ContractDefinition> = {}; | ||
const namespacedStorageLayouts: Record<string, StorageLayout> = {}; | ||
|
||
const origContractDefs = []; | ||
for (const def of findAll( | ||
'ContractDefinition', | ||
origSolcOutput.sources['contracts/test/NamespacedConflictsWithFunctions.sol'].ast, | ||
)) { | ||
origContractDefs.push(def); | ||
} | ||
const namespacedContractDefs = []; | ||
for (const def of findAll( | ||
'ContractDefinition', | ||
namespacedSolcOutput.sources['contracts/test/NamespacedConflictsWithVariables.sol'].ast, | ||
)) { | ||
namespacedContractDefs.push(def); | ||
} | ||
|
||
for (let i = 0; i < origContractDefs.length; i++) { | ||
const origContractDef = origContractDefs[i]; | ||
const namespacedContractDef = namespacedContractDefs[i]; | ||
|
||
origContracts[origContractDef.name] = origContractDef; | ||
namespacedContracts[namespacedContractDef.name] = namespacedContractDef; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
origStorageLayouts[origContractDef.name] = | ||
origSolcOutput.contracts['contracts/test/NamespacedConflictsWithFunctions.sol'][ | ||
origContractDef.name | ||
].storageLayout!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
namespacedStorageLayouts[namespacedContractDef.name] = | ||
namespacedSolcOutput.contracts['contracts/test/NamespacedConflictsWithVariables.sol'][ | ||
namespacedContractDef.name | ||
].storageLayout!; | ||
} | ||
const origDeref = astDereferencer(origSolcOutput); | ||
const namespacedDeref = astDereferencer(namespacedSolcOutput); | ||
|
||
const decodeSrc = solcInputOutputDecoder(origBuildInfo.input, origSolcOutput); | ||
t.context.extractStorageLayout = name => | ||
extractStorageLayout(origContracts[name], decodeSrc, origDeref, origStorageLayouts[name], { | ||
deref: namespacedDeref, | ||
contractDef: namespacedContracts[name], | ||
storageLayout: origStorageLayouts[name], | ||
}); | ||
}); | ||
|
||
test('duplicate namespace', t => { | ||
const error = t.throws(() => t.context.extractStorageLayout('DuplicateNamespace')); | ||
t.snapshot(error?.message); | ||
}); | ||
|
||
test('conflicts with parent', t => { | ||
const error = t.throws(() => t.context.extractStorageLayout('ConflictsWithParent')); | ||
t.snapshot(error?.message); | ||
}); | ||
|
||
test('conflicts in both parents', t => { | ||
const error = t.throws(() => t.context.extractStorageLayout('ConflictsInBothParents')); | ||
t.snapshot(error?.message); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/core/src/storage-namespaced-conflicts-with-layout.test.ts.md
This file contains 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,43 @@ | ||
# Snapshot report for `src/storage-namespaced-conflicts-with-layout.test.ts` | ||
|
||
The actual snapshot is saved in `storage-namespaced-conflicts-with-layout.test.ts.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## duplicate namespace | ||
|
||
> Snapshot 1 | ||
`Namespace erc7201:conflicting is defined multiple times for contract DuplicateNamespace␊ | ||
␊ | ||
The namespace erc7201:conflicting was found in structs at the following locations:␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:10␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:15␊ | ||
␊ | ||
Use a unique namespace id for each struct annotated with '@custom:storage-location erc7201:<NAMESPACE_ID>' in your contract and its inherited contracts.` | ||
|
||
## conflicts with parent | ||
|
||
> Snapshot 1 | ||
`Namespace erc7201:conflicting is defined multiple times for contract ConflictsWithParent␊ | ||
␊ | ||
The namespace erc7201:conflicting was found in structs at the following locations:␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:30␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:10␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:15␊ | ||
␊ | ||
Use a unique namespace id for each struct annotated with '@custom:storage-location erc7201:<NAMESPACE_ID>' in your contract and its inherited contracts.` | ||
|
||
## conflicts in both parents | ||
|
||
> Snapshot 1 | ||
`Namespace erc7201:conflicting is defined multiple times for contract ConflictsInBothParents␊ | ||
␊ | ||
The namespace erc7201:conflicting was found in structs at the following locations:␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:30␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:10␊ | ||
- contracts/test/NamespacedConflictsWithFunctions.sol:15␊ | ||
␊ | ||
Use a unique namespace id for each struct annotated with '@custom:storage-location erc7201:<NAMESPACE_ID>' in your contract and its inherited contracts.` |
Binary file added
BIN
+440 Bytes
packages/core/src/storage-namespaced-conflicts-with-layout.test.ts.snap
Binary file not shown.
This file contains 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
This file contains 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
Binary file modified
BIN
+9 Bytes
(100%)
packages/core/src/storage-namespaced-conflicts.test.ts.snap
Binary file not shown.
This file contains 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