diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3da3c70 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,37 @@ +name: CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + env: + NEXT_TELEMETRY_DISABLED: 1 + + steps: + - uses: actions/checkout@v4 + + - name: Use Node.js 24.x LTS + uses: actions/setup-node@v4 + with: + node-version: '24.x' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Lint + run: npm run lint + + - name: Build + run: npm run build + + - name: Test + run: npm test diff --git a/AGENTS.md b/AGENTS.md index ecd7b69..87e2867 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -2,5 +2,6 @@ - Use Conventional Commits for commit messages, PR titles, and squash merges. - Use **Why**, **Change**, and **Validation** sections by default in PR descriptions. Explain the reason, summarize what changed, and report what was actually checked. Reserve comments for decisions, blockers, and review replies. -- Install dependencies and run builds in a fresh development VM. For frontend changes, run `npm ci`, `npm run lint`, and `npm run build` from the repository root. +- Install dependencies and run builds in a fresh development VM. For frontend changes, run `npm ci`, `npm run lint`, `npm run build`, and `npm test` from the repository root. CI runs the same checks on Node 24. - For registry or contract-page changes, check both Mainnet and Testnet in a browser. Keep a recorded verification distinct from a match with the currently deployed code. An RPC failure must show an unknown status, never a match or mismatch. +- `npm test` runs `tests/**/*.test.cjs` with the Node test runner, which loads `.ts` modules directly (Node 22.18 or later). Put tested logic in modules whose `@/` imports are type-only. diff --git a/README.md b/README.md index f6d865d..4254f33 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ The registry rows show recorded verifications. The contract workspace checks dep Registry totals come from the REST API configured by `NEXT_PUBLIC_API_HOST`. IPFS source snapshots use the app's `/api/proxy-ipfs` route. +The Evidence tab names the repository and commit of the recorded snapshot: the record's own `github`, or the git checkout stored in the snapshot. The contract's NEP-330 metadata, read at the latest final block, is shown on the Build tab as current metadata. The source tree opens the folder it names, labelled as current metadata: the registry doesn't record which crate was built. The snapshot's git checkout is read only when the Evidence or Build tab is open. + ## Learn More To learn more about Next.js, take a look at the following resources: diff --git a/components/Common/PaneBoundary.tsx b/components/Common/PaneBoundary.tsx new file mode 100644 index 0000000..73b1e3c --- /dev/null +++ b/components/Common/PaneBoundary.tsx @@ -0,0 +1,37 @@ +import StatePanel from '@/components/Common/StatePanel' +import { Button } from '@chakra-ui/react' +import { Component, ErrorInfo, ReactNode } from 'react' + +/** + * Contains a render error to one workspace pane, so the verdict, tabs and + * header stay usable. Give it a `key` that changes with the pane's input to + * start over when the reader moves on. + */ +export default class PaneBoundary extends Component<{ label: string; children: ReactNode }, { failed: boolean }> { + state = { failed: false } + + static getDerivedStateFromError() { + return { failed: true } + } + + componentDidCatch(error: Error, info: ErrorInfo) { + console.error(`${this.props.label} failed to render`, error, info.componentStack) + } + + render() { + if (!this.state.failed) return this.props.children + return ( + this.setState({ failed: false })}> + Try again + + } + > + Something in this section failed to render. The verdict and the other sections are unaffected. + + ) + } +} diff --git a/components/Workspace/BuildPane.tsx b/components/Workspace/BuildPane.tsx index 45e57af..675f83a 100644 --- a/components/Workspace/BuildPane.tsx +++ b/components/Workspace/BuildPane.tsx @@ -1,9 +1,13 @@ import CopyButton from '@/components/Common/CopyButton' import ExternalTextLink from '@/components/Common/ExternalTextLink' +import GithubLink from '@/components/Common/GithubLink' import { Field, Fields, Panel } from '@/components/Common/Panel' -import type { MetadataLoad } from '@/hooks/useContract' +import type { GithubDto } from '@/Interfaces/github/github.dto' import { parseImageRef, registryPage } from '@/utils/buildImage' -import { Badge, Box, Code, Flex, HStack, Spinner, Stack, Text } from '@chakra-ui/react' +import type { MetadataField, MetadataLoad } from '@/utils/contractMetadata' +import { liveRelation, LiveSource, RecordedSource } from '@/utils/evidence' +import { githubTreeUrl } from '@/utils/urls' +import { Badge, Box, Button, Code, Flex, HStack, Spinner, Stack, Text } from '@chakra-ui/react' import { ReactNode } from 'react' const Mono = (props: { children: ReactNode }) => ( @@ -12,6 +16,12 @@ const Mono = (props: { children: ReactNode }) => ( ) const NotSet = (props: { children: ReactNode }) => {props.children} +/** A field the contract published with the wrong type: flagged, never rendered. */ +const Flagged = (props: { children: ReactNode }) => ( + + {props.children} + +) /** * The exact image reference from the metadata (tag and digest included), with a @@ -46,59 +56,152 @@ function BuildImage(props: { value: string }) { ) } -export default function BuildPane(props: { metadata: MetadataLoad }) { +const TITLE = 'Current build metadata (NEP-330)' + +/** The folder on GitHub, or null when the path can't be put into a URL. */ +function githubFolderUrl(g: GithubDto, dir: string): string | null { + try { + return `${githubTreeUrl(g)}/${dir.split('/').map(encodeURIComponent).join('/')}` + } catch { + return null + } +} + +const ABSENT: Record['reason'], string> = { + no_method: 'The deployed contract doesn’t expose contract_source_metadata, so no build metadata is published on-chain.', + no_code: 'No contract code is deployed on this account at the latest final block, so there is no build metadata.', + no_account: 'This account doesn’t exist at the latest final block, so there is no build metadata.', + null: 'The deployed contract’s contract_source_metadata returns null, so no build metadata is published.', +} + +function Status(props: { tone?: 'muted' | 'warn'; children: ReactNode }) { + return ( + + + {props.children} + + + ) +} + +/** The contract's NEP-330 metadata as read now, labelled as describing the code deployed now. */ +export default function BuildPane(props: { metadata: MetadataLoad; onRetry: () => void; recorded: RecordedSource; live: LiveSource }) { const { metadata } = props - if (metadata.status !== 'ready') + if (metadata.status === 'loading') return ( - - - {metadata.status === 'loading' ? ( - - - Loading contract_source_metadata… - - ) : metadata.status === 'missing' ? ( - 'This contract doesn’t expose contract_source_metadata, so no build metadata is published on-chain.' - ) : ( - 'Couldn’t load the contract metadata from the RPC.' - )} - - + + + + Loading contract_source_metadata… + + ) + if (metadata.status === 'unknown') + return ( + + + Couldn’t read contract_source_metadata: the RPC request failed or the call returned an error. The build metadata is unknown. + + + + ) + if (metadata.status === 'absent') return {ABSENT[metadata.reason]} + if (metadata.status === 'invalid') + return The deployed contract’s contract_source_metadata returns a value that isn’t a NEP-330 metadata object, so it isn’t shown. + const m = metadata.metadata - const build = m.build_info - const env = typeof build?.build_environment === 'string' ? build.build_environment.trim() : '' - const standards = Array.isArray(m.standards) ? m.standards : [] - const command = Array.isArray(build?.build_command) ? build?.build_command ?? [] : [] + const build = m.buildInfo + const bad = (field: MetadataField) => m.invalid.includes(field) + const wrongType = Published with the wrong type; not shown + const env = build?.buildEnvironment?.trim() ?? '' + const relation = liveRelation(props.recorded, props.live) + const liveGithub = props.live.status === 'ready' ? props.live.github : null + const folderUrl = liveGithub && build?.contractDir ? githubFolderUrl(liveGithub, build.contractDir) : null + return ( - + + + Read from the deployed contract at the latest final block. It describes the code deployed now, which can differ from the verified build. + - {m.version ? {m.version} : Not specified} + {bad('version') ? wrongType : m.version ? {m.version} : Not specified} - {standards.length ? ( - - {standards.map((s) => ( - - {s.standard} {s.version} - - ))} - - ) : ( - None declared - )} - - - {command.length ? ( - - {command.join(' ')} - - ) : ( - Not specified - )} + + {m.standards.length ? ( + + {m.standards.map((s) => ( + + {s.standard} {s.version} + + ))} + + ) : ( + !bad('standards') && None declared + )} + {bad('standards') && Entries that aren’t a standard and version string were ignored.} + - {env ? : Not specified} - {build?.contract_path ? {build.contract_path} : / (repository root)} - {build?.source_code_snapshot ? {build.source_code_snapshot} : Not specified} + {bad('build_info') || !build ? ( + {bad('build_info') ? wrongType : Not published} + ) : ( + <> + + {bad('build_command') ? ( + Not a list of strings; not shown + ) : build.buildCommand?.length ? ( + + {build.buildCommand.join(' ')} + + ) : ( + Not specified + )} + + {bad('build_environment') ? wrongType : env ? : Not specified} + + {build.contractPath ? ( + + {build.contractPath} + {bad('contract_path') && Not a relative folder inside the source, so it isn’t used to open the source.} + {folderUrl && ( + + + This folder on GitHub at the current metadata’s commit + + + )} + + ) : bad('contract_path') ? ( + wrongType + ) : ( + / (repository root) + )} + + + {bad('source_code_snapshot') ? ( + wrongType + ) : build.sourceCodeSnapshot ? ( + + {build.sourceCodeSnapshot} + {liveGithub && } + {relation === 'same' && ( + + Same repository and commit as the verified snapshot. + + )} + {relation === 'different' && ( + + Not the repository and commit of the verified snapshot; the Evidence tab shows the verified one. + + )} + + ) : ( + Not specified + )} + + + )} ) diff --git a/components/Workspace/EvidencePane.tsx b/components/Workspace/EvidencePane.tsx index 8240190..32a5113 100644 --- a/components/Workspace/EvidencePane.tsx +++ b/components/Workspace/EvidencePane.tsx @@ -6,10 +6,10 @@ import { Field, Fields, Panel } from '@/components/Common/Panel' import { verdictSummary } from '@/components/Contracts/Verdict' import { NETWORKS, NetworkType } from '@/contexts/NetworkContext' import type { HashCheck } from '@/hooks/useCodeHashCheck' -import type { GithubDto } from '@/Interfaces/github/github.dto' import { ContractData, recordedBlock } from '@/utils/contracts' +import type { RecordedSource } from '@/utils/evidence' import { explorerContractUrl } from '@/utils/urls' -import { HStack, Text } from '@chakra-ui/react' +import { Button, HStack, Spinner, Stack, Text } from '@chakra-ui/react' import { ReactNode } from 'react' const Mono = (props: { children: ReactNode }) => ( @@ -18,14 +18,62 @@ const Mono = (props: { children: ReactNode }) => ( ) +const Muted = (props: { children: ReactNode }) => ( + + {props.children} + +) + +/** Only the repository the record itself proves: the record's own, or the git checkout inside its snapshot. */ +function RecordedRepository(props: { recorded: RecordedSource; onRetry: () => void }) { + const { recorded } = props + if (recorded.status === 'loading') + return ( + + + Reading the snapshot… + + ) + if (recorded.status === 'known') + return ( + + + {recorded.origin === 'snapshot' && ( + + From the git checkout stored in the verified snapshot. + + )} + + ) + if (recorded.status === 'unknown') + return ( + + + Unknown: the snapshot couldn’t be read. + + + + ) + return ( + + Not recorded. The registry stores the snapshot above, not a repository, and{' '} + {recorded.reason === 'no_checkout' + ? 'the snapshot has no git checkout.' + : 'the git checkout in the snapshot doesn’t name exactly one GitHub repository and commit.'}{' '} + The Build tab shows the repository named in the contract’s current metadata, which may describe a later deployment. + + ) +} + export default function EvidencePane(props: { accountId: string network: NetworkType data: ContractData check: HashCheck - github: GithubDto | null - entryFileUrl: string | null - entryFile: string + recorded: RecordedSource + onRetryRecorded: () => void }) { const { accountId, network, data, check } = props return ( @@ -67,14 +115,9 @@ export default function EvidencePane(props: { - {props.github ? : Not published} - {props.github && props.entryFileUrl && ( - - - {props.entryFile || 'Repository root'} - - - )} + + + {NETWORKS[network].contract} diff --git a/components/Workspace/SourcePane.tsx b/components/Workspace/SourcePane.tsx index 3e2ae08..6862aab 100644 --- a/components/Workspace/SourcePane.tsx +++ b/components/Workspace/SourcePane.tsx @@ -4,6 +4,7 @@ import { verdictTone } from '@/components/Contracts/Verdict' import type { NetworkType } from '@/contexts/NetworkContext' import type { HashCheck } from '@/hooks/useCodeHashCheck' import type { ContractData } from '@/utils/contracts' +import { FolderChoice, folderUnknown, isSnapshotFilePath, SourceRoot } from '@/utils/evidence' import { cachedSource, cachedSourcePaths, entryPath, fetchSource, listSourcePaths } from '@/utils/sourceFiles' import { HEADER_H } from '@/utils/theme' import { ipfsFileUrl } from '@/utils/urls' @@ -11,7 +12,9 @@ import { Box, Button, Center, + Code, Flex, + HStack, Icon, Input, InputGroup, @@ -40,14 +43,102 @@ const STRIPE: Record = { unavailable: 'The deployed code could not be checked.', } -type Files = { key: string; status: 'loading' | 'error' | 'ready'; paths: string[] } +type Files = { key: string; status: 'loading' | 'error' | 'ready'; paths: string[]; partial: boolean } type Content = { key: string; status: 'loading' | 'error' | 'ready'; text: string } +/** + * Says which folder the tree shows and where that came from. The registry + * doesn't record which crate was built, so a folder taken from the current + * metadata is always labelled as such, even when it names the recorded commit. + */ +function FolderNotice(props: { root: SourceRoot; onChooseFolder: (_choice: FolderChoice) => void; onRetryMetadata: () => void }) { + const { root } = props + const hint = root.hint + const atLink = root.basis === 'link' + if (hint.status === 'none' && !atLink) return null + const warn = hint.status === 'unknown' + const linkFolder = ( + <> + Showing {root.rootPath}/, the folder of the linked file. + + ) + const button = (label: string, onClick: () => void, mono = false) => ( + + ) + return ( + + + {hint.status === 'unknown' ? ( + <>Couldn’t read the contract’s current metadata, so it’s unknown which folder of this snapshot holds the contract. {atLink ? linkFolder : 'Showing the snapshot root.'} + ) : root.basis === 'current_metadata' && hint.status === 'offered' ? ( + <> + Showing {hint.dir}/, the folder named in the contract’s current metadata. The verification record doesn’t name the crate + that was built, so this may not be the verified crate. + + ) : hint.status === 'offered' ? ( + <> + {atLink && <>{linkFolder} }The contract’s current metadata names {hint.dir}/. The verification record doesn’t name the + crate that was built. + + ) : ( + linkFolder + )} + + + {hint.status === 'unknown' && button('Try again', props.onRetryMetadata)} + {hint.status === 'offered' && root.basis !== 'current_metadata' && button(`Open ${hint.dir}/`, () => props.onChooseFolder('metadata'), true)} + {(root.basis === 'current_metadata' || atLink) && button('Show snapshot root', () => props.onChooseFolder('root'))} + + + ) +} + export default function SourcePane(props: { network: NetworkType data: ContractData - /** NEP-330 contract_path, '/' when absent; null while metadata is still loading. */ + root: SourceRoot + onChooseFolder: (_choice: FolderChoice) => void + onRetryMetadata: () => void + /** `?file=` from the URL. */ + requested: string | null + hrefFor: (_file: string) => string + onSelect: (_file: string) => void + check: HashCheck +}) { + const { root, onChooseFolder, onRetryMetadata, ...tree } = props + return ( + <> + + + + ) +} + +function SourceTree(props: { + network: NetworkType + data: ContractData + /** Snapshot folder to list ('/' = root); null while it is still being decided. */ rootPath: string | null + /** The metadata read failed, so which folder holds the contract is unknown. */ + folderUnknown: boolean + onRetryMetadata: () => void /** `?file=` from the URL. */ requested: string | null hrefFor: (_file: string) => string @@ -65,18 +156,18 @@ export default function SourcePane(props: { const filesKey = `${network}:${data.cid}:${rootPath}` const [files, setFiles] = useState(() => { const hit = rootPath !== null ? cachedSourcePaths(network, data.cid, rootPath) : undefined - return { key: filesKey, status: hit ? 'ready' : 'loading', paths: hit ?? [] } + return { key: filesKey, status: hit ? 'ready' : 'loading', paths: hit ?? [], partial: false } }) useEffect(() => { if (rootPath === null) return const hit = cachedSourcePaths(network, data.cid, rootPath) - if (hit) return setFiles({ key: filesKey, status: 'ready', paths: hit }) + if (hit) return setFiles({ key: filesKey, status: 'ready', paths: hit, partial: false }) let active = true - setFiles({ key: filesKey, status: 'loading', paths: [] }) + setFiles({ key: filesKey, status: 'loading', paths: [], partial: false }) listSourcePaths(network, data.cid, rootPath) - .then((paths) => active && setFiles({ key: filesKey, status: 'ready', paths })) - .catch(() => active && setFiles({ key: filesKey, status: 'error', paths: [] })) + .then(({ paths, partial }) => active && setFiles({ key: filesKey, status: 'ready', paths, partial })) + .catch(() => active && setFiles({ key: filesKey, status: 'error', paths: [], partial: false })) return () => { active = false } @@ -84,8 +175,22 @@ export default function SourcePane(props: { const ready = files.key === filesKey && files.status === 'ready' const paths = useMemo(() => (ready ? files.paths : []), [ready, files.paths]) - const missing = ready && !!props.requested && !paths.includes(props.requested) - const active = ready ? (props.requested && paths.includes(props.requested) ? props.requested : entryPath(paths, data.lang)) : props.requested + const partial = ready && files.partial + // A linked file opens even when the listing doesn't show it (it can sit + // deeper than one folder below the root). Only when reading it fails does + // the tree fall back to its entry file, without claiming the file is absent. + const requested = props.requested && isSnapshotFilePath(props.requested) ? props.requested : null + const listed = !!requested && paths.includes(requested) + const linkKey = requested ? `${data.cid}:${requested}` : null + const [linkFailed, setLinkFailed] = useState(null) + const openLink = !!requested && (listed || linkFailed !== linkKey) + const active = ready ? (openLink ? requested : entryPath(paths, data.lang)) : props.requested + const unlistedLink = ready && openLink && !listed + const linkFallback = ready && !!props.requested && !openLink + const retryLink = () => { + setLinkFailed(null) + setContentAttempt((n) => n + 1) + } const contentKey = `${data.cid}:${active}` const [content, setContent] = useState(() => { @@ -101,11 +206,15 @@ export default function SourcePane(props: { setContent({ key: contentKey, status: 'loading', text: '' }) fetchSource(network, data.cid, active) .then((text) => active_ && setContent({ key: contentKey, status: 'ready', text })) - .catch(() => active_ && setContent({ key: contentKey, status: 'error', text: '' })) + .catch(() => { + if (!active_) return + if (active === requested && !listed) setLinkFailed(linkKey) + else setContent({ key: contentKey, status: 'error', text: '' }) + }) return () => { active_ = false } - }, [ready, active, network, data.cid, contentKey, contentAttempt]) + }, [ready, active, network, data.cid, contentKey, contentAttempt, requested, listed, linkKey]) // Only ever show the content that belongs to the file named in the header. const shown: Content = content.key === contentKey ? content : { key: contentKey, status: 'loading', text: '' } @@ -185,10 +294,51 @@ export default function SourcePane(props: { ) - if (ready && paths.length === 0) + // With the folder unknown, an empty listing only means the contract wasn't + // found there, not that the snapshot has no source. + if (ready && paths.length === 0 && !openLink && props.folderUnknown) + return ( +
+ + {linkFallback && Couldn’t open {props.requested} from this snapshot.} + The contract’s source folder is unknown until its current metadata can be read. + + +
+ ) + + if (ready && paths.length === 0 && !openLink) return (
- The snapshot has no .rs or .toml files at {rootPath === '/' ? 'its root' : rootPath} or one folder below it. + + {linkFallback && Couldn’t open {props.requested} from this snapshot.} + + No .rs or .toml files at {rootPath === '/' ? 'the snapshot root' : `${rootPath}/`} or one folder below it + {partial ? ', but some of those folders couldn’t be read.' : '.'} + + {(partial || (linkFallback && requested)) && ( + + )} +
) @@ -255,6 +405,14 @@ export default function SourcePane(props: { )} + {partial && ( + + Some folders couldn’t be read. + + + )} {ready ? `Rust and TOML files: ${paths.length}` : 'Reading snapshot…'} @@ -267,6 +425,7 @@ export default function SourcePane(props: {