Skip to content

Commit

Permalink
Merge pull request #112 from sygmaprotocol/nmlinaric/generate-func-si…
Browse files Browse the repository at this point in the history
…gnatures

feat: add function for generating function signatures
  • Loading branch information
P1sar authored Jan 31, 2023
2 parents d60b0bb + 8bc6a74 commit 9efc635
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: make install-deps
- name: Compile contracts
run: make compile
- name: Store contract artifacts
uses: actions/upload-artifact@v3
with:
name: contracts-artifacts
path: build
- name: Ganache Tests
run: |
SILENT=true make start-ganache
make test
coverage:
needs: Test
needs: test
name: Coverage
runs-on: ubuntu-latest
strategy:
Expand All @@ -45,6 +52,11 @@ jobs:
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: Download contracts artifacts
uses: actions/download-artifact@v3
with:
name: contracts-artifacts
path: build
- name: Yarn install
run: yarn install --frozen-lockfile
- name: Run coverage
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ bindings: compile
@echo " > \033[32mCreating go bindings for ethereum contracts... \033[0m "
./scripts/create_bindings.sh

func-signatures:
@echo " > \033[32mGenerating signature hashes... \033[0m "
node -e "require('./scripts/generateFuncSignatures.js').generateAccessControlFuncSignatures()"

## license: Adds license header to missing files.
license:
@echo " > \033[32mAdding license headers...\033[0m "
Expand All @@ -36,4 +40,3 @@ license-check:
@echo " > \033[Checking for license headers...\033[0m "
GO111MODULE=off go get -u github.com/google/addlicense
addlicense -check -c "SYgma" -f ./scripts/header.txt -y 2021 .

49 changes: 49 additions & 0 deletions scripts/generateFuncSignatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node
/**
* Copyright 2020 ChainSafe Systems
* SPDX-License-Identifier: LGPL-3.0-only
*/

const fs = require("fs");
const {utils} = require("ethers");
const {resolve} = require("path");


const BRIDGE_CONTRACT_PATH = resolve(__dirname, "../contracts/Bridge.sol");
const ARTIFACTS_PATH = resolve(__dirname, "../build/contracts/Bridge.json");

function generateAccessControlFuncSignatures() {
const bridgeAbiJson = JSON.parse(fs.readFileSync(ARTIFACTS_PATH));
const bridgeContractMethods = bridgeAbiJson.userdoc.methods
const bridgeContract = fs.readFileSync(BRIDGE_CONTRACT_PATH);

// regex that will match all functions that have "onlyAllowed" modifier
const regex = RegExp("function\\s+(?:(?!_onlyAllowed|function).)+onlyAllowed", "gs");

let a;
const b = [];
// fetch all functions that have "onlyAllowed" modifier from "Bridge.sol"
while ((a = regex.exec(bridgeContract)) !== null) {
// filter out only function name from matching (onlyAllowed) functions
b.push(a[0].split(/[\s()]+/)[1]);
}

let accessControlFuncSignatures = []
// filter out from Bridge ABI functions signatures with "onlyAllowed" modifier
accessControlFuncSignatures = Object.keys(bridgeContractMethods).filter(
el1 => b.some(
el2 => el1.includes(el2))).map(
func => ({
function: func,
hash: utils.keccak256(Buffer.from(func)).substring(0,10)
})
);

console.table(accessControlFuncSignatures);

return accessControlFuncSignatures;
}

module.exports = {
generateAccessControlFuncSignatures
};
21 changes: 5 additions & 16 deletions test/gasBenchmarks/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Copyright 2020 ChainSafe Systems
* SPDX-License-Identifier: LGPL-3.0-only
*/

const Helpers = require("../../test/helpers");

const BridgeContract = artifacts.require("Bridge");
const AccessControlSegregatorContract = artifacts.require(
"AccessControlSegregator"
Expand Down Expand Up @@ -30,22 +33,8 @@ contract("Gas Benchmark - [contract deployments]", async (accounts) => {

it("Should deploy all contracts and print benchmarks", async () => {
const accessControlInstance = await AccessControlSegregatorContract.new(
[
"0x80ae1c28",
"0xad71c7d2",
"0xcb10f215",
"0x5a1ad87c",
"0x8c0c2631",
"0xedc20c3c",
"0xd15ef64e",
"0x9d33b6d4",
"0x8b63aebf",
"0xbd2a1820",
"0x6ba6db6b",
"0xd2e5fae9",
"0xf5f63b39",
],
Array(13).fill(accounts[0])
Helpers.accessControlFuncSignatures,
Array(13).fill(accounts[0])
);
let contractInstances = [accessControlInstance];
contractInstances = contractInstances.concat(
Expand Down
22 changes: 5 additions & 17 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* SPDX-License-Identifier: LGPL-3.0-only
*/

const Ethers = require("ethers");
const ethSigUtil = require("eth-sig-util");
const Ethers = require("ethers");
const ethSigUtil = require("eth-sig-util");
const Utils = require("../scripts/generateFuncSignatures");

const AccessControlSegregatorContract = artifacts.require(
"AccessControlSegregator"
Expand Down Expand Up @@ -279,21 +280,8 @@ const decimalToPaddedBinary = (decimal) => {
return decimal.toString(2).padStart(64, "0");
};

const accessControlFuncSignatures = [
"0x80ae1c28", // adminPauseTransfers
"0xffaac0eb", // adminUnpauseTransfers
"0x8a3234c7", // adminSetResource
"0x8c0c2631", // adminSetBurnable
"0xedc20c3c", // adminSetDepositNonce
"0xd15ef64e", // adminSetForwarder
"0x9d33b6d4", // adminChangeAccessControl
"0x8b63aebf", // adminChangeFeeHandler
"0xbd2a1820", // adminWithdraw
"0x6ba6db6b", // startKeygen
"0xd2e5fae9", // endKeygen
"0xd8236744", // refreshKey
"0x366b4885", // retry
];
// filter out only func signatures
const accessControlFuncSignatures = Utils.generateAccessControlFuncSignatures().map(e => e.hash);

const deployBridge = async (domainID, admin) => {
const accessControlInstance = await AccessControlSegregatorContract.new(
Expand Down

0 comments on commit 9efc635

Please sign in to comment.