This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Add an experimental layout command #101
Open
sz-piotr
wants to merge
10
commits into
main
Choose a base branch
from
layout-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
220f287
Add a layout command
sz-piotr dcf8e8a
Get the storage layout
sz-piotr 9423ac2
Define ideal layout format
sz-piotr fcbd685
Process solidity layout
sz-piotr 9fefac3
Update to use a single address for now
sz-piotr 19a05d4
Improve types
sz-piotr 4a1b607
Add flattening
sz-piotr 318ba97
Add mergeFlatLayouts
sz-piotr 5677de1
Add multi-address support
sz-piotr 0da27c2
Update version
sz-piotr 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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
build | ||
.env | ||
cache | ||
layout.json |
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
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
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,45 @@ | ||
import { Logger } from '@l2beat/backend-tools' | ||
import { writeFileSync } from 'fs' | ||
|
||
import { DiscoveryCliConfig } from '../config/config.discovery' | ||
import { flattenLayout } from '../layout/flattenLayout' | ||
import { mergeFlatLayouts } from '../layout/mergeFlatLayouts' | ||
import { parseAndGetLayout } from '../layout/parseAndGetLayout' | ||
import { EthereumAddress } from '../utils/EthereumAddress' | ||
import { EtherscanLikeClient } from '../utils/EtherscanLikeClient' | ||
import { HttpClient } from '../utils/HttpClient' | ||
|
||
export async function layoutCommand( | ||
config: DiscoveryCliConfig, | ||
logger: Logger, | ||
): Promise<void> { | ||
if (!config.layout) { | ||
return | ||
} | ||
const http = new HttpClient() | ||
const etherscanClient = EtherscanLikeClient.createForDiscovery( | ||
http, | ||
config.chain.etherscanUrl, | ||
config.chain.etherscanApiKey, | ||
config.chain.etherscanUnsupported, | ||
) | ||
await runLayout(etherscanClient, config.layout.addresses, logger) | ||
} | ||
|
||
async function runLayout( | ||
etherscanClient: EtherscanLikeClient, | ||
addresses: EthereumAddress[], | ||
logger: Logger, | ||
): Promise<void> { | ||
const sources = await Promise.all( | ||
addresses.map((address) => etherscanClient.getContractSource(address)), | ||
) | ||
logger.info('Got sources', { | ||
lengths: sources.map((source) => source.SourceCode.length), | ||
}) | ||
const layout = mergeFlatLayouts( | ||
sources.map((s) => flattenLayout(parseAndGetLayout(s))), | ||
) | ||
writeFileSync('layout.json', JSON.stringify(layout, null, 2)) | ||
logger.info('Saved layout', { filename: 'layout.json', items: layout.length }) | ||
} |
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
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,77 @@ | ||
export type LayoutItem = | ||
| StaticItem | ||
| StructItem | ||
| StaticArrayItem | ||
| DynamicArrayItem | ||
| MappingItem | ||
| DynamicBytesItem | ||
|
||
export type AnonymousItem = | ||
| Anonymize<StaticItem> | ||
| Anonymize<StructItem> | ||
| Anonymize<StaticArrayItem> | ||
| Anonymize<DynamicArrayItem> | ||
| Anonymize<MappingItem> | ||
| Anonymize<DynamicBytesItem> | ||
|
||
type Anonymize<T> = Omit<T, 'name' | 'slot' | 'offset'> | ||
|
||
export interface StaticItem { | ||
name: string | ||
kind: 'static' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
} | ||
|
||
export interface StructItem { | ||
name: string | ||
kind: 'struct' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
children: LayoutItem[] | ||
} | ||
|
||
export interface StaticArrayItem { | ||
name: string | ||
kind: 'static array' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
length: number | ||
item: AnonymousItem | ||
} | ||
|
||
export interface DynamicArrayItem { | ||
name: string | ||
kind: 'dynamic array' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
item: AnonymousItem | ||
} | ||
|
||
export interface MappingItem { | ||
name: string | ||
kind: 'mapping' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
key: AnonymousItem | ||
value: AnonymousItem | ||
} | ||
|
||
export interface DynamicBytesItem { | ||
name: string | ||
kind: 'dynamic bytes' | ||
type: string | ||
slot: number | ||
offset: number | ||
size: number | ||
} |
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,52 @@ | ||
export type SlotView = | ||
| SingleSlotView | ||
| CompositeSlotView | ||
| BytesSlotView | ||
| MappingSlotView | ||
| ArraySlotView | ||
|
||
export interface SingleSlotView { | ||
kind: 'static single' | ||
path: string[] | ||
slot: number | ||
variable: SlotVariable | ||
} | ||
|
||
export interface CompositeSlotView { | ||
kind: 'static composite' | ||
path: string[] | ||
slot: number | ||
variables: SlotVariable[] | ||
} | ||
|
||
export interface BytesSlotView { | ||
kind: 'dynamic bytes' | ||
path: string[] | ||
slot: number | ||
variable: SlotVariable | ||
} | ||
|
||
export interface MappingSlotView { | ||
kind: 'dynamic mapping' | ||
path: string[] | ||
slot: number | ||
variable: SlotVariable | ||
keyType: string | ||
valueView: SlotView[] | ||
} | ||
|
||
export interface ArraySlotView { | ||
kind: 'dynamic array' | ||
path: string[] | ||
slot: number | ||
variable: SlotVariable | ||
itemView: SlotView[] | ||
} | ||
|
||
export interface SlotVariable { | ||
name: string | ||
aliases: string[] | ||
type: string | ||
offset: number | ||
size: number | ||
} |
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,28 @@ | ||
import { z } from 'zod' | ||
|
||
export type SolidityStorageEntry = z.infer<typeof SolidityStorageEntry> | ||
export const SolidityStorageEntry = z.strictObject({ | ||
astId: z.number(), | ||
contract: z.string(), | ||
label: z.string(), | ||
offset: z.number(), | ||
slot: z.string(), | ||
type: z.string(), | ||
}) | ||
|
||
export type SolidityTypeEntry = z.infer<typeof SolidityTypeEntry> | ||
export const SolidityTypeEntry = z.strictObject({ | ||
encoding: z.enum(['inplace', 'mapping', 'dynamic_array', 'bytes']), | ||
label: z.string(), | ||
numberOfBytes: z.string(), | ||
key: z.string().optional(), | ||
value: z.string().optional(), | ||
base: z.string().optional(), | ||
members: z.array(SolidityStorageEntry).optional(), | ||
}) | ||
|
||
export type SolidityStorageLayout = z.infer<typeof SolidityStorageLayout> | ||
export const SolidityStorageLayout = z.strictObject({ | ||
storage: z.array(SolidityStorageEntry), | ||
types: z.record(SolidityTypeEntry).nullable(), | ||
}) |
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.
What do you think about making this function return the results (the
layout
variable) and the function above can save them. This would allow us to later export thisrunLayout
and use the results without having to interface with the filesystem