Skip to content

Commit

Permalink
wip 34.5
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 3, 2023
1 parent b01024c commit 80da55b
Show file tree
Hide file tree
Showing 30 changed files with 1,743 additions and 1,766 deletions.
10 changes: 7 additions & 3 deletions agent/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ export async function testUnauthenticated () {

assert(chain.contract() instanceof ContractClient)

assert(await chain.getCodeId(''))
assert(await chain.getCodeHashOfAddress(''))
assert(await chain.getCodeHashOfCodeId(''))
const state = new Stub.ChainState()
state.uploads.set("123", { codeHash: "abc", codeData: new Uint8Array() })

Check failure on line 54 in agent/chain.test.ts

View workflow job for this annotation

GitHub Actions / Create and build a project by compiled checkout

Argument of type '{ codeHash: string; codeData: Uint8Array; }' is not assignable to parameter of type '{ chainId: string; codeId: string; codeHash: string; codeData: Uint8Array; }'.
state.instances.set("stub1abc", { codeId: "123" })
chain = new Stub.Agent({ state })
assert.equal(await chain.getCodeId('stub1abc'), "123")
assert.equal(await chain.getCodeHashOfAddress('stub1abc'), "abc")
assert.equal(await chain.getCodeHashOfCodeId('123'), "abc")
}

