Skip to content

Commit

Permalink
Merge branch 'proxy-contracts' into proxy-contracts-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Jul 28, 2023
2 parents 0007a56 + b0f922c commit 42ba62a
Show file tree
Hide file tree
Showing 38 changed files with 2,098 additions and 454 deletions.
2 changes: 2 additions & 0 deletions core/packages/contracts/.envrc-example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source_up_if_exists
## BeefyClient
export RANDAO_COMMIT_DELAY=3
export RANDAO_COMMIT_EXP=8
export PREV_RANDAO=377
export ValidatorSetSize=300

## ParachainClient
export PARAID=1000
Expand Down
114 changes: 70 additions & 44 deletions core/packages/contracts/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,44 +1,70 @@
BeefyClientTest:testScaleEncodeCommit() (gas: 32228)
BeefyClientTest:testSubmit() (gas: 640426)
BeefyClientTest:testSubmitFailForPrevRandaoCapturedMoreThanOnce() (gas: 269498)
BeefyClientTest:testSubmitFailForPrevRandaoTooEarlyOrTooLate() (gas: 210184)
BeefyClientTest:testSubmitFailInvalidSignature() (gas: 581588)
BeefyClientTest:testSubmitFailValidatorNotInBitfield() (gas: 568657)
BeefyClientTest:testSubmitFailWithInvalidBitfield() (gas: 559304)
BeefyClientTest:testSubmitFailWithStaleCommitment() (gas: 757824)
BeefyClientTest:testSubmitFailWithoutPrevRandao() (gas: 535343)
BeefyClientTest:testSubmitWithHandover() (gas: 668392)
BeefyClientTest:testSubmitWithHandoverFailStaleCommitment() (gas: 781735)
BeefyClientTest:testSubmitWithHandoverFailWithoutPrevRandao() (gas: 559560)
InboundQueueTest:testSubmit() (gas: 117979)
InboundQueueTest:testSubmitShouldFailInsufficientBalance() (gas: 92436)
InboundQueueTest:testSubmitShouldFailInvalidNonce() (gas: 66955)
InboundQueueTest:testSubmitShouldFailInvalidProof() (gas: 62637)
InboundQueueTest:testSubmitShouldNotFailOnHandlerFailure() (gas: 142351)
InboundQueueTest:testSubmitShouldNotFailOnHandlerOOG() (gas: 641523)
NativeTokensTest:testCreateFailOnBadFeePayment() (gas: 11593)
NativeTokensTest:testCreateFailOnBadToken() (gas: 19174)
NativeTokensTest:testCreateSuccessful() (gas: 79603)
NativeTokensTest:testHandleRevertsUnknownOrigin() (gas: 12280)
NativeTokensTest:testHandleRevertsUnknownSender() (gas: 44241)
NativeTokensTest:testHandleUnlockMessageSuccessful() (gas: 154612)
NativeTokensTest:testLockRevertsZeroAmount() (gas: 11361)
NativeTokensTest:testLockSuccessful() (gas: 161012)
OutboundQueueTest:testSubmit() (gas: 82052)
OutboundQueueTest:testSubmitFailFeePaymentTooLow() (gas: 30057)
ParachainClientTest:testCreateParachainHeaderMerkleFailInvalidHeader() (gas: 12690)
ParachainClientTest:testCreateParachainHeaderMerkleLeaf() (gas: 23474)
ParachainClientTest:testIsCommitmentInHeaderDigest() (gas: 23454)
ScaleCodecTest:testEncodeU128() (gas: 193)
ScaleCodecTest:testEncodeU16() (gas: 414)
ScaleCodecTest:testEncodeU256() (gas: 453)
ScaleCodecTest:testEncodeU32() (gas: 171)
ScaleCodecTest:testEncodeU64() (gas: 281)
TokenVaultTest:testDepositSuccessful() (gas: 106091)
TokenVaultTest:testInsufficientBalance() (gas: 17425)
TokenVaultTest:testNonOwnerCannotWithdraw() (gas: 44613)
TokenVaultTest:testTokenTransferFailedInsufficientAllowance() (gas: 69957)
TokenVaultTest:testWithdrawSuccessful() (gas: 121381)
UpgradeProxyTest:testUpgrade() (gas: 35658)
UpgradeProxyTest:testUpgradeFail() (gas: 20802)
UpgradeProxyTest:testUpgradeFailBadOrigin() (gas: 11326)
AgentTest:testInvoke() (gas: 20810)
AgentTest:testInvokeFail() (gas: 13429)
AgentTest:testInvokeUnauthorized() (gas: 13974)
BeefyClientTest:testCreateFinalBitfield() (gas: 278225)
BeefyClientTest:testCreateFinalBitfieldInvalid() (gas: 265008)
BeefyClientTest:testCreateInitialBitfield() (gas: 650507)
BeefyClientTest:testCreateInitialBitfieldInvalid() (gas: 578841)
BeefyClientTest:testScaleEncodeCommit() (gas: 32353)
BeefyClientTest:testSubmit():((uint32,uint64,(bytes2,bytes)[])) (gas: 708627)
BeefyClientTest:testSubmitFailForPrevRandaoCapturedMoreThanOnce() (gas: 266184)
BeefyClientTest:testSubmitFailForPrevRandaoTooEarlyOrTooLate() (gas: 207388)
BeefyClientTest:testSubmitFailInvalidSignature() (gas: 657123)
BeefyClientTest:testSubmitFailValidatorNotInBitfield() (gas: 645077)
BeefyClientTest:testSubmitFailWithInvalidBitfield() (gas: 634383)
BeefyClientTest:testSubmitFailWithInvalidMMRLeaf() (gas: 748779)
BeefyClientTest:testSubmitFailWithInvalidMMRLeafProof() (gas: 752764)
BeefyClientTest:testSubmitFailWithInvalidTicket() (gas: 638107)
BeefyClientTest:testSubmitFailWithInvalidValidatorSet() (gas: 642432)
BeefyClientTest:testSubmitFailWithNotEnoughClaims() (gas: 197586)
BeefyClientTest:testSubmitFailWithStaleCommitment() (gas: 835952)
BeefyClientTest:testSubmitFailWithoutPrevRandao() (gas: 532261)
BeefyClientTest:testSubmitWithHandover() (gas: 736139)
BeefyClientTest:testSubmitWithHandoverFailStaleCommitment() (gas: 859922)
BeefyClientTest:testSubmitWithHandoverFailWithInvalidValidatorSet() (gas: 666242)
BeefyClientTest:testSubmitWithHandoverFailWithoutPrevRandao() (gas: 556301)
GatewayTest:testAgentExecution() (gas: 52791)
GatewayTest:testAgentExecutionBadOrigin() (gas: 23354)
GatewayTest:testAgentExecutionBadPayload() (gas: 22077)
GatewayTest:testCreateAgent() (gas: 254909)
GatewayTest:testCreateAgentAlreadyCreated() (gas: 256489)
GatewayTest:testCreateChannel() (gas: 332117)
GatewayTest:testCreateChannelFailsAgentDoesNotExist() (gas: 19185)
GatewayTest:testCreateChannelFailsChannelAlreadyExists() (gas: 335995)
GatewayTest:testDisableOutboundMessaging() (gas: 67040)
GatewayTest:testDisableOutboundMessagingForChannel() (gas: 161374)
GatewayTest:testGetters() (gas: 37883)
GatewayTest:testHandlersNotExternallyCallable() (gas: 52292)
GatewayTest:testInitializeNotExternallyCallable() (gas: 16762)
GatewayTest:testRegisterToken() (gas: 85845)
GatewayTest:testRelayerNotRewarded() (gas: 75289)
GatewayTest:testRelayerRewardedFromAgent() (gas: 310728)
GatewayTest:testSendTokenAddress20() (gas: 128049)
GatewayTest:testSendTokenAddress20FailsInvalidDestination() (gas: 59703)
GatewayTest:testSendTokenAddress32() (gas: 129387)
GatewayTest:testSendTokenAddress32ToAssetHub() (gas: 128776)
GatewayTest:testSetOperatingMode() (gas: 39208)
GatewayTest:testSubmitFailInvalidChannel() (gas: 27926)
GatewayTest:testSubmitFailInvalidNonce() (gas: 318948)
GatewayTest:testSubmitHappyPath() (gas: 312342)
GatewayTest:testUpdateChannel() (gas: 58121)
GatewayTest:testUpdateChannelFailDoesNotExist() (gas: 20107)
GatewayTest:testUpdateChannelSanityChecksForBridgeHubChannel() (gas: 22147)
GatewayTest:testUpgrade() (gas: 208455)
GatewayTest:testUpgradeFailCodeHashMismatch() (gas: 178843)
GatewayTest:testUpgradeFailOnInitializationFailure() (gas: 182568)
GatewayTest:testUserDoesNotProvideEnoughFees() (gas: 218463)
GatewayTest:testUserPaysFees() (gas: 244678)
GatewayTest:testWithdrawAgentFunds() (gas: 64315)
MMRProofTest:testVerifyLeafProof() (gas: 370749)
MMRProofTest:testVerifyLeafProofFailsExceededProofSize() (gas: 358741)
ScaleCodecTest:testCheckedEncodeCompactU32() (gas: 5120)
ScaleCodecTest:testEncodeCompactU32() (gas: 9722)
ScaleCodecTest:testEncodeU128() (gas: 192)
ScaleCodecTest:testEncodeU16() (gas: 434)
ScaleCodecTest:testEncodeU256() (gas: 478)
ScaleCodecTest:testEncodeU32() (gas: 170)
ScaleCodecTest:testEncodeU64() (gas: 280)
VerificationTest:testCreateParachainHeaderMerkleFailInvalidHeader() (gas: 11341)
VerificationTest:testCreateParachainHeaderMerkleLeaf() (gas: 20830)
VerificationTest:testIsCommitmentInHeaderDigest() (gas: 21863)
5 changes: 3 additions & 2 deletions core/packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ optimizer = true
optimizer_runs = 20_000
via_ir = true
test = 'test'
ffi = true
fs_permissions = [{ access = "read", path = "test/data"}, { access = "read", path = "./"}]
fs_permissions = [{ access = "read-write", path = "test/data"}, { access = "read", path = "./"}]

