-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix Code.getOpcode & Code.toUint, tested
- Loading branch information
Showing
5 changed files
with
132 additions
and
32 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
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,57 @@ | ||
pragma solidity ^0.5.2; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "../EVMCode.slb"; | ||
import "../MemOps.slb"; | ||
|
||
|
||
contract EVMCodeMock { | ||
using EVMCode for EVMCode.Code; | ||
// below is 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f | ||
uint data0 = 1780731860627700044960722568376592200742329637303199754547598369979440671; | ||
// below is 202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f | ||
uint data1 = 14532552714582660066924456880521368950258152170031413196862950297402215317055; | ||
event FoundByte(uint found); | ||
|
||
function testFromArrayGetOpcode() public { | ||
EVMCode.Code memory code; | ||
EVMCode.RawCode[50] memory rawCodes; | ||
|
||
rawCodes[0] = EVMCode.RawCode(0, data0); | ||
rawCodes[1] = EVMCode.RawCode(1, data1); | ||
code = EVMCode.fromArray(rawCodes, 2); | ||
for (uint i = 0; i < 64; i++) { | ||
uint opcode = code.getOpcodeAt(i); | ||
require(opcode == i, 'get wrong opcode'); | ||
} | ||
} | ||
|
||
function testError_FromArrayWrongOrder() public { | ||
EVMCode.Code memory code; | ||
EVMCode.RawCode[50] memory rawCodes; | ||
rawCodes[0] = EVMCode.RawCode(2, 1); | ||
rawCodes[1] = EVMCode.RawCode(1, 1); | ||
code = EVMCode.fromArray(rawCodes, 2); | ||
} | ||
|
||
function testToUint() public { | ||
EVMCode.Code memory code; | ||
EVMCode.RawCode[50] memory rawCodes; | ||
|
||
rawCodes[0] = EVMCode.RawCode(0, data0); | ||
rawCodes[1] = EVMCode.RawCode(1, data1); | ||
code = EVMCode.fromArray(rawCodes, 2); | ||
|
||
uint res = 0; | ||
uint got; | ||
|
||
for (uint pos = 0; pos < 64; pos++) { | ||
res = 0; | ||
for (uint len = 1; (len <= 32) && (pos + len <= 64); len++) { | ||
res = (res << 8) | (pos + len - 1); | ||
got = code.toUint(pos, len); | ||
require(res == got, 'get wrong uint'); | ||
} | ||
} | ||
} | ||
} |
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,24 @@ | ||
const { deployContract } = require('./../helpers/utils'); | ||
|
||
const EVMCodeMock = artifacts.require('EVMCodeMock.sol'); | ||
const assert = require('assert'); | ||
|
||
contract('TestEVMCode', function () { | ||
let evmCode; | ||
|
||
before(async () => { | ||
evmCode = await deployContract(EVMCodeMock); | ||
}); | ||
|
||
it('fromArray', async function () { | ||
await evmCode.testFromArrayGetOpcode(); | ||
}); | ||
|
||
it('fromArray 2 element wrong order', async function () { | ||
await assert.rejects(evmCode.testError_FromArrayWrongOrder()); | ||
}); | ||
|
||
it('toUint', async function () { | ||
await evmCode.testToUint(); | ||
}); | ||
}); |