Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tycho upgrade on bsc network #41

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5.0'
version = '0.6.0'

dependencies {
compileOnly("foundation.icon:javaee-api:$javaeeVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ private void verify(ChainConfig config, Header head) {
Context.require(head.getGasLimit().compareTo(MAX_GAS_LIMIT) <= 0, "Invalid gas limit(> max)");
Context.require(head.getGasUsed().compareTo(head.getGasLimit()) < 0, "Invalid gas used");
Context.require(head.getSigner(BigInteger.valueOf(config.ChainID)).equals(head.getCoinbase()), "Coinbase mismatch");

if (config.isTycho(head.getNumber())) {
Context.require(head.getWithdrawalsHash().equals(EMPTY_WITHDRAWALS_HASH), "Invalid withdrawals hash");
}
}

private void verify(ChainConfig config, Header head, Header parent, Snapshot snap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ChainConfig {
public final long Epoch;
public final long Period;
public final BigInteger Hertz;
public final BigInteger Tycho;

private static ChainConfig instance;

Expand All @@ -34,11 +35,12 @@ public static ChainConfig setChainID(BigInteger cid) {
return instance;
}

private ChainConfig(long chainId, long epoch, long period, BigInteger hertz) {
private ChainConfig(long chainId, long epoch, long period, BigInteger hertz, BigInteger tycho) {
this.ChainID = chainId;
this.Epoch = epoch;
this.Period = period;
this.Hertz = hertz;
this.Tycho = tycho;
}

public static ChainConfig getInstance() {
Expand All @@ -48,13 +50,14 @@ public static ChainConfig getInstance() {
public static ChainConfig fromChainID(BigInteger cid) {
if (cid.longValue() == 56L) {
// BSC Mainnet
return new ChainConfig(56L, 200L, 3L, BigInteger.valueOf(31302048L));
return new ChainConfig(56L, 200L, 3L, BigInteger.valueOf(31302048L), null);
} else if (cid.longValue() == 97L) {
// BSC Testnet
return new ChainConfig(97L, 200L, 3L, BigInteger.valueOf(31103030L));
return new ChainConfig(97L, 200L, 3L, BigInteger.valueOf(31103030L),
BigInteger.valueOf(39539137L));
} else if (cid.longValue() == 99L) {
// Private BSC Testnet
return new ChainConfig(99L, 200L, 3L, BigInteger.valueOf(8));
return new ChainConfig(99L, 200L, 3L, BigInteger.valueOf(8), null);
}

Context.require(false, "No Chain Config - ChainID(" + cid.intValue() + ")");
Expand All @@ -69,4 +72,8 @@ public boolean isHertz(BigInteger number) {
return Hertz != null && Hertz.compareTo(number) <= 0;
}

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

}
53 changes: 48 additions & 5 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 @@ -31,7 +31,11 @@ public class Header {
public static final int VALIDATOR_NUMBER_SIZE = 1;
public static final int VALIDATOR_BYTES_LENGTH = EthAddress.LENGTH + BLSPublicKey.LENGTH;
// pre-calculated constant uncle hash:) rlp([])
public static final Hash UNCLE_HASH = Hash.of("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347");
public static final Hash UNCLE_HASH =
Hash.of("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347");
// known hash of empty withdrawl set
public static final Hash EMPTY_WITHDRAWALS_HASH =
Hash.of("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
public static final BigInteger INTURN_DIFF = BigInteger.valueOf(2L);
public static final BigInteger NOTURN_DIFF = BigInteger.valueOf(1L);
public static final BigInteger GAS_LIMIT_BOUND_DIVISOR = BigInteger.valueOf(256L);
Expand All @@ -54,6 +58,9 @@ public class Header {
private final Hash mixDigest;
private final byte[] nonce;
private final BigInteger baseFee;
private final Hash withdrawalsHash;
private final BigInteger blobGasUsed;
private final BigInteger excessBlobGas;

// caches
private Hash hashCache;
Expand All @@ -63,7 +70,8 @@ public class Header {
public Header(Hash parentHash, Hash uncleHash, EthAddress coinbase, Hash root,
Hash txHash, Hash receiptHash, byte[] bloom, BigInteger difficulty,
BigInteger number, BigInteger gasLimit, BigInteger gasUsed, long time,
byte[] extra, Hash mixDigest, byte[] nonce, BigInteger baseFee)
byte[] extra, Hash mixDigest, byte[] nonce, BigInteger baseFee, Hash withdrawalsHash,
BigInteger blobGasUsed, BigInteger excessBlobGas)
{
this.parentHash = parentHash;
this.uncleHash = uncleHash;
Expand All @@ -81,6 +89,9 @@ public Header(Hash parentHash, Hash uncleHash, EthAddress coinbase, Hash root,
this.mixDigest = mixDigest;
this.nonce = nonce;
this.baseFee = baseFee;
this.withdrawalsHash = withdrawalsHash;
this.blobGasUsed = blobGasUsed;
this.excessBlobGas = excessBlobGas;
}

public static Header readObject(ObjectReader r) {
Expand All @@ -100,17 +111,37 @@ public static Header readObject(ObjectReader r) {
byte[] extra = r.readByteArray();
Hash mixDigest = r.read(Hash.class);
byte[] nonce = r.readByteArray();

// For Hertz Upgrade
BigInteger baseFee = null;
if (ChainConfig.getInstance().isHertz(number)) {
baseFee = r.readBigInteger();
}

// For Tycho Upgrade
Hash withdrawalsHash = Hash.EMPTY;
BigInteger blobGasUsed = null;
BigInteger excessBlobGas = null;
if (ChainConfig.getInstance().isTycho(number)) {
withdrawalsHash = r.read(Hash.class);
blobGasUsed = r.readBigInteger();
excessBlobGas = r.readBigInteger();
}

r.end();
return new Header(parentHash, uncleHash, coinbase, root, txHash, receiptHash, bloom,
difficulty, number, gasLimit, gasUsed, time, extra, mixDigest, nonce, baseFee);
difficulty, number, gasLimit, gasUsed, time, extra, mixDigest, nonce, baseFee,
withdrawalsHash, blobGasUsed, excessBlobGas);
}

public static void writeObject(ObjectWriter w, Header o) {
w.beginList(15 + (o.baseFee != null ? 1 : 0));
if (ChainConfig.getInstance().isTycho(o.number)) {
w.beginList(19);
} else if (ChainConfig.getInstance().isHertz(o.number)) {
w.beginList(16);
} else {
w.beginList(15);
}
w.write(o.parentHash);
w.write(o.uncleHash);
w.write(o.coinbase);
Expand All @@ -126,9 +157,17 @@ public static void writeObject(ObjectWriter w, Header o) {
w.write(o.extra);
w.write(o.mixDigest);
w.write(o.nonce);
if (o.baseFee != null) {
if (ChainConfig.getInstance().isHertz(o.number)) {
Context.require(o.baseFee != null, "no fields for hertz");
w.write(o.baseFee);
}
if (ChainConfig.getInstance().isTycho(o.number)) {
Context.require(o.withdrawalsHash != Hash.EMPTY && o.blobGasUsed != null
&& o.excessBlobGas != null, "no fields for tycho");
w.write(o.withdrawalsHash);
w.write(o.blobGasUsed);
w.write(o.excessBlobGas);
}
w.end();
}

Expand Down Expand Up @@ -275,4 +314,8 @@ public byte[] getExtra() {
public Hash getMixDigest() {
return this.mixDigest;
}

public Hash getWithdrawalsHash() {
return this.withdrawalsHash;
}
}
32 changes: 32 additions & 0 deletions bmv/bsc2/src/test/resources/testnet.json

Large diffs are not rendered by default.

Loading