Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(protocol): optimize keyForName #13557

Merged
merged 17 commits into from
Apr 6, 2023
Merged
7 changes: 2 additions & 5 deletions packages/protocol/contracts/common/AddressResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ abstract contract AddressResolver {
function keyForName(
uint256 chainId,
string memory name
) public pure returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
// TODO: the next line is cheaper in gas but will break
// many Hardhat tests.
// key = string(bytes.concat(bytes32(chainId), bytes(name)));
) public pure virtual returns (string memory) {
return string(bytes.concat(bytes32(chainId), bytes(name)));
dantaik marked this conversation as resolved.
Show resolved Hide resolved
}

function _init(address addressManager_) internal virtual {
Expand Down
9 changes: 9 additions & 0 deletions packages/protocol/contracts/test/L1/TestTaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pragma solidity ^0.8.18;

import {TaikoL1} from "../../L1/TaikoL1.sol";
import {TaikoData} from "../../L1/TaikoData.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract TestTaikoL1 is TaikoL1 {
function getConfig()
Expand Down Expand Up @@ -45,4 +46,12 @@ contract TestTaikoL1 is TaikoL1 {
dampingFactorBips: 5000
});
}

// The old implementation that is also used in hardhat tests.
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pragma solidity ^0.8.18;

import {TaikoL1} from "../../L1/TaikoL1.sol";
import {TaikoData} from "../../L1/TaikoData.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract TestTaikoL1EnableTokenomics is TaikoL1 {
function getConfig()
Expand Down Expand Up @@ -47,4 +48,12 @@ contract TestTaikoL1EnableTokenomics is TaikoL1 {
dampingFactorBips: 5000
});
}

// The old implementation that is also used in hardhat tests.
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}
82 changes: 82 additions & 0 deletions packages/protocol/contracts/test/TestContracts.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: MIT
// _____ _ _ _ _
// |_ _|_ _(_) |_____ | | __ _| |__ ___
// | |/ _` | | / / _ \ | |__/ _` | '_ (_-<
// |_|\__,_|_|_\_\___/ |____\__,_|_.__/__/

pragma solidity ^0.8.18;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SignalService} from "../signal/SignalService.sol";
import {TokenVault} from "../bridge/TokenVault.sol";
import {EtherVault} from "../bridge/EtherVault.sol";
import {BridgedERC20} from "../bridge/BridgedERC20.sol";
import {Bridge} from "../bridge/Bridge.sol";
import {TaikoToken} from "../L1/TaikoToken.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

// The old implementation that is also used in hardhat tests.

contract TestERC20 is ERC20 {
constructor(uint256 initialSupply) ERC20("TestERC20", "TEST") {
_mint(msg.sender, initialSupply);
}
}

contract TestSignalService is SignalService {
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}

contract TestTokenVault is TokenVault {
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}

contract TestEtherVault is EtherVault {
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}

contract TestBridgedERC20 is BridgedERC20 {
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}

contract TestBridge is Bridge {
function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}

contract TestTaikoToken is TaikoToken {
function mintAnyone(address account, uint256 amount) public {
_mint(account, amount);
}

function keyForName(
uint256 chainId,
string memory name
) public pure override returns (string memory key) {
key = string.concat(Strings.toString(chainId), ".", name);
}
}
10 changes: 0 additions & 10 deletions packages/protocol/contracts/test/thirdparty/TestTaikoToken.sol

This file was deleted.

