Skip to main content

Test case: virtual regions

A virtual region holds a computed value instead of stored bytes. This test computes two values from a uint256 in storage: x, the value plus one, and tag, the first 4 bytes of the value's hash.

Test source​

/**
* the computed values example: `x` (`a + 1`) and `tag` (the first 4 bytes
* of the hash of `a`) exist only as virtual regions computed from `a`
*/
const virtualRegionsTest: ObserveTraceTest<{ x: number; tag: string }> = {
pointer: findExamplePointer("virtual-operand"),
compileOptions: singleSourceCompilation({
path: "VirtualRegions.sol",
contractName: "VirtualRegions",
content: `contract VirtualRegions {
uint256 a;

constructor() {
a = 2;
}
}
`,
}),

expectedValues: [
{ x: 1, tag: "0x290decd9" },
{ x: 3, tag: "0x405787fa" },
],

async observe({ regions, read }) {
const x = Number((await read(regions.lookup["x"])).asUint());

const tag = (await read(regions.lookup["tag"])).toHex();

return { x, tag };
},

equals(a, b) {
return a.x === b.x && a.tag === b.tag;
},
};

Tested pointer​

# example computed values: `x = a + 1` after the optimizer removed `x`,
# and `tag`, the first 4 bytes of the hash of `a`, where `a` is a
# `uint256` in storage slot 0
group:
- name: "virtual-operand"
location: storage
slot: 0

- name: "x"
location: virtual
expression:
~wordsized:
~sum:
- ~read: "virtual-operand"
- 1

- name: "tag"
location: virtual
expression:
~keccak256:
- ~read: "virtual-operand"
length: 4