forked from bitcoinj/bitcoinj
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StoredBlockTest: add tests for serializeCompact(), deserializeCompact…
…(), moreWorkThan()
- Loading branch information
1 parent
009f19a
commit f1b3363
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright by the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.bitcoinj.core; | ||
|
||
import org.junit.Test; | ||
|
||
import java.math.BigInteger; | ||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class StoredBlockTest { | ||
|
||
// Max chain work to fit in 12 bytes | ||
private static final BigInteger VERY_LARGE_WORK = new BigInteger(/* 12 bytes */ "ffffffffffffffffffffffff", 16); | ||
// Chain work too large to fit in 12 bytes | ||
private static final BigInteger TOO_LARGE_WORK = new BigInteger(/* 13 bytes */ "ffffffffffffffffffffffffff", 16); | ||
// Just an arbitrary block | ||
private static final Block BLOCK = Block.createGenesis(Instant.now(), Block.EASIEST_DIFFICULTY_TARGET); | ||
|
||
@Test | ||
public void roundtripSerializeCompact_zeroChainWork() { | ||
roundtripSerializeCompact(BigInteger.ZERO); | ||
} | ||
|
||
@Test | ||
public void roundtripSerializeCompact_largeChainWork() { | ||
roundtripSerializeCompact(BigInteger.valueOf(Long.MAX_VALUE)); | ||
} | ||
|
||
@Test | ||
public void roundtripSerializeCompact_veryLargeChainWork() { | ||
roundtripSerializeCompact(VERY_LARGE_WORK); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void roundtripSerializeCompact_tooLargeChainWork_shouldFail() { | ||
roundtripSerializeCompact(TOO_LARGE_WORK); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void roundtripSerializeCompact_negativeChainWork_shouldFail() { | ||
roundtripSerializeCompact(BigInteger.valueOf(-1)); | ||
} | ||
|
||
@Test | ||
public void moreWorkThan() { | ||
StoredBlock noWorkBlock = new StoredBlock(BLOCK, BigInteger.ZERO, 0); | ||
StoredBlock smallWorkBlock = new StoredBlock(BLOCK, BigInteger.ONE, 0); | ||
StoredBlock veryLargeWorkBlock = new StoredBlock(BLOCK, VERY_LARGE_WORK, 0); | ||
|
||
assertTrue(smallWorkBlock.moreWorkThan(noWorkBlock)); | ||
assertTrue(veryLargeWorkBlock.moreWorkThan(noWorkBlock)); | ||
assertTrue(veryLargeWorkBlock.moreWorkThan(smallWorkBlock)); | ||
} | ||
|
||
private void roundtripSerializeCompact(BigInteger chainWork) { | ||
StoredBlock block = new StoredBlock(BLOCK, chainWork, 0); | ||
ByteBuffer buf = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE); | ||
block.serializeCompact(buf); | ||
assertEquals(StoredBlock.COMPACT_SERIALIZED_SIZE, buf.position()); | ||
((Buffer) buf).rewind(); | ||
assertEquals(StoredBlock.deserializeCompact(buf), block); | ||
} | ||
} |