Skip to content

Commit

Permalink
Merge pull request #2529 from rsksmart/chainwork-fix
Browse files Browse the repository at this point in the history
Chainwork fix
  • Loading branch information
aeidelman authored Jul 4, 2024
2 parents 59cd40b + b867dd8 commit 19c80a0
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rskj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ ext {
jaxwsRtVer : '2.3.5',
picocliVer : '4.6.3',

bitcoinjThinVer: '0.14.4-rsk-14',
bitcoinjThinVer: '0.14.4-rsk-15-SNAPSHOT',
rskjNativeVer: '1.3.0',
]

Expand Down
19 changes: 19 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ public void receiveHeaders(BtcBlock[] headers) throws IOException, BlockStoreExc
this.ensureBtcBlockChain();
for (BtcBlock header : headers) {
try {
StoredBlock previousBlock = btcBlockStore.get(header.getPrevBlockHash());
if (cannotProcessNextBlock(previousBlock)) {
logger.warn("[receiveHeaders] Header {} has too much work to be processed", header.getHash());
break;
}
btcBlockChain.add(header);
} catch (Exception e) {
// If we try to add an orphan header bitcoinj throws an exception
Expand Down Expand Up @@ -253,6 +258,11 @@ public Integer receiveHeader(BtcBlock header) throws IOException, BlockStoreExce
return RECEIVE_HEADER_BLOCK_TOO_OLD;
}

if (cannotProcessNextBlock(previousBlock)) {
logger.warn("[receiveHeader] Header {} has too much work to be processed", header.getHash());
return RECEIVE_HEADER_UNEXPECTED_EXCEPTION;
}

try {
btcBlockChain.add(header);
} catch (Exception e) {
Expand All @@ -265,6 +275,15 @@ public Integer receiveHeader(BtcBlock header) throws IOException, BlockStoreExce
return 0;
}

private boolean cannotProcessNextBlock(StoredBlock previousBlock) {
int nextBlockHeight = previousBlock.getHeight() + 1;
boolean networkIsMainnet = btcContext.getParams().equals(NetworkParameters.fromID(NetworkParameters.ID_MAINNET));

return nextBlockHeight >= bridgeConstants.getBlockWithTooMuchChainWorkHeight()
&& networkIsMainnet
&& !activations.isActive(ConsensusRule.RSKIP434);
}

/**
* Get the wallet for the currently active federation
* @return A BTC wallet for the currently active federation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public abstract class BridgeConstants {

protected int btcHeightWhenPegoutTxIndexActivates;
protected int pegoutTxIndexGracePeriodInBtcBlocks;
protected int blockWithTooMuchChainWorkHeight;

public NetworkParameters getBtcParams() {
return NetworkParameters.fromID(btcParamsString);
Expand Down Expand Up @@ -197,4 +198,6 @@ public int getBtcHeightWhenPegoutTxIndexActivates() {
public int getPegoutTxIndexGracePeriodInBtcBlocks() {
return pegoutTxIndexGracePeriodInBtcBlocks;
}

public int getBlockWithTooMuchChainWorkHeight() { return blockWithTooMuchChainWorkHeight; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ private BridgeMainNetConstants() {

btcHeightWhenPegoutTxIndexActivates = 837_589; // Estimated date Wed, 03 Apr 2024 15:00:00 GMT. 832,430 was the block number at time of calculation
pegoutTxIndexGracePeriodInBtcBlocks = 4_320; // 30 days in BTC blocks (considering 1 block every 10 minutes)

blockWithTooMuchChainWorkHeight = 849_138;
}

public static BridgeMainNetConstants getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {

btcHeightWhenPegoutTxIndexActivates = 250;
pegoutTxIndexGracePeriodInBtcBlocks = 100;

blockWithTooMuchChainWorkHeight = Integer.MAX_VALUE;
}

public static BridgeRegTestConstants getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public class BridgeTestNetConstants extends BridgeConstants {

btcHeightWhenPegoutTxIndexActivates = 2_589_553; // Estimated date Wed, 20 Mar 2024 15:00:00 GMT. 2,579,823 was the block number at time of calculation
pegoutTxIndexGracePeriodInBtcBlocks = 1_440; // 10 days in BTC blocks (considering 1 block every 10 minutes)

blockWithTooMuchChainWorkHeight = Integer.MAX_VALUE;
}

public static BridgeTestNetConstants getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public enum ConsensusRule {
RSKIP415("rskip415"),
RSKIP417("rskip417"),
RSKIP428("rskip428"),
RSKIP434("rskip434")
;

private String configKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum NetworkUpgrade {
HOP401("hop401"),
FINGERROOT500("fingerroot500"),
ARROWHEAD600("arrowhead600"),
ARROWHEAD631("arrowhead631"),
LOVELL700("lovell700");

private String name;
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/resources/config/main.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ blockchain.config {
hop401 = 4976300,
fingerroot500 = 5468000,
arrowhead600 = 6223700,
arrowhead631 = -1,
lovell700 = -1
}
}
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/resources/config/regtest.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ blockchain.config {
hop401 = 0,
fingerroot500 = 0,
arrowhead600 = 0,
arrowhead631 = -1,
lovell700 = 0
},
consensusRules = {
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/resources/config/testnet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ blockchain.config {
hop401 = 3362200,
fingerroot500 = 4015800,
arrowhead600 = 4927100,
arrowhead631 = -1,
lovell700 = -1
},
consensusRules = {
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/resources/expected.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ blockchain = {
hop401 = <height>
fingerroot500 = <height>
arrowhead600 = <height>
arrowhead631 = <height>
lovell700 = <height>
}
consensusRules = {
Expand Down Expand Up @@ -94,6 +95,7 @@ blockchain = {
rskip415 = <hardforkName>
rskip417 = <hardforkName>
rskip428 = <hardforkName>
rskip434 = <hardforkName>
}
}
gc = {
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ blockchain = {
rskip415 = arrowhead600
rskip417 = arrowhead600
rskip428 = lovell700
rskip434 = arrowhead631
}
}
gc = {
Expand Down
Loading

0 comments on commit 19c80a0

Please sign in to comment.