This repository was archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvalidateMeta.ts
More file actions
151 lines (135 loc) · 4.27 KB
/
validateMeta.ts
File metadata and controls
151 lines (135 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { assert } from "chai";
import { BytesLike, concat } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { IInterpreterV1Consumer, Rainterpreter } from "../../../../typechain";
import {
assertError,
max_uint256,
memoryOperand,
MemoryType,
op,
Opcode,
randomUint256,
standardEvaluableConfig,
} from "../../../../utils";
import { rainterpreterDeploy } from "../../../../utils/deploy/interpreter/shared/rainterpreter/deploy";
import deploy1820 from "../../../../utils/deploy/registry1820/deploy";
import { expressionConsumerDeploy } from "../../../../utils/deploy/test/iinterpreterV1Consumer/deploy";
import OpHash from "../../../../contracts/interpreter/ops/crypto/OpHash.opmeta.json";
import { constructByBits, OperandArgs } from "rainlang";
describe("HASH Opcode test", async function () {
let rainInterpreter: Rainterpreter;
let logic: IInterpreterV1Consumer;
before(async () => {
// Deploy ERC1820Registry
const signers = await ethers.getSigners();
await deploy1820(signers[0]);
rainInterpreter = await rainterpreterDeploy();
const consumerFactory = await ethers.getContractFactory(
"IInterpreterV1Consumer"
);
logic = (await consumerFactory.deploy()) as IInterpreterV1Consumer;
await logic.deployed();
});
// get array of a particualr length
const getArrayOfLength = (length: number) => {
let arr = [];
for (let i = 0; i < length; i++) {
arr.push(randomUint256());
}
return arr;
};
// get a int between min and max inclusive
const randomIntFromInterval = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
// build source array from constants array
const buildSources = (constants: Array<number>, op_: number | number[]) => {
let soruceArray = [];
for (let i = 0; i < constants.length; i++) {
soruceArray.push(
op(Opcode.read_memory, memoryOperand(MemoryType.Constant, i))
);
}
soruceArray.push(op(Opcode.hash, op_));
let sources = concat(soruceArray);
return sources;
};
it("should build operand from array of fuzzed? operand values ", async () => {
for (let i = 0; i < 30; i++) {
// get random value for range
let range = randomIntFromInterval(1, 300);
let operandArgs: OperandArgs = [
{
name: "",
bits: [0, 7],
validRange: [[1, range]],
},
];
// array of values that will be build as a single operand
let values = [range];
const constants = getArrayOfLength(range);
// Constructing arguments for the constructByBits function
// ref resolveOp method of rain parser
let constructArgs = operandArgs.map((e, i) => {
return {
value: values[i],
bits: e.bits,
computation: e.computation,
validRange: e.validRange,
};
});
// getting the operand
let op_ = constructByBits(constructArgs);
let source = buildSources(constants, op_);
// Case if operand is zero
if (op_[0] === 0) {
await assertError(
async () =>
await expressionConsumerDeploy(
[source],
constants,
rainInterpreter,
1
),
"OperandUnderflow",
"Underflow"
);
} else if (op_[0] > 255) {
// Case if operand is greater than enforced length
await assertError(
async () =>
await expressionConsumerDeploy(
[source],
constants,
rainInterpreter,
1
),
"OperandOverflow",
"Overflow"
);
} else {
const expression0 = await expressionConsumerDeploy(
[source],
constants,
rainInterpreter,
1
);
await logic["eval(address,uint256,uint256[][])"](
rainInterpreter.address,
expression0.dispatch,
[]
);
const result = await logic.stackTop();
const expectedValue = ethers.utils.solidityKeccak256(
["uint256[]"],
[constants]
);
assert(
result.eq(expectedValue),
`Invalid output, expected ${expectedValue}, actual ${result}`
);
}
}
});
});