generated from spknetwork/js-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: block creation testing cycle 1, more in comments
- Submit offchain TX over API for new testnet (still in progress) - Smart contract execution & hook in with webassembly VM - Offchain TX support (wip) - Add deserialize function for BlsCiruit - Custom webassembly compiler to handle embedded async - Changes to WASM VM imports structure - Introduction of VSC smart contract SDK - New smart contract deployment script - MVP block creation & verification (no ingestion pipeline yet/inflow of blocks) - Smart address indexing wip
- Loading branch information
Showing
19 changed files
with
1,110 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { PrivateKey } from "@hiveio/dhive" | ||
import { HiveClient } from "../../utils" | ||
import { CoreService } from "../../services" | ||
|
||
const contractId = 'vs41q9c3ygy36jwdd06qe4wgmhxm8m3dxvapy69ckdgn6tp6esuusvd7tupu7smvypyg' | ||
|
||
void (async () => { | ||
const core = new CoreService({ | ||
prefix: 'manual tx core', | ||
printMetadata: true, | ||
level: 'debug', | ||
mode: 'lite' | ||
}) | ||
|
||
await core.start() | ||
|
||
const broadcast = await HiveClient.broadcast.json({ | ||
|
||
required_auths: [], | ||
required_posting_auths: [process.env.HIVE_ACCOUNT], | ||
id: "vsc.tx", | ||
json: JSON.stringify({ | ||
net_id: core.config.get('network.id'), | ||
__v: '0.1', | ||
__t: 'native', | ||
data: { | ||
op: 'call_contract', | ||
action: 'testJSON', | ||
contract_id: contractId, | ||
payload: { | ||
testData: "hello-world" | ||
} | ||
} | ||
}) | ||
}, PrivateKey.fromString(process.env.HIVE_ACCOUNT_POSTING)) | ||
console.log(broadcast) | ||
process.exit() | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { Collection } from "mongodb"; | ||
import { NewCoreService } from "."; | ||
import { AddrRecord } from "./types"; | ||
import { encodePayload } from 'dag-jose-utils' | ||
import { bech32 } from "bech32"; | ||
import { VmContainer } from "./vm/utils"; | ||
|
||
|
||
enum ContractErrors { | ||
success = 0, | ||
invalid_action = -1, | ||
runtime_error = -2 | ||
} | ||
|
||
export class ContractEngineV2 { | ||
self: NewCoreService; | ||
addrsDb: Collection<AddrRecord>; | ||
contractDb: Collection<{ | ||
id: string | ||
code: string | ||
name: string | ||
description:string | ||
creator: string | ||
}>; | ||
|
||
constructor(self: NewCoreService) { | ||
this.self = self; | ||
|
||
|
||
this.blockTick = this.blockTick.bind(this) | ||
} | ||
|
||
async blockTick([opPayload, tx]) { | ||
console.log('opPayload, tx', opPayload, tx) | ||
for(let index in tx.operations) { | ||
const [opName, op] = tx.operations[index] | ||
const json = JSON.parse(op.json) | ||
|
||
console.log('OPPAYLOAD DATA INSERT', op, opName) | ||
if(op.id === "vsc.create_contract") { | ||
const contractIdHash = (await encodePayload({ | ||
ref_id: tx.transaction_id, | ||
index | ||
})).cid | ||
|
||
const bech32Addr = bech32.encode('vs4', bech32.toWords(contractIdHash.bytes)); | ||
|
||
console.log('smart contract addr', bech32Addr) | ||
await this.contractDb.findOneAndUpdate({ | ||
id: bech32Addr | ||
}, { | ||
$set: { | ||
code: json.code, | ||
name: json.name, | ||
description: json.description, | ||
creator: opPayload.required_auths[0], | ||
state_merkle: (await this.self.ipfs.object.new({ template: 'unixfs-dir' })).toString(), | ||
ref_id: tx.transaction_id | ||
} | ||
}, { | ||
upsert: true | ||
}) | ||
} | ||
} | ||
} | ||
|
||
async init() { | ||
this.addrsDb = this.self.db.collection('addrs') | ||
this.contractDb = this.self.db.collection('contracts') | ||
this.self.chainBridge.registerTickHandle('contract-engine', this.blockTick, { | ||
type: 'tx' | ||
}) | ||
} | ||
|
||
|
||
async createContractOutput(args: { | ||
txs: any | ||
contract_id: string | ||
}) { | ||
const contractInfo = await this.contractDb.findOne({ | ||
id: args.contract_id | ||
}) | ||
if(!contractInfo) { | ||
throw new Error('Contract not registered with node or does not exist') | ||
} | ||
|
||
|
||
if(args.txs.length === 0) { | ||
return null; | ||
} | ||
|
||
const vm = new VmContainer({ | ||
state_merkle: (contractInfo as any).state_merkle, | ||
cid: contractInfo.code, | ||
contract_id: args.contract_id | ||
}) | ||
|
||
await vm.init() | ||
await vm.onReady() | ||
|
||
const txResults = [] | ||
|
||
for(let tx of args.txs) { | ||
const result = await vm.call({ | ||
action: tx.data.action, | ||
payload: JSON.stringify(tx.data.payload) | ||
}) | ||
|
||
|
||
let ret | ||
let code | ||
let msg | ||
if(result.ret) { | ||
const parsedResult: { | ||
msg?: string | ||
code: number | ||
ret?: string | ||
} = JSON.parse((result as any).ret); | ||
ret = parsedResult.ret, | ||
code = parsedResult.code | ||
msg = parsedResult.msg | ||
} | ||
console.log('parsed result', result) | ||
txResults.push({ | ||
ret: ret, | ||
code: code || result.errorType, | ||
logs: (result as any).logs, | ||
//Dont store gas usage if 0 | ||
...(result.IOGas > 0 ? {gas: result.IOGas} : {}) | ||
}) | ||
} | ||
const state_merkle = await vm.finishAndCleanup() | ||
console.log('finishing and cleaning up') | ||
|
||
const returnObj = { | ||
input_map: args.txs.map(e => e.id), | ||
state_merkle, | ||
results: txResults | ||
} | ||
|
||
console.log('returnObj', returnObj) | ||
|
||
return returnObj | ||
} | ||
|
||
async start() { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.