3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 11:15:51 +00:00

Remove dependency on Java 7 in AltcoinBlock

This commit is contained in:
Ross Nicoll 2015-12-20 22:01:45 +00:00
parent bf9539076f
commit c15a742ffa
2 changed files with 80 additions and 1 deletions

View File

@ -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;
}
/**

View File

@ -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());
}
}