All files / src/upgrade validators.ts

93.51% Statements 101/108
12.5% Branches 1/8
100% Functions 15/15
93.45% Lines 100/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 5004x                             4x 4x       4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x     4x     4x 4x                             4x                 4x                                                 21x 21x 21x 21x               18x               1x                                                                                       21x 21x 21x 21x 21x               3x                           1009x 1009x           1009x                           1022x 1022x 1022x 1022x 1022x 1022x 1022x 1022x     1022x 1022x                                                                     21x 21x 21x 21x 21x 21x                                                         9x 9x       9x     9x 9x                                       3x 3x       3x 3x                                 7x   7x                         14x 14x 14x             4x                                   6x           6x                               3x         3x                             5x 5x 5x       5x                       4x 4x 4x 4x                       4x 4x     4x 4x 4x 4x 20x 20x 20x       20x 20x     4x     4x         4x     4x       4x 3x     3x       3x     3x         4x    
import {
  Struct,
  Field,
  PublicKey,
  Signature,
  ZkProgram,
  Poseidon,
  SelfProof,
  UInt32,
  Experimental,
  DynamicProof,
  FeatureFlags,
  Void,
  Bool,
} from "o1js";
import { fieldFromString, Storage, PublicKeyOption } from "../contracts";
import { createIpfsURL, deserializeIndexedMerkleMap } from "zkcloudworker";
import { ValidatorsListData } from "./upgrade";
 
export {
  ValidatorsList,
  UpgradeAuthorityDatabase,
  ValidatorsState,
  ValidatorsDecision,
  ValidatorDecisionType,
  ValidatorsDecisionState,
  ValidatorsVoting,
  ValidatorsVotingProof,
  ValidatorsVotingNativeProof,
  UpgradeDatabaseState,
  UpgradeDatabaseStatePacked,
  ChainId,
  ValidatorsListData,
  checkValidatorsList,
};
 
const { IndexedMerkleMap } = Experimental;
type IndexedMerkleMap = Experimental.IndexedMerkleMap;
 
const VALIDATORS_LIST_HEIGHT = 10;
const UPGRADE_AUTHORITY_DATABASE_HEIGHT = 20;
 
/**
 * The `ValidatorsList` is an indexed Merkle map used to store the list of validators.
 */
class ValidatorsList extends IndexedMerkleMap(VALIDATORS_LIST_HEIGHT) {}
 
/**
 * The `UpgradeAuthorityDatabase` is an indexed Merkle map used to manage upgrade proposals.
 */
class UpgradeAuthorityDatabase extends IndexedMerkleMap(
  UPGRADE_AUTHORITY_DATABASE_HEIGHT
) {}
 
/** Chain IDs following Auro Wallet naming conventions. */
const ChainId = {
  "mina:mainnet": fieldFromString("mina:mainnet"),
  "mina:devnet": fieldFromString("mina:devnet"),
  "zeko:mainnet": fieldFromString("zeko:mainnet"),
  "zeko:devnet": fieldFromString("zeko:devnet"),
};
type ChainId = keyof typeof ChainId;
 
/** Validator decision types for upgrade proposals. */
const ValidatorDecisionType = {
  updateDatabase: fieldFromString("updateDatabase"),
  updateValidatorsList: fieldFromString("updateValidatorsList"),
} as const;
type ValidatorDecisionType = keyof typeof ValidatorDecisionType;
 
/**
 * Represents the state of the validators.
 */
class ValidatorsState extends Struct({
  /** Chain ID (e.g., 'mina:mainnet') */
  chainId: Field,
  /** Merkle root of the ValidatorsList */
  root: Field,
  /** Sum of the hashes of validators' public keys */
  hashSum: Field,
  /** Number of validators */
  count: UInt32,
}) {
  /**
   * Asserts that two `ValidatorsState` instances are equal.
   * @param a First `ValidatorsState` instance.
   * @param b Second `ValidatorsState` instance.
   */
  static assertEquals(a: ValidatorsState, b: ValidatorsState) {
    a.chainId.assertEquals(b.chainId);
    a.root.assertEquals(b.root);
    a.hashSum.assertEquals(b.hashSum);
    a.count.assertEquals(b.count);
  }
 
  /**
   * Computes the hash of the validators state.
   * @returns Hash of the current state.
   */
  hash() {
    return Poseidon.hashPacked(ValidatorsState, this);
  }
 
  /**
   * Returns an empty `ValidatorsState`.
   * @returns An empty `ValidatorsState` instance.
   */
  static empty() {
    return new ValidatorsState({
      chainId: Field(0),
      root: Field(0),
      hashSum: Field(0),
      count: UInt32.zero,
    });
  }
}
 
