mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-09 09:14:06 +00:00
Test cases in solidity-mapper for contract, fixed-size byte arrays and enum types (#26)
* Add tests for getStorageInfo and getEventNameTopics. * Lint solidity-mapper package code. * Add test for contract type. * Add test for fixed size byte arrays. * Add test for Enum types. * Add tests for variables packed together and using single slot. * Fix comments in test contracts. Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
co-authored by
nikugogoi
parent
f6870d88dc
commit
b243025ca8
@@ -0,0 +1,33 @@
|
||||
import { expect } from 'chai';
|
||||
import { artifacts, ethers } from 'hardhat';
|
||||
|
||||
import { getEventNameTopics } from './logs';
|
||||
|
||||
const TEST_DATA = [
|
||||
{
|
||||
name: 'TestIntegers',
|
||||
output: {}
|
||||
},
|
||||
{
|
||||
name: 'TestEvents',
|
||||
output: {
|
||||
// Signature of event is a keccak256 hash of event name and input argument types.
|
||||
// keccak256('Event1(string,string)')
|
||||
Event1: '0xead5fc99a8133dbf3f4e87d1ada4e5a4cf65170fad6445d34042e643f6a30b79'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
it('get event name topics', async function () {
|
||||
const testPromises = TEST_DATA.map(async ({ name, output }) => {
|
||||
const Contract = await ethers.getContractFactory(name);
|
||||
const contract = await Contract.deploy();
|
||||
await contract.deployed();
|
||||
const { abi } = await artifacts.readArtifact(name);
|
||||
|
||||
const eventNameTopics = getEventNameTopics(abi);
|
||||
expect(eventNameTopics).to.eql(output);
|
||||
});
|
||||
|
||||
await Promise.all(testPromises);
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { JsonFragment } from "@ethersproject/abi"
|
||||
import { utils } from "ethers";
|
||||
import { JsonFragment } from '@ethersproject/abi';
|
||||
import { utils } from 'ethers';
|
||||
|
||||
interface EventNameTopic {
|
||||
interface EventNameTopics {
|
||||
[eventName: string]: string
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ interface EventNameTopic {
|
||||
* Function to get event name topics from abi.
|
||||
* @param abi
|
||||
*/
|
||||
export const getEventNameTopics = (abi: JsonFragment[]): EventNameTopic => {
|
||||
export const getEventNameTopics = (abi: JsonFragment[]): EventNameTopics => {
|
||||
const eventFragments = abi.filter(({ type }) => type === 'event');
|
||||
|
||||
return eventFragments.reduce((acc: EventNameTopic, { name, inputs }) => {
|
||||
return eventFragments.reduce((acc: EventNameTopics, { name, inputs }) => {
|
||||
if (inputs && name) {
|
||||
const inputParamsString = inputs.map(({ type }) => type)
|
||||
.join(',');
|
||||
@@ -22,5 +22,5 @@ export const getEventNameTopics = (abi: JsonFragment[]): EventNameTopic => {
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
}
|
||||
}, {});
|
||||
};
|
||||
|
||||
@@ -1,89 +1,247 @@
|
||||
import { Contract } from "@ethersproject/contracts";
|
||||
import { expect } from "chai";
|
||||
import hre from "hardhat";
|
||||
import "@nomiclabs/hardhat-ethers";
|
||||
import { Contract } from '@ethersproject/contracts';
|
||||
import { expect } from 'chai';
|
||||
import { ethers } from 'hardhat';
|
||||
import '@nomiclabs/hardhat-ethers';
|
||||
|
||||
import { getStorageValue, StorageLayout } from "./storage";
|
||||
import { getStorageLayout, getStorageAt } from "../test/utils";
|
||||
import { getStorageInfo, getStorageValue, StorageLayout } from './storage';
|
||||
import { getStorageLayout, getStorageAt } from '../test/utils';
|
||||
|
||||
describe("Storage", function() {
|
||||
it("get value for integer type", async function() {
|
||||
const Integers = await hre.ethers.getContractFactory("TestIntegers");
|
||||
const TEST_DATA = [
|
||||
{
|
||||
name: 'TestBooleans',
|
||||
variable: 'bool1',
|
||||
output: {
|
||||
label: 'bool1',
|
||||
offset: 0,
|
||||
slot: '0x00',
|
||||
type: 't_bool'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'TestIntegers',
|
||||
variable: 'int2',
|
||||
output: {
|
||||
slot: '0x00',
|
||||
offset: 1,
|
||||
type: 't_int16',
|
||||
label: 'int2'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'TestUnsignedIntegers',
|
||||
variable: 'uint3',
|
||||
output: {
|
||||
label: 'uint3',
|
||||
offset: 0,
|
||||
slot: '0x01',
|
||||
type: 't_uint256'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'TestAddress',
|
||||
variable: 'address1',
|
||||
output: {
|
||||
label: 'address1',
|
||||
offset: 0,
|
||||
slot: '0x00',
|
||||
type: 't_address'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'TestStrings',
|
||||
variable: 'string2',
|
||||
output: {
|
||||
label: 'string2',
|
||||
offset: 0,
|
||||
slot: '0x01',
|
||||
type: 't_string_storage'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
it('get storage information', async function () {
|
||||
const testPromises = TEST_DATA.map(async ({ name, variable, output }) => {
|
||||
const Contract = await ethers.getContractFactory(name);
|
||||
const contract = await Contract.deploy();
|
||||
await contract.deployed();
|
||||
const storageLayout = await getStorageLayout(name);
|
||||
|
||||
const storageInfo = getStorageInfo(storageLayout, variable);
|
||||
expect(storageInfo).to.include(output);
|
||||
});
|
||||
|
||||
await Promise.all(testPromises);
|
||||
});
|
||||
|
||||
describe('Get value from storage', function () {
|
||||
it('get value for integer type variables packed together', async function () {
|
||||
const Integers = await ethers.getContractFactory('TestIntegers');
|
||||
const integers = await Integers.deploy();
|
||||
await integers.deployed();
|
||||
const storageLayout = await getStorageLayout("TestIntegers");
|
||||
const storageLayout = await getStorageLayout('TestIntegers');
|
||||
|
||||
// if (storageLayout)
|
||||
let value = 12;
|
||||
await integers.setInt1(value);
|
||||
let storageValue = await getStorageValue(integers.address, storageLayout, getStorageAt, "int1");
|
||||
let storageValue = await getStorageValue(integers.address, storageLayout, getStorageAt, 'int1');
|
||||
expect(storageValue).to.equal(value);
|
||||
|
||||
value = 34;
|
||||
await integers.setInt2(value);
|
||||
storageValue = await getStorageValue(integers.address, storageLayout, getStorageAt, 'int2');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it("get value for unsigned integer type", async function() {
|
||||
const UnsignedIntegers = await hre.ethers.getContractFactory("TestUnsignedIntegers");
|
||||
const unsignedIntegers = await UnsignedIntegers.deploy();
|
||||
await unsignedIntegers.deployed();
|
||||
const storageLayout = await getStorageLayout("TestUnsignedIntegers");
|
||||
it('get value for integer type variables using single slot', async function () {
|
||||
const Integers = await ethers.getContractFactory('TestIntegers');
|
||||
const integers = await Integers.deploy();
|
||||
await integers.deployed();
|
||||
const storageLayout = await getStorageLayout('TestIntegers');
|
||||
|
||||
const value = 123;
|
||||
await unsignedIntegers.setUint1(value);
|
||||
const storageValue = await getStorageValue(unsignedIntegers.address, storageLayout, getStorageAt, "uint1");
|
||||
await integers.setInt3(value);
|
||||
const storageValue = await getStorageValue(integers.address, storageLayout, getStorageAt, 'int3');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it("get value for boolean type", async function() {
|
||||
const Booleans = await hre.ethers.getContractFactory("TestBooleans");
|
||||
const booleans = await Booleans.deploy();
|
||||
await booleans.deployed();
|
||||
const storageLayout = await getStorageLayout("TestBooleans");
|
||||
it('get value for unsigned integer type variables packed together', async function () {
|
||||
const UnsignedIntegers = await ethers.getContractFactory('TestUnsignedIntegers');
|
||||
const unsignedIntegers = await UnsignedIntegers.deploy();
|
||||
await unsignedIntegers.deployed();
|
||||
const storageLayout = await getStorageLayout('TestUnsignedIntegers');
|
||||
|
||||
let value = true
|
||||
await booleans.setBool1(value);
|
||||
let storageValue = await getStorageValue(booleans.address, storageLayout, getStorageAt, "bool1");
|
||||
expect(storageValue).to.equal(value)
|
||||
let value = 12;
|
||||
await unsignedIntegers.setUint1(value);
|
||||
let storageValue = await getStorageValue(unsignedIntegers.address, storageLayout, getStorageAt, 'uint1');
|
||||
expect(storageValue).to.equal(value);
|
||||
|
||||
value = false
|
||||
await booleans.setBool2(value);
|
||||
storageValue = await getStorageValue(booleans.address, storageLayout, getStorageAt, "bool2")
|
||||
expect(storageValue).to.equal(value)
|
||||
value = 34;
|
||||
await unsignedIntegers.setUint2(value);
|
||||
storageValue = await getStorageValue(unsignedIntegers.address, storageLayout, getStorageAt, 'uint2');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it("get value for address type", async function() {
|
||||
const Address = await hre.ethers.getContractFactory("TestAddress");
|
||||
it('get value for unsigned integer type variables using single slot', async function () {
|
||||
const UnsignedIntegers = await ethers.getContractFactory('TestUnsignedIntegers');
|
||||
const unsignedIntegers = await UnsignedIntegers.deploy();
|
||||
await unsignedIntegers.deployed();
|
||||
const storageLayout = await getStorageLayout('TestUnsignedIntegers');
|
||||
|
||||
const value = 123;
|
||||
await unsignedIntegers.setUint3(value);
|
||||
const storageValue = await getStorageValue(unsignedIntegers.address, storageLayout, getStorageAt, 'uint3');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it('get value for boolean type', async function () {
|
||||
const Booleans = await ethers.getContractFactory('TestBooleans');
|
||||
const booleans = await Booleans.deploy();
|
||||
await booleans.deployed();
|
||||
const storageLayout = await getStorageLayout('TestBooleans');
|
||||
|
||||
let value = true;
|
||||
await booleans.setBool1(value);
|
||||
let storageValue = await getStorageValue(booleans.address, storageLayout, getStorageAt, 'bool1');
|
||||
expect(storageValue).to.equal(value);
|
||||
|
||||
value = false;
|
||||
await booleans.setBool2(value);
|
||||
storageValue = await getStorageValue(booleans.address, storageLayout, getStorageAt, 'bool2');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it('get value for address type', async function () {
|
||||
const Address = await ethers.getContractFactory('TestAddress');
|
||||
const address = await Address.deploy();
|
||||
await address.deployed();
|
||||
const storageLayout = await getStorageLayout("TestAddress");
|
||||
const storageLayout = await getStorageLayout('TestAddress');
|
||||
|
||||
const [signer] = await hre.ethers.getSigners();
|
||||
const [signer] = await ethers.getSigners();
|
||||
await address.setAddress1(signer.address);
|
||||
const storageValue = await getStorageValue(address.address, storageLayout, getStorageAt, "address1");
|
||||
const storageValue = await getStorageValue(address.address, storageLayout, getStorageAt, 'address1');
|
||||
expect(storageValue).to.be.a('string');
|
||||
expect(String(storageValue).toLowerCase()).to.equal(signer.address.toLowerCase());
|
||||
});
|
||||
|
||||
describe("string type", function () {
|
||||
it('get value for contract type', async function () {
|
||||
const contracts = ['TestContractTypes', 'TestAddress'];
|
||||
|
||||
const contractPromises = contracts.map(async (contractName) => {
|
||||
const Contract = await ethers.getContractFactory(contractName);
|
||||
const contract = await Contract.deploy();
|
||||
return contract.deployed();
|
||||
});
|
||||
|
||||
const [testContractTypes, testAddress] = await Promise.all(contractPromises);
|
||||
const storageLayout = await getStorageLayout('TestContractTypes');
|
||||
|
||||
await testContractTypes.setAddressContract1(testAddress.address);
|
||||
const storageValue = await getStorageValue(testContractTypes.address, storageLayout, getStorageAt, 'addressContract1');
|
||||
expect(storageValue).to.equal(testAddress.address.toLowerCase());
|
||||
});
|
||||
|
||||
it('get value for fixed size byte arrays packed together', async function () {
|
||||
const TestBytes = await ethers.getContractFactory('TestBytes');
|
||||
const testBytes = await TestBytes.deploy();
|
||||
await testBytes.deployed();
|
||||
const storageLayout = await getStorageLayout('TestBytes');
|
||||
|
||||
let value = ethers.utils.hexlify(ethers.utils.randomBytes(10));
|
||||
await testBytes.setBytesTen(value);
|
||||
let storageValue = await getStorageValue(testBytes.address, storageLayout, getStorageAt, 'bytesTen');
|
||||
expect(storageValue).to.equal(value);
|
||||
|
||||
value = ethers.utils.hexlify(ethers.utils.randomBytes(20));
|
||||
await testBytes.setBytesTwenty(value);
|
||||
storageValue = await getStorageValue(testBytes.address, storageLayout, getStorageAt, 'bytesTwenty');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it('get value for fixed size byte arrays using single slot', async function () {
|
||||
const TestBytes = await ethers.getContractFactory('TestBytes');
|
||||
const testBytes = await TestBytes.deploy();
|
||||
await testBytes.deployed();
|
||||
const storageLayout = await getStorageLayout('TestBytes');
|
||||
|
||||
const value = ethers.utils.hexlify(ethers.utils.randomBytes(30));
|
||||
await testBytes.setBytesThirty(value);
|
||||
const storageValue = await getStorageValue(testBytes.address, storageLayout, getStorageAt, 'bytesThirty');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it('get value for enum types', async function () {
|
||||
const TestEnums = await ethers.getContractFactory('TestEnums');
|
||||
const testEnums = await TestEnums.deploy();
|
||||
await testEnums.deployed();
|
||||
const storageLayout = await getStorageLayout('TestEnums');
|
||||
|
||||
const value = 1;
|
||||
await testEnums.setChoicesEnum1(value);
|
||||
const storageValue = await getStorageValue(testEnums.address, storageLayout, getStorageAt, 'choicesEnum1');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
describe('string type', function () {
|
||||
let strings: Contract, storageLayout: StorageLayout;
|
||||
|
||||
before(async () => {
|
||||
const Strings = await hre.ethers.getContractFactory("TestStrings");
|
||||
const Strings = await ethers.getContractFactory('TestStrings');
|
||||
strings = await Strings.deploy();
|
||||
await strings.deployed();
|
||||
storageLayout = await getStorageLayout("TestStrings");
|
||||
})
|
||||
storageLayout = await getStorageLayout('TestStrings');
|
||||
});
|
||||
|
||||
it("get value for string length less than 32 bytes", async function() {
|
||||
const value = 'Hello world.'
|
||||
it('get value for string length less than 32 bytes', async function () {
|
||||
const value = 'Hello world.';
|
||||
await strings.setString1(value);
|
||||
const storageValue = await getStorageValue(strings.address, storageLayout, getStorageAt, "string1");
|
||||
const storageValue = await getStorageValue(strings.address, storageLayout, getStorageAt, 'string1');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
|
||||
it("get value for string length more than 32 bytes", async function() {
|
||||
const value = 'This sentence is more than 32 bytes long.'
|
||||
it('get value for string length more than 32 bytes', async function () {
|
||||
const value = 'This sentence is more than 32 bytes long.';
|
||||
await strings.setString2(value);
|
||||
const storageValue = await getStorageValue(strings.address, storageLayout, getStorageAt, "string2");
|
||||
const storageValue = await getStorageValue(strings.address, storageLayout, getStorageAt, 'string2');
|
||||
expect(storageValue).to.equal(value);
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,7 +31,7 @@ export type GetStorageAt = (address: string, position: string) => Promise<string
|
||||
*/
|
||||
export const getStorageInfo = (storageLayout: StorageLayout, variableName: string): StorageInfo => {
|
||||
const { storage, types } = storageLayout;
|
||||
const targetState = storage.find((state) => state.label === variableName)
|
||||
const targetState = storage.find((state) => state.label === variableName);
|
||||
|
||||
// Throw if state variable could not be found in storage layout.
|
||||
if (!targetState) {
|
||||
@@ -42,8 +42,8 @@ export const getStorageInfo = (storageLayout: StorageLayout, variableName: strin
|
||||
...targetState,
|
||||
slot: utils.hexlify(BigNumber.from(targetState.slot)),
|
||||
types
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to get the value from storage for a contract variable.
|
||||
@@ -54,7 +54,7 @@ export const getStorageInfo = (storageLayout: StorageLayout, variableName: strin
|
||||
*/
|
||||
export const getStorageValue = async (address: string, storageLayout: StorageLayout, getStorageAt: GetStorageAt, variableName: string): Promise<number | string | boolean | undefined> => {
|
||||
const { slot, offset, type, types } = getStorageInfo(storageLayout, variableName);
|
||||
const { encoding, numberOfBytes, label } = types[type]
|
||||
const { encoding, numberOfBytes, label } = types[type];
|
||||
|
||||
// Get value according to encoding i.e. how the data is encoded in storage.
|
||||
// https://docs.soliditylang.org/en/v0.8.4/internals/layout_in_storage.html#json-output
|
||||
@@ -63,31 +63,30 @@ export const getStorageValue = async (address: string, storageLayout: StorageLay
|
||||
case 'inplace': {
|
||||
const valueArray = await getInplaceArray(address, slot, offset, numberOfBytes, getStorageAt);
|
||||
|
||||
// Parse value for address type.
|
||||
if (['address', 'address payable'].some(type => type === label)) {
|
||||
return utils.hexlify(valueArray);
|
||||
}
|
||||
|
||||
// Parse value for boolean type.
|
||||
if (label === 'bool') {
|
||||
return !BigNumber.from(valueArray).isZero();
|
||||
}
|
||||
|
||||
// Parse value for uint/int type.
|
||||
return BigNumber.from(valueArray).toNumber();
|
||||
if (label.match(/^enum|u?int[0-9]+/)) {
|
||||
return BigNumber.from(valueArray).toNumber();
|
||||
}
|
||||
|
||||
return utils.hexlify(valueArray);
|
||||
}
|
||||
|
||||
// https://docs.soliditylang.org/en/v0.8.4/internals/layout_in_storage.html#bytes-and-string
|
||||
case 'bytes': {
|
||||
const valueArray = await getBytesArray(address, slot, getStorageAt);
|
||||
|
||||
return utils.toUtf8String(valueArray)
|
||||
return utils.toUtf8String(valueArray);
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to get array value for inplace encoding.
|
||||
@@ -104,10 +103,10 @@ const getInplaceArray = async (address: string, slot: string, offset: number, nu
|
||||
// Get value according to offset.
|
||||
const start = uintArray.length - (offset + Number(numberOfBytes));
|
||||
const end = uintArray.length - offset;
|
||||
const offsetArray = uintArray.slice(start, end)
|
||||
const offsetArray = uintArray.slice(start, end);
|
||||
|
||||
return offsetArray;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to get array value for bytes encoding.
|
||||
@@ -116,7 +115,7 @@ const getInplaceArray = async (address: string, slot: string, offset: number, nu
|
||||
* @param getStorageAt
|
||||
*/
|
||||
const getBytesArray = async (address: string, slot: string, getStorageAt: GetStorageAt) => {
|
||||
let value = await getStorageAt(address, slot);
|
||||
const value = await getStorageAt(address, slot);
|
||||
const uintArray = utils.arrayify(value);
|
||||
let length = 0;
|
||||
|
||||
@@ -145,11 +144,11 @@ const getBytesArray = async (address: string, slot: string, getStorageAt: GetSto
|
||||
const position = utils.keccak256(paddedSlotHex);
|
||||
|
||||
// Get value from consecutive storage slots for longer data.
|
||||
for(let i = 0; i < length / 32; i++) {
|
||||
for (let i = 0; i < length / 32; i++) {
|
||||
const value = await getStorageAt(address, BigNumber.from(position).add(i).toHexString());
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
// Slice trailing bytes according to length of value.
|
||||
return utils.concat(values).slice(0, length);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user