Android Open Source - bitcoin-wallet Block






From Project

Back to project page bitcoin-wallet.

License

The source code is released under:

Copyright (C) 2011 by Caleb Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the ...

If you think the Android project bitcoin-wallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.dirtyfilthy.bitcoin.core;
/*w  w  w . j av a2s  . c o m*/
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import net.dirtyfilthy.bitcoin.protocol.Packet;
import net.dirtyfilthy.bitcoin.protocol.ProtocolVersion;
import net.dirtyfilthy.bitcoin.util.BigIntegerTools;
import net.dirtyfilthy.bitcoin.util.HashTools;

public class Block implements ByteArrayable {
  private long blockVersion;
  private byte[] previousHash=new byte[32];
  private byte[] merkleRoot=new byte[32];
  private java.util.Date timestamp;
  private long bits;
  private long nonce;
  private byte hash[];
  private Vector<Tx> transactions=new Vector<Tx>();
  private boolean headersOnly=false;
  private BigInteger bigIntegerHash;
  private BigInteger previousBigIntegerHash;
  private int height;
  private BigInteger totalWork;
  static final private BigInteger MAX_WORK=BigInteger.valueOf(1).shiftLeft(256);
  
  
  public Block(){
    this.blockVersion=1;
  }
  
  public BigInteger bigIntegerHash(){
    if(bigIntegerHash==null){
      bigIntegerHash=new BigInteger(this.hash());
    }
    return bigIntegerHash;
  }
  
  public BigInteger previousBigIntegerHash(){
    if(previousBigIntegerHash==null){
      previousBigIntegerHash=new BigInteger(this.previousHash);
      
    }
    return previousBigIntegerHash;
  }
  
  public Block(DataInputStream in, boolean includeTransactions) throws IOException{
    this.blockVersion=((long) Integer.reverseBytes(in.readInt())) & 0xffffffffL;
    in.readFully(previousHash);
    in.readFully(merkleRoot);
    this.timestamp=new java.util.Date((((long) Integer.reverseBytes(in.readInt())) & 0xffffffffL)*1000);
    this.bits=((long) Integer.reverseBytes(in.readInt())) & 0xffffffffL;
    this.nonce=((long) Integer.reverseBytes(in.readInt())) & 0xffffffffL;
    
    transactions=new Vector<Tx>();
    if(includeTransactions){
      int items=(int) Packet.readUnsignedVarInt(in);
      for(int i=0;i<items;i++){
        this.transactions.add(new Tx(in));
      }
    }
    else{
      headersOnly=true;
    }
  }
  
  public long getTime(){
    return this.timestamp.getTime();
  }
  
  public BigInteger getWork(){
    BigInteger work=MAX_WORK.divide(this.targetHash().add(BigInteger.ONE));
    return work;
  }
  
  public BigInteger targetHash(){
    return BigIntegerTools.uncompactBigInt(bits);
  }
  
  public byte[] toByteArray(){
    return toByteArray(true);
  }
  
  public byte[] toByteArray(boolean includeTransactions){
    try{
    ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
    DataOutputStream dataOut=new DataOutputStream(byteOut);
    dataOut.writeInt(Integer.reverseBytes((int) this.blockVersion));
    dataOut.write(this.previousHash);
    dataOut.write(this.merkleRoot);
    dataOut.writeInt(Integer.reverseBytes((int) (this.timestamp.getTime()/1000)));
    dataOut.writeInt(Integer.reverseBytes((int) this.bits));
    dataOut.writeInt(Integer.reverseBytes((int) this.nonce));
    if(includeTransactions){
      dataOut.write(Packet.createUnsignedVarInt(transactions.size()));
      for(Tx tx : transactions){
        dataOut.write(tx.toByteArray());
      }
    }
  
    return byteOut.toByteArray();
    }
    catch(IOException e){
      throw new RuntimeException(e);
    }
  }
  
  public byte[] hash(){
    if(this.hash!=null){
      return this.hash;
    }
    this.hash=HashTools.doubleSha256(this.toByteArray(false));
    return hash;
  }
  
  public boolean validProofOfWork() {
    byte[] h=HashTools.reverseByteArray(this.hash());
    if(h[0]!=0){
      return false;
    }
    return (new BigInteger(h).compareTo(this.targetHash())<=0);
  }
  
  
  public void setBlockVersion(long blockVersion) {
    this.blockVersion = blockVersion;
  }
  public long getBlockVersion() {
    return blockVersion;
  }
  public void setMerkleRoot(byte[] merkleRoot) {
    this.merkleRoot = merkleRoot;
  }
  public byte[] getMerkleRoot() {
    return merkleRoot;
  }
  public void setTimestamp(java.util.Date timestamp) {
    this.timestamp = timestamp;
  }
  public java.util.Date getTimestamp() {
    return timestamp;
  }
  public void setBits(long bits) {
    this.bits = bits;
  }
  public long getBits() {
    return bits;
  }
  public void setNonce(long nonce) {
    this.nonce = nonce;
  }
  public long getNonce() {
    return nonce;
  }