/**
 * Represents the packed state of the upgrade database.
 */
class UpgradeDatabaseStatePacked extends Struct({
  /** Root of the UpgradeAuthority database */
  root: Field,
  /** Storage information (e.g., IPFS hash) */
  storage: Storage,
  /** X-coordinate of the next upgrade authority's public key */
  nextUpgradeAuthorityX: Field,
  /** Packed data containing version, validFrom, and flags */
  data: Field,
}) {}
 
/**
 * Represents the state of the upgrade database.
 */
class UpgradeDatabaseState extends Struct({
  /** Root of the UpgradeAuthority database */
  root: Field,
  /** Storage information (e.g., IPFS hash) */
  storage: Storage,
  /** Optional public key of the next upgrade authority */
  nextUpgradeAuthority: PublicKeyOption,
  /** Version of the UpgradeAuthorityDatabase */
  version: UInt32,
  /** Slot when the UpgradeAuthority is valid from */
  validFrom: UInt32,
}) {
  /**
   * Asserts that two `UpgradeDatabaseState` instances are equal.
   * @param a First `UpgradeDatabaseState` instance.
   * @param b Second `UpgradeDatabaseState` instance.
   */
  static assertEquals(a: UpgradeDatabaseState, b: UpgradeDatabaseState) {
    a.root.assertEquals(b.root);
    Storage.assertEquals(a.storage, b.storage);
    a.nextUpgradeAuthority.value.assertEquals(b.nextUpgradeAuthority.value);
    a.nextUpgradeAuthority.isSome.assertEquals(b.nextUpgradeAuthority.isSome);
    a.version.assertEquals(b.version);
  }
 
  /**
   * Returns an empty `UpgradeDatabaseState`.
   * @returns An empty `UpgradeDatabaseState` instance.
   */
  static empty() {
    return new UpgradeDatabaseState({
      root: new UpgradeAuthorityDatabase().root,
      storage: Storage.empty(),
      nextUpgradeAuthority: PublicKeyOption.none(),
      version: UInt32.zero,
      validFrom: UInt32.MAXINT(),
    });
  }
 
  /**
   * Packs the `UpgradeDatabaseState` into a `UpgradeDatabaseStatePacked`.
   * @returns A packed representation of the upgrade database state.
   */
  pack(): UpgradeDatabaseStatePacked {
    const nextUpgradeAuthorityX = this.nextUpgradeAuthority.value.x;
    const data = Field.fromBits([
      ...this.version.value.toBits(32),
      ...this.validFrom.value.toBits(32),
      this.nextUpgradeAuthority.value.isOdd,
      this.nextUpgradeAuthority.isSome,
    ]);
    return new UpgradeDatabaseStatePacked({
      root: this.root,
      storage: this.storage,
      nextUpgradeAuthorityX,
      data,
    });
  }
 
  /**
   * Unpacks a `UpgradeDatabaseStatePacked` into a `UpgradeDatabaseState`.
   * @param packed The packed upgrade database state.
   * @returns An unpacked `UpgradeDatabaseState` instance.
   */
  static unpack(packed: UpgradeDatabaseStatePacked): UpgradeDatabaseState {
    const bits = packed.data.toBits(66);
    const versionBits = bits.slice(0, 32);
    const validFromBits = bits.slice(32, 64);
    const isOddBit = bits[64];
    const isSomeBit = bits[65];
    const version = UInt32.Unsafe.fromField(Field.fromBits(versionBits));
    const validFrom = UInt32.Unsafe.fromField(Field.fromBits(validFromBits));
    const nextUpgradeAuthority = PublicKeyOption.from(
      PublicKey.from({ x: packed.nextUpgradeAuthorityX, isOdd: isOddBit })
    );
    nextUpgradeAuthority.isSome = isSomeBit;
    return new UpgradeDatabaseState({
      root: packed.root,
      storage: packed.storage,
      nextUpgradeAuthority,
      version,
      validFrom,
    });
  }
}
 
/**
 * Represents a decision made by the validators.
 */
