Skip to content

Commit

Permalink
chore: remove unnecessary BRIDGE_ID (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id authored Nov 28, 2022
1 parent 5f3d1ff commit 362e7da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 73 deletions.
31 changes: 7 additions & 24 deletions src/QuantumGravityBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ contract QuantumGravityBridge is IDAOracle {
// be accounted for in any upgrades. Always test an exact upgrade on testnet
// and localhost before mainnet upgrades.

////////////////
// Immutables //
////////////////

bytes32 public immutable BRIDGE_ID;

/////////////
// Storage //
/////////////
Expand Down Expand Up @@ -99,25 +93,20 @@ contract QuantumGravityBridge is IDAOracle {
// Functions //
///////////////

/// @param _bridge_id Identifier of the bridge, used in signatures for
/// domain separation.
/// @param _nonce Initial event nonce.
/// @param _powerThreshold Initial voting power that is needed to approve
/// operations.
/// @param _validatorSetHash Initial validator set hash. This does not need
/// to be the genesis validator set of the bridged chain, only the initial
/// validator set of the bridge.
constructor(
bytes32 _bridge_id,
uint256 _nonce,
uint256 _powerThreshold,
bytes32 _validatorSetHash
) {
BRIDGE_ID = _bridge_id;

// CHECKS

bytes32 newCheckpoint = domainSeparateValidatorSetHash(_bridge_id, _nonce, _powerThreshold, _validatorSetHash);
bytes32 newCheckpoint = domainSeparateValidatorSetHash(_nonce, _powerThreshold, _validatorSetHash);

// EFFECTS

Expand Down Expand Up @@ -156,20 +145,18 @@ contract QuantumGravityBridge is IDAOracle {
/// @dev Make a domain-separated commitment to the validator set.
/// A hash of all relevant information about the validator set.
/// The format of the hash is:
/// keccak256(bridge_id, VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, nonce, power_threshold, validator_set_hash)
/// keccak256(VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, nonce, power_threshold, validator_set_hash)
/// The elements in the validator set should be monotonically decreasing by power.
/// @param _bridge_id Bridge ID.
/// @param _nonce Nonce.
/// @param _powerThreshold The voting power threshold.
/// @param _validatorSetHash Validator set hash.
function domainSeparateValidatorSetHash(
bytes32 _bridge_id,
uint256 _nonce,
uint256 _powerThreshold,
bytes32 _validatorSetHash
) private pure returns (bytes32) {
bytes32 c = keccak256(
abi.encode(_bridge_id, VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, _nonce, _powerThreshold, _validatorSetHash)
abi.encode(VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, _nonce, _powerThreshold, _validatorSetHash)
);

return c;
Expand All @@ -178,17 +165,15 @@ contract QuantumGravityBridge is IDAOracle {
/// @dev Make a domain-separated commitment to a data root tuple root.
/// A hash of all relevant information about a data root tuple root.
/// The format of the hash is:
/// keccak256(bridge_id, DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, nonce, dataRootTupleRoot)
/// @param _bridge_id Bridge ID.
/// keccak256(DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, nonce, dataRootTupleRoot)
/// @param _nonce Event nonce.
/// @param _dataRootTupleRoot Data root tuple root.
function domainSeparateDataRootTupleRoot(
bytes32 _bridge_id,
uint256 _nonce,
bytes32 _dataRootTupleRoot
) private pure returns (bytes32) {
bytes32 c = keccak256(
abi.encode(_bridge_id, DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, _nonce, _dataRootTupleRoot)
abi.encode(DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, _nonce, _dataRootTupleRoot)
);

return c;
Expand Down Expand Up @@ -276,15 +261,14 @@ contract QuantumGravityBridge is IDAOracle {
// Check that the supplied current validator set matches the saved checkpoint.
bytes32 currentValidatorSetHash = computeValidatorSetHash(_currentValidatorSet);
if (
domainSeparateValidatorSetHash(BRIDGE_ID, _oldNonce, currentPowerThreshold, currentValidatorSetHash) !=
domainSeparateValidatorSetHash(_oldNonce, currentPowerThreshold, currentValidatorSetHash) !=
state_lastValidatorSetCheckpoint
) {
revert SuppliedValidatorSetInvalid();
}

// Check that enough current validators have signed off on the new validator set.
bytes32 newCheckpoint = domainSeparateValidatorSetHash(
BRIDGE_ID,
_newNonce,
_newPowerThreshold,
_newValidatorSetHash
Expand Down Expand Up @@ -346,7 +330,6 @@ contract QuantumGravityBridge is IDAOracle {
bytes32 currentValidatorSetHash = computeValidatorSetHash(_currentValidatorSet);
if (
domainSeparateValidatorSetHash(
BRIDGE_ID,
_validatorSetNonce,
currentPowerThreshold,
currentValidatorSetHash
Expand All @@ -357,7 +340,7 @@ contract QuantumGravityBridge is IDAOracle {

// Check that enough current validators have signed off on the data
// root tuple root and nonce.
bytes32 c = domainSeparateDataRootTupleRoot(BRIDGE_ID, _newNonce, _dataRootTupleRoot);
bytes32 c = domainSeparateDataRootTupleRoot(_newNonce, _dataRootTupleRoot);
checkValidatorSignatures(_currentValidatorSet, _sigs, c, currentPowerThreshold);

// EFFECTS
Expand Down
15 changes: 5 additions & 10 deletions src/test/QuantumGravityBridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ contract RelayerTest is DSTest {
uint256 constant testPriv1 = 0x64a1d6f0e760a8d62b4afdde4096f16f51b401eaaecc915740f71770ea76a8ad;
uint256 constant testPriv2 = 0x6e8bdfa979ab645b41c4d17cb1329b2a44684c82b61b1b060ea9b6e1c927a4f4;

// 32 bytes, chosen at random.
bytes32 constant BRIDGE_ID = 0x6de92bac0b357560d821f8e7b6f5c9fe4f3f88f6c822283efd7ab51ad56a640e;

QuantumGravityBridge bridge;

Validator[] private validators;
Expand All @@ -43,7 +40,7 @@ contract RelayerTest is DSTest {

validators.push(Validator(cheats.addr(testPriv1), votingPower));
bytes32 hash = computeValidatorSetHash(validators);
bridge = new QuantumGravityBridge(BRIDGE_ID, initialVelsetNonce, (2 * votingPower) / 3, hash);
bridge = new QuantumGravityBridge(initialVelsetNonce, (2 * votingPower) / 3, hash);
}

function testUpdateValidatorSet() public {
Expand All @@ -58,7 +55,7 @@ contract RelayerTest is DSTest {
votingPower += votingPower;
uint256 newPowerThreshold = (2 * votingPower) / 3;
bytes32 newVSHash = keccak256(abi.encode(validators));
bytes32 newCheckpoint = domainSeparateValidatorSetHash(BRIDGE_ID, newNonce, newPowerThreshold, newVSHash);
bytes32 newCheckpoint = domainSeparateValidatorSetHash(newNonce, newPowerThreshold, newVSHash);

// Signature for the first validator set update.
Signature[] memory sigs = new Signature[](1);
Expand All @@ -79,7 +76,7 @@ contract RelayerTest is DSTest {
// 32 bytes, chosen at random.
bytes32 newTupleRoot = 0x0de92bac0b356560d821f8e7b6f5c9fe4f3f88f6c822283efd7ab51ad56a640e;

bytes32 newDataRootTupleRoot = domainSeparateDataRootTupleRoot(BRIDGE_ID, nonce, newTupleRoot);
bytes32 newDataRootTupleRoot = domainSeparateDataRootTupleRoot(nonce, newTupleRoot);

// Signature for the update.
Signature[] memory sigs = new Signature[](1);
Expand All @@ -101,25 +98,23 @@ contract RelayerTest is DSTest {
}

function domainSeparateValidatorSetHash(
bytes32 _bridge_id,
uint256 _nonce,
uint256 _powerThreshold,
bytes32 _validatorSetHash
) private pure returns (bytes32) {
bytes32 c = keccak256(
abi.encode(_bridge_id, VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, _nonce, _powerThreshold, _validatorSetHash)
abi.encode(VALIDATOR_SET_HASH_DOMAIN_SEPARATOR, _nonce, _powerThreshold, _validatorSetHash)
);

return c;
}

function domainSeparateDataRootTupleRoot(
bytes32 _bridge_id,
uint256 _nonce,
bytes32 _dataRootTupleRoot
) private pure returns (bytes32) {
bytes32 c = keccak256(
abi.encode(_bridge_id, DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, _nonce, _dataRootTupleRoot)
abi.encode(DATA_ROOT_TUPLE_ROOT_DOMAIN_SEPARATOR, _nonce, _dataRootTupleRoot)
);

return c;
Expand Down
Loading

0 comments on commit 362e7da

Please sign in to comment.