From e25ce49530131213c574ad0226c71280dc4f00de Mon Sep 17 00:00:00 2001 From: EunKi Yu Date: Thu, 29 Aug 2024 09:44:17 +0900 Subject: [PATCH] Fix wrong recent sealers management --- .../icon/btp/bmv/bsc2/BTPMessageVerifier.java | 9 +++++---- .../icon/btp/bmv/bsc2/Snapshot.java | 19 ++++++++++--------- .../foundation/icon/btp/bmv/bsc2/Utils.java | 4 ++++ .../foundation/icon/btp/bmv/bsc2/BMVTest.java | 2 +- .../icon/btp/bmv/bsc2/DataSource.java | 4 ++-- bmv/bsc2/src/test/resources/testnet.json | 5 +++++ 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BTPMessageVerifier.java b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BTPMessageVerifier.java index c8db21d..f591343 100644 --- a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BTPMessageVerifier.java +++ b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BTPMessageVerifier.java @@ -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); @@ -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); @@ -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) diff --git a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java index 46c047b..ce46a7f 100644 --- a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java +++ b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Snapshot.java @@ -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); } @@ -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) { @@ -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) { diff --git a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Utils.java b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Utils.java index ecb47ed..9bd8b41 100644 --- a/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Utils.java +++ b/bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Utils.java @@ -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; diff --git a/bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/BMVTest.java b/bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/BMVTest.java index ce81912..6aa2396 100644 --- a/bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/BMVTest.java +++ b/bmv/bsc2/src/test/java/foundation/icon/btp/bmv/bsc2/BMVTest.java @@ -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