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

refactor: change metadata system contract and add annotation #1449

Merged
merged 12 commits into from
Oct 9, 2023
93 changes: 8 additions & 85 deletions builtin-contract/metadata/contracts/metadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

pragma solidity >=0.7.0;

// import "hardhat/console.sol";
// **Notice**
// The solidity contract only define the interface of metadata contract. The real
// implementation is in `core/executor/src/system_contract/metadata`.
Flouse marked this conversation as resolved.
Show resolved Hide resolved

contract MetadataManager {
uint64 constant U64_MAX = 2 ** 64 - 1;
Expand Down Expand Up @@ -60,92 +62,13 @@ contract MetadataManager {
// to identify current highest epoch number
uint64 highest_epoch;

function construct() public {
highest_epoch = U64_MAX;
}
function construct() public {}

// push new metadata into `metadata_set`
function appendMetadata(Metadata memory metadata) public {
require(metadata.epoch >= 0, "fatal/invalid epoch");

bool find_sender = false;
for (uint256 i = 0; i < metadata.verifier_list.length; i++) {
if (metadata.verifier_list[i].address_ == msg.sender) {
find_sender = true;
break;
}
}
require(find_sender, "fatal/verifier_list has no sender");

uint64 epoch = metadata.epoch;
if (highest_epoch != U64_MAX) {
require(highest_epoch + 1 == epoch, "fatal/discontinuous epoch");
require(
metadata.version.start ==
metadata_set[highest_epoch].version.end + 1,
"fatal/discontinuous version"
);
}

Metadata storage target = metadata_set[epoch];
target.version = metadata.version;
target.epoch = metadata.epoch;
target.consensus_config.gas_limit = metadata.consensus_config.gas_limit;
target.consensus_config.gas_price = metadata.consensus_config.gas_price;
target.consensus_config.interval = metadata.consensus_config.interval;
target.consensus_config.propose_ratio = metadata
.consensus_config
.propose_ratio;
target.consensus_config.prevote_ratio = metadata
.consensus_config
.prevote_ratio;
target.consensus_config.precommit_ratio = metadata
.consensus_config
.precommit_ratio;
target.consensus_config.brake_ratio = metadata
.consensus_config
.brake_ratio;
target.consensus_config.tx_num_limit = metadata
.consensus_config
.tx_num_limit;
target.consensus_config.max_tx_size = metadata
.consensus_config
.max_tx_size;
for (uint256 i = 0; i < metadata.propose_counter.length; i++) {
target.propose_counter.push(metadata.propose_counter[i]);
}
for (uint256 i = 0; i < metadata.verifier_list.length; i++) {
target.verifier_list.push(metadata.verifier_list[i]);
}
highest_epoch = epoch;
}
function appendMetadata(Metadata memory metadata) public {}

// update current consensus_config
function updateConsensusConfig(ConsensusConfig memory config) public view {
Metadata memory highest_metadata = metadata_set[highest_epoch];

bool find_sender = false;
for (uint256 i = 0; i < highest_metadata.verifier_list.length; i++) {
if (highest_metadata.verifier_list[i].address_ == msg.sender) {
find_sender = true;
break;
}
}
require(find_sender, "fatal/verifier_list has no sender");
highest_metadata.consensus_config = config;
}
function updateConsensusConfig(ConsensusConfig memory config) public view {}

// get metadata from `metadata_set` by epoch
function getMetadata(uint64 epoch) public view returns (Metadata memory) {
Metadata memory metadata = metadata_set[epoch];
require(
metadata.consensus_config.gas_limit != 0,
"fatal/non-indexed epoch"
);
return metadata;
}
function getMetadata(uint64 epoch) public view returns (Metadata memory) {}

function setCkbRelatedInfo(
CkbRelatedInfo memory ckbRelatedInfo
) public view {}
function setCkbRelatedInfo() public view {}
}
7 changes: 6 additions & 1 deletion core/executor/src/system_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,15 @@ pub fn init<Adapter: ExecutorAdapter + ApplyBackend>(
let current_cell_root =
adapter.storage(CKB_LIGHT_CLIENT_CONTRACT_ADDRESS, *HEADER_CELL_ROOT_KEY);

// Current cell root is zero means there is no image cell and header contains in
// the MPT. Because of the empty cell root is zero rather than NLP_NULL, it is
// necessary to init the ckb light client and image account in state MPT. The
// initial process is set the storage root of the two accounts as H256::zero().
if current_cell_root.is_zero() {
let changes = generate_mpt_root_changes(adapter, IMAGE_CELL_CONTRACT_ADDRESS);
let changes = generate_mpt_root_changes(adapter, CKB_LIGHT_CLIENT_CONTRACT_ADDRESS);
adapter.apply(changes, vec![], false);
}

(current_metadata_root, current_cell_root)
}

Expand Down
Loading