-
Notifications
You must be signed in to change notification settings - Fork 39
/
metadata.sol
151 lines (131 loc) · 4.64 KB
/
metadata.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0;
// import "hardhat/console.sol";
contract MetadataManager {
uint64 constant U64_MAX = 2 ** 64 - 1;
struct MetadataVersion {
uint64 start;
uint64 end;
}
struct ValidatorExtend {
bytes bls_pub_key;
bytes pub_key;
address address_;
uint32 propose_weight;
uint32 vote_weight;
}
struct Metadata {
MetadataVersion version;
uint64 epoch;
ValidatorExtend[] verifier_list;
ProposeCount[] propose_counter;
ConsensusConfig consensus_config;
}
struct ProposeCount {
address address_;
uint64 count;
}
struct ConsensusConfig {
uint64 propose_ratio;
uint64 prevote_ratio;
uint64 precommit_ratio;
uint64 brake_ratio;
uint64 tx_num_limit;
uint64 max_tx_size;
uint64 gas_limit;
uint64 gas_price;
uint64 interval;
}
struct CkbRelatedInfo {
bytes32 metadata_type_id;
bytes32 checkpoint_type_id;
bytes32 xudt_args;
bytes32 stake_smt_type_id;
bytes32 delegate_smt_type_id;
bytes32 reward_smt_type_id;
}
// to store all metadata with epoch as key
mapping(uint64 => Metadata) metadata_set;
// to identify current highest epoch number
uint64 highest_epoch;
function construct() public {
highest_epoch = U64_MAX;
}
// 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;
}
// 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;
}
// 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 setCkbRelatedInfo(
CkbRelatedInfo memory ckbRelatedInfo
) public view {}
}