Skip to content

Commit

Permalink
kernel: test simple procedure contract
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeOShannessy committed Jun 5, 2019
1 parent 097212d commit f912c3d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion kernel-ewasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"deploy": "node script/deploy.js",
"test": "mocha tests/**/**.js"
"test": "test.sh"
},
"author": "",
"license": "ISC",
Expand Down
9 changes: 5 additions & 4 deletions kernel-ewasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ pub mod token {
// Next we get the code of the contract, using EXTCODECOPY under
// the hood.
let code: pwasm_std::Vec<u8> = self.code_copy(target);
// Next we deserialise the code from Vec<u8> into a Module.
// TODO: this is causing an out of bounds memory access error
// code_slice is magic number and version number only
let code_slice = &[0, 97, 115, 109, 1, 0, 0, 0];
// big_code_slice is magic number, version number and a simple
// data section.
let big_code_slice = &[0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0B, 0x07, 0x01, 0x00, 0x41, 0x01, 0x0B, 0x01, 0x54, 0x00, 0x08, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x02, 0x01, 0x00];

// Next we deserialise the code from Vec<u8> into a Module.
let module: Module = match deserialize_buffer(code.as_slice()) {
// let module: Module = match deserialize_buffer(code_slice) {
Ok(module) => module,
Expand All @@ -145,7 +146,7 @@ pub mod token {
};
// // Then we perform a boolen is_valid() check.
module.is_valid();
true
false
}
}

Expand Down
71 changes: 43 additions & 28 deletions kernel-ewasm/tests/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,32 @@ async function newKernelInstance() {
}

async function deployContract(interfacePath, codePath) {
// Create Account
const newAccount = await createAccount(DEFAULT_ACCOUNT.NAME, DEFAULT_ACCOUNT.PASSWORD);
const accounts = await web3.eth.personal.getAccounts()
if (accounts.length == 0) throw `Got zero accounts`;
const account = web3.utils.toChecksumAddress(accounts[0], web3.utils.hexToNumber(CHAIN_CONFIG.params.networkId));
web3.eth.defaultAccount = account;
// read JSON ABI
const abi = JSON.parse(fs.readFileSync(path.resolve(interfacePath)));
// convert Wasm binary to hex format
const codeHex = '0x' + fs.readFileSync(path.resolve(codePath)).toString('hex');
const Contract = new web3.eth.Contract(abi, null, { data: codeHex, from: account, transactionConfirmationBlocks: 1 });
const DeployTransaction = Contract.deploy({ data: codeHex, arguments: [] });
await web3.eth.personal.unlockAccount(accounts[0], "user", null)
let gas = await DeployTransaction.estimateGas()
let contract_tx = DeployTransaction.send({ gasLimit: gas, from: account })
let tx_hash = await new Promise((res, rej) => contract_tx.on('transactionHash', res).on('error', rej));
let tx_receipt = await web3.eth.getTransactionReceipt(tx_hash);
let contract_addr = tx_receipt.contractAddress;
let contract = Contract.clone();
contract.address = contract_addr;
return contract;
try {

// Create Account
const newAccount = await createAccount(DEFAULT_ACCOUNT.NAME, DEFAULT_ACCOUNT.PASSWORD);
const accounts = await web3.eth.personal.getAccounts()
if (accounts.length == 0) throw `Got zero accounts`;
const account = web3.utils.toChecksumAddress(accounts[0], web3.utils.hexToNumber(CHAIN_CONFIG.params.networkId));
web3.eth.defaultAccount = account;
// read JSON ABI
const abi = JSON.parse(fs.readFileSync(path.resolve(interfacePath)));
// convert Wasm binary to hex format
const codeHex = '0x' + fs.readFileSync(path.resolve(codePath)).toString('hex');
const Contract = new web3.eth.Contract(abi, null, { data: codeHex, from: account, transactionConfirmationBlocks: 1 });
const DeployTransaction = Contract.deploy({ data: codeHex, arguments: [] });
await web3.eth.personal.unlockAccount(accounts[0], "user", null)
let gas = await DeployTransaction.estimateGas()
let contract_tx = DeployTransaction.send({ gasLimit: gas, from: account })
let tx_hash = await new Promise((res, rej) => contract_tx.on('transactionHash', res).on('error', rej));
let tx_receipt = await web3.eth.getTransactionReceipt(tx_hash);
let contract_addr = tx_receipt.contractAddress;
let contract = Contract.clone();
contract.address = contract_addr;
return contract;
} catch (e) {
throw new Error(e);
}
}

describe('Kernel', function() {
Expand Down Expand Up @@ -162,12 +167,6 @@ describe('Kernel', function() {
let rec_validation = await kernel.methods.get_code_size(kernelAddress).call();
assert.strictEqual(typeof rec_validation, "number")
})
it('should return false when trying to validate the kernel itself', async function() {
const kernelAddress = kernel.options.address;
assert(web3.utils.isAddress(kernelAddress), "The kernel address should be a valid address")
let rec_validation = await kernel.methods.check_contract(kernelAddress).call();
assert.strictEqual(rec_validation, false)
})

it('should copy the code of the kernel', async function() {
const kernelAddress = kernel.options.address;
Expand All @@ -178,8 +177,24 @@ describe('Kernel', function() {
assert.strictEqual(code_size, code.length, "The code length should be as given by EXTCODESIZE");
})

it('should return false when trying to validate the kernel itself', async function() {
const kernelAddress = kernel.options.address;
assert(web3.utils.isAddress(kernelAddress), "The kernel address should be a valid address")
let rec_validation = await kernel.methods.check_contract(kernelAddress).call();
assert.strictEqual(rec_validation, false)
})

it('should copy the code of an example contract', async function() {
const contract = await deployContract("example_contract_2/build/ExampleContract2Interface.json", "example_contract_2/build/example_contract_2.wasm");
assert(web3.utils.isAddress(contract.address), "The contract address should be a valid address")
// const code_size = await kernel.methods.get_code_size(contract.address).call();
// const code_hex = await kernel.methods.code_copy(contract.address).call();
// const code = web3.utils.hexToBytes(code_hex);
// assert.strictEqual(code_size, code.length, "The code length should be as given by EXTCODESIZE");
})

it('should correctly validate an example contract', async function() {
const contract = await deployContract("example_contract_1/build/ExampleContract1Interface.json", "example_contract_1/build/example_contract_1.wasm");
const contract = await deployContract("example_contract_2/build/ExampleContract2Interface.json", "example_contract_2/build/example_contract_2.wasm");
assert(web3.utils.isAddress(contract.address), "The contract address should be a valid address")
const code_size = await kernel.methods.get_code_size(contract.address).call();
const code_hex = await kernel.methods.code_copy(contract.address).call();
Expand Down

0 comments on commit f912c3d

Please sign in to comment.