evm: Make finality a state variable

This commit is contained in:
Csongor Kiss 2022-05-20 16:06:53 +01:00 committed by Csongor Kiss
parent 0c22d6c3d6
commit c453f4cf6d
34 changed files with 120 additions and 61 deletions

View File

@ -51,6 +51,7 @@ export async function query_contract_evm(
result.isInitialized = await core.isInitialized(result.implementation)
break
case "TokenBridge":
// TODO: add finality (need new sdk release)
contract_address = contract_address ? contract_address : contracts.token_bridge;
if (contract_address === undefined) {
throw Error(`Unknown token bridge contract on ${network} for ${chain}`)
@ -74,6 +75,7 @@ export async function query_contract_evm(
}
break
case "NFTBridge":
// TODO: add finality (need new sdk release)
contract_address = contract_address ? contract_address : contracts.nft_bridge;
if (contract_address === undefined) {
throw Error(`Unknown nft bridge contract on ${network} for ${chain}`)

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xc
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x0000000000000000000000000000000000000000
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0x9
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0xC9BdeEd33CD01541e1eeD10f90519d2C06Fe3feB
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0x9
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x9D29f395524B3C817ed86e2987A14c1897aFF849
BRIDGE_INIT_FINALITY=1

View File

@ -13,3 +13,4 @@ BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
# There is no WCELO.
BRIDGE_INIT_WETH=0x0000000000000000000000000000000000000000
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xE
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x524d97A67f50F4A062C28c74F60703Aec9028a94
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xa
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xa
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0xf1277d1Ed8AD466beddF92ef448A132661956621
BRIDGE_INIT_FINALITY=1

View File

@ -13,3 +13,4 @@ BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
# There is no WETH on Karura.
BRIDGE_INIT_WETH=0x0000000000000000000000000000000000000000
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xb
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x0000000000000000000000000000000000000000
BRIDGE_INIT_FINALITY=1

View File

@ -13,3 +13,4 @@ BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
# WKLAY
BRIDGE_INIT_WETH=0xe4f05a66ec68b54a58b17c22107b02e0232cc817
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0xd
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0x762ac6e8183db5a8e912a66fcc1a09f5a7ac96a9
BRIDGE_INIT_FINALITY=1

View File

@ -11,4 +11,5 @@ INIT_GOV_CONTRACT=0x000000000000000000000000000000000000000000000000000000000000
BRIDGE_INIT_CHAIN_ID=0x10
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0xD909178CC99d318e4D46e7E66a972955859670E1
BRIDGE_INIT_WETH=0xD909178CC99d318e4D46e7E66a972955859670E1
BRIDGE_INIT_FINALITY=1

View File

@ -12,3 +12,4 @@ BRIDGE_INIT_CHAIN_ID=0x11
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0xf8ad328e98f85fccbf09e43b16dcbbda7e84beab
BRIDGE_INIT_FINALITY=32

View File

@ -16,3 +16,4 @@ BRIDGE_INIT_CHAIN_ID= # 0x02
BRIDGE_INIT_GOV_CHAIN_ID= # 0x3
BRIDGE_INIT_GOV_CONTRACT= # 0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH= # 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
BRIDGE_INIT_FINALITY= # 15

View File

@ -9,6 +9,7 @@ BRIDGE_INIT_CHAIN_ID=0x2
BRIDGE_INIT_GOV_CHAIN_ID=0x1
BRIDGE_INIT_GOV_CONTRACT=0x0000000000000000000000000000000000000000000000000000000000000004
BRIDGE_INIT_WETH=0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E
BRIDGE_INIT_FINALITY=15
# Chain registration VAAs
REGISTER_SOL_TOKEN_BRIDGE_VAA=01000000000100c9f4230109e378f7efc0605fb40f0e1869f2d82fda5b1dfad8a5a2dafee85e033d155c18641165a77a2db6a7afbf2745b458616cb59347e89ae0c7aa3e7cc2d400000000010000000100010000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000546f6b656e4272696467650100000001c69a1b1a65dd336bf1df6a77afb501fc25db7fc0938cb08595a9ef473265cb4f

View File

@ -1,44 +0,0 @@
diff --git a/ethereum/contracts/bridge/Bridge.sol b/ethereum/contracts/bridge/Bridge.sol
index 56672cc1..7d287972 100644
--- a/ethereum/contracts/bridge/Bridge.sol
+++ b/ethereum/contracts/bridge/Bridge.sol
@@ -55,7 +55,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{
value : msg.value
- }(nonce, encoded, 15);
+ }(nonce, encoded, 1);
}
/*
@@ -314,7 +314,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{value: callValue}(
nonce,
encoded,
- 15
+ 1
);
}
@@ -351,7 +351,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{value: callValue}(
nonce,
encoded,
- 15
+ 1
);
}
function updateWrapped(bytes memory encodedVm) external returns (address token) {
diff --git a/ethereum/contracts/nft/NFTBridge.sol b/ethereum/contracts/nft/NFTBridge.sol
index b2b9fb99..895c1921 100644
--- a/ethereum/contracts/nft/NFTBridge.sol
+++ b/ethereum/contracts/nft/NFTBridge.sol
@@ -94,7 +94,7 @@ contract NFTBridge is NFTBridgeGovernance {
sequence = wormhole().publishMessage{
value : callValue
- }(nonce, encoded, 15);
+ }(nonce, encoded, 1);
}
function completeTransfer(bytes memory encodedVm) public {

View File

@ -55,7 +55,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{
value : msg.value
}(nonce, encoded, 15);
}(nonce, encoded, finality());
}
/*
@ -314,7 +314,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{value: callValue}(
nonce,
encoded,
15
finality()
);
}
@ -351,7 +351,7 @@ contract Bridge is BridgeGovernance, ReentrancyGuard {
sequence = wormhole().publishMessage{value: callValue}(
nonce,
encoded,
15
finality()
);
}
function updateWrapped(bytes memory encodedVm) external returns (address token) {

View File

@ -61,10 +61,14 @@ contract BridgeGetters is BridgeState {
function isWrappedAsset(address token) public view returns (bool){
return _state.isWrappedAsset[token];
}
function finality() public view returns (uint8) {
return _state.provider.finality;
}
}
interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint amount) external;
}
}

View File

@ -17,6 +17,28 @@ contract BridgeImplementation is Bridge {
function initialize() initializer public virtual {
// this function needs to be exposed for an upgrade to pass
uint8 finality;
uint16 chain = chainId();
// Wormhole chain ids explicitly enumerated
if (chain == 2) { finality = 15; // ethereum
} else if (chain == 4) { finality = 15; // bsc
} else if (chain == 5) { finality = 15; // polygon
} else if (chain == 6) { finality = 1; // avalanche
} else if (chain == 7) { finality = 1; // oasis
} else if (chain == 9) { finality = 1; // aurora
} else if (chain == 10) { finality = 1; // fantom
} else if (chain == 11) { finality = 1; // karura
} else if (chain == 12) { finality = 1; // acala
} else if (chain == 13) { finality = 1; // klaytn
} else if (chain == 14) { finality = 1; // celo
} else if (chain == 16) { finality = 1; // moonbeam
} else if (chain == 17) { finality = 32; // neon
} else {
revert("Unknown chain id.");
}
setFinality(finality);
}
modifier initializer() {

View File

@ -54,4 +54,8 @@ contract BridgeSetters is BridgeState {
function setOutstandingBridged(address token, uint256 outstanding) internal {
_state.outstandingBridged[token] = outstanding;
}
}
function setFinality(uint8 finality) internal {
_state.provider.finality = finality;
}
}

View File

@ -16,7 +16,8 @@ contract BridgeSetup is BridgeSetters, ERC1967Upgrade {
uint16 governanceChainId,
bytes32 governanceContract,
address tokenImplementation,
address WETH
address WETH,
uint8 finality
) public {
setChainId(chainId);
@ -29,6 +30,8 @@ contract BridgeSetup is BridgeSetters, ERC1967Upgrade {
setWETH(WETH);
setFinality(finality);
_upgradeTo(implementation);
}
}

View File

@ -9,6 +9,8 @@ contract BridgeStorage {
struct Provider {
uint16 chainId;
uint16 governanceChainId;
// Required number of block confirmations to assume finality
uint8 finality;
bytes32 governanceContract;
address WETH;
}

View File

@ -94,7 +94,7 @@ contract NFTBridge is NFTBridgeGovernance {
sequence = wormhole().publishMessage{
value : callValue
}(nonce, encoded, 15);
}(nonce, encoded, finality());
}
function completeTransfer(bytes memory encodedVm) public {

View File

@ -57,4 +57,8 @@ contract NFTBridgeGetters is NFTBridgeState {
function splCache(uint256 tokenId) public view returns (NFTBridgeStorage.SPLCache memory) {
return _state.splCache[tokenId];
}
}
function finality() public view returns (uint8) {
return _state.provider.finality;
}
}

View File

@ -17,6 +17,28 @@ contract NFTBridgeImplementation is NFTBridge {
function initialize() initializer public virtual {
// this function needs to be exposed for an upgrade to pass
uint8 finality;
uint16 chain = chainId();
// Wormhole chain ids explicitly enumerated
if (chain == 2) { finality = 15; // ethereum
} else if (chain == 4) { finality = 15; // bsc
} else if (chain == 5) { finality = 15; // polygon
} else if (chain == 6) { finality = 1; // avalanche
} else if (chain == 7) { finality = 1; // oasis
} else if (chain == 9) { finality = 1; // aurora
} else if (chain == 10) { finality = 1; // fantom
} else if (chain == 11) { finality = 1; // karura
} else if (chain == 12) { finality = 1; // acala
} else if (chain == 13) { finality = 1; // klaytn
} else if (chain == 14) { finality = 1; // celo
} else if (chain == 16) { finality = 1; // moonbeam
} else if (chain == 17) { finality = 32; // neon
} else {
revert("Unknown chain id.");
}
setFinality(finality);
}
modifier initializer() {

View File

@ -54,4 +54,8 @@ contract NFTBridgeSetters is NFTBridgeState {
function clearSplCache(uint256 tokenId) internal {
delete _state.splCache[tokenId];
}
}
function setFinality(uint8 finality) internal {
_state.provider.finality = finality;
}
}

View File

@ -15,7 +15,8 @@ contract NFTBridgeSetup is NFTBridgeSetters, ERC1967Upgrade {
address wormhole,
uint16 governanceChainId,
bytes32 governanceContract,
address tokenImplementation
address tokenImplementation,
uint8 finality
) public {
setChainId(chainId);
@ -26,6 +27,8 @@ contract NFTBridgeSetup is NFTBridgeSetters, ERC1967Upgrade {
setTokenImplementation(tokenImplementation);
setFinality(finality);
_upgradeTo(implementation);
}
}

View File

@ -9,6 +9,8 @@ contract NFTBridgeStorage {
struct Provider {
uint16 chainId;
uint16 governanceChainId;
// Required number of block confirmations to assume finality
uint8 finality;
bytes32 governanceContract;
}
@ -53,4 +55,4 @@ contract NFTBridgeStorage {
contract NFTBridgeState {
NFTBridgeStorage.State _state;
}
}

View File

@ -10,6 +10,7 @@ const chainId = process.env.BRIDGE_INIT_CHAIN_ID;
const governanceChainId = process.env.BRIDGE_INIT_GOV_CHAIN_ID;
const governanceContract = process.env.BRIDGE_INIT_GOV_CONTRACT; // bytes32
const WETH = process.env.BRIDGE_INIT_WETH;
const finality = process.env.BRIDGE_INIT_FINALITY;
module.exports = async function (deployer) {
// deploy token implementation
@ -30,7 +31,8 @@ module.exports = async function (deployer) {
governanceChainId,
governanceContract,
TokenImplementation.address,
WETH
WETH,
finality
).encodeABI();
// deploy proxy

View File

@ -9,6 +9,7 @@ const Wormhole = artifacts.require("Wormhole");
const chainId = process.env.BRIDGE_INIT_CHAIN_ID;
const governanceChainId = process.env.BRIDGE_INIT_GOV_CHAIN_ID;
const governanceContract = process.env.BRIDGE_INIT_GOV_CONTRACT; // bytes32
const finality = process.env.BRIDGE_INIT_FINALITY;
module.exports = async function (deployer) {
// deploy token implementation
@ -28,7 +29,8 @@ module.exports = async function (deployer) {
(await Wormhole.deployed()).address,
governanceChainId,
governanceContract,
TokenImplementation.address
TokenImplementation.address,
finality
).encodeABI();
// deploy proxy

View File

@ -22,6 +22,7 @@ contract("Bridge", function () {
const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
const testChainId = "2";
const testFinality = "15";
const testGovernanceChainId = "1";
const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
let WETH = process.env.BRIDGE_INIT_WETH;
@ -48,6 +49,10 @@ contract("Bridge", function () {
const chainId = await initialized.methods.chainId().call();
assert.equal(chainId, testChainId);
// finality
const finality = await initialized.methods.finality().call();
assert.equal(finality, testFinality);
// governance
const governanceChainId = await initialized.methods.governanceChainId().call();
assert.equal(governanceChainId, testGovernanceChainId);

View File

@ -19,6 +19,7 @@ contract("NFT", function () {
const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
const testChainId = "2";
const testFinality = "15";
const testGovernanceChainId = "1";
const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
let WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
@ -42,6 +43,10 @@ contract("NFT", function () {
const chainId = await initialized.methods.chainId().call();
assert.equal(chainId, testChainId);
// finality
const finality = await initialized.methods.finality().call();
assert.equal(finality, testFinality);
// governance
const governanceChainId = await initialized.methods.governanceChainId().call();
assert.equal(governanceChainId, testGovernanceChainId);
@ -722,4 +727,4 @@ function zeroPadBytes(value, length) {
value = "0" + value;
}
return value;
}
}

View File

@ -25,6 +25,7 @@ contract("Update Bridge", function (accounts) {
let WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const testForeignChainId = "1";
const testForeignBridgeContract = "0x000000000000000000000000000000000000000000000000000000000000ffff";
const testFinality = 15;
const currentImplementation = "0x6c4c12987303b2c94b2C76c612Fc5F4D2F0360F7";
let bridgeProxy;
@ -39,7 +40,8 @@ contract("Update Bridge", function (accounts) {
testGovernanceChainId,
testGovernanceContract,
TokenImplementation.address,
WETH
WETH,
testFinality
).encodeABI();
const deploy = await TokenBridge.new(BridgeSetup.address, initData);
@ -411,4 +413,4 @@ function zeroPadBytes(value, length) {
value = "0" + value;
}
return value;
}
}