Skip to content

Commit

Permalink
Fix bsc plato bmv bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnie307 committed Jul 10, 2023
1 parent 20aba4d commit bb1a43d
Show file tree
Hide file tree
Showing 25 changed files with 104 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static ChainConfig fromChainID(BigInteger cid) {
return new ChainConfig(97L, 200L, 3L, 1010000L, 28196022L, 29295050L);
} else if (cid.longValue() == 99L) {
// Private BSC Testnet
return new ChainConfig(99L, 200L, 3L, 0L, 0L, 0L);
return new ChainConfig(99L, 200L, 3L, 0L, 0L, 6L);
}

Context.require(false, "No Chain Config - ChainID(" + cid.intValue() + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package foundation.icon.btp.bmv.bsc;

import foundation.icon.score.util.StringUtil;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;
import scorex.util.ArrayList;
Expand Down
2 changes: 0 additions & 2 deletions bmv/bsc2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ version = '0.1.0'
dependencies {
compileOnly("foundation.icon:javaee-api:$javaeeVersion")
implementation("foundation.icon:javaee-scorex:$scorexVersion")
implementation project(':score-util')
implementation project(':lib')

testImplementation("org.junit.jupiter:junit-jupiter-api:$jupiterVersion")
Expand All @@ -20,7 +19,6 @@ dependencies {

optimizedJar {
dependsOn(project(':lib').jar)
dependsOn(project(':score-util').jar)
mainClassName = 'foundation.icon.btp.bmv.bsc2.BTPMessageVerifier'
archivesBaseName = 'bmv-bsc2'
from {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class BLSPublicKey {

public static final int LENGTH = 48;
private byte[] data;
private final byte[] data;

public BLSPublicKey(byte[] data) {
Context.require(data.length == LENGTH, "Invalid bls public key");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, byte[] _header,

ChainConfig config = ChainConfig.fromChainID(_chainId);
Header head = Header.fromBytes(_header);
// Context.require(config.isEpoch(head.getNumber()), "No epoch block");
verify(config, head);

MerkleTreeAccumulator mta = new MerkleTreeAccumulator();
Expand All @@ -68,8 +67,8 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, byte[] _header,
this.tree.set(new BlockTree(head.getHash()));
this.mta.set(mta);
this.heads.set(head.getHash().toBytes(), head);
// TODO attestation shouldn't be null
VoteAttestation attestation = head.getVoteAttestation(config);
Context.require(attestation != null, "No vote attestation");
this.snap.set(new Snapshot(
head.getHash(),
head.getNumber(),
Expand Down Expand Up @@ -131,7 +130,6 @@ private List<Header> handleBlockUpdate(ChainConfig config, BlockUpdate bu, Block
if (newHeads.isEmpty()) {
return new ArrayList<>();
}

List<Hash> ancestors = tree.getStem(newHeads.get(0).getParentHash());
Context.require(ancestors.size() > 0, "Inconsistent block");

Expand Down Expand Up @@ -169,6 +167,13 @@ private List<Header> handleBlockUpdate(ChainConfig config, BlockUpdate bu, Block
}

this.snap.set(snaps.get(finality));

// ascending ordered finalized heads
List<Header> finalities = collect(heads, tree.getRoot(), finality);
for (Header head : finalities) {
mta.add(head.getHash().toBytes());
}

tree.prune(finality, new BlockTree.OnRemoveListener() {
@Override
public void onRemove(Hash node) {
Expand All @@ -191,12 +196,6 @@ public void onRemove(Hash node) {
for (Header newHead : newHeads) {
this.heads.set(newHead.getHash().toBytes(), newHead);
}

// ascending ordered finalized heads
List<Header> finalities = collect(heads, tree.getRoot(), finality);
for (Header head : finalities) {
mta.add(head.getHash().toBytes());
}
return finalities;
}

Expand Down Expand Up @@ -363,7 +362,6 @@ private Hash getFinalizedBlockHash(ChainConfig config, Map<Hash, Header> heads,
Context.require(snaps.get(range.getSourceHash()) != null, "Unknown justified block hash");
return range.getSourceHash();
}
//snap = snaps.get(range.getSourceHash());
snap = snaps.get(head.getParentHash());
head = heads.get(head.getParentHash());
}
Expand Down Expand Up @@ -407,10 +405,10 @@ private List<EthAddress> toSortedList(byte[][] addrs) {

private static List<Header> collect(Map<Hash, Header> heads, Hash from, Hash to) {
List<Header> cols = new ArrayList<>();
Header head = heads.get(from);
while (!head.getHash().equals(to)) {
Header head = heads.get(to);
while (!head.getHash().equals(from)) {
cols.add(head);
head = heads.get(head.getHash());
head = heads.get(head.getParentHash());
Context.require(head != null, "Inconsistent chain");
}
reverse(cols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.util.List;

public class BlockProof {
private Header header;
private BigInteger height;
private byte[][] witness;
private final Header header;
private final BigInteger height;
private final byte[][] witness;

public BlockProof(Header header, BigInteger height, byte[][] witness) {
this.header = header;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class BlockTree {

private Hash root;
private Map<Hash, List<Hash>> nodes;
private final Map<Hash, List<Hash>> nodes;

public BlockTree(Hash root) {
this.root = root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;

public class BlockUpdate {
private List<Header> headers;
private final List<Header> headers;

public BlockUpdate(List<Header> headers) {
this.headers = Collections.unmodifiableList(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,4 @@ public boolean isEpoch(BigInteger number) {
return number.longValue() % this.Epoch == 0;
}

public boolean isRamanujan(BigInteger number) {
return number.longValue() >= this.RamanujanBlock;
}

public boolean isPlanck(BigInteger number) {
return this.PlanckBlock <= number.longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package foundation.icon.btp.bmv.bsc2;

import foundation.icon.score.util.StringUtil;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;
Expand All @@ -24,22 +23,12 @@
import java.util.List;

public class EthAddresses {
private List<EthAddress> addresses;
private final List<EthAddress> addresses;

public EthAddresses(EthAddress[] addresses) {
this.addresses = new ArrayList<>(List.of(addresses));
}
public EthAddresses(List<EthAddress> addresses) {
this.addresses = addresses;
}

public EthAddresses(byte[][] addresses) {
this.addresses = new ArrayList<>();
for (int i = 0; i < addresses.length; i++) {
this.addresses.add(new EthAddress(StringUtil.bytesToHex(addresses[i])));
}
}

public EthAddresses(EthAddresses o) {
this.addresses = new ArrayList<>(o.addresses);
}
Expand All @@ -52,15 +41,6 @@ public EthAddress[] toArray() {
return addresses;
}

public static EthAddresses fromString(String o) {
List<String> tokens = StringUtil.tokenize(o, ',');
List<EthAddress> addresses = new ArrayList<>();
for (String token : tokens) {
addresses.add(EthAddress.of(token));
}
return new EthAddresses(addresses);
}

public EthAddress get(int i) {
return addresses.get(i);
}
Expand All @@ -77,10 +57,6 @@ public EthAddress remove(int i) {
return addresses.remove(i);
}

public EthAddresses subList(int from, int to) {
return new EthAddresses(addresses.subList(from, to));
}

public int size() {
return addresses.size();
}
Expand Down Expand Up @@ -114,21 +90,6 @@ public static void writeObject(ObjectWriter w, EthAddresses o) {
w.end();
}

public static void sort(EthAddress[] a) {
int len = a.length;
for (int i = 0; i < len; i++) {
EthAddress v = a[i];
for (int j = i+1; j < len; j++) {
if (v.compareTo(a[j]) > 0) {
EthAddress t = v;
v = a[j];
a[j] = t;
}
}
a[i] = v;
}
}

public static void sort(List<EthAddress> a) {
int len = a.size();
for (int i = 0; i < len; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.util.List;

public class EventLog {
private EthAddress address;
private List<byte[]> topics;
private byte[] data;
private final EthAddress address;
private final List<byte[]> topics;
private final byte[] data;

public EventLog(EthAddress address, List<byte[]> topics, byte[] data) {
this.address = address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class Hash {
public static final Hash EMPTY = new Hash(new byte[32]);
private byte[] data;
private final byte[] data;

public Hash(byte[] data) {
this.data = data;
Expand Down
45 changes: 28 additions & 17 deletions bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;
import scorex.util.ArrayList;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

public class Header {
public static final int EXTRA_VANITY = 32;
Expand All @@ -36,21 +38,21 @@ public class Header {
public static final BigInteger MAX_GAS_LIMIT = BigInteger.valueOf(0x7FFFFFFFFFFFFFFFL); // (2^63-1)
public static final BigInteger MIN_GAS_LIMIT = BigInteger.valueOf(5000L);

private Hash parentHash;
private Hash uncleHash;
private EthAddress coinbase;
private Hash root;
private Hash txHash;
private Hash receiptHash;
private byte[] bloom;
private BigInteger difficulty;
private BigInteger number;
private BigInteger gasLimit;
private BigInteger gasUsed;
private long time;
private byte[] extra;
private Hash mixDigest;
private byte[] nonce;
private final Hash parentHash;
private final Hash uncleHash;
private final EthAddress coinbase;
private final Hash root;
private final Hash txHash;
private final Hash receiptHash;
private final byte[] bloom;
private final BigInteger difficulty;
private final BigInteger number;
private final BigInteger gasLimit;
private final BigInteger gasUsed;
private final long time;
private final byte[] extra;
private final Hash mixDigest;
private final byte[] nonce;

// caches
private Hash hashCache;
Expand Down Expand Up @@ -141,7 +143,17 @@ public Hash getHash() {

public Validators getValidators(ChainConfig config) {
if (valsCache == null) {
valsCache = Validators.fromBytes(getValidatorBytes(config));
List<Validator> validators = new ArrayList<>();
byte[] b = getValidatorBytes(config);
int n = b.length / VALIDATOR_BYTES_LENGTH;
for (int i = 0; i < n; i++) {
byte[] consensus = Arrays.copyOfRange(b, i * VALIDATOR_BYTES_LENGTH,
i * VALIDATOR_BYTES_LENGTH + EthAddress.LENGTH);
byte[] vote = Arrays.copyOfRange(b, i * VALIDATOR_BYTES_LENGTH + EthAddress.LENGTH,
(i + 1) * VALIDATOR_BYTES_LENGTH);
validators.add(new Validator(new EthAddress(consensus), new BLSPublicKey(vote)));
}
valsCache = new Validators(validators);
}
return valsCache;
}
Expand All @@ -166,7 +178,6 @@ public byte[] getValidatorBytes(ChainConfig config) {

public VoteAttestation getVoteAttestation(ChainConfig config) {
if (extra.length <= EXTRA_VANITY + EXTRA_SEAL) {
Context.println("No vote attestation - height(" + number.intValue() + ")");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class MessageEvent {
// keccak256("Message(string,uint256,bytes)")
public static final Hash SIGNATURE = Hash.of("37be353f216cf7e33639101fd610c542e6a0c0109173fa1c1d8b04d34edb7c1b");

private BTPAddress next;
private BigInteger sequence;
private byte[] message;
private final BTPAddress next;
private final BigInteger sequence;
private final byte[] message;

public MessageEvent(BTPAddress next, BigInteger sequence, byte[] message) {
this.next = next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

public class MessageProof {
// block hash
private Hash id;
private List<ReceiptProof> proofs;
private final Hash id;
private final List<ReceiptProof> proofs;

public MessageProof(Hash id, List<ReceiptProof> proofs) {
this.id = id;
Expand Down
Loading

0 comments on commit bb1a43d

Please sign in to comment.