Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e25ce49

Browse files
committedAug 29, 2024·
Fix wrong recent sealers management
1 parent 5e3523b commit e25ce49

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed
 

‎bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BTPMessageVerifier.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class BTPMessageVerifier implements BMV {
4545

4646
public BTPMessageVerifier(Address _bmc, BigInteger _chainId, @Optional byte[] _header,
4747
@Optional byte[] _validators, @Optional byte[] _candidates,
48-
@Optional byte[] _recents, @Optional Integer _currTurnLength, @Optional Integer _nextTurnLength) {
48+
@Optional byte[] _recents, @Optional int _currTurnLength, @Optional int _nextTurnLength) {
4949
ChainConfig config = ChainConfig.setChainID(_chainId);
5050
if (_header != null) {
5151
Header head = Header.fromBytes(_header);
@@ -54,13 +54,14 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, @Optional byte[] _h
5454
MerkleTreeAccumulator mta = new MerkleTreeAccumulator(head.getNumber().longValueExact());
5555
mta.add(head.getHash().toBytes());
5656

57+
Context.require(_currTurnLength > 0 && _nextTurnLength > 0, "Invalid turn lengths");
5758
Validators validators = Validators.fromBytes(_validators);
5859
EthAddresses recents = EthAddresses.fromBytes(_recents);
5960
if (head.getNumber().compareTo(BigInteger.ZERO) == 0) {
6061
Context.require(recents.size() == 1, "Wrong recent signers");
6162
} else {
62-
Context.require(recents.size() == validators.size() / 2 + 1,
63-
"Wrong recent signers - validators/2+1");
63+
Context.require(recents.size() == Utils.calcMinerHistoryLength(validators.size(),
64+
_currTurnLength) + 1, "Wrong recent signer counts");
6465
}
6566

6667
this.bmc.set(_bmc);
@@ -84,7 +85,7 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, @Optional byte[] _h
8485

8586
@External(readonly = true)
8687
public String getVersion() {
87-
return "0.7.1";
88+
return "0.7.2";
8889
}
8990

9091
@External(readonly = true)

‎bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Snapshot apply(ChainConfig config, Header head) {
9696
Context.require(validators.contains(sealer), "UnauthorizedValidator");
9797

9898
EthAddresses newRecents = new EthAddresses(recents);
99-
if (newNumber.longValue() >= getMinerHistoryLength() + 1 && newRecents.size() > 0) {
99+
if (newRecents.size() >= getMinerHistoryLength() + 1) {
100100
newRecents.remove(0);
101101
}
102102

@@ -123,18 +123,19 @@ public Snapshot apply(ChainConfig config, Header head) {
123123
if (newNumber.longValue() % config.Epoch == getMinerHistoryLength()) {
124124
newCurrTurnLength = nextTurnLength;
125125
newValidators = candidates;
126+
127+
// If the number of current validators is less than the number of previous validators,
128+
// the capacity of the recent signers should be adjusted
129+
int limit = Utils.calcMinerHistoryLength(newValidators.size(), newCurrTurnLength) + 1;
130+
for (int i = 0; i < newRecents.size() - limit; i++) {
131+
newRecents.remove(i);
132+
}
133+
126134
if (config.isBohr(head.getTime())) {
127135
// BEP-404: Clear Miner History when switching validators set
128136
for (int i = 0; i < newRecents.size(); i++) {
129137
newRecents.set(i, EthAddress.EMPTY);
130138
}
131-
} else {
132-
// If the number of current validators is less than the number of previous validators,
133-
// the capacity of the recent signers should be adjusted
134-
int limit = newValidators.size() / 2;
135-
for (int i = 0; i < newRecents.size() - limit; i++) {
136-
newRecents.remove(i);
137-
}
138139
}
139140
}
140141
if (newNumber.longValue() % config.Epoch == (long) (voters.size() / 2 + 1) * currTurnLength) {
@@ -146,7 +147,7 @@ public Snapshot apply(ChainConfig config, Header head) {
146147
}
147148

148149
public int getMinerHistoryLength() {
149-
return (validators.size() / 2 + 1) * currTurnLength - 1;
150+
return Utils.calcMinerHistoryLength(validators.size(), currTurnLength);
150151
}
151152

152153
public boolean isRecentlySigned(EthAddress signer) {

‎bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Utils.java

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
public class Utils {
44

5+
public static int calcMinerHistoryLength(int nvals, int nturns) {
6+
return (nvals / 2 + 1) * nturns - 1;
7+
}
8+
59
public static int ceilDiv(int x, int y) {
610
if (y == 0) {
711
return 0;

‎bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/BMVTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void handleRelayMessageTest(DataSource.Case c, Score bmv, String p
3636
byte[][] ret = (byte[][]) sm.call(BMC, BigInteger.ZERO, bmv.getAddress(), "handleRelayMessage",
3737
BMC_BTP_ADDR.toString(), prev, BigInteger.valueOf(0), p.getMessage());
3838

39-
if (p.getResult().size() > 0) {
39+
if (!p.getResult().isEmpty()) {
4040
assertEquals(p.getResult().size(), ret.length);
4141
for (int i=0; i<p.getResult().size(); i++) {
4242
assertEquals(p.getResult().get(i), new String(ret[i]));

‎bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/DataSource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public static class Deployment {
6161
private String validators;
6262
private String candidates;
6363
private String recents;
64-
private Integer currTurnLength;
65-
private Integer nextTurnLength;
64+
private int currTurnLength;
65+
private int nextTurnLength;
6666

6767
public byte[] getHeader() {
6868
return StringUtil.hexToBytes(header);

0 commit comments

Comments
 (0)