export async function testAuthenticated () {
Expand Down
6 changes: 3 additions & 3 deletions agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { CodeHash, CodeId } from './code'
import { CompiledCode, UploadedCode } from './code'
import { ContractInstance, } from './deploy'
import { ContractClient, ContractClientClass } from './client'
import type { DevnetHandle } from './devnet'
import type { Devnet } from './devnet'
import { assignDevnet } from './devnet'

/** A chain can be in one of the following modes: */
Expand Down Expand Up @@ -69,7 +69,7 @@ export abstract class Agent {
fees?: { send?: IFee, upload?: IFee, init?: IFee, exec?: IFee }

/** If this is a devnet, this contains an interface to the devnet container. */
devnet?: DevnetHandle
devnet?: Devnet

/** Whether this chain is stopped. */
stopped?: boolean
Expand All @@ -89,7 +89,7 @@ export abstract class Agent {
if (properties?.chainId && properties?.chainId !== properties?.devnet?.chainId) {
this.log.warn('chain.id: ignoring override (devnet)')
}
if (properties?.url && properties?.url.toString() !== properties?.devnet?.url.toString()) {
if (properties?.url && properties?.url.toString() !== properties?.devnet?.url?.toString()) {
this.log.warn('chain.url: ignoring override (devnet)')
}
if (properties?.mode && properties?.mode !== Mode.Devnet) {
Expand Down
6 changes: 3 additions & 3 deletions agent/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ export async function testCodeUnits () {
deepEqual(compiled1.toReceipt(), {
codeHash: undefined,
codePath: undefined,
buildInfo: undefined,
})
console.log(compiled1)
assert(!compiled1.isValid())
compiled1.codePath = fixture('empty.wasm')
assert(compiled1.isValid())
await(compiled1.computeHash())
assert(await compiled1.computeHash())

const uploaded1 = new UploadedCode()
deepEqual(uploaded1.toReceipt(), {
codeHash: undefined,
chainId: undefined,
codeId: undefined,
uploadBy: undefined,
uploadTx: undefined
uploadTx: undefined,
uploadGas: undefined
})
console.log(uploaded1)
assert(!uploaded1.isValid())
Expand Down
21 changes: 11 additions & 10 deletions agent/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ export class Deployment extends Map<Name, DeploymentUnit> {
return unit
}

async build (
options: Parameters<ContractCode["compile"]>[0] = {}
): Promise<Record<CodeHash, CompiledCode & { codeHash: CodeHash }>> {
async build (options: Parameters<ContractCode["compile"]>[0] = {}):
Promise<Record<CodeHash, CompiledCode & { codeHash: CodeHash }>>
{
const building: Array<Promise<CompiledCode & { codeHash: CodeHash }>> = []
for (const [name, unit] of this.entries()) {
console.log({unit})
if (!unit.source?.isValid() && unit.compiled?.isValid()) {
this.log.warn(`Missing source for ${unit.compiled.codeHash}`)
} else {
Expand All @@ -251,9 +250,9 @@ export class Deployment extends Map<Name, DeploymentUnit> {
return built
}

async upload (
options: Parameters<ContractCode["upload"]>[0] = {}
): Promise<Record<CodeId, UploadedCode & { codeId: CodeId }>> {
async upload (options: Parameters<ContractCode["upload"]>[0] = {}):
Promise<Record<CodeId, UploadedCode & { codeId: CodeId }>>
{
const uploading: Array<Promise<UploadedCode & { codeId: CodeId }>> = []
for (const [name, unit] of this.entries()) {
uploading.push(unit.upload(options))
Expand All @@ -265,9 +264,11 @@ export class Deployment extends Map<Name, DeploymentUnit> {
return uploaded
}

async deploy (
options: Parameters<ContractInstance["deploy"]>[0] = {}
): Promise<Record<Address, ContractInstance & { address: Address }>> {
async deploy (options: Parameters<ContractInstance["deploy"]>[0] & {
deployStore?: DeployStore
} = {}):
Promise<Record<Address, ContractInstance & { address: Address }>>
{
const deploying: Array<Promise<ContractInstance & { address: Address }>> = []
for (const [name, unit] of this.entries()) {
if (unit instanceof ContractInstance) {
Expand Down
58 changes: 40 additions & 18 deletions agent/devnet.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
import assert from 'node:assert'
import { Error } from './base'
import * as Stub from './stub'
import { Mode } from './chain'
import { assignDevnet } from './devnet'
import type { Agent } from './chain'
import { Devnet, assignDevnet } from './devnet'

export default async function testDevnet () {
const devnet = {
accounts: [],
chainId: 'foo',
platform: 'bar',
running: false,
stateDir: '/tmp/foo',
url: new URL('http://example.com'),
async start () { return this },
async getAccount () { return {} },
async assertPresence () {}
class MyDevnet extends Devnet {
accounts = []
chainId = 'foo'
platform = 'bar'
running = false
stateDir = '/tmp/foo'
url = new URL('http://example.com')

async start (): Promise<this> {
this.running = true
return this
}

async pause (): Promise<this> {
this.running = false
return this
}

async import (...args: unknown[]): Promise<unknown> {
throw new Error("unimplemented")
}

async export (...args: unknown[]) {
throw new Error("unimplemented")
}

async mirror (...args: unknown[]) {
throw new Error("unimplemented")
}

async getAccount (name: string): Promise<Partial<Agent>> {
return { name }
}
const chain = new Stub.Agent({
mode: Mode.Devnet, chainId: 'bar', url: 'http://asdf.com', devnet,
})
}

export default async function testDevnet () {
const devnet = new MyDevnet()
const chain = new Stub.Agent({ mode: Mode.Devnet, chainId: 'bar', url: 'http://asdf.com', devnet })
// Properties from Devnet are passed onto Chain
assert.equal(chain.devnet, devnet)
//assert.equal(chain.chainId, 'foo')
Expand All @@ -29,9 +54,7 @@ export default async function testDevnet () {
assert.throws(()=>chain.devnet=devnet)
assert.throws(()=>chain.stopped=true)
await chain.authenticate({ name: 'Alice' })

const chain2 = new Stub.Agent({ mode: Mode.Mainnet, devnet })

const agent: any = {}
assignDevnet(agent as any, devnet)
agent.id
Expand All @@ -44,5 +67,4 @@ export default async function testDevnet () {
assert.throws(()=>agent.mode = "")
assert.throws(()=>agent.devnet = "")
assert.throws(()=>agent.stopped = "")

}
51 changes: 32 additions & 19 deletions agent/devnet.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import type { Agent } from './chain'
import { assign } from './base'
import type { Agent, ChainId } from './chain'
import { Mode } from './chain'

/** Interface for Devnet (implementation is in @hackbg/fadroma). */
export interface DevnetHandle {
accounts: string[]
chainId: string
platform: string
running: boolean
stateDir: string
url: URL

containerId?: string
imageTag?: string
port?: string|number

start (): Promise<this>
getAccount (name: string): Promise<Partial<Agent>>
assertPresence (): Promise<void>
export abstract class Devnet {
/** List of genesis accounts that will be given an initial balance
* when creating the devnet container for the first time. */
accounts: Array<string> = [ 'Admin', 'Alice', 'Bob', 'Carol', 'Mallory' ]
/** The chain ID that will be passed to the devnet node. */
chainId?: ChainId
/** Which kind of devnet to launch */
platform?: string
/** Is this thing on? */
running: boolean = false
/** URL for connecting to a remote devnet. */
url?: string|URL

abstract start (): Promise<this>

abstract pause (): Promise<this>

abstract import (...args: unknown[]): Promise<unknown>

abstract export (...args: unknown[]): Promise<unknown>

abstract mirror (...args: unknown[]): Promise<unknown>

abstract getAccount (name: string): Promise<Partial<Agent>>

constructor (properties?: Partial<Devnet>) {
assign(this, properties, ["accounts", "chainId", "platform", "running", "url"])
}
}

export function assignDevnet (agent: Agent, devnet: DevnetHandle) {
export function assignDevnet (agent: Agent, devnet: Devnet) {
Object.defineProperties(agent, {
id: {
enumerable: true, configurable: true,
Expand All @@ -30,7 +43,7 @@ export function assignDevnet (agent: Agent, devnet: DevnetHandle) {
},
url: {
enumerable: true, configurable: true,
get: () => devnet.url.toString(),
get: () => devnet.url?.toString(),
set: () => {
throw new Error("can't override url of devnet")
}
Expand Down
16 changes: 10 additions & 6 deletions agent/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
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 { Console, Error } from './base'
import type { Class, Name } from './base'
import type { Name } from './base'
import type { CodeHash } from './code'
import type { ChainId } from './chain'
import { SourceCode, CompiledCode, UploadedCode } from './code'
import { UploadedCode } from './code'
import { Deployment } from './deploy'
import type { DeploymentClass, DeploymentState } from './deploy'
import type { DeploymentState } from './deploy'

export class UploadStore extends Map<CodeHash, UploadedCode> {
log = new Console('UploadStore')
Expand Down Expand Up @@ -40,8 +39,13 @@ export class DeployStore extends Map<Name, DeploymentState> {
super()
}

get (name: Name): DeploymentState|undefined {
return super.get(name)
selected?: DeploymentState = undefined

get (name?: Name): DeploymentState|undefined {
if (arguments.length === 0) {
return this.selected
}
return super.get(name!)
}

set (name: Name, state: Partial<Deployment>|DeploymentState): this {
Expand Down
Loading

0 comments on commit 80da55b

Please sign in to comment.