Skip to content

Commit

Permalink
wip: refactor: fixin' type checks (pt.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 15, 2024
1 parent b13c91d commit 44982b5
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 123 deletions.
5 changes: 3 additions & 2 deletions fixtures/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { fileURLToPath } from 'node:url'
import { readFileSync } from 'node:fs'
import ok from 'node:assert'
import { SyncFS, FileFormat, withTmpDir } from '@hackbg/file'
import { Console, bold, Chain, Compute, Connection, Identity, Backend } from '@fadroma/agent'
import { Console, bold, Chain, Connection, Identity, Backend } from '@fadroma/agent'
import { Deployment } from '@fadroma/deploy'

//@ts-ignore
export const here = dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -40,7 +41,7 @@ export const tmpDir = () => {
return x
}

export class TestProjectDeployment extends Compute.Deployment {
export class TestProjectDeployment extends Deployment {

t = this.template('t', {
chainId: 'stub',
Expand Down
15 changes: 7 additions & 8 deletions packages/agent/test/agent-chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Backend,
Identity,
Batch,
Compute
} from '../index'
import { fixture } from '@fadroma/fixtures'
import * as Stub from '../stub/stub'
Expand Down Expand Up @@ -83,7 +82,7 @@ export async function testAuth () {
await agent.chain.fetchCodeInstances('1')
rejects(agent.chain.fetchCodeInstances(null as any))
await agent.chain.fetchCodeInstances(['1', '2'])
await agent.chain.fetchCodeInstances({'1': Compute.Contract, '2': Compute.Contract})
await agent.chain.fetchCodeInstances({'1': Contract, '2': Contract})

await agent.execute('stub', {}, {})
await agent.execute('stub', 'method', {})
Expand Down Expand Up @@ -123,10 +122,10 @@ export async function testClient () {
await client.query({foo: 'bar'})
await client.execute({foo: 'bar'})
await agent.chain.fetchContractInfo('addr')
assert(new Compute.Contract({ address: 'addr' }))
assert.throws(()=>new Compute.Contract({}).query({}))
assert.throws(()=>new Compute.Contract({ agent }).query({}))
assert.throws(()=>new Compute.Contract({}).execute({}))
assert.throws(()=>new Compute.Contract({ agent }).execute({}))
assert.throws(()=>new Compute.Contract({ agent: {} as any }).execute({}))
assert(new Contract({ address: 'addr' }))
assert.throws(()=>new Contract({}).query({}))
assert.throws(()=>new Contract({ agent }).query({}))
assert.throws(()=>new Contract({}).execute({}))
assert.throws(()=>new Contract({ agent }).execute({}))
assert.throws(()=>new Contract({ agent: {} as any }).execute({}))
}
48 changes: 24 additions & 24 deletions packages/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/** Fadroma. Copyright (C) 2023 Hack.bg. License: GNU AGPLv3 or custom.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import { bold, Compute, Console } from '@fadroma/agent'
import {
bold, colors, randomColor, assign, Compiler, RustSourceCode, CompiledCode, Console
} from '@fadroma/agent'
import * as OCI from '@fadroma/oci'

import { Config } from '@hackbg/conf'
Expand All @@ -16,15 +18,13 @@ import { randomBytes } from 'node:crypto'

import { packageRoot, console } from './package'

export const Compiler = Compute.Compiler

export function getCompiler ({
config = new Config(),
useContainer = !config.getFlag('FADROMA_BUILD_RAW', ()=>false),
...options
}: |({ useContainer?: false } & Partial<RawLocalRustCompiler>)
|({ useContainer: true } & Partial<ContainerizedLocalRustCompiler>) = {}
): Compute.Compiler { // class dispatch, ever awkward
): Compiler { // class dispatch, ever awkward
if (useContainer) {
return new ContainerizedLocalRustCompiler({
config, ...options as Partial<ContainerizedLocalRustCompiler>
Expand Down Expand Up @@ -117,7 +117,7 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
/** Version of Rust toolchain to use. */
toolchain: string|null = this.config.getString('FADROMA_RUST', ()=>'')
/** Default Git reference from which to build sources. */
revision: string = Compute.HEAD
revision: string = HEAD
/** Owner uid that is set on build artifacts. */
buildUid?: number = (process.getuid ? process.getuid() : undefined) // process.env.FADROMA_BUILD_UID
/** Owner gid that is set on build artifacts. */
Expand All @@ -134,7 +134,7 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
}

/** @returns a fully populated RustSourceCode from the original. */
protected resolveSource (source: string|Partial<Compute.RustSourceCode>): Partial<Compute.RustSourceCode> {
protected resolveSource (source: string|Partial<RustSourceCode>): Partial<RustSourceCode> {
if (typeof source === 'string') {
source = { cargoCrate: source }
}
Expand All @@ -145,7 +145,7 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
}

/** Compile a single contract. */
async build (contract: string|Partial<Compute.RustSourceCode>): Promise<Compute.CompiledCode> {
async build (contract: string|Partial<RustSourceCode>): Promise<CompiledCode> {
return (await this.buildMany([contract]))[0]
}

Expand All @@ -154,14 +154,14 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
* and have it build all the crates from that combination in sequence,
* reusing the container's internal intermediate build cache. */
async buildMany (
contracts: (string|(Partial<Compute.RustSourceCode>))[],
contracts: (string|(Partial<RustSourceCode>))[],
// options: { parallel?: boolean } // TODO
): Promise<Compute.CompiledCode[]> {
const results: Compute.CompiledCode[] = []
): Promise<CompiledCode[]> {
const results: CompiledCode[] = []
// Group contracts by source root, source ref, and cargo workspace.
const batches = this.collectBatches(contracts.map(
x => (typeof x === 'string') ? { sourcePath: x } : x
) as Partial<Compute.RustSourceCode>[])
) as Partial<RustSourceCode>[])
// Run each root/ref pair in a container, executing one or more cargo build commands.
for (const [root, refs] of Object.entries(batches)) {
for (const [ref, batch] of Object.entries(refs)) {
Expand All @@ -177,7 +177,7 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
return results
}

protected collectBatches (contracts: Array<Partial<Compute.RustSourceCode>>): CompileBatches {
protected collectBatches (contracts: Array<Partial<RustSourceCode>>): CompileBatches {
// Batch by sourcePath, then by sourceRef, then group by workspace
const batches: CompileBatches = {}
// Assign each contract to its appropriate branch and group
Expand Down Expand Up @@ -227,14 +227,14 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
* If it does, don't rebuild it but return it from there. */
protected tryGetCached (
outputDir: string,
{ sourceRef, cargoCrate }: Partial<Compute.RustSourceCode>
): Compute.CompiledCode|null {
{ sourceRef, cargoCrate }: Partial<RustSourceCode>
): CompiledCode|null {
if (this.caching && cargoCrate) {
const location = new SyncFS.File(
outputDir, codePathName(cargoCrate, sourceRef||Compute.HEAD)
outputDir, codePathName(cargoCrate, sourceRef||HEAD)
)
if (location.exists()) {
return new Compute.CompiledCode({
return new CompiledCode({
codePath: location.url,
codeHash: location.sha256()
})
Expand All @@ -247,20 +247,20 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
outputDir, sourceRef, tasks,
}: {
outputDir: string, sourceRef: string, tasks: Set<CompileTask>
}): Promise<Record<number, Compute.CompiledCode>> {
const results: Record<number, Compute.CompiledCode> = {}
}): Promise<Record<number, CompiledCode>> {
const results: Record<number, CompiledCode> = {}
for (const task of tasks) {
if ((task as CompileWorkspaceTask).cargoWorkspace) {
for (const { buildIndex, cargoCrate } of (task as CompileWorkspaceTask).cargoCrates) {
const wasmName = `${sanitize(cargoCrate)}@${sanitize(sourceRef)}.wasm`
const compiled = await new Compute.LocalCompiledCode({
const compiled = await new LocalCompiledCode({
codePath: new Path(outputDir, wasmName).absolute
}).computeHash()
results[buildIndex] = compiled
}
} else if ((task as CompileCrateTask).cargoCrate) {
const wasmName = `${sanitize((task as CompileCrateTask).cargoCrate)}@${sanitize(sourceRef)}.wasm`
const compiled = await new Compute.LocalCompiledCode({
const compiled = await new LocalCompiledCode({
codePath: new Path(outputDir, wasmName).absolute
}).computeHash()
results[(task as CompileCrateTask).buildIndex] = compiled
Expand All @@ -279,7 +279,7 @@ export abstract class LocalRustCompiler extends ConfiguredCompiler {
}

protected abstract buildBatch (batch: CompileBatch):
Promise<Record<number, Compute.CompiledCode>>
Promise<Record<number, CompiledCode>>

protected logStart (sourcePath: string, sourceRef: string, tasks: Set<CompileTask>) {
this.log.log(
Expand All @@ -298,7 +298,7 @@ export class RawLocalRustCompiler extends LocalRustCompiler {

protected async buildBatch (
batch: CompileBatch, options: { outputDir?: string, buildScript?: string|Path } = {}
): Promise<Record<number, Compute.CompiledCode>> {
): Promise<Record<number, CompiledCode>> {
const { sourcePath, sourceRef, tasks } = batch
const safeRef = sanitize(sourceRef)
const { outputDir = this.outputDir.absolute, buildScript = this.script } = options
Expand Down Expand Up @@ -414,7 +414,7 @@ export class ContainerizedLocalRustCompiler extends LocalRustCompiler {
buildScript = this.script
}: { outputDir?: string, buildScript?: string|Path } = {}

): Promise<Record<number, Compute.CompiledCode>> {
): Promise<Record<number, CompiledCode>> {
const safeRef = sanitize(sourceRef)
if (!buildScript) {
throw new Error('missing build script')
Expand Down Expand Up @@ -508,7 +508,7 @@ export class ContainerizedLocalRustCompiler extends LocalRustCompiler {

protected getLogStream (revision: string, cb: (data: string)=>void) {
let log = new Console(`compiling in container(${bold(this.buildImage.name)})`)
if (revision && revision !== Compute.HEAD) {
if (revision && revision !== HEAD) {
log = log.sub(`(from ${bold(revision)})`)
}
// This stream collects the output from the build container, i.e. the build logs.
Expand Down
6 changes: 3 additions & 3 deletions packages/create/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import assert from 'node:assert'
import type { ChainId } from '@fadroma/agent'
import { Error, bold, timestamp, Bip39, Bip39EN, Compute, Stub } from '@fadroma/agent'
import { Error, bold, timestamp, Bip39, Bip39EN, Stub } from '@fadroma/agent'

import { tmpDir, TestProjectDeployment } from '@fadroma/fixtures'
import * as Projects from './create'
Expand Down Expand Up @@ -97,12 +97,12 @@ export async function testProjectCreate () {

export async function testDeployment () {
const deployment = new TestProjectDeployment()
assert.ok(deployment.t instanceof Compute.ContractTemplate)
assert.ok(deployment.t instanceof ContractTemplate)
await deployment.deploy({
uploader: new Stub.Agent({}),
deployer: new Stub.Agent({}),
})
assert.ok([deployment.a1, deployment.a2, deployment.a3, ...Object.values(deployment.b)].every(
c=>c instanceof Compute.ContractInstance
c=>c instanceof ContractInstance
))
}
8 changes: 4 additions & 4 deletions packages/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import type { ChainId } from '@fadroma/agent'
import { Error, bold, timestamp, Bip39, Bip39EN, Compute } from '@fadroma/agent'
import { Error, bold, timestamp, Bip39, Bip39EN } from '@fadroma/agent'

import type { Path } from '@hackbg/file'
import { SyncFS, FileFormat } from '@hackbg/file'
Expand Down Expand Up @@ -277,11 +277,11 @@ export class Project extends ProjectDirectory {
}
return console
}
createDeployment (): Compute.Deployment {
createDeployment (): Deployment {
console.warn('createDeployment: not implemented')
return new Compute.Deployment()
return new Deployment()
}
async getDeployment (): Promise<Compute.Deployment> {
async getDeployment (): Promise<Deployment> {
const packageJson = new SyncFS.File(this.path, 'package.json').setFormat(FileFormat.JSON)
if (!packageJson.exists()) {
throw new Error(`Could not find ${packageJson.short}`)
Expand Down
6 changes: 4 additions & 2 deletions packages/devnet/devnet-base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import portManager, { waitPort } from '@hackbg/port'
import { Path, SyncFS, FileFormat } from '@hackbg/file'
import { timestamp, assign, Console, Compute, Connection, Identity, Backend, Token } from '@fadroma/agent'
import {
timestamp, assign, Console, CompiledCode, Connection, Identity, Backend, Token
} from '@fadroma/agent'
import type { Address, CodeId, Uint128 } from '@fadroma/agent'
import * as OCI from '@fadroma/oci'
import * as Impl from './devnet-impl'
Expand Down Expand Up @@ -59,7 +61,7 @@ export class DevnetContainerConfig {
/** Initial accounts. */
genesisAccounts: Record<Address, number|bigint|Uint128> = {}
/** Initial uploads. */
genesisUploads: Record<CodeId, Partial<Compute.CompiledCode>> = {}
genesisUploads: Record<CodeId, Partial<CompiledCode>> = {}
/** If set, overrides the script that launches the devnet in the container. */
initScript: Path = new SyncFS.File(packageRoot, 'dockerfiles', 'devnet.init.mjs')
/** Function that waits for port to open after launching container.
Expand Down
2 changes: 1 addition & 1 deletion packages/oci/oci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hideProperties as hide } from '@hackbg/hide'
import { Writable, Transform } from 'node:stream'
import { basename, dirname } from 'node:path'
import Docker from 'dockerode'
import { assign, bold, colors, randomColor, Chain, Connection, Agent, SigningConnection, Compute } from '@fadroma/agent'
import { assign, bold, colors, randomColor, Chain, Connection, Agent, SigningConnection } from '@fadroma/agent'
import { Error, Console } from './oci-base'
import type { DockerHandle } from './oci-base'
import * as Mock from './oci-mock'
Expand Down
Loading

0 comments on commit 44982b5

Please sign in to comment.