class ValidatorsDecision extends Struct({
  /** Type of decision (e.g., 'updateDatabase') */
  decisionType: Field,
  /** UpgradeAuthority contract address */
  contractAddress: PublicKey,
  /** Chain ID */
  chainId: Field,
  /** Current validators state */
  validators: ValidatorsState,
  /** Current upgrade database state */
  upgradeDatabase: UpgradeDatabaseState,
  /** Proposed update to validators state */
  updateValidatorsList: ValidatorsState,
  /** Slot when decision expires */
  expiry: UInt32,
}) {
  /**
   * Asserts that two `ValidatorsDecision` instances are equal.
   * @param a First `ValidatorsDecision` instance.
   * @param b Second `ValidatorsDecision` instance.
   */
  static assertEquals(a: ValidatorsDecision, b: ValidatorsDecision) {
    a.decisionType.assertEquals(b.decisionType);
    a.contractAddress.assertEquals(b.contractAddress);
    a.chainId.assertEquals(b.chainId);
    ValidatorsState.assertEquals(a.validators, b.validators);
    UpgradeDatabaseState.assertEquals(a.upgradeDatabase, b.upgradeDatabase);
    a.expiry.assertEquals(b.expiry);
  }
}
 
/**
 * Represents the state of a validators decision during the voting process.
 */
class ValidatorsDecisionState extends Struct({
  /** The validators' decision */
  decision: ValidatorsDecision,
  /** Sum of the hashes of validators who have voted */
  hashSum: Field,
  /** Count of votes in favor */
  count: UInt32,
}) {
  /**
   * Records a vote in favor of the decision by the given validator.
   * @param decision The validators' decision.
   * @param validatorAddress The public key of the validator.
   * @param validatorsList The ValidatorsList.
   * @param signature The signature of the validator.
   * @returns A new `ValidatorsDecisionState` reflecting the vote.
   */
  static vote(
    decision: ValidatorsDecision,
    validatorAddress: PublicKey,
    validatorsList: ValidatorsList,
    signature: Signature
  ) {
    const hash = Poseidon.hashPacked(PublicKey, validatorAddress);
    validatorsList
      .get(hash)
      .assertBool("Wrong ValidatorsList format")
      .assertTrue("Validator doesn't have authority to sign");
    signature
      .verify(validatorAddress, ValidatorsDecision.toFields(decision))
      .assertTrue("Wrong validator signature");
    decision.validators.root.assertEquals(validatorsList.root);
    return new ValidatorsDecisionState({
      decision,
      count: UInt32.from(1), // count as vote for the decision
      hashSum: hash,
    });
  }
 
  /**
   * Records an abstention or vote against the decision by the given validator.
   * @param decision The validators' decision.
   * @param validatorAddress The public key of the validator.
   * @param validatorsList The ValidatorsList.
   * @returns A new `ValidatorsDecisionState` reflecting the abstention.
   */
  static abstain(
    decision: ValidatorsDecision,
    validatorAddress: PublicKey,
    validatorsList: ValidatorsList
    // We do not require the signature if the vote is not for the decision
  ) {
    const hash = Poseidon.hashPacked(PublicKey, validatorAddress);
    validatorsList
      .get(hash)
      .assertBool("Wrong ValidatorsList format")
      .assertTrue("Validator doesn't have authority to sign");
    decision.validators.root.assertEquals(validatorsList.root);
    return new ValidatorsDecisionState({
      decision,
      count: UInt32.zero, // count as abstain or against
      hashSum: hash,
    });
  }
 
  /**
   * Merges two `ValidatorsDecisionState` instances.
   * @param state1 The first decision state.
   * @param state2 The second decision state.
   * @returns A new `ValidatorsDecisionState` representing the combined state.
   */
  static merge(
    state1: ValidatorsDecisionState,
    state2: ValidatorsDecisionState
  ) {
    ValidatorsDecision.assertEquals(state1.decision, state2.decision);
 
    return new ValidatorsDecisionState({
      decision: state1.decision,
      count: state1.count.add(state2.count),
      hashSum: state1.hashSum.add(state2.hashSum),
    });
  }
 
  /**
   * Asserts that two `ValidatorsDecisionState` instances are equal.
   * @param a First `ValidatorsDecisionState` instance.
   * @param b Second `ValidatorsDecisionState` instance.
   */
  static assertEquals(a: ValidatorsDecisionState, b: ValidatorsDecisionState) {
    ValidatorsDecision.assertEquals(a.decision, b.decision);
    a.count.assertEquals(b.count);
    a.hashSum.assertEquals(b.hashSum);
  }
}
 
/**
 * The `ValidatorsVoting` ZkProgram implements the voting logic for validators.
 */