ignored_error_codes = [
# DeployScript.sol is never deployed
5574
]

# no_match_test = "testRegenerateBitField"
63 changes: 0 additions & 63 deletions core/packages/contracts/scripts/ffiWrapper.ts

This file was deleted.

98 changes: 98 additions & 0 deletions core/packages/contracts/scripts/generateBeefyTestFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env ts-node
import { ValidatorSet, createRandomSubset, readSetBits } from "./helpers"
import { BigNumber, ethers } from "ethers"
import fs from "fs"
import type { BeefyClient } from "../types/BeefyClient"
import { accounts } from "./wallets"
import path from "path"
const encoder = new ethers.utils.AbiCoder()

const run = async () => {
const fixtureData = JSON.parse(
fs.readFileSync(
path.join(process.env.contract_dir || ".", "test/data/beefy-commitment.json"),
"utf8"
)
)
const BeefyValidatorSetFile = path.join(
process.env.contract_dir || ".",
"test/data/beefy-validator-set.json"
)
const BeefyBitFieldFile = path.join(
process.env.contract_dir || ".",
"test/data/beefy-final-bitfield.json"
)
const BeefyValidatorProofFile = path.join(
process.env.contract_dir || ".",
"test/data/beefy-final-proof.json"
)
const command = process.argv[2]
const validatorSetID = fixtureData.params.id
const validatorSetSize =
process.env["FixedSet"] == "true"
? accounts.length
: process.env["ValidatorSetSize"]
? parseInt(process.env["ValidatorSetSize"])
: 300
const commitHash = fixtureData.commitmentHash
let validatorSet: ValidatorSet
if (process.env["FixedSet"] == "true") {
validatorSet = new ValidatorSet(
validatorSetID,
validatorSetSize,
accounts.map((account) => account.privateKey)
)
} else {
validatorSet = new ValidatorSet(validatorSetID, validatorSetSize)
}

if (command == "GenerateInitialSet") {
const mmrLeaf: BeefyClient.MMRLeafStruct = fixtureData.params.leaf

const absentSubsetSize = Math.floor((validatorSetSize - 1) / 3)
const subsetSize = validatorSetSize - absentSubsetSize
const randomSet = createRandomSubset(validatorSetSize, subsetSize)
const participants = randomSet.participants
const absentees = randomSet.absentees

const testFixture = {
validatorSetSize,
participants,
absentees,
validatorRoot: validatorSet.root,
}
fs.writeFileSync(BeefyValidatorSetFile, JSON.stringify(testFixture, null, 2), "utf8")
console.log("Beefy fixture writing to dest file: " + BeefyValidatorSetFile)
} else if (command == "GenerateProofs") {
const testFixture = JSON.parse(fs.readFileSync(BeefyBitFieldFile, "utf8"))
const bitField = encoder.decode(["uint256[]"], testFixture.final.finalBitFieldRaw)[0]
console.log(bitField)
let finalBitfield: BigNumber[] = []
for (let i = 0; i < bitField.length; i++) {
finalBitfield.push(bitField[i])
}
const finalValidatorsProof: BeefyClient.ValidatorProofStruct[] = readSetBits(
finalBitfield
).map((i) => validatorSet.createSignatureProof(i, commitHash))
console.log("Final Validator proofs:", finalValidatorsProof)
const finalValidatorsProofRaw = encoder.encode(
[
"tuple(uint8 v, bytes32 r, bytes32 s, uint256 index,address account,bytes32[] proof)[]",
],
[finalValidatorsProof]
)
fs.writeFileSync(
BeefyValidatorProofFile,
JSON.stringify({ finalValidatorsProof, finalValidatorsProofRaw }, null, 2),
"utf8"
)
console.log("Beefy fixture writing to dest file: " + BeefyValidatorProofFile)
}
}

