Skip to content

Commit

Permalink
Fix wrong recent sealers management
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnie307 committed Aug 29, 2024
1 parent 5e3523b commit 8d736c5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bmv/bsc2/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = '0.7.1'
version = '0.7.2'

dependencies {
compileOnly("foundation.icon:javaee-api:$javaeeVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BTPMessageVerifier implements BMV {

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

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

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

@External(readonly = true)
public String getVersion() {
return "0.7.1";
return "0.7.2";
}

@External(readonly = true)
Expand Down
19 changes: 10 additions & 9 deletions bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Snapshot apply(ChainConfig config, Header head) {
Context.require(validators.contains(sealer), "UnauthorizedValidator");

EthAddresses newRecents = new EthAddresses(recents);
if (newNumber.longValue() >= getMinerHistoryLength() + 1 && newRecents.size() > 0) {
if (newRecents.size() >= getMinerHistoryLength() + 1) {
newRecents.remove(0);
}

Expand All @@ -123,18 +123,19 @@ public Snapshot apply(ChainConfig config, Header head) {
if (newNumber.longValue() % config.Epoch == getMinerHistoryLength()) {
newCurrTurnLength = nextTurnLength;
newValidators = candidates;

// If the number of current validators is less than the number of previous validators,
// the capacity of the recent signers should be adjusted
int limit = Utils.calcMinerHistoryLength(newValidators.size(), newCurrTurnLength) + 1;
for (int i = 0; i < newRecents.size() - limit; i++) {
newRecents.remove(i);
}

if (config.isBohr(head.getTime())) {
// BEP-404: Clear Miner History when switching validators set
for (int i = 0; i < newRecents.size(); i++) {
newRecents.set(i, EthAddress.EMPTY);
}
} else {
// If the number of current validators is less than the number of previous validators,
// the capacity of the recent signers should be adjusted
int limit = newValidators.size() / 2;
for (int i = 0; i < newRecents.size() - limit; i++) {
newRecents.remove(i);
}
}
}
if (newNumber.longValue() % config.Epoch == (long) (voters.size() / 2 + 1) * currTurnLength) {
Expand All @@ -146,7 +147,7 @@ public Snapshot apply(ChainConfig config, Header head) {
}

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

public boolean isRecentlySigned(EthAddress signer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class Utils {

public static int calcMinerHistoryLength(int nvals, int nturns) {
return (nvals / 2 + 1) * nturns - 1;
}

public static int ceilDiv(int x, int y) {
if (y == 0) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void handleRelayMessageTest(DataSource.Case c, Score bmv, String p
byte[][] ret = (byte[][]) sm.call(BMC, BigInteger.ZERO, bmv.getAddress(), "handleRelayMessage",
BMC_BTP_ADDR.toString(), prev, BigInteger.valueOf(0), p.getMessage());

if (p.getResult().size() > 0) {
if (!p.getResult().isEmpty()) {
assertEquals(p.getResult().size(), ret.length);
for (int i=0; i<p.getResult().size(); i++) {
assertEquals(p.getResult().get(i), new String(ret[i]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public static class Deployment {
private String validators;
private String candidates;
private String recents;
private Integer currTurnLength;
private Integer nextTurnLength;
private int currTurnLength;
private int nextTurnLength;

public byte[] getHeader() {
return StringUtil.hexToBytes(header);
Expand Down
Loading

0 comments on commit 8d736c5

Please sign in to comment.