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

Improve beefy tests #901

Merged
merged 10 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/packages/contracts/.envrc-example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source_up_if_exists
## BeefyClient
export RANDAO_COMMIT_DELAY=3
export RANDAO_COMMIT_EXP=8
export PREV_RANDAO=377

## 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)
4 changes: 3 additions & 1 deletion core/packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next steps - to implement in a new ticket - let's move this script and helpers.ts to a new package under core/packages. We can call the package test-helpers or something like that.

The goal is to remove javascript stuff from the contracts project.

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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a new package under core/packages with our contract bindings. Can call the package directory contract-types, and the package name @snowbridge/contract-types.

You'll need to include this script command in the new package:

"build:types": "typechain --target ethers-v5 'out/**/!(*.t|test).sol/!(*.abi).json' --out-dir types",

Then this dependency should change to:

import type { BeefyClient } from "@snowbridge/contract-types"

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 }
Loading
Loading