Skip to content

Commit

Permalink
feat(api-sync): database restore (#741)
Browse files Browse the repository at this point in the history
* tx refactor

* use RLP encoding for signature / hash calculation

* regenerate genesis block

* style: resolve style guide violations

* fix some tests

* style: resolve style guide violations

* update imports

* set default network

* fix crypto-block fixtures

* style: resolve style guide violations

* unit test fixes

* update e2e configs

* remove obsolete deps

* style: resolve style guide violations

* remove obsolete type field

* regenerate genesis

* update e2e

* style: resolve style guide violations

* database restore

* call restore when database is empty

* style: resolve style guide violations

* style: resolve style guide violations

* cleanup merge conflicts

* ingest votes

* implement `getValidatorRounds` for consensus contract

* restore validator rounds

* style: resolve style guide violations

* improve reset logic by comparing stored heights

* dont keep all receipts in memory

* Emoty commit

---------

Co-authored-by: oXtxNt9U <[email protected]>
Co-authored-by: ItsANameToo <[email protected]>
Co-authored-by: sebastijankuzner <[email protected]>
  • Loading branch information
4 people authored Oct 30, 2024
1 parent 718ef1c commit ed85be7
Show file tree
Hide file tree
Showing 17 changed files with 1,720 additions and 182 deletions.
26 changes: 26 additions & 0 deletions contracts/src/consensus/Consensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ struct VoteResult {
address validator;
}

struct ValidatorRoundValidator {
address validatorAddress;
uint256 voteBalance;
}

struct ValidatorRound {
uint256 round;
ValidatorRoundValidator[] validators;
}

event ValidatorRegistered(address addr, bytes bls12_381_public_key);

event ValidatorResigned(address addr);
Expand Down Expand Up @@ -72,6 +82,8 @@ contract Consensus {
uint256 private _topValidatorsCount = 0;
address[] private _calculatedTopValidators;

ValidatorRound[] private _validatorRounds;

constructor() {
_owner = msg.sender;
}
Expand Down Expand Up @@ -148,6 +160,8 @@ contract Consensus {
}
}

// TODO: update _validatorRounds

address next = _topValidatorsHead;
delete _calculatedTopValidators;
_calculatedTopValidators = new address[](top);
Expand Down Expand Up @@ -399,6 +413,18 @@ contract Consensus {
}
}

// TODO: allow passing limit to cap maximum number of returned items in case validator count is very high.
// the caller can paginate to retrieve all items.
function getValidatorRounds() public view onlyOwner returns (ValidatorRound[] memory) {
ValidatorRound[] memory result = new ValidatorRound[](_validatorRounds.length);
for (uint256 i = 0; i < _validatorRounds.length; i++) {
ValidatorRound storage data = _validatorRounds[i];
result[i] = data;
}

return result;
}

function _updateVoter(address addr) private {
Vote storage voter = _voters[addr];
if (voter.validator == address(0)) {
Expand Down
21 changes: 21 additions & 0 deletions contracts/test/consensus/Consensus-ValidatorRounds.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE
pragma solidity ^0.8.13;

import {Test, console} from "@forge-std/Test.sol";
import {Consensus, ValidatorRound, ValidatorRoundValidator} from "@contracts/consensus/Consensus.sol";

contract ConsensusTest is Test {
Consensus public consensus;

function setUp() public {
consensus = new Consensus();
}

function test_getValidatorRounds() public view {
ValidatorRound[] memory validatorRounds = new ValidatorRound[](0);

validatorRounds = consensus.getValidatorRounds();

assertEq(validatorRounds.length, 0);
}
}
Loading

0 comments on commit ed85be7

Please sign in to comment.