6 changes: 4 additions & 2 deletions packages/protocol/test/bridge/BridgedERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe("BridgedERC20", function () {

beforeEach(async function () {
unInitAddressManager = await deployAddressManager(owner);
unInitERC20 = await (await ethers.getContractFactory("BridgedERC20"))
unInitERC20 = await (
await ethers.getContractFactory("TestBridgedERC20")
)
.connect(owner)
.deploy();

Expand All @@ -42,7 +44,7 @@ describe("BridgedERC20", function () {
tokenVault.address
);

erc20 = await (await ethers.getContractFactory("BridgedERC20"))
erc20 = await (await ethers.getContractFactory("TestBridgedERC20"))
.connect(owner)
.deploy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("LibBridgeProcess", async function () {
).deploy();
await addressManager.init();

etherVault = await (await ethers.getContractFactory("EtherVault"))
etherVault = await (await ethers.getContractFactory("TestEtherVault"))
.connect(etherVaultOwner)
.deploy();

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/bridge/libs/LibBridgeRetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("LibBridgeRetry", function () {
owner
);

etherVault = await (await ethers.getContractFactory("EtherVault"))
etherVault = await (await ethers.getContractFactory("TestEtherVault"))
.connect(etherVaultOwner)
.deploy();

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/bridge/libs/LibBridgeSend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("LibBridgeSend", function () {
);

const etherVault: EtherVault = await (
await ethers.getContractFactory("EtherVault")
await ethers.getContractFactory("TestEtherVault")
)
.connect(etherVaultOwner)
.deploy();
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/etherVault/EtherVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("EtherVault", function () {
owner
);

etherVault = await (await ethers.getContractFactory("EtherVault"))
etherVault = await (await ethers.getContractFactory("TestEtherVault"))
.connect(owner)
.deploy();
await etherVault.init(addressManager.address);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/libs/LibTrieProof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("integration:LibTrieProof", function () {
chainId
);

const BridgeFactory = await ethers.getContractFactory("Bridge", {
const BridgeFactory = await ethers.getContractFactory("TestBridge", {
libraries: {
LibTrieProof: testLibTrieProof.address,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.18;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract TestERC20 is ERC20 {
constructor(uint256 initialSupply) ERC20("TestERC20", "TEST") {
contract FooToken is ERC20 {
constructor(uint256 initialSupply) ERC20("FooToken", "FOO") {
_mint(msg.sender, initialSupply);
}
}
2 changes: 1 addition & 1 deletion packages/protocol/test/tokenVault/TokenVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("TokenVault", function () {
const addressManagerFactory: AddressManager__factory =
await ethers.getContractFactory("AddressManager");
const tokenVaultFactory: TokenVault__factory =
await ethers.getContractFactory("TokenVault");
await ethers.getContractFactory("TestTokenVault");

tokenVaultAddressManager = await addressManagerFactory.deploy();
await tokenVaultAddressManager.init();
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/utils/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function deployBridge(
.connect(signer)
.deploy();

const BridgeFactory = await hardhatEthers.getContractFactory("Bridge", {
const BridgeFactory = await hardhatEthers.getContractFactory("TestBridge", {
libraries: {
LibTrieProof: libTrieProof.address,
},
Expand All @@ -35,7 +35,7 @@ async function deployBridge(
await bridge.connect(signer).init(addressManager.address);

const etherVault: EtherVault = await (
await hardhatEthers.getContractFactory("EtherVault")
await hardhatEthers.getContractFactory("TestEtherVault")
)
.connect(signer)
.deploy();
Expand Down
12 changes: 8 additions & 4 deletions packages/protocol/test/utils/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ import { ethers, Signer } from "ethers";
import RLP from "rlp";
import { ethers as hardhatEthers } from "hardhat";
import { BlockHeader, EthGetProofResponse } from "./rpc";
import { AddressManager, SignalService, LibTrieProof } from "../../typechain";
import {
AddressManager,
TestSignalService,
LibTrieProof,
} from "../../typechain";

async function deploySignalService(
signer: Signer,
addressManager: AddressManager,
srcChain: number
): Promise<{ signalService: SignalService }> {
): Promise<{ signalService: TestSignalService }> {
const libTrieProof: LibTrieProof = await (
await hardhatEthers.getContractFactory("LibTrieProof")
)
.connect(signer)
.deploy();

const SignalServiceFactory = await hardhatEthers.getContractFactory(
"SignalService",
"TestSignalService",
{
libraries: {
LibTrieProof: libTrieProof.address,
},
}
);

const signalService: SignalService = await SignalServiceFactory.connect(
const signalService: TestSignalService = await SignalServiceFactory.connect(
signer
).deploy();

Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/test/utils/taikoL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ async function deployTaikoL2(
signer: ethers.Signer,
gasLimit: number | undefined = undefined
): Promise<TaikoL2> {
const taikoL2 = await (await hardhatEthers.getContractFactory("TaikoL2"))
const taikoL2 = await (
await hardhatEthers.getContractFactory("TestTaikoL2")
)
.connect(signer)
.deploy({ gasLimit });

Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/test/utils/taikoToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const deployTaikoToken = async (
addressManager: AddressManager,
protoBroker: string
) => {
const token = await (await hardhatEthers.getContractFactory("TaikoToken"))
const token = await (
await hardhatEthers.getContractFactory("TestTaikoToken")
)
.connect(signer)
.deploy();
await token.init(addressManager.address, "Taiko Token", "TKO", [], []);
Expand Down
12 changes: 5 additions & 7 deletions packages/protocol/test2/genesis/GenerateGenesis.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {TokenVault} from "../../contracts/bridge/TokenVault.sol";
import {EtherVault} from "../../contracts/bridge/EtherVault.sol";
import {SignalService} from "../../contracts/signal/SignalService.sol";
import {LibBridgeStatus} from "../../contracts/bridge/libs/LibBridgeStatus.sol";
import {TestERC20} from "../../contracts/test/thirdparty/TestERC20.sol";
import {FooToken} from "../../contracts/test/FooToken.sol";

contract TestGenerateGenesis is Test, AddressResolver {
using stdJson for string;
Expand All @@ -37,7 +37,7 @@ contract TestGenerateGenesis is Test, AddressResolver {
checkDeployedCode("TokenVault");
checkDeployedCode("EtherVault");
checkDeployedCode("Bridge");
checkDeployedCode("TestERC20");
checkDeployedCode("FooToken");
checkDeployedCode("AddressManager");
checkDeployedCode("SignalService");
}
Expand Down Expand Up @@ -174,12 +174,10 @@ contract TestGenerateGenesis is Test, AddressResolver {
}

function testERC20() public {
TestERC20 testErc20 = TestERC20(
getPredeployedContractAddress("TestERC20")
);
FooToken fooErc20 = FooToken(getPredeployedContractAddress("FooToken"));
dantaik marked this conversation as resolved.
Show resolved Hide resolved

assertEq(testErc20.name(), "PredeployERC20");
assertEq(testErc20.symbol(), "PRE");
assertEq(fooErc20.name(), "FooToken");
assertEq(fooErc20.symbol(), "FOO");
}

function getPredeployedContractAddress(
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/utils/generate_genesis/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function deployERC20(

const artifact = require(path.join(
ARTIFACTS_PATH,
"./TestERC20.sol/TestERC20.json"
"./TestContracts.sol/TestERC20.json"
));

artifact.contractName = "TestERC20";
Expand Down