Skip to content

Commit

Permalink
Merge pull request #23 from icon-project/eunki-hertz
Browse files Browse the repository at this point in the history
Support hertz upgrade
  • Loading branch information
sink772 authored Jul 18, 2023
2 parents 3e18ffa + 7f15052 commit 52d3cce
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package foundation.icon.btp.bmv.bsc2;

import foundation.icon.score.util.StringUtil;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;

import java.util.Arrays;

public class BLSPublicKey {

public static final int LENGTH = 48;
Expand All @@ -14,6 +17,19 @@ public BLSPublicKey(byte[] data) {
this.data = copy(data);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BLSPublicKey that = (BLSPublicKey) o;
return Arrays.equals(data, that.data);
}

@Override
public String toString() {
return StringUtil.toString(data);
}

public static BLSPublicKey readObject(ObjectReader r) {
return new BLSPublicKey(r.readByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

public class BMVException extends BTPException.BMV {

public BMVException(Code c) {
super(c, c.name());
}

public BMVException(Code c, String message) {
super(c, message);
}
Expand All @@ -35,10 +31,6 @@ public static BMVException notVerifiable(String message) {
return new BMVException(Code.NotVerifiable, message);
}

public static BMVException alreadyVerified(String message) {
return new BMVException(Code.AlreadyVerified, message);
}

public static BMVException invalidBlockWitnessOld(String message) {
return new BMVException(Code.InvalidBlockWitnessOld, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

public class BMVStatusExtra {
// mta offset
private long offset;
private BlockTree tree;
private final long offset;
private final BlockTree tree;

public BMVStatusExtra(long offset, BlockTree tree) {
this.offset = offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

public class BTPMessageVerifier implements BMV {
private final VarDB<Address> bmc = Context.newVarDB("bmc", Address.class);
private final VarDB<BigInteger> cid = Context.newVarDB("cid", BigInteger.class);
private final VarDB<BlockTree> tree = Context.newVarDB("tree", BlockTree.class);
private final VarDB<Snapshot> snap = Context.newVarDB("snap", Snapshot.class);
private final VarDB<MerkleTreeAccumulator> mta = Context.newVarDB("mta", MerkleTreeAccumulator.class);
Expand All @@ -43,7 +42,7 @@ public class BTPMessageVerifier implements BMV {
public BTPMessageVerifier(Address _bmc, BigInteger _chainId, byte[] _header,
byte[] _validators, byte[] _candidates, byte[] _recents) {

ChainConfig config = ChainConfig.fromChainID(_chainId);
ChainConfig config = ChainConfig.setChainID(_chainId);
Header head = Header.fromBytes(_header);
verify(config, head);

Expand All @@ -62,7 +61,6 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, byte[] _header,
}

this.bmc.set(_bmc);
this.cid.set(_chainId);
this.tree.set(new BlockTree(head.getHash()));
this.mta.set(mta);
this.heads.set(head.getHash().toBytes(), head);
Expand All @@ -73,6 +71,7 @@ public BTPMessageVerifier(Address _bmc, BigInteger _chainId, byte[] _header,
head.getNumber(),
Validators.fromBytes(_validators),
Validators.fromBytes(_candidates),
Validators.fromBytes(_validators),
EthAddresses.fromBytes(_recents),
attestation));
}
Expand All @@ -94,7 +93,7 @@ public byte[][] handleRelayMessage(String _bmc, String _prev, BigInteger _seq, b

BlockTree tree = this.tree.get();
MerkleTreeAccumulator mta = this.mta.get();
ChainConfig config = ChainConfig.fromChainID(this.cid.get());
ChainConfig config = ChainConfig.getInstance();
List<Header> confirmations = new ArrayList<>();
List<MessageEvent> msgs = new ArrayList<>();
BigInteger seq = _seq.add(BigInteger.ONE);
Expand Down Expand Up @@ -150,7 +149,7 @@ private List<Header> handleBlockUpdate(ChainConfig config, BlockUpdate bu, Block
for (Header newHead : newHeads) {
verify(config, newHead, parent, snap);
if (newHead.getVoteAttestation(config) != null) {
verifyVoteAttestation(config, newHead, snaps.get(newHead.getParentHash()));
verifyVoteAttestation(config, newHead, snap);
}
tree.add(snap.getHash(), newHead.getHash());
snap = snap.apply(config, newHead);
Expand All @@ -160,7 +159,7 @@ private List<Header> handleBlockUpdate(ChainConfig config, BlockUpdate bu, Block
}

// current `parent` refer to leaf header
Hash finality = getFinalizedBlockHash(config, heads, snaps, heads.get(tree.getRoot()), parent);
Hash finality = getFinalizedBlockHash(heads, snaps, heads.get(tree.getRoot()), parent);
if (finality == null) {
return new ArrayList<>();
}
Expand Down Expand Up @@ -333,7 +332,6 @@ private void verifyVoteAttestation(ChainConfig config, Header head, Snapshot sna

Context.require(head.getParentHash().equals(snap.getHash()),
"Invalid snapshot, no parent snapshot");
VoteRange range = atte.getVoteRange();
Context.require(atte.getVoteRange() != null, "Invalid attestation, vote range is null");

// target block should be direct parent
Expand All @@ -342,10 +340,10 @@ private void verifyVoteAttestation(ChainConfig config, Header head, Snapshot sna
VoteRange pvr = snap.getVoteAttestation().getVoteRange();
Context.require(atte.isSourceOf(pvr.getTargetNumber(), pvr.getTargetHash()),
"Invalid attestation, source mismatch");
atte.verify(snap.getValidators());
atte.verify(snap.getVoters());
}

private Hash getFinalizedBlockHash(ChainConfig config, Map<Hash, Header> heads, Map<Hash, Snapshot> snaps, Header root, Header from) {
private Hash getFinalizedBlockHash(Map<Hash, Header> heads, Map<Hash, Snapshot> snaps, Header root, Header from) {
Snapshot snap = snaps.get(from.getHash());
Header head = heads.get(from.getHash());
while (snap != null && !snap.getHash().equals(root.getHash())) {
Expand All @@ -362,11 +360,11 @@ private Hash getFinalizedBlockHash(ChainConfig config, Map<Hash, Header> heads,

private static final long DEFAULT_BACKOFF_TIME = 1L;
private void verifyForRamanujanFork(ChainConfig config, Snapshot snap, Header head, Header parent) {
long diffTime = config.Period + getBackOffTime(config, snap, head);
long diffTime = config.Period + getBackOffTime(snap, head);
Context.require(head.getTime() >= parent.getTime() + diffTime, "Future block - number("+head.getNumber()+")");
}

private long getBackOffTime(ChainConfig config, Snapshot snap, Header head) {
private long getBackOffTime(Snapshot snap, Header head) {
if (snap.inturn(head.getCoinbase())) {
return 0L;
}
Expand All @@ -386,15 +384,6 @@ private void checkAccessible() {
}
}

private List<EthAddress> toSortedList(byte[][] addrs) {
List<EthAddress> list = new ArrayList<>();
for (int i = 0; i < addrs.length; i++) {
list.add(new EthAddress(addrs[i]));
}
EthAddresses.sort(list);
return list;
}

private static List<Header> collect(Map<Hash, Header> heads, Hash from, Hash to) {
List<Header> cols = new ArrayList<>();
Header head = heads.get(to);
Expand Down
37 changes: 16 additions & 21 deletions bmv/bsc2/src/main/java/foundation/icon/btp/bmv/bsc2/BlockTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public class BlockTree {

public BlockTree(Hash root) {
this.root = root;
this.nodes = new HashMap<>() {{
put(root, new ArrayList<>());
}};
this.nodes = new HashMap<>();
this.nodes.put(root, new ArrayList<>());
}

private BlockTree(Hash root, Map<Hash, List<Hash>> nodes) {
Expand All @@ -44,8 +43,8 @@ private BlockTree(Hash root, Map<Hash, List<Hash>> nodes) {
}

private static class Item {
private int nleaves;
private Hash id;
private final int nleaves;
private final Hash id;

private Item(int nleaves, Hash id) {
this.nleaves = nleaves;
Expand All @@ -59,9 +58,8 @@ public static BlockTree readObject(ObjectReader r) {
r.beginList();
int nleaves = r.readInt();
Hash root = Hash.of(r.readByteArray());
List<Item> items = new ArrayList<>() {{
add(new Item(nleaves, root));
}};
List<Item> items = new ArrayList<>();
items.add(new Item(nleaves, root));

while(items.size() > 0) {
Item item = items.remove(0);
Expand All @@ -75,15 +73,12 @@ public static BlockTree readObject(ObjectReader r) {
nodes.put(id, children);
}
r.end();
BlockTree bt = new BlockTree(root, nodes);
return bt;
return new BlockTree(root, nodes);
}

public static void writeObject(ObjectWriter w, BlockTree o) {
List<Hash> children = new ArrayList<>() {{
add(o.root);
}};

List<Hash> children = new ArrayList<>();
children.add(o.root);
w.beginList(o.nodes.size());
while (children.size() > 0) {
Hash node = children.remove(0);
Expand All @@ -97,12 +92,6 @@ public static void writeObject(ObjectWriter w, BlockTree o) {
w.end();
}

public byte[] toBytes() {
ByteArrayObjectWriter w = Context.newByteArrayObjectWriter("RLP");
writeObject(w, this);
return w.toByteArray();
}

public static BlockTree fromBytes(byte[] bytes) {
ObjectReader r = Context.newByteArrayObjectReader("RLP", bytes);
return BlockTree.readObject(r);
Expand Down Expand Up @@ -167,7 +156,8 @@ public interface OnRemoveListener {
}

public void prune(Hash until, OnRemoveListener lst) {
List<Hash> removals = new ArrayList<>() {{ add(root); }};
List<Hash> removals = new ArrayList<>();
removals.add(root);
while (removals.size() > 0) {
List<Hash> buf = new ArrayList<>();
for (Hash removal : removals) {
Expand Down Expand Up @@ -195,6 +185,11 @@ public String toString() {
'}';
}

@Override
public int hashCode() {
return super.hashCode();
}

@Override
public boolean equals(Object o) {
if (o == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,38 @@ public class ChainConfig {
public final long ChainID;
public final long Epoch;
public final long Period;
private final long RamanujanBlock;
private final long PlanckBlock;
public final BigInteger Hertz;

private ChainConfig(long chainId, long epoch, long period, long ramanujanBlock, long planckBlock) {
private static ChainConfig instance;

public static ChainConfig setChainID(BigInteger cid) {
if (instance == null || instance.ChainID != cid.longValueExact()) {
instance = fromChainID(cid);
}
return instance;
}

private ChainConfig(long chainId, long epoch, long period, BigInteger hertz) {
this.ChainID = chainId;
this.Epoch = epoch;
this.Period = period;
this.RamanujanBlock = ramanujanBlock;
this.PlanckBlock = planckBlock;
this.Hertz = hertz;
}

public static ChainConfig getInstance() {
return instance;
}

public static ChainConfig fromChainID(BigInteger cid) {
if (cid.longValue() == 56L) {
// BSC Mainnet
return new ChainConfig(56L, 200L, 3L, 0L, 27281024L);
return new ChainConfig(56L, 200L, 3L, null);
} else if (cid.longValue() == 97L) {
// BSC Testnet
return new ChainConfig(97L, 200L, 3L, 1010000L, 28196022L);
return new ChainConfig(97L, 200L, 3L, BigInteger.valueOf(31103030L));
} else if (cid.longValue() == 99L) {
// Private BSC Testnet
return new ChainConfig(99L, 200L, 3L, 0L, 0L);
return new ChainConfig(99L, 200L, 3L, null);
}

Context.require(false, "No Chain Config - ChainID(" + cid.intValue() + ")");
Expand All @@ -54,4 +65,8 @@ public boolean isEpoch(BigInteger number) {
return number.longValue() % this.Epoch == 0;
}

public boolean isHertz(BigInteger number) {
return Hertz != null && Hertz.compareTo(number) <= 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ public class EthAddress implements Comparable<EthAddress> {

private byte[] data;

public EthAddress() {
}

public EthAddress(byte[] data) {
this.data = data;
}

public EthAddress(String data) {
if (data.substring(0, 2).compareTo("0x") == 0) {
data = data.substring(2);
}
this.data = StringUtil.hexToBytes(data);
}

public static EthAddress of(String data) {
if (data.substring(0, 2).compareTo("0x") == 0) {
data = data.substring(2);
Expand All @@ -52,10 +42,6 @@ public static EthAddress of(byte[] data) {
return new EthAddress(data);
}

public void setEthAddress(byte[] data) {
this.data = data;
}

public byte[] getEthAddress() {
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,4 @@ public static void writeObject(ObjectWriter w, EthAddresses o) {
w.end();
}

public static void sort(List<EthAddress> a) {
int len = a.size();
for (int i = 0; i < len; i++) {
EthAddress v = a.get(i);
for (int j = i+1; j < len; j++) {
if (v.compareTo(a.get(j)) > 0) {
EthAddress t = v;
v = a.get(j);
a.set(j, t);
}
}
a.set(i, v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return StringUtil.toString(data).hashCode();
return toString().hashCode();
}

@Override
Expand Down
Loading

0 comments on commit 52d3cce

Please sign in to comment.