Skip to content

Commit

Permalink
Fix validator set and batch storage (#36)
Browse files Browse the repository at this point in the history
* fix

* NewValidatorSetStored event

* comments fix
  • Loading branch information
goran-ethernal authored Sep 23, 2024
1 parent 37185ef commit 0da2a77
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 17 deletions.
48 changes: 44 additions & 4 deletions contracts/blade/BridgeStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,52 @@ pragma solidity ^0.8.19;
import "./ValidatorSetStorage.sol";

contract BridgeStorage is ValidatorSetStorage {
mapping(uint256 => BridgeMessageBatch) public batches;
mapping(uint256 => SignedBridgeMessageBatch) public batches;
mapping(uint256 => SignedValidatorSet) public commitedValidatorSets;
mapping(uint256 => uint256) public lastCommitted;
mapping(uint256 => uint256) public lastCommittedInternal;
/// @custom:security write-protection="onlySystemCall()"
uint256 public batchCounter;
/// @custom:security write-protection="onlySystemCall()"
uint256 public validatorSetCounter;

event NewBatch(uint256 indexed id);
event NewValidatorSetStored(uint256 indexed id);

/**
* @notice commits new validator set
* @param newValidatorSet new validator set
* @param signature aggregated signature of validators that signed the new validator set
* @param bitmap bitmap of which validators signed the message
*/
function commitValidatorSet(
Validator[] calldata newValidatorSet,
uint256[2] calldata signature,
bytes calldata bitmap
) external override onlySystemCall {
_commitValidatorSet(newValidatorSet, signature, bitmap);

SignedValidatorSet storage signedValidatorSet = commitedValidatorSets[validatorSetCounter];
signedValidatorSet.signature = signature;
signedValidatorSet.bitmap = bitmap;

event NewBatch(uint256 id);
for (uint256 i = 0; i < newValidatorSet.length; ) {
signedValidatorSet.newValidatorSet[i] = newValidatorSet[i];
unchecked {
++i;
}
}

emit NewValidatorSetStored(validatorSetCounter);

validatorSetCounter++;
}

/**
* @notice commits new batch
* @param batch new batch
* @param signature aggregated signature of validators that signed the new batch
* @param bitmap bitmap of which validators signed the message
*/
function commitBatch(
BridgeMessageBatch calldata batch,
Expand All @@ -26,9 +61,14 @@ contract BridgeStorage is ValidatorSetStorage {
bytes memory hash = abi.encode(keccak256(abi.encode(batch)));
verifySignature(bls.hashToPoint(DOMAIN_BRIDGE, hash), signature, bitmap);

batches[batchCounter++] = batch;
SignedBridgeMessageBatch storage signedBatch = batches[batchCounter];
signedBatch.batch = batch;
signedBatch.signature = signature;
signedBatch.bitmap = bitmap;

emit NewBatch(batchCounter);

emit NewBatch(batchCounter - 1);
batchCounter++;
}

/**
Expand Down
36 changes: 26 additions & 10 deletions contracts/blade/ValidatorSetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
Validator[] calldata newValidatorSet,
uint256[2] calldata signature,
bytes calldata bitmap
) external onlySystemCall {
require(newValidatorSet.length > 0, "EMPTY_VALIDATOR_SET");

bytes memory hash = abi.encode(keccak256(abi.encode(newValidatorSet)));

verifySignature(bls.hashToPoint(DOMAIN_VALIDATOR_SET, hash), signature, bitmap);

_setNewValidatorSet(newValidatorSet);

emit NewValidatorSet(newValidatorSet);
) external virtual onlySystemCall {
_commitValidatorSet(newValidatorSet, signature, bitmap);
}

/**
Expand Down Expand Up @@ -136,4 +128,28 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
// Get the value of the bit at the given 'index' in a byte.
return uint8(bitmap[byteNumber]) & (1 << bitNumber) > 0;
}

/**
* @notice Commits a new validator set by verifying its signature and setting it in the storage.
* @dev This function requires that the new validator set is non-empty and verifies its signature before updating the validator set.
* @param newValidatorSet The array of validators to be set as the new validator set
* @param signature The aggregated signature of the validators that signed the new validator set
* @param bitmap The bitmap representing which validators signed the new validator set
* Emits a `NewValidatorSet` event after successfully setting the new validator set.
*/
function _commitValidatorSet(
Validator[] calldata newValidatorSet,
uint256[2] calldata signature,
bytes calldata bitmap
) internal {
require(newValidatorSet.length > 0, "EMPTY_VALIDATOR_SET");

bytes memory hash = abi.encode(keccak256(abi.encode(newValidatorSet)));

verifySignature(bls.hashToPoint(DOMAIN_VALIDATOR_SET, hash), signature, bitmap);

_setNewValidatorSet(newValidatorSet);

emit NewValidatorSet(newValidatorSet);
}
}
22 changes: 22 additions & 0 deletions contracts/interfaces/blade/IValidatorSetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ struct BridgeMessageBatch {
uint256 destinationChainId;
}

/**
* @param batch batch that is signed
* @param signature aggregated signature of validators that signed the batch
* @param bitmap bitmap of which validators signed the message
*/
struct SignedBridgeMessageBatch {
BridgeMessageBatch batch;
uint256[2] signature;
bytes bitmap;
}

/**
* @param newValidatorSet new validator set
* @param signature aggregated signature of validators that signed the new validator set
* @param bitmap bitmap of which validators signed the message
*/
struct SignedValidatorSet {
Validator[] newValidatorSet;
uint256[2] signature;
bytes bitmap;
}

interface IValidatorSetStorage {
event NewValidatorSet(Validator[] newValidatorSet);

Expand Down
45 changes: 42 additions & 3 deletions docs/blade/BridgeStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function batchCounter() external view returns (uint256)
### batches

```solidity
function batches(uint256) external view returns (uint256 sourceChainId, uint256 destinationChainId)
function batches(uint256) external view returns (struct BridgeMessageBatch batch, bytes bitmap)
```


Expand All @@ -166,8 +166,8 @@ function batches(uint256) external view returns (uint256 sourceChainId, uint256

| Name | Type | Description |
|---|---|---|
| sourceChainId | uint256 | undefined |
| destinationChainId | uint256 | undefined |
| batch | BridgeMessageBatch | undefined |
| bitmap | bytes | undefined |

### bls

Expand Down Expand Up @@ -239,6 +239,28 @@ function commitValidatorSet(Validator[] newValidatorSet, uint256[2] signature, b
| signature | uint256[2] | undefined |
| bitmap | bytes | undefined |

### commitedValidatorSets

```solidity
function commitedValidatorSets(uint256) external view returns (bytes bitmap)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

#### Returns

| Name | Type | Description |
|---|---|---|
| bitmap | bytes | undefined |

### currentValidatorSet

```solidity
Expand Down Expand Up @@ -369,6 +391,23 @@ function totalVotingPower() external view returns (uint256)



#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

### validatorSetCounter

```solidity
function validatorSetCounter() external view returns (uint256)
```






#### Returns

| Name | Type | Description |
Expand Down

0 comments on commit 0da2a77

Please sign in to comment.