run()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
7 changes: 5 additions & 2 deletions core/packages/contracts/scripts/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ class ValidatorSet {

function createRandomSubset(population: number, size: number) {
seedrandom("test", { global: true })
return _.runInContext()
.sampleSize(Array.from(Array(population).keys()), size)
const all = Array.from(Array(population).keys())
const participants = _.runInContext()
.sampleSize(all, size)
.sort((a, b) => a - b)
const absentees = all.filter((o) => !participants.includes(o))
return { participants, absentees }
}

export { encodeLog, createRandomSubset, ValidatorSet, readSetBits }
8 changes: 3 additions & 5 deletions core/packages/contracts/src/AgentExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.20;

import {ParaID} from "./Types.sol";
import {AgentExecuteCommand, ParaID} from "./Types.sol";
import {SubstrateTypes} from "./SubstrateTypes.sol";

import {IERC20} from "./interfaces/IERC20.sol";
Expand All @@ -14,15 +14,13 @@ contract AgentExecutor {
using SafeTokenTransfer for IERC20;
using SafeNativeTransfer for address payable;

bytes32 internal constant COMMAND_TRANSFER_TOKEN = keccak256("transferToken");

/// @dev Execute a message which originated from the Polkadot side of the bridge. In other terms,
/// the `data` parameter is constructed by the BridgeHub parachain.
///
/// NOTE: There are no authorization checks here. Since this contract is only used for its code.
function execute(address, bytes memory data) external {
(bytes32 command, bytes memory params) = abi.decode(data, (bytes32, bytes));
if (command == COMMAND_TRANSFER_TOKEN) {
(AgentExecuteCommand command, bytes memory params) = abi.decode(data, (AgentExecuteCommand, bytes));
if (command == AgentExecuteCommand.TransferToken) {
(address token, address recipient, uint128 amount) = abi.decode(params, (address, address, uint128));
_transferToken(token, recipient, amount);
}
Expand Down
10 changes: 5 additions & 5 deletions core/packages/contracts/src/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ library Assets {

_transferToAgent(assetHubAgent, token, sender, amount);
if (destinationChain == assetHubParaID) {
payload = SubstrateTypes.SendToken(assetHubAgent, token, destinationAddress, amount);
payload = SubstrateTypes.SendToken(address(this), token, destinationAddress, amount);
} else {
payload = SubstrateTypes.SendToken(assetHubAgent, token, destinationChain, destinationAddress, amount);
payload = SubstrateTypes.SendToken(address(this), token, destinationChain, destinationAddress, amount);
}
extraFee = $.sendTokenFee;

Expand All @@ -75,7 +75,7 @@ library Assets {

_transferToAgent(assetHubAgent, token, sender, amount);

payload = SubstrateTypes.SendToken(assetHubAgent, token, destinationChain, destinationAddress, amount);
payload = SubstrateTypes.SendToken(address(this), token, destinationChain, destinationAddress, amount);
extraFee = $.sendTokenFee;
emit TokenSent(sender, token, destinationChain, abi.encodePacked(destinationAddress), amount);
}
Expand All @@ -95,7 +95,7 @@ library Assets {

/// @dev Enqueues a create native token message to substrate.
/// @param token The ERC20 token address.
function registerToken(address gateway, address token, bytes2 createTokenCallID)
function registerToken(address token, bytes2 createTokenCallID)
external
returns (bytes memory payload, uint256 extraFee)
{
Expand All @@ -105,7 +105,7 @@ library Assets {
revert InvalidToken();
}

payload = SubstrateTypes.RegisterToken(gateway, token, createTokenCallID);
payload = SubstrateTypes.RegisterToken(address(this), token, createTokenCallID);
extraFee = $.registerTokenFee;

emit TokenRegistrationSent(token);
Expand Down
Loading

0 comments on commit 42ba62a

Please sign in to comment.