evm: update parseGuardianSet function name

This commit is contained in:
gator-boi 2023-10-04 16:46:46 -05:00
parent 5bd07f926f
commit 71e5682b4b
2 changed files with 49 additions and 49 deletions

View File

@ -13,13 +13,13 @@ contract Messages is Getters {
using BytesParsing for bytes;
function parseAndVerifyVMOptimized(
bytes calldata encodedVM,
bytes calldata guardianSet,
bytes calldata encodedVM,
bytes calldata guardianSet,
uint32 guardianSetIndex
) public view returns (Structs.VM memory vm, bool valid, string memory reason) {
// Verify that the specified guardian set is a valid.
// Verify that the specified guardian set is a valid.
require(
getGuardianSetHash(guardianSetIndex) == keccak256(guardianSet),
getGuardianSetHash(guardianSetIndex) == keccak256(guardianSet),
"invalid guardian set"
);
@ -28,13 +28,13 @@ contract Messages is Getters {
// Verify that the VM is signed with the same guardian set that was specified.
require(vm.guardianSetIndex == guardianSetIndex, "mismatched guardian set index");
(valid, reason) = verifyVMInternal(vm, parseGuardianSetOptimized(guardianSet), false);
(valid, reason) = verifyVMInternal(vm, parseGuardianSet(guardianSet), false);
}
function parseGuardianSetOptimized(bytes calldata guardianSetData) public pure returns (Structs.GuardianSet memory guardianSet) {
function parseGuardianSet(bytes calldata guardianSetData) public pure returns (Structs.GuardianSet memory guardianSet) {
// Fetch the guardian set length.
uint256 endGuardianKeyIndex = guardianSetData.length - 4;
uint256 guardianCount = endGuardianKeyIndex / 20;
uint256 endGuardianKeyIndex = guardianSetData.length - 4;
uint256 guardianCount = endGuardianKeyIndex / 20;
guardianSet = Structs.GuardianSet({
keys : new address[](guardianCount),
@ -45,11 +45,11 @@ contract Messages is Getters {
uint256 offset = 0;
for(uint256 i = 0; i < guardianCount;) {
(guardianSet.keys[i], offset) = guardianSetData.asAddressUnchecked(offset);
unchecked {
++i;
}
}
}
unchecked {
++i;
}
}
}
/// @dev parseAndVerifyVM serves to parse an encodedVM and wholy validate it for consumption
function parseAndVerifyVM(bytes calldata encodedVM) public view returns (Structs.VM memory vm, bool valid, string memory reason) {
@ -67,7 +67,7 @@ contract Messages is Getters {
* - it aims to verify the hash field provided against the contents of the vm
*/
function verifyVM(Structs.VM memory vm) public view returns (bool valid, string memory reason) {
(valid, reason) = verifyVMInternal(vm, getGuardianSet(vm.guardianSetIndex), true);
(valid, reason) = verifyVMInternal(vm, getGuardianSet(vm.guardianSetIndex), true);
}
/**
@ -179,7 +179,7 @@ contract Messages is Getters {
/// If we are here, we've validated that the provided signatures are valid for the provided guardianSet
return (true, "");
}
}
/**
* @dev parseVM serves to parse an encodedVM into a vm struct
@ -188,18 +188,18 @@ contract Messages is Getters {
function parseVM(bytes memory encodedVM) public view virtual returns (Structs.VM memory vm) {
uint256 offset = 0;
// SECURITY: Note that currently the VM.version is not part of the hash
// and for reasons described below it cannot be made part of the hash.
// This means that this field's integrity is not protected and cannot be trusted.
// This is not a problem today since there is only one accepted version, but it
// could be a problem if we wanted to allow other versions in the future.
// SECURITY: Note that currently the VM.version is not part of the hash
// and for reasons described below it cannot be made part of the hash.
// This means that this field's integrity is not protected and cannot be trusted.
// This is not a problem today since there is only one accepted version, but it
// could be a problem if we wanted to allow other versions in the future.
(vm.version, offset) = encodedVM.asUint8Unchecked(offset);
require(vm.version == 1, "invalid payload id");
// Guardian set index.
// Guardian set index.
(vm.guardianSetIndex, offset) = encodedVM.asUint32Unchecked(offset);
// Parse sigs.
// Parse sigs.
uint256 signersLen;
(signersLen, offset) = encodedVM.asUint8Unchecked(offset);
@ -209,18 +209,18 @@ contract Messages is Getters {
(vm.signatures[i].r, offset) = encodedVM.asBytes32Unchecked(offset);
(vm.signatures[i].s, offset) = encodedVM.asBytes32Unchecked(offset);
(vm.signatures[i].v, offset) = encodedVM.asUint8Unchecked(offset);
unchecked {
unchecked {
vm.signatures[i].v += 27;
++i;
++i;
}
}
/*
Hash the body
SECURITY: Do not change the way the hash of a VM is computed!
Changing it could result into two different hashes for the same observation.
SECURITY: Do not change the way the hash of a VM is computed!
Changing it could result into two different hashes for the same observation.
But xDapps rely on the hash of an observation for replay protection.
*/
bytes memory body;

View File

@ -11,15 +11,15 @@ import "forge-std/Test.sol";
import "forge-std/Vm.sol";
contract WormholeSigner is Test {
// Signer wallet.
// Signer wallet.
struct Wallet {
address addr;
uint256 key;
}
function encodeAndSignMessage(
Structs.VM memory vm_,
uint256[] memory guardianKeys,
Structs.VM memory vm_,
uint256[] memory guardianKeys,
uint32 guardianSetIndex
) public pure returns (bytes memory signedMessage) {
// Compute the hash of the body
@ -48,7 +48,7 @@ contract WormholeSigner is Test {
signatures,
body
);
}
}
}
contract ExportedMessages is Messages, Setters {
@ -66,11 +66,11 @@ contract TestMessages is Test {
uint256 constant testGuardian = 93941733246223705020089879371323733820373732307041878556247502674739205313440;
ExportedMessages messages;
WormholeSigner wormholeSimulator;
WormholeSigner wormholeSimulator;
Structs.GuardianSet guardianSet;
// Guardian set with 19 guardians and wallets with each signing key.
// Guardian set with 19 guardians and wallets with each signing key.
Structs.GuardianSet guardianSetOpt;
uint256[] guardianKeys = new uint256[](19);
@ -83,16 +83,16 @@ contract TestMessages is Test {
}
function setupMultiGuardian() internal {
// initialize guardian set with 19 guardians
// initialize guardian set with 19 guardians
address[] memory keys = new address[](19);
for (uint256 i = 0; i < 19; ++i) {
// create a keypair for each guardian
// create a keypair for each guardian
VmSafe.Wallet memory wallet = vm.createWallet(string(abi.encodePacked("guardian", i)));
keys[i] = wallet.addr;
guardianKeys[i] = wallet.privateKey;
keys[i] = wallet.addr;
guardianKeys[i] = wallet.privateKey;
}
guardianSetOpt = Structs.GuardianSet(keys, 0);
require(messages.quorum(guardianSetOpt.keys.length) == 13, "Quorum should be 13");
guardianSetOpt = Structs.GuardianSet(keys, 0);
require(messages.quorum(guardianSetOpt.keys.length) == 13, "Quorum should be 13");
}
function setUp() public {
@ -102,7 +102,7 @@ contract TestMessages is Test {
wormholeSimulator = new WormholeSigner();
setupSingleGuardian();
setupMultiGuardian();
}
}
function getSignedVM(
bytes memory payload,
@ -269,13 +269,13 @@ contract TestMessages is Test {
}
encodedGuardianSet = abi.encodePacked(encodedGuardianSet, guardianSetOpt.expirationTime);
// Parse the guardian set.
Structs.GuardianSet memory parsedSet = messages.parseGuardianSetOptimized(encodedGuardianSet);
// Parse the guardian set.
Structs.GuardianSet memory parsedSet = messages.parseGuardianSet(encodedGuardianSet);
// Validate the results by comparing the parsed set to the original set.
for (uint256 i = 0; i < guardianCount; ++i) {
assert(parsedSet.keys[i] == guardianSetOpt.keys[i]);
}
}
assert(parsedSet.expirationTime == guardianSetOpt.expirationTime);
}
@ -290,7 +290,7 @@ contract TestMessages is Test {
messages.storeGuardianSetPub(guardianSetOpt, currentSetIndex);
messages.setGuardianSetHash(currentSetIndex);
// Create a message with an arbitrary payload.
// Create a message with an arbitrary payload.
bytes memory signedMessage = getSignedVM(
payload,
emitterAddress,
@ -299,14 +299,14 @@ contract TestMessages is Test {
currentSetIndex
);
// Parse and verify the VM.
// Parse and verify the VM.
(Structs.VM memory vm_, bool valid,) = messages.parseAndVerifyVM(signedMessage);
assertEq(valid, true);
// Parse and verify the VM using the optimized endpoint.
// Parse and verify the VM using the optimized endpoint.
(Structs.VM memory vmOptimized, bool valid_,) = messages.parseAndVerifyVMOptimized(
signedMessage,
messages.getEncodedGuardianSet(currentSetIndex),
signedMessage,
messages.getEncodedGuardianSet(currentSetIndex),
currentSetIndex
);
assertEq(valid_, true);
@ -328,6 +328,6 @@ contract TestMessages is Test {
assertEq(vm_.signatures[i].r, vmOptimized.signatures[i].r);
assertEq(vm_.signatures[i].s, vmOptimized.signatures[i].s);
assertEq(vm_.signatures[i].v, vmOptimized.signatures[i].v);
}
}
}
}