  public void setPreviousHash(byte[] previousHash) {
    this.previousHash = previousHash;
  }


  public byte[] getPreviousHash() {
    return previousHash;
  }

  public void setTransactions(Vector<Tx> transactions) {
    this.transactions = transactions;
  }

  public Vector<Tx> getTransactions() {
    return transactions;
  }
  
  public void addTransaction(Tx tx){
    transactions.add(tx);
    
  }

  public void setHeadersOnly(boolean headersOnly) {
    this.headersOnly = headersOnly;
  }

  public boolean isHeadersOnly() {
    return headersOnly;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public int getHeight() {
    return height;
  }

  public void setTotalWork(BigInteger totalWork) {
    this.totalWork = totalWork;
  }

  public BigInteger getTotalWork() {
    return totalWork;
  }
  
  

}




Java Source Code List

net.dirtyfilthy.bitcoin.core.Address.java
net.dirtyfilthy.bitcoin.core.Base58Hash160.java
net.dirtyfilthy.bitcoin.core.BlockChain.java
net.dirtyfilthy.bitcoin.core.BlockExistsException.java
net.dirtyfilthy.bitcoin.core.BlockStore.java
net.dirtyfilthy.bitcoin.core.Block.java
net.dirtyfilthy.bitcoin.core.BtcValue.java
net.dirtyfilthy.bitcoin.core.ByteArrayable.java
net.dirtyfilthy.bitcoin.core.InvalidBlockException.java
net.dirtyfilthy.bitcoin.core.OpCode.java
net.dirtyfilthy.bitcoin.core.OpData.java
net.dirtyfilthy.bitcoin.core.OrphanBlockException.java
net.dirtyfilthy.bitcoin.core.Script.java
net.dirtyfilthy.bitcoin.core.TxIn.java
net.dirtyfilthy.bitcoin.core.TxOut.java
net.dirtyfilthy.bitcoin.core.Tx.java
net.dirtyfilthy.bitcoin.protocol.AddressBook.java
net.dirtyfilthy.bitcoin.protocol.AddressPacket.java
net.dirtyfilthy.bitcoin.protocol.BlockPacket.java
net.dirtyfilthy.bitcoin.protocol.ConnectionHandler.java
net.dirtyfilthy.bitcoin.protocol.Connection.java
net.dirtyfilthy.bitcoin.protocol.GetAddressPacket.java
net.dirtyfilthy.bitcoin.protocol.GetBlocksPacket.java
net.dirtyfilthy.bitcoin.protocol.GetDataPacket.java
net.dirtyfilthy.bitcoin.protocol.GetHeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.HeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryVector.java
net.dirtyfilthy.bitcoin.protocol.IrcBootStrap.java
net.dirtyfilthy.bitcoin.protocol.MalformedPacketException.java
net.dirtyfilthy.bitcoin.protocol.PacketFactory.java
net.dirtyfilthy.bitcoin.protocol.PacketType.java
net.dirtyfilthy.bitcoin.protocol.Packet.java
net.dirtyfilthy.bitcoin.protocol.PingPacket.java
net.dirtyfilthy.bitcoin.protocol.ProtocolVersion.java
net.dirtyfilthy.bitcoin.protocol.ReplyPacket.java
net.dirtyfilthy.bitcoin.protocol.TxPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionAckPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionPacket.java
net.dirtyfilthy.bitcoin.util.Base58.java
net.dirtyfilthy.bitcoin.util.BigIntegerTools.java
net.dirtyfilthy.bitcoin.util.HashTools.java
net.dirtyfilthy.bitcoin.util.KeyTools.java
net.dirtyfilthy.bitcoin.util.MyHex.java
net.dirtyfilthy.bitcoin.wallet.ExposedSQLiteCursor.java
net.dirtyfilthy.bitcoin.wallet.InvalidPasswordException.java
net.dirtyfilthy.bitcoin.wallet.KeyRing.java
net.dirtyfilthy.bitcoin.wallet.SqlBlockStore.java
net.dirtyfilthy.bitcoin.wallet.Wallet.java