Skip to content

Commit

Permalink
StoredBlock: use ByteUtils.bigIntegerToBytes to convert chainWork
Browse files Browse the repository at this point in the history
This is a fix for Issue bitcoinj#3410 that will allow main net blocks above
849,137 to be processed. As of block 849,138 we can no longer fit
total chainwork in a 12-byte *signed* field. This fix "kicks the can
down the road" by making the field 12-bytes *unsigned*.

We should open a new issue to address the long term need for bigger
values. Note that converting the field to 12-byte unsigned precludes
us from using the most-significant bit as a flag for a new format, but
we should be able to pick some arbitrary value, say 0xA0 as a version
flag and declare that values less than 0xA0 are "unversioned".
  • Loading branch information
msgilligan committed Jun 23, 2024
1 parent 15b0ffe commit fae543b
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions core/src/main/java/org/bitcoinj/core/StoredBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ public StoredBlock getPrev(BlockStore store) throws BlockStoreException {

/** Serializes the stored block to a custom packed format. Used by {@link CheckpointManager}. */
public void serializeCompact(ByteBuffer buffer) {
byte[] chainWorkBytes = getChainWork().toByteArray();
checkState(chainWorkBytes.length <= CHAIN_WORK_BYTES, () ->
"ran out of space to store chain work!");
byte[] chainWorkBytes = ByteUtils.bigIntegerToBytes(getChainWork(), CHAIN_WORK_BYTES);
if (chainWorkBytes.length < CHAIN_WORK_BYTES) {
// Pad to the right size.
buffer.put(EMPTY_BYTES, 0, CHAIN_WORK_BYTES - chainWorkBytes.length);
Expand Down

0 comments on commit fae543b

Please sign in to comment.