mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-05 03:26:43 +00:00
add block header
This commit is contained in:
parent
2bd81b7044
commit
9820491cd5
5 changed files with 72 additions and 16 deletions
|
@ -1,4 +1,60 @@
|
||||||
package com.sparrowwallet.drongo.protocol;
|
package com.sparrowwallet.drongo.protocol;
|
||||||
|
|
||||||
public class BlockHeader {
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class BlockHeader extends Message {
|
||||||
|
private long version;
|
||||||
|
private Sha256Hash prevBlockHash;
|
||||||
|
private Sha256Hash merkleRoot, witnessRoot;
|
||||||
|
private long time;
|
||||||
|
private long difficultyTarget; // "nBits"
|
||||||
|
private long nonce;
|
||||||
|
|
||||||
|
public BlockHeader(byte[] rawheader) {
|
||||||
|
super(rawheader, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void parse() throws ProtocolException {
|
||||||
|
version = readUint32();
|
||||||
|
prevBlockHash = readHash();
|
||||||
|
merkleRoot = readHash();
|
||||||
|
time = readUint32();
|
||||||
|
difficultyTarget = readUint32();
|
||||||
|
nonce = readUint32();
|
||||||
|
|
||||||
|
length = cursor - offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sha256Hash getPrevBlockHash() {
|
||||||
|
return prevBlockHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sha256Hash getMerkleRoot() {
|
||||||
|
return merkleRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sha256Hash getWitnessRoot() {
|
||||||
|
return witnessRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getTimeAsDate() {
|
||||||
|
return new Date(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDifficultyTarget() {
|
||||||
|
return difficultyTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getNonce() {
|
||||||
|
return nonce;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ public abstract class Message {
|
||||||
public static final int MAX_SIZE = 0x02000000; // 32MB
|
public static final int MAX_SIZE = 0x02000000; // 32MB
|
||||||
public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE;
|
public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE;
|
||||||
|
|
||||||
protected byte[] rawtx;
|
protected byte[] payload;
|
||||||
|
|
||||||
// The offset is how many bytes into the provided byte array this message payload starts at.
|
// The offset is how many bytes into the provided byte array this message payload starts at.
|
||||||
protected int offset;
|
protected int offset;
|
||||||
|
@ -23,8 +23,8 @@ public abstract class Message {
|
||||||
|
|
||||||
protected int length = UNKNOWN_LENGTH;
|
protected int length = UNKNOWN_LENGTH;
|
||||||
|
|
||||||
public Message(byte[] rawtx, int offset) {
|
public Message(byte[] payload, int offset) {
|
||||||
this.rawtx = rawtx;
|
this.payload = payload;
|
||||||
this.cursor = this.offset = offset;
|
this.cursor = this.offset = offset;
|
||||||
|
|
||||||
parse();
|
parse();
|
||||||
|
@ -53,7 +53,7 @@ public abstract class Message {
|
||||||
|
|
||||||
protected long readUint32() throws ProtocolException {
|
protected long readUint32() throws ProtocolException {
|
||||||
try {
|
try {
|
||||||
long u = Utils.readUint32(rawtx, cursor);
|
long u = Utils.readUint32(payload, cursor);
|
||||||
cursor += 4;
|
cursor += 4;
|
||||||
return u;
|
return u;
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
@ -63,7 +63,7 @@ public abstract class Message {
|
||||||
|
|
||||||
protected long readInt64() throws ProtocolException {
|
protected long readInt64() throws ProtocolException {
|
||||||
try {
|
try {
|
||||||
long u = Utils.readInt64(rawtx, cursor);
|
long u = Utils.readInt64(payload, cursor);
|
||||||
cursor += 8;
|
cursor += 8;
|
||||||
return u;
|
return u;
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
@ -72,12 +72,12 @@ public abstract class Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte[] readBytes(int length) throws ProtocolException {
|
protected byte[] readBytes(int length) throws ProtocolException {
|
||||||
if ((length > MAX_SIZE) || (cursor + length > rawtx.length)) {
|
if ((length > MAX_SIZE) || (cursor + length > payload.length)) {
|
||||||
throw new ProtocolException("Claimed value length too large: " + length);
|
throw new ProtocolException("Claimed value length too large: " + length);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
byte[] b = new byte[length];
|
byte[] b = new byte[length];
|
||||||
System.arraycopy(rawtx, cursor, b, 0, length);
|
System.arraycopy(payload, cursor, b, 0, length);
|
||||||
cursor += length;
|
cursor += length;
|
||||||
return b;
|
return b;
|
||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
@ -91,7 +91,7 @@ public abstract class Message {
|
||||||
|
|
||||||
protected long readVarInt(int offset) throws ProtocolException {
|
protected long readVarInt(int offset) throws ProtocolException {
|
||||||
try {
|
try {
|
||||||
VarInt varint = new VarInt(rawtx, cursor + offset);
|
VarInt varint = new VarInt(payload, cursor + offset);
|
||||||
cursor += offset + varint.getOriginalSizeInBytes();
|
cursor += offset + varint.getOriginalSizeInBytes();
|
||||||
return varint.value;
|
return varint.value;
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
|
|
@ -182,7 +182,7 @@ public class Transaction extends ChildMessage {
|
||||||
// version
|
// version
|
||||||
version = readUint32();
|
version = readUint32();
|
||||||
// peek at marker
|
// peek at marker
|
||||||
byte marker = rawtx[cursor];
|
byte marker = payload[cursor];
|
||||||
segwit = (marker == 0);
|
segwit = (marker == 0);
|
||||||
// marker, flag
|
// marker, flag
|
||||||
if (segwit) {
|
if (segwit) {
|
||||||
|
@ -206,7 +206,7 @@ public class Transaction extends ChildMessage {
|
||||||
long numInputs = readVarInt();
|
long numInputs = readVarInt();
|
||||||
inputs = new ArrayList<>(Math.min((int) numInputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
inputs = new ArrayList<>(Math.min((int) numInputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
||||||
for (long i = 0; i < numInputs; i++) {
|
for (long i = 0; i < numInputs; i++) {
|
||||||
TransactionInput input = new TransactionInput(this, rawtx, cursor);
|
TransactionInput input = new TransactionInput(this, payload, cursor);
|
||||||
inputs.add(input);
|
inputs.add(input);
|
||||||
long scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH);
|
long scriptLen = readVarInt(TransactionOutPoint.MESSAGE_LENGTH);
|
||||||
cursor += scriptLen + 4;
|
cursor += scriptLen + 4;
|
||||||
|
@ -217,7 +217,7 @@ public class Transaction extends ChildMessage {
|
||||||
long numOutputs = readVarInt();
|
long numOutputs = readVarInt();
|
||||||
outputs = new ArrayList<>(Math.min((int) numOutputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
outputs = new ArrayList<>(Math.min((int) numOutputs, Utils.MAX_INITIAL_ARRAY_LENGTH));
|
||||||
for (long i = 0; i < numOutputs; i++) {
|
for (long i = 0; i < numOutputs; i++) {
|
||||||
TransactionOutput output = new TransactionOutput(this, rawtx, cursor);
|
TransactionOutput output = new TransactionOutput(this, payload, cursor);
|
||||||
outputs.add(output);
|
outputs.add(output);
|
||||||
long scriptLen = readVarInt(8);
|
long scriptLen = readVarInt(8);
|
||||||
cursor += scriptLen;
|
cursor += scriptLen;
|
||||||
|
@ -227,7 +227,7 @@ public class Transaction extends ChildMessage {
|
||||||
private void parseWitnesses() {
|
private void parseWitnesses() {
|
||||||
int numWitnesses = inputs.size();
|
int numWitnesses = inputs.size();
|
||||||
for (int i = 0; i < numWitnesses; i++) {
|
for (int i = 0; i < numWitnesses; i++) {
|
||||||
TransactionWitness witness = new TransactionWitness(this, rawtx, cursor);
|
TransactionWitness witness = new TransactionWitness(this, payload, cursor);
|
||||||
inputs.get(i).setWitness(witness);
|
inputs.get(i).setWitness(witness);
|
||||||
cursor += witness.getLength();
|
cursor += witness.getLength();
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class TransactionInput extends ChildMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void parse() throws ProtocolException {
|
protected void parse() throws ProtocolException {
|
||||||
outpoint = new TransactionOutPoint(rawtx, cursor, this);
|
outpoint = new TransactionOutPoint(payload, cursor, this);
|
||||||
cursor += outpoint.getMessageSize();
|
cursor += outpoint.getMessageSize();
|
||||||
int scriptLen = (int) readVarInt();
|
int scriptLen = (int) readVarInt();
|
||||||
length = cursor - offset + scriptLen + 4;
|
length = cursor - offset + scriptLen + 4;
|
||||||
|
@ -62,7 +62,7 @@ public class TransactionInput extends ChildMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
void setScriptBytes(byte[] scriptBytes) {
|
void setScriptBytes(byte[] scriptBytes) {
|
||||||
super.rawtx = null;
|
super.payload = null;
|
||||||
this.scriptSig = null;
|
this.scriptSig = null;
|
||||||
int oldLength = length;
|
int oldLength = length;
|
||||||
this.scriptBytes = scriptBytes;
|
this.scriptBytes = scriptBytes;
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class TransactionOutput extends ChildMessage {
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
bitcoinSerializeToStream(baos);
|
bitcoinSerializeToStream(baos);
|
||||||
rawtx = baos.toByteArray();
|
payload = baos.toByteArray();
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
//ignore
|
//ignore
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue