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

Generate beefy checkpoint from runtime storage #1197

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
56 changes: 36 additions & 20 deletions web/packages/test-helpers/src/generateBeefyCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { ApiPromise, WsProvider } from "@polkadot/api"
import { MerkleTree } from "merkletreejs"
import createKeccakHash from "keccak"
import { publicKeyConvert } from "secp256k1"
import type {
ValidatorSetId,
BeefyId,
} from "@polkadot/types/interfaces/beefy/types"
import type { ValidatorSetId, BeefyId } from "@polkadot/types/interfaces/beefy/types"
import fs from "fs"
import path from "path"
import { u32, u64 } from "@polkadot/types-codec";
import { H256 } from "@polkadot/types/interfaces";
import { Struct } from "@polkadot/types";

interface NextAuthoritySet extends Struct {
id: u64;
len: u32;
keysetCommitment: H256;
import { u32, u64 } from "@polkadot/types-codec"
import { H256 } from "@polkadot/types/interfaces"
import { Struct } from "@polkadot/types"

interface AuthoritySet extends Struct {
id: u64
len: u32
keysetCommitment: H256
}

async function generateBeefyCheckpoint() {
Expand Down Expand Up @@ -50,23 +47,42 @@ async function generateBeefyCheckpoint() {
const validatorSetId = await api.query.beefy.validatorSetId<ValidatorSetId>()
const authorities = await api.query.beefy.authorities<BeefyId[]>()

console.log("validatorSetId:", validatorSetId)
console.log("authority length is:" + authorities.length)

let addrs = []

for (let i = 0; i < authorities.length; i++) {
let publicKey = publicKeyConvert(authorities[i], false).slice(1)
let publicKeyHashed = createKeccakHash("keccak256").update(Buffer.from(publicKey)).digest()
addrs.push(publicKeyHashed.slice(12))
console.log("index is:" + i + ",authority is:" + authorities[i])
try {
let publicKey = publicKeyConvert(authorities[i], false).slice(1)
let publicKeyHashed = createKeccakHash("keccak256")
.update(Buffer.from(publicKey))
.digest()
addrs.push(publicKeyHashed.slice(12))
Copy link
Contributor

Choose a reason for hiding this comment

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

Wont this change the length of the list of validator addresses, which in turn will affect the merkle tree and eventual root? If the same logic is applied in the relayer then I think its fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As the title mentioned, we don't manually construct the merkle root from the addresses as before, instead get currentAuthorities from runtime storage. The printing here is only for debug purpose.

} catch (err) {
console.log(err)
}
}

const tree = createMerkleTree(addrs)
console.log("valid authority length is:" + addrs.length)

const nextAuthorities = await api.query.mmrLeaf.beefyNextAuthorities<NextAuthoritySet>()
let currentAuthorities, nextAuthorities

if (process.env.NODE_ENV == "production") {
currentAuthorities = await api.query.beefyMmrLeaf.beefyAuthorities<AuthoritySet>()
nextAuthorities = await api.query.beefyMmrLeaf.beefyNextAuthorities<AuthoritySet>()
} else {
currentAuthorities = await api.query.mmrLeaf.beefyAuthorities<AuthoritySet>()
nextAuthorities = await api.query.mmrLeaf.beefyNextAuthorities<AuthoritySet>()
}

const beefyCheckpoint = {
startBlock: beefyStartBlock,
current: {
id: validatorSetId.toNumber(),
root: tree.getHexRoot(),
length: addrs.length,
id: currentAuthorities.id.toNumber(),
root: currentAuthorities.keysetCommitment.toHex(),
length: currentAuthorities.len.toNumber(),
},
next: {
id: nextAuthorities.id.toNumber(),
Expand Down