Skip to content

Commit

Permalink
feat: async executor
Browse files Browse the repository at this point in the history
  • Loading branch information
krigga authored Mar 1, 2024
1 parent 8bc68c2 commit 8159c51
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/blockchain/Blockchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Blockchain', () => {
printTransactionFees(res.transactions)

let nft = await blockchain.getContract(Address.parse('EQDTbyyOixs9JsO8bmHjk9WJYN8deL-qJeNZvWx147pM8qeO'))
let data = nft.get('get_nft_data')
let data = await nft.get('get_nft_data')

let [, , , owner] = [data.stackReader.pop(), data.stackReader.pop(), data.stackReader.pop(), data.stackReader.readAddress()]

Expand Down Expand Up @@ -218,7 +218,7 @@ describe('Blockchain', () => {
// Current time in receiveMessage should match blockchain.now
const nowSmc = await blockchain.getContract(contract.address)

let smcRes = nowSmc.receiveMessage(internal({
let smcRes = await nowSmc.receiveMessage(internal({
from: sender.address,
to: nowSmc.address,
body: beginCell().storeUint(0, 32).storeAddress(sender.address).endCell(),
Expand All @@ -234,7 +234,7 @@ describe('Blockchain', () => {

// Make sure now is still overridable in receiveMessage call

smcRes = nowSmc.receiveMessage(internal({
smcRes = await nowSmc.receiveMessage(internal({
from: sender.address,
to: nowSmc.address,
body: beginCell().storeUint(0, 32).storeAddress(sender.address).endCell(),
Expand Down Expand Up @@ -598,11 +598,11 @@ describe('Blockchain', () => {
}))

const smc = await blockchain.getContract(testAddr)
let res = smc.runTickTock('tock')
let res = await smc.runTickTock('tock')
if (res.description.type !== 'tick-tock')
throw new Error('Tick tock transaction expected')
expect(res.description.isTock).toBe(true)
res = smc.runTickTock('tick')
res = await smc.runTickTock('tick')
if (res.description.type !== 'tick-tock')
throw new Error('Tick tock transaction expected')
expect(res.description.isTock).toBe(false)
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class Blockchain {
}

async runGetMethod(address: Address, method: number | string, stack: TupleItem[] = [], params?: GetMethodParams) {
return (await this.getContract(address)).get(method, stack, {
return await (await this.getContract(address)).get(method, stack, {
now: this.now,
...params,
})
Expand Down Expand Up @@ -282,10 +282,10 @@ export class Blockchain {
}

this.currentLt += LT_ALIGN
tx = (await this.getContract(message.info.dest)).receiveMessage(message, params)
tx = await (await this.getContract(message.info.dest)).receiveMessage(message, params)
} else {
this.currentLt += LT_ALIGN
tx = (await this.getContract(message.on)).runTickTock(message.which, params)
tx = await (await this.getContract(message.on)).runTickTock(message.which, params)
}

const transaction: BlockchainTransaction = {
Expand Down
16 changes: 8 additions & 8 deletions src/blockchain/SmartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,27 +253,27 @@ export class SmartContract {
}
}

receiveMessage(message: Message, params?: MessageParams) {
async receiveMessage(message: Message, params?: MessageParams) {
// Sync now with blockchain instance if not specified in parameters
params = {
now: this.blockchain.now,
...params,
}
return this.runCommon(() => this.blockchain.executor.runTransaction({
return await this.runCommon(() => this.blockchain.executor.runTransaction({
...this.createCommonArgs(params),
message: beginCell().store(storeMessage(message)).endCell(),
}))
}

runTickTock(which: TickOrTock, params?: MessageParams) {
return this.runCommon(() => this.blockchain.executor.runTickTock({
async runTickTock(which: TickOrTock, params?: MessageParams) {
return await this.runCommon(() => this.blockchain.executor.runTickTock({
...this.createCommonArgs(params),
which,
}))
}

protected runCommon(run: () => EmulationResult): SmartContractTransaction {
const res = run()
protected async runCommon(run: () => Promise<EmulationResult>): Promise<SmartContractTransaction> {
const res = await run()

if (this.verbosity.print && this.verbosity.blockchainLogs && res.logs.length > 0) {
console.log(res.logs)
Expand Down Expand Up @@ -305,12 +305,12 @@ export class SmartContract {
}
}

get(method: string | number, stack: TupleItem[] = [], params?: GetMethodParams): GetMethodResult {
async get(method: string | number, stack: TupleItem[] = [], params?: GetMethodParams): Promise<GetMethodResult> {
if (this.account.account?.storage.state.type !== 'active') {
throw new Error('Trying to run get method on non-active contract')
}

const res = this.blockchain.executor.runGetMethod({
const res = await this.blockchain.executor.runGetMethod({
code: this.account.account?.storage.state.state.code!,
data: this.account.account?.storage.state.state.data!,
methodId: typeof method === 'string' ? getSelectorForMethod(method) : method,
Expand Down
6 changes: 3 additions & 3 deletions src/executor/Executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ describe('Executor', () => {
executor = await Executor.create()
})

it('should run get method', () => {
it('should run get method', async () => {
let code = Cell.fromBoc(Buffer.from('te6ccsEBAgEAEQANEQEU/wD0pBP0vPLICwEABNOgu3u26g==', 'base64'))[0];
let data = beginCell().endCell()

let res = executor.runGetMethod({
let res = await executor.runGetMethod({
verbosity: 'full_location',
code,
data,
Expand All @@ -30,7 +30,7 @@ describe('Executor', () => {
})

it('should run transaction', async () => {
let res = executor.runTransaction({
let res = await executor.runTransaction({
config: defaultConfig,
libs: null,
verbosity: 'full_location',
Expand Down
14 changes: 10 additions & 4 deletions src/executor/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ class Heap {
}
}

export class Executor {
export interface IExecutor {
runGetMethod(args: GetMethodArgs): Promise<GetMethodResult>
runTickTock(args: RunTickTockArgs): Promise<EmulationResult>
runTransaction(args: RunTransactionArgs): Promise<EmulationResult>
}

export class Executor implements IExecutor {
private module: any
private heap: Heap
private emulator?: {
Expand All @@ -228,7 +234,7 @@ export class Executor {
return ex
}

runGetMethod(args: GetMethodArgs): GetMethodResult {
async runGetMethod(args: GetMethodArgs): Promise<GetMethodResult> {
const params: GetMethodInternalParams = {
code: args.code.toBoc().toString('base64'),
data: args.data.toBoc().toString('base64'),
Expand Down Expand Up @@ -299,7 +305,7 @@ export class Executor {
};
}

runTickTock(args: RunTickTockArgs): EmulationResult {
async runTickTock(args: RunTickTockArgs): Promise<EmulationResult> {
const params: EmulationInternalParams = {
...runCommonArgsToInternalParams(args),
is_tick_tock: true,
Expand All @@ -315,7 +321,7 @@ export class Executor {
])
}

runTransaction(args: RunTransactionArgs): EmulationResult {
async runTransaction(args: RunTransactionArgs): Promise<EmulationResult> {
const params: EmulationInternalParams = runCommonArgsToInternalParams(args)

return this.runCommon([
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export {

export {
TickOrTock,
IExecutor,
Executor,
GetMethodArgs as ExecutorGetMethodArgs,
GetMethodResult as ExecutorGetMethodResult,
RunTickTockArgs as ExecutorRunTickTockArgs,
EmulationResult as ExecutorEmulationResult,
RunTransactionArgs as ExecutorRunTransactionArgs,
ExecutorVerbosity,
} from './executor/Executor';

export {
Expand Down

0 comments on commit 8159c51

Please sign in to comment.