Skip to content

Commit

Permalink
wip 31
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 1, 2023
1 parent c435710 commit df8e0cb
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 217 deletions.
2 changes: 1 addition & 1 deletion agent/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function assign <T extends {}> (
assign.allowed = new Map<string, Set<string|number|symbol>>()

/** Add properties to the allow list for a given value object class. */
assign.allow = <T>(name: string, props: Array<keyof T>) => {
assign.allow = <T>(name: string, props: Array<keyof Omit<T, symbol>>) => {
const allowedProperties = assign.allowed.get(name) || new Set()
for (const prop of props) allowedProperties.add(prop)
assign.allowed.set(name, allowedProperties)
Expand Down
15 changes: 9 additions & 6 deletions agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ export abstract class Agent {
`msec: code id`,
bold(String(result.codeId)),
`on chain`,
bold(result.chainId)
bold(result.chainId),
`from code hash`,
bold(result.codeHash)
)
return new UploadedCode({
...template, ...result
Expand Down Expand Up @@ -309,13 +311,14 @@ export abstract class Agent {
initMsg: await into(options.initMsg)
})
const t1 = performance.now() - t0
this.log.debug(
this.log.log(
`Instantiated in`,
bold(t1.toFixed(3)),
`msec: code id`,
`msec:`,
bold(String(options.label)),
`(${bold(result.address)})`,
`from code id`,
bold(String(contract.codeId)),
`address`,
bold(result.address)
)
return new ContractInstance({
...options, ...result
Expand All @@ -335,7 +338,7 @@ export abstract class Agent {
const t0 = performance.now()
const result = await this.doExecute(contract as { address: Address }, message, options)
const t1 = performance.now() - t0
this.log.debug(
this.log.log(
`Executed in`,
bold(t1.toFixed(3)),
`msec: address`,
Expand Down
1 change: 1 addition & 0 deletions agent/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export default async function testClient () {
assert.throws(()=>new ContractClient({}, agent).query({}))
assert.throws(()=>new ContractClient({}).execute({}))
assert.throws(()=>new ContractClient({}, agent).execute({}))
assert.throws(()=>new ContractClient({}, {} as any).execute({}))
}

163 changes: 89 additions & 74 deletions agent/code.test.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,110 @@
/** 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 assert from 'node:assert'
import { ContractCode, SourceCode, CompiledCode, UploadedCode } from './code'
import assert, { rejects, deepEqual, equal } from 'node:assert'
import { ContractCode, SourceCode, RustSourceCode, CompiledCode, UploadedCode } from './code'
import * as Stub from './stub'
import { fixture } from '../fixtures/fixtures'

export default async function testContracts () {
const contract1 = new ContractCode({
source: {}, compiler: {} as any, compiled: {}, uploader: {} as any, uploaded: {},
})

const contract2 = new ContractCode({
source: {}, compiled: {}, uploaded: {},
})
import { Suite } from '@hackbg/ensuite'
export default new Suite([
['compiler', testCodeCompiler],
['units', testCodeUnits],
['contract', testCodeContract]
])

assert(contract2.source instanceof SourceCode)
assert(contract2.compiled instanceof CompiledCode)
assert(contract2.uploaded instanceof UploadedCode)

assert(!(contract2.source.isValid()))
assert(!(contract2.compiled.isValid()))
assert(!(contract2.uploaded.isValid()))

// can't compile missing code
assert.rejects(()=>contract2.compile())
const validSource = new class extends SourceCode { isValid () { return true } }
const invalidSource = new class extends SourceCode { isValid () { return false } }
const brokenCompiler = { build: () => Promise.resolve({ isValid: () => false }) }
assert.rejects(
()=>new ContractCode({ source: validSource }).compile({ compiler: brokenCompiler as any })
)
assert.rejects(
()=>new ContractCode({ source: invalidSource }).compile({ compiler: new Stub.Compiler() })
)
assert.ok(
new ContractCode({ source: validSource }).compile({ compiler: new Stub.Compiler() })
)
export async function testCodeCompiler () {
assert((await new Stub.Compiler().build('')) instanceof CompiledCode)
assert((await new Stub.Compiler().buildMany([{}]))[0] instanceof CompiledCode)
}

// can't upload missing code
assert.rejects(
()=>contract2.upload()
)
assert.rejects(()=>contract2.upload({
uploader: new Stub.Agent()
}))
assert.rejects(()=>contract2.upload({
uploader: { upload: () => Promise.resolve({ isValid: () => false }) } as any
}))
export async function testCodeUnits () {
const source1 = new SourceCode()
deepEqual(source1.toReceipt(), {
sourcePath: undefined,
sourceRepo: undefined,
sourceRef: undefined,
sourceDirty: undefined
})
assert(!source1.isValid())
console.log(source1)
source1.sourcePath = 'foo'
assert(source1.isValid())

assert.deepEqual(contract2.source.toReceipt(), {
crate: undefined,
dirty: undefined,
features: undefined,
repository: undefined,
revision: undefined,
workspace: undefined,
const rustSource1 = new RustSourceCode()
deepEqual(rustSource1.toReceipt(), {
sourcePath: undefined,
sourceRepo: undefined,
sourceRef: undefined,
sourceDirty: undefined,
cargoWorkspace: undefined,
cargoCrate: undefined,
cargoFeatures: undefined,
})
assert.deepEqual(contract2.compiled.toReceipt(), {
console.log(rustSource1)
assert(!rustSource1.isValid())
rustSource1.sourcePath = 'foo'
assert(rustSource1.isValid())
rustSource1.cargoWorkspace = 'bar'
assert(!rustSource1.isValid())
rustSource1.cargoCrate = 'baz'
assert(rustSource1.isValid())

const compiled1 = new CompiledCode()
deepEqual(compiled1.toReceipt(), {
codeHash: undefined,
codePath: undefined,
buildInfo: undefined,
})
assert.deepEqual(contract2.uploaded.toReceipt(), {
console.log(compiled1)
assert(!compiled1.isValid())
compiled1.codePath = fixture('empty.wasm')
assert(compiled1.isValid())
await(compiled1.computeHash())

const uploaded1 = new UploadedCode()
deepEqual(uploaded1.toReceipt(), {
codeHash: undefined,
chainId: undefined,
codeId: undefined,
uploadBy: undefined,
uploadTx: undefined
})
console.log(uploaded1)
assert(!uploaded1.isValid())
uploaded1.chainId = 'foo'
uploaded1.codeId = 'bar'
assert(uploaded1.isValid())
}

assert(contract2.source[Symbol.toStringTag] || true)
assert(contract2.compiled[Symbol.toStringTag] || true)
//assert(contract2.uploaded[Symbol.toStringTag])
//assert(contract2.instance[Symbol.toStringTag])

assert.rejects(()=>new CompiledCode().fetch())

assert.rejects(()=>new CompiledCode({
codePath: ''
}).fetch())

assert.rejects(()=>new CompiledCode({
codePath: new URL('', 'file:')
}).fetch())

assert.rejects(()=>new CompiledCode({
codePath: new URL('http://foo.bar')
}).fetch())

assert.rejects(()=>new CompiledCode({
codePath: 0 as any
}).fetch())

export async function testCodeContract () {
const contract1 = new ContractCode({
source: new SourceCode(),
compiled: new CompiledCode(),
uploaded: new UploadedCode()
})
assert(contract1.source instanceof SourceCode)
assert(contract1.compiled instanceof CompiledCode)
assert(contract1.uploaded instanceof UploadedCode)
// can't compile missing code
rejects(()=>contract1.compile())
const validSource = new class extends SourceCode { isValid () { return true } }
const invalidSource = new class extends SourceCode { isValid () { return false } }
const brokenCompiler: any = { build: () => Promise.resolve({ isValid: () => false }) }
rejects(()=>new ContractCode({source: validSource}).compile({compiler: brokenCompiler}))
rejects(()=>new ContractCode({source: invalidSource}).compile({compiler: new Stub.Compiler()}))
assert(new ContractCode({ source: validSource }).compile({ compiler: new Stub.Compiler() }))
// can't upload missing code
rejects(()=>contract1.upload())
rejects(()=>contract1.upload({uploader: new Stub.Agent()}))
rejects(()=>contract1.upload({uploader: {upload: () => Promise.resolve({ isValid: () => false })} as any}))
assert(contract1.source[Symbol.toStringTag] || true)
assert(contract1.compiled[Symbol.toStringTag] || true)
//assert(contract1.uploaded[Symbol.toStringTag])
//assert(contract1.instance[Symbol.toStringTag])
rejects(()=>new CompiledCode().fetch())
rejects(()=>new CompiledCode({ codePath: '' }).fetch())
rejects(()=>new CompiledCode({ codePath: new URL('', 'file:') }).fetch())
rejects(()=>new CompiledCode({ codePath: new URL('http://foo.bar') }).fetch())
rejects(()=>new CompiledCode({ codePath: 0 as any }).fetch())
}
Loading

0 comments on commit df8e0cb

Please sign in to comment.