Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
staticcall plus sha256 and ripemd160 precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
androlo committed May 25, 2018
1 parent fc13c4f commit 55952f5
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 56 deletions.
48 changes: 48 additions & 0 deletions __tests__/evm.test.js
Expand Up @@ -2334,6 +2334,22 @@ describe('solidity contracts', function () {
}
});
}); });
it('should call test function on TestContractCreateAndStaticCall', function () { return __awaiter(_this, void 0, void 0, function () {
var code, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
code = io_1.readText(path.join(constants_1.BIN_OUTPUT_PATH, 'TestContractCreateAndStaticCall.bin-runtime'));
return [4 /*yield*/, adapter_1.execute(code, constants_1.CONTRACT_TEST_SIG)];
case 1:
result = _a.sent();
//console.log(result);
expect(result.errno).toBe(constants_1.NO_ERROR);
expect(result.returnData).toBe('0000000000000000000000000000000000000000000000000000000000000003');
return [2 /*return*/];
}
});
}); });
it('should call test function on TestContractCallchainSameContract', function () { return __awaiter(_this, void 0, void 0, function () {
var code, result;
return __generator(this, function (_a) {
Expand Down Expand Up @@ -2541,6 +2557,38 @@ describe('solidity contracts', function () {
}
});
}); });
it('should call test function on TestContractPrecompileSha256', function () { return __awaiter(_this, void 0, void 0, function () {
var code, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
code = io_1.readText(path.join(constants_1.BIN_OUTPUT_PATH, 'TestContractPrecompileSha256.bin-runtime'));
return [4 /*yield*/, adapter_1.execute(code, constants_1.CONTRACT_TEST_SIG)];
case 1:
result = _a.sent();
//prettyPrintResults(result);
expect(result.errno).toBe(constants_1.NO_ERROR);
expect(result.returnData).toBe('66840dda154e8a113c31dd0ad32f7f3a366a80e8136979d8f5a101d3d29d6f72');
return [2 /*return*/];
}
});
}); });
it('should call test function on TestContractPrecompileRipemd160', function () { return __awaiter(_this, void 0, void 0, function () {
var code, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
code = io_1.readText(path.join(constants_1.BIN_OUTPUT_PATH, 'TestContractPrecompileRipemd160.bin-runtime'));
return [4 /*yield*/, adapter_1.execute(code, constants_1.CONTRACT_TEST_SIG)];
case 1:
result = _a.sent();
//prettyPrintResults(result);
expect(result.errno).toBe(constants_1.NO_ERROR);
expect(result.returnData).toBe('c9883eece7dca619b830dc9d87e82c38478111c0000000000000000000000000');
return [2 /*return*/];
}
});
}); });
});
describe('solidity contracts - advanced', function () {
it('should call test function on TestContractEVMStack', function () { return __awaiter(_this, void 0, void 0, function () {
Expand Down
58 changes: 57 additions & 1 deletion __tests__/evm.test.ts
Expand Up @@ -34,7 +34,7 @@ import {
ROOT_PATH,
SDIV, SGT, SLOAD, SLT,
SMOD, SOL_ETH_SRC,
SRC_PATH, SSTORE, STOP,
SRC_PATH, SSTORE, STATICCALL, STOP,
SUB,
SWAP1,
SWAP10,
Expand Down Expand Up @@ -83,6 +83,7 @@ const runTest = async (code, data, resExpected) => {
return result;
};


describe('single instructions', async () => {

describe('stop and arithmetic ops', () => {
Expand Down Expand Up @@ -1557,8 +1558,38 @@ describe('single instructions', async () => {

});

/*
it('ecrecover', async () => {
const code = PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '00' + PUSH1 + '01' + PUSH1 + '00' + STATICCALL;
const data = "";
const result = await execute(code, data);
prettyPrintResults(result);
});
it('sha256', async () => {
const code = PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '00' + PUSH1 + '02' + PUSH1 + '00' + STATICCALL;
const data = "";
const result = await execute(code, data);
prettyPrintResults(result);
});
it('ripemd160', async () => {
const code = PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '00' + PUSH1 + '03' + PUSH1 + '00' + STATICCALL;
const data = "";
const result = await execute(code, data);
prettyPrintResults(result);
});
it('identity', async () => {
const code = PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '20' + PUSH1 + '00' + PUSH1 + '04' + PUSH1 + '00' + STATICCALL;
const data = "";
const result = await execute(code, data);
prettyPrintResults(result);
});
*/
});


describe('solidity contracts', () => {

it('should call test function on TestContractNoop', async () => {
Expand Down Expand Up @@ -1651,6 +1682,14 @@ describe('solidity contracts', () => {
expect(result.returnData).toBe('0000000000000000000000000000000000000000000000000000000000000003');
});

it('should call test function on TestContractCreateAndStaticCall', async () => {
const code = readText(path.join(BIN_OUTPUT_PATH, 'TestContractCreateAndStaticCall.bin-runtime'));
const result = await execute(code, CONTRACT_TEST_SIG);
//console.log(result);
expect(result.errno).toBe(NO_ERROR);
expect(result.returnData).toBe('0000000000000000000000000000000000000000000000000000000000000003');
});

it('should call test function on TestContractCallchainSameContract', async () => {
const code = readText(path.join(BIN_OUTPUT_PATH, 'TestContractCallchainSameContract.bin-runtime'));
const result = await execute(code, CONTRACT_TEST_SIG);
Expand Down Expand Up @@ -1789,8 +1828,25 @@ describe('solidity contracts', () => {
expect(log2.data).toBe("0000000000000000000000000101010101010101010101010101010101010101");
});

it('should call test function on TestContractPrecompileSha256', async () => {
const code = readText(path.join(BIN_OUTPUT_PATH, 'TestContractPrecompileSha256.bin-runtime'));
const result = await execute(code, CONTRACT_TEST_SIG);
//prettyPrintResults(result);
expect(result.errno).toBe(NO_ERROR);
expect(result.returnData).toBe('66840dda154e8a113c31dd0ad32f7f3a366a80e8136979d8f5a101d3d29d6f72');
});

it('should call test function on TestContractPrecompileRipemd160', async () => {
const code = readText(path.join(BIN_OUTPUT_PATH, 'TestContractPrecompileRipemd160.bin-runtime'));
const result = await execute(code, CONTRACT_TEST_SIG);
//prettyPrintResults(result);
expect(result.errno).toBe(NO_ERROR);
expect(result.returnData).toBe('c9883eece7dca619b830dc9d87e82c38478111c0000000000000000000000000');
});

});


describe('solidity contracts - advanced', () => {

it('should call test function on TestContractEVMStack', async () => {
Expand Down
1 change: 1 addition & 0 deletions genesis.json
Expand Up @@ -15,4 +15,5 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
}

}
9 changes: 0 additions & 9 deletions script/adapter.js
Expand Up @@ -239,15 +239,6 @@ exports.printStack = function (stack) {
console.log("" + elem.toString(16));
}
};
/*
errno: new BigNumber(dec['0']).toNumber(),
errpc: new BigNumber(dec['1']).toNumber(),
returnData: returnData,
stack: stack,
mem: mem,
accounts: accs,
logs: logs
*/
exports.prettyPrintResults = function (result) {
var resultF = {};
resultF['errno'] = result.errno;
Expand Down
11 changes: 1 addition & 10 deletions script/adapter.ts
Expand Up @@ -220,15 +220,6 @@ export const printStack = (stack) => {
}
};

/*
errno: new BigNumber(dec['0']).toNumber(),
errpc: new BigNumber(dec['1']).toNumber(),
returnData: returnData,
stack: stack,
mem: mem,
accounts: accs,
logs: logs
*/
export const prettyPrintResults = (result) => {
const resultF = {};
resultF['errno'] = result.errno;
Expand Down Expand Up @@ -277,4 +268,4 @@ export const prettyPrintResults = (result) => {
resultF['logs'] = logsF;

console.log(JSON.stringify(resultF, null, '\t'));
};
};
1 change: 1 addition & 0 deletions src/EVMConstants.sol
Expand Up @@ -177,6 +177,7 @@ contract EVMConstants {
uint constant internal ERROR_INSUFFICIENT_FUNDS = 0x08;
uint constant internal ERROR_CONTRACT_CREATION_COLLISION = 0x09;
uint constant internal ERROR_MAX_CODE_SIZE_EXCEEDED = 0x0a;
uint constant internal ERROR_ILLEGAL_WRITE_OPERATION = 0x0b;

// ENV

Expand Down
7 changes: 7 additions & 0 deletions src/EVMUtils.slb
Expand Up @@ -14,6 +14,13 @@ library EVMUtils {
data = data >> 8 * (32 - numBytes);
}

function fromUint(uint x) internal pure returns (bytes memory bts) {
bts = new bytes(32);
assembly {
mstore(add(bts, 0x20), x)
}
}

function copy(bytes memory src, bytes memory dest, uint srcIdx, uint destIdx, uint len) internal pure {
assert(srcIdx + len <= src.length);
assert(destIdx + len <= dest.length);
Expand Down

0 comments on commit 55952f5

Please sign in to comment.