diff --git a/src/main/java/org/bitcoinj/core/AltcoinBlock.java b/src/main/java/org/bitcoinj/core/AltcoinBlock.java index 5579eb30..496576d3 100644 --- a/src/main/java/org/bitcoinj/core/AltcoinBlock.java +++ b/src/main/java/org/bitcoinj/core/AltcoinBlock.java @@ -46,6 +46,8 @@ import org.libdohj.core.AuxPoWNetworkParameters; * specifically using {@link Peer#getBlock(Sha256Hash)}, or grab one from a downloaded {@link BlockChain}. */ public class AltcoinBlock extends org.bitcoinj.core.Block { + private static final int BYTE_BITS = 8; + private boolean auxpowParsed = false; private boolean auxpowBytesValid = false; @@ -167,7 +169,16 @@ public class AltcoinBlock extends org.bitcoinj.core.Block { * @return flags as a bitset. */ public BitSet getVersionFlags() { - return BitSet.valueOf(new long[] {(this.getRawVersion() & 0xff00) >> 8}); + final BitSet bitset = new BitSet(BYTE_BITS); + final int bits = (int) (this.getRawVersion() & 0xff00) >> 8; + + for (int bit = 0; bit < BYTE_BITS; bit++) { + if ((bits & (1 << bit)) > 0) { + bitset.set(bit); + } + } + + return bitset; } /** diff --git a/src/test/java/org/bitcoinj/core/AltcoinBlockTest.java b/src/test/java/org/bitcoinj/core/AltcoinBlockTest.java new file mode 100644 index 00000000..f56500ff --- /dev/null +++ b/src/test/java/org/bitcoinj/core/AltcoinBlockTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2015 jrn. + * + * 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 java.util.BitSet; + +import static org.junit.Assert.assertArrayEquals; +import org.junit.Before; +import org.junit.Test; + +import org.libdohj.params.DogecoinMainNetParams; + +/** + * + * @author jrn + */ +public class AltcoinBlockTest { + private final NetworkParameters params = DogecoinMainNetParams.get(); + + @Before + public void setUp() throws Exception { + Context context = new Context(params); + } + + /** + * Test extraction of flags from the block version, for coins with AuxPoW + * support. + */ + @Test + public void testGetVersionFlags() { + AltcoinBlock block = new AltcoinBlock(params, 0L); + BitSet expected = new BitSet(8); + assertArrayEquals(block.getVersionFlags().toByteArray(), expected.toByteArray()); + + // Set everything but the version flags + block = new AltcoinBlock(params, 0xffff00ff); + assertArrayEquals(block.getVersionFlags().toByteArray(), expected.toByteArray()); + + // Set everything + block = new AltcoinBlock(params, 0xffffffff); + expected.set(0, 8); + assertArrayEquals(block.getVersionFlags().toByteArray(), expected.toByteArray()); + + // Set only the version flags + block = new AltcoinBlock(params, 0x0000ff00); + assertArrayEquals(block.getVersionFlags().toByteArray(), expected.toByteArray()); + + // Set some of the version flags + block = new AltcoinBlock(params, 0x00001700); + expected.clear(0, 8); + expected.set(0, 3); + expected.set(4); + assertArrayEquals(block.getVersionFlags().toByteArray(), expected.toByteArray()); + } +}