From 8875735581b971fd36e640aae0af8c348dfda214 Mon Sep 17 00:00:00 2001 From: Jonathan Claudius Date: Tue, 7 Jun 2022 09:45:47 -0400 Subject: [PATCH] Move quorum out of verifyVM (#1162) * Move quorum out of verifyVM * Add stub out to demonstrate sol-based tests * Remove unnecessary import * Neaten up the quorum conditional * Another iteration on figuring out the test logic * Fix indentation * Refactor quorum and setup unit-test cases * fix return naming * More variable name clean up * Move length check inside loop structure * Fix condition by wrapping * Fix syntax error * fix type definition for testCases * Drop inline struct usage for simple 2d array * Fix logic in quorum check * Make test public and fully qualify quorum * Make wormhole.sol fail * Specify where to store testCases * Define array directly * Fix syntax error on array definition * Fix variable name * Move back to memory-based variable * Add remove qualified quorum call * Get really direct on quorum tests * Simplify quorum logic * Add test for 19 guardians --- ethereum/contracts/Messages.sol | 9 ++++++++- ethereum/test/wormhole.sol | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 ethereum/test/wormhole.sol diff --git a/ethereum/contracts/Messages.sol b/ethereum/contracts/Messages.sol index d41053965..18bf4d51b 100644 --- a/ethereum/contracts/Messages.sol +++ b/ethereum/contracts/Messages.sol @@ -51,7 +51,7 @@ contract Messages is Getters { * if making any changes to this, obtain additional peer review. If guardianSet key length is 0 and * vm.signatures length is 0, this could compromise the integrity of both vm and signature verification. */ - if(((guardianSet.keys.length * 10 / 3) * 2) / 10 + 1 > vm.signatures.length){ + if (vm.signatures.length < quorum(guardianSet.keys.length)){ return (false, "no quorum"); } @@ -145,4 +145,11 @@ contract Messages is Getters { vm.payload = encodedVM.slice(index, encodedVM.length - index); } + + /** + * @dev quorum serves solely to determine the number of signatures required to acheive quorum + */ + function quorum(uint numGuardians) public pure virtual returns (uint numSignaturesRequiredForQuorum) { + return ((numGuardians * 2) / 3) + 1; + } } diff --git a/ethereum/test/wormhole.sol b/ethereum/test/wormhole.sol new file mode 100644 index 000000000..f24807a87 --- /dev/null +++ b/ethereum/test/wormhole.sol @@ -0,0 +1,27 @@ +// contracts/Messages.sol +// SPDX-License-Identifier: Apache 2 + +pragma solidity ^0.8.0; + +import "truffle/Assert.sol"; +import "../contracts/Messages.sol"; + +contract TestMessages is Messages { + function testQuorum() public { + Assert.equal(quorum(0), 1, "it should return quorum"); + Assert.equal(quorum(1), 1, "it should return quorum"); + Assert.equal(quorum(2), 2, "it should return quorum"); + Assert.equal(quorum(3), 3, "it should return quorum"); + Assert.equal(quorum(4), 3, "it should return quorum"); + Assert.equal(quorum(5), 4, "it should return quorum"); + Assert.equal(quorum(6), 5, "it should return quorum"); + Assert.equal(quorum(7), 5, "it should return quorum"); + Assert.equal(quorum(8), 6, "it should return quorum"); + Assert.equal(quorum(9), 7, "it should return quorum"); + Assert.equal(quorum(10), 7, "it should return quorum"); + Assert.equal(quorum(11), 8, "it should return quorum"); + Assert.equal(quorum(12), 9, "it should return quorum"); + Assert.equal(quorum(19), 13, "it should return quorum"); + Assert.equal(quorum(20), 14, "it should return quorum"); + } +} \ No newline at end of file