Skip to content

Commit 7e8e082

Browse files
committed
bsc2: Fix voters change at the wrong block number
1 parent 7b0f518 commit 7e8e082

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, @Optional byte[] _h
7777
Validators.fromBytes(_candidates),
7878
Validators.fromBytes(_validators),
7979
EthAddresses.fromBytes(_recents),
80-
attestation, _currTurnLength, _nextTurnLength));
80+
attestation, _currTurnLength, _nextTurnLength, _currTurnLength));
8181
} else {
8282
Context.require(_bmc.equals(this.bmc.get()), "Mismatch BMC address");
8383
}

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public class Snapshot {
3131
private final VoteAttestation attestation;
3232
private final int currTurnLength;
3333
private final int nextTurnLength;
34+
private final int pastTurnLength;
3435

3536
public Snapshot(Hash hash, BigInteger number, Validators validators,
3637
Validators candidates, Validators voters, EthAddresses recents,
37-
VoteAttestation attestation, int currTurnLength, int nextTurnLength) {
38+
VoteAttestation attestation, int currTurnLength, int nextTurnLength, int pastTurnLength) {
3839

3940
this.hash = hash;
4041
this.number = number;
@@ -47,6 +48,7 @@ public Snapshot(Hash hash, BigInteger number, Validators validators,
4748
this.attestation = attestation;
4849
this.currTurnLength = currTurnLength;
4950
this.nextTurnLength = nextTurnLength;
51+
this.pastTurnLength = pastTurnLength;
5052
}
5153

5254
public static void writeObject(ObjectWriter w, Snapshot o) {
@@ -60,6 +62,7 @@ public static void writeObject(ObjectWriter w, Snapshot o) {
6062
w.write(o.attestation);
6163
w.write(o.currTurnLength);
6264
w.write(o.nextTurnLength);
65+
w.write(o.pastTurnLength);
6366
w.end();
6467
}
6568

@@ -74,9 +77,10 @@ public static Snapshot readObject(ObjectReader r) {
7477
VoteAttestation attestation = r.read(VoteAttestation.class);
7578
int currTurnLength = r.readOrDefault(Integer.class, Header.DEFAULT_TURN_LENGTH);
7679
int nextTurnLength = r.readOrDefault(Integer.class, Header.DEFAULT_TURN_LENGTH);
80+
int pastTurnLength = r.readOrDefault(Integer.class, currTurnLength);
7781
r.end();
7882
return new Snapshot(hash, number, validators, candidates, voters, recents, attestation,
79-
currTurnLength, nextTurnLength);
83+
currTurnLength, nextTurnLength, pastTurnLength);
8084
}
8185

8286
public boolean inturn(EthAddress validator) {
@@ -111,6 +115,7 @@ public Snapshot apply(ChainConfig config, Header head) {
111115
newAttestation = attestation;
112116
}
113117

118+
int newPastTurnLength = pastTurnLength;
114119
int newCurrTurnLength = currTurnLength;
115120
int newNextTurnLength = nextTurnLength;
116121
Validators newValidators = validators;
@@ -136,12 +141,13 @@ public Snapshot apply(ChainConfig config, Header head) {
136141
}
137142
}
138143
}
139-
if (newNumber.longValue() % config.Epoch == (long) (voters.size() / 2 + 1) * currTurnLength) {
144+
if (newNumber.longValue() % config.Epoch == Utils.calcMinerHistoryLength(voters.size(), pastTurnLength) + 1) {
140145
newVoters = validators;
146+
newPastTurnLength = currTurnLength;
141147
}
142148

143149
return new Snapshot(head.getHash(), newNumber, newValidators, newCandidates, newVoters,
144-
newRecents, newAttestation, newCurrTurnLength, newNextTurnLength);
150+
newRecents, newAttestation, newCurrTurnLength, newNextTurnLength, newPastTurnLength);
145151
}
146152

147153
public int getMinerHistoryLength() {

0 commit comments

Comments
 (0)