const ValidatorsVoting = ZkProgram({
  name: "ValidatorsVoting",
  publicInput: ValidatorsDecisionState,
 
  methods: {
    /**
     * Records a vote in favor of a decision.
     */
    vote: {
      privateInputs: [ValidatorsDecision, PublicKey, ValidatorsList, Signature],
 
      async method(
        state: ValidatorsDecisionState,
        decision: ValidatorsDecision,
        validatorAddress: PublicKey,
        validatorsList: ValidatorsList,
        signature: Signature
      ) {
        const calculatedState = ValidatorsDecisionState.vote(
          decision,
          validatorAddress,
          validatorsList,
          signature
        );
        ValidatorsDecisionState.assertEquals(state, calculatedState);
      },
    },
 
    /**
     * Records an abstention or vote against a decision.
     */
    abstain: {
      privateInputs: [ValidatorsDecision, PublicKey, ValidatorsList],
 
      async method(
        state: ValidatorsDecisionState,
        decision: ValidatorsDecision,
        validatorAddress: PublicKey,
        validatorsList: ValidatorsList
      ) {
        const calculatedState = ValidatorsDecisionState.abstain(
          decision,
          validatorAddress,
          validatorsList
        );
        ValidatorsDecisionState.assertEquals(state, calculatedState);
      },
    },
 
    /**
     * Merges two `ValidatorsDecisionState` proofs.
     */
    merge: {
      privateInputs: [SelfProof, SelfProof],
 
      async method(
        state: ValidatorsDecisionState,
        proof1: SelfProof<ValidatorsDecisionState, void>,
        proof2: SelfProof<ValidatorsDecisionState, void>
      ) {
        proof1.verify();
        proof2.verify();
        const calculatedState = ValidatorsDecisionState.merge(
          proof1.publicInput,
          proof2.publicInput
        );
        ValidatorsDecisionState.assertEquals(state, calculatedState);
      },
    },
  },
});
 
/** Proof classes for the `ValidatorsVoting` ZkProgram. */
class ValidatorsVotingNativeProof extends ZkProgram.Proof(ValidatorsVoting) {}
class ValidatorsVotingProof extends DynamicProof<
  ValidatorsDecisionState,
  Void
> {
  static publicInputType = ValidatorsDecisionState;
  static publicOutputType = Void;
  static maxProofsVerified = 2 as const;
  static featureFlags = FeatureFlags.allMaybe;
}
 
/**
 * Checks the validators list against the provided storage.
 * @param params Object containing validators state and storage.
 * @returns An object containing the `ValidatorsList` and `ValidatorsListData`.
 */
async function checkValidatorsList(params: {
  validators?: ValidatorsState;
  storage: Storage;
}): Promise<{ map: ValidatorsList; data: ValidatorsListData }> {
  const { validators, storage } = params;
  const data = (await (
    await fetch(createIpfsURL({ hash: storage.toString() }))
  ).json()) as unknown as ValidatorsListData;
  const validatorsList = new ValidatorsList();
  let hashSum = Field(0);
  let count = 0;
  for (let i = 0; i < data.validators.length; i++) {
    const publicKey = PublicKey.fromBase58(data.validators[i].publicKey);
    const key = Poseidon.hashPacked(PublicKey, publicKey);
    validatorsList.set(
      key,
      Field(Bool(data.validators[i].authorizedToVote).value)
    );
    count++;
    hashSum = hashSum.add(key);
  }
 
  Iif (data.hashSum !== hashSum.toJSON()) {
    throw new Error("Invalid validators list hash sum");
  }
  const map = deserializeIndexedMerkleMap({
    serializedIndexedMap: data.map,
    type: ValidatorsList,
  });
 
  Iif (!map) {
    throw new Error("Invalid validators list");
  }
  Iif (map.root.equals(validatorsList.root).toBoolean() === false) {
    throw new Error("Invalid validators list root");
  }
 
  if (validators) {
    Iif (data.validatorsCount !== Number(validators.count.toBigint())) {
      throw new Error("Invalid validators list count");
    }
    Iif (data.root !== validators.root.toJSON()) {
      throw new Error("Invalid validators list root");
    }
 
    Iif (hashSum.equals(validators.hashSum).toBoolean() === false) {
      throw new Error("Invalid validators list hash sum");
    }
    Iif (Number(validators.count.toBigint()) > count) {
      throw new Error("Invalid validators list count");
    }
  }
 
  return { map, data };
}