Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge storage refactoring #52

Draft
wants to merge 5 commits into
base: feat/bridge
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions contracts/blade/BridgeStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import "./ValidatorSetStorage.sol";

contract BridgeStorage is ValidatorSetStorage {
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;
uint256 public validatorSetCounter;

event NewBatch(uint256 indexed id);
event NewValidatorSetStored(uint256 indexed id);
Expand All @@ -24,9 +22,7 @@ contract BridgeStorage is ValidatorSetStorage {
function initialize(IBLS newBls, IBN256G2 newBn256G2, Validator[] calldata validators) public override initializer {
bls = newBls;
bn256G2 = newBn256G2;
_setNewValidatorSet(validators);

validatorSetCounter = 1;
_setInitialValidatorSet(validators);
}

/**
Expand All @@ -43,24 +39,9 @@ contract BridgeStorage is ValidatorSetStorage {
) external override onlySystemCall {
_commitValidatorSet(newValidatorSet, signature, bitmap, blockMetadata);

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

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

_insertNewValidatorSetBatchRef();

emit NewValidatorSetStored(validatorSetCounter);

validatorSetCounter++;
}

/**
Expand Down Expand Up @@ -146,14 +127,6 @@ contract BridgeStorage is ValidatorSetStorage {
return unexecutedBatches;
}

/**
* @notice Returns the committed validator set based on provided id
* @param id validator set id
*/
function getCommittedValidatorSet(uint256 id) external view returns (SignedValidatorSet memory) {
return commitedValidatorSets[id];
}

/**
* @notice Inserts an empty batch used as a reference for each committed validator set batch
*/
Expand Down
56 changes: 43 additions & 13 deletions contracts/blade/ValidatorSetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
IBLS public bls;
IBN256G2 public bn256G2;

mapping(uint256 => Validator) public currentValidatorSet;
mapping(uint256 => SignedValidatorSet) committedValidatorSets;
uint256 public validatorSetCounter;
uint256 public currentValidatorSetLength;
bytes32 public currentValidatorSetHash;
uint256 public totalVotingPower;
Expand All @@ -25,7 +26,7 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
function initialize(IBLS newBls, IBN256G2 newBn256G2, Validator[] calldata validators) public virtual initializer {
bls = newBls;
bn256G2 = newBn256G2;
_setNewValidatorSet(validators);
_setInitialValidatorSet(validators);
}

/**
Expand All @@ -48,16 +49,16 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
* @notice Internal function that sets the new validator set
* @param newValidatorSet new validator set
*/
function _setNewValidatorSet(Validator[] calldata newValidatorSet) internal {
uint256 length = newValidatorSet.length;
currentValidatorSetLength = length;
function _setInitialValidatorSet(Validator[] calldata newValidatorSet) internal {
currentValidatorSetHash = keccak256(abi.encode(newValidatorSet));
uint256 totalPower = 0;
for (uint256 i = 0; i < length; ) {

SignedValidatorSet storage signedValidatorSet = committedValidatorSets[validatorSetCounter];
for (uint256 i = 0; i < newValidatorSet.length; ) {
signedValidatorSet.newValidatorSet.push(newValidatorSet[i]);
uint256 votingPower = newValidatorSet[i].votingPower;
require(votingPower > 0, "VOTING_POWER_ZERO");
totalPower += votingPower;
currentValidatorSet[i] = newValidatorSet[i];
unchecked {
++i;
}
Expand All @@ -77,16 +78,15 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
uint256[2] calldata signature,
bytes calldata bitmap
) internal view {
uint256 length = currentValidatorSetLength;
// slither-disable-next-line uninitialized-local
uint256[4] memory aggPubkey;
uint256 aggVotingPower = 0;
for (uint256 i = 0; i < length; ) {
for (uint256 i = 0; i < committedValidatorSets[validatorSetCounter].newValidatorSet.length; ) {
if (_getValueFromBitmap(bitmap, i)) {
if (aggVotingPower == 0) {
aggPubkey = currentValidatorSet[i].blsKey;
aggPubkey = committedValidatorSets[validatorSetCounter].newValidatorSet[i].blsKey;
} else {
uint256[4] memory blsKey = currentValidatorSet[i].blsKey;
uint256[4] memory blsKey = committedValidatorSets[validatorSetCounter].newValidatorSet[i].blsKey;
// slither-disable-next-line calls-loop
(aggPubkey[0], aggPubkey[1], aggPubkey[2], aggPubkey[3]) = bn256G2.ecTwistAdd(
aggPubkey[0],
Expand All @@ -99,7 +99,7 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
blsKey[3]
);
}
aggVotingPower += currentValidatorSet[i].votingPower;
aggVotingPower += committedValidatorSets[validatorSetCounter].newValidatorSet[i].votingPower;
}
unchecked {
++i;
Expand Down Expand Up @@ -137,6 +137,7 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
* @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
* @param blockMetadata The blockMetadata represents
* Emits a `NewValidatorSet` event after successfully setting the new validator set.
*/
function _commitValidatorSet(
Expand All @@ -145,14 +146,43 @@ contract ValidatorSetStorage is IValidatorSetStorage, Initializable, System {
bytes calldata bitmap,
BlockMetadata calldata blockMetadata
) internal {
uint256 totalPower = 0;

require(newValidatorSet.length > 0, "EMPTY_VALIDATOR_SET");

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

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

_setNewValidatorSet(newValidatorSet);
validatorSetCounter++;

currentValidatorSetHash = keccak256(abi.encode(newValidatorSet));

SignedValidatorSet storage signedValidatorSet = committedValidatorSets[validatorSetCounter];
signedValidatorSet.signature = signature;
signedValidatorSet.bitmap = bitmap;
signedValidatorSet.blockMetadata = blockMetadata;
uint256 length = newValidatorSet.length;
for (uint256 i = 0; i < length; ) {
signedValidatorSet.newValidatorSet.push(newValidatorSet[i]);
uint256 votingPower = newValidatorSet[i].votingPower;
require(votingPower > 0, "VOTING_POWER_ZERO");
totalPower += votingPower;
unchecked {
++i;
}
}

totalVotingPower = totalPower;

emit NewValidatorSet(newValidatorSet);
}

/**
* @notice Returns the committed validator set based on provided id
* @param id validator set id
*/
function getCommittedValidatorSet(uint256 id) external view returns (SignedValidatorSet memory) {
return committedValidatorSets[id];
}
}
46 changes: 0 additions & 46 deletions docs/blade/BridgeStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,52 +245,6 @@ function commitValidatorSet(Validator[] newValidatorSet, uint256[2] signature, b
| bitmap | bytes | undefined |
| blockMetadata | BlockMetadata | undefined |

### commitedValidatorSets

```solidity
function commitedValidatorSets(uint256) external view returns (bytes bitmap, struct BlockMetadata blockMetadata)
```





#### Parameters

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

#### Returns

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

### currentValidatorSet

```solidity
function currentValidatorSet(uint256) external view returns (address _address, uint256 votingPower)
```





#### Parameters

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

#### Returns

| Name | Type | Description |
|---|---|---|
| _address | address | undefined |
| votingPower | uint256 | undefined |

### currentValidatorSetHash

```solidity
Expand Down
46 changes: 31 additions & 15 deletions docs/blade/Gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,27 @@ function counter() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined |

### currentValidatorSet
### currentValidatorSetHash

```solidity
function currentValidatorSet(uint256) external view returns (address _address, uint256 votingPower)
function currentValidatorSetHash() external view returns (bytes32)
```





#### Parameters

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

#### Returns

| Name | Type | Description |
|---|---|---|
| _address | address | undefined |
| votingPower | uint256 | undefined |
| _0 | bytes32 | undefined |

### currentValidatorSetHash
### currentValidatorSetLength

```solidity
function currentValidatorSetHash() external view returns (bytes32)
function currentValidatorSetLength() external view returns (uint256)
```


Expand All @@ -254,24 +248,29 @@ function currentValidatorSetHash() external view returns (bytes32)

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

### currentValidatorSetLength
### getCommittedValidatorSet

```solidity
function currentValidatorSetLength() external view returns (uint256)
function getCommittedValidatorSet(uint256 id) external view returns (struct SignedValidatorSet)
```

Returns the committed validator set based on provided id



#### Parameters

| Name | Type | Description |
|---|---|---|
| id | uint256 | validator set id |

#### Returns

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

### getMessagesInRange

Expand Down Expand Up @@ -404,6 +403,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
Loading
Loading