Skip to content

Commit

Permalink
implements BlockInf
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzs committed Apr 20, 2016
1 parent d0693e3 commit 1a581b7
Show file tree
Hide file tree
Showing 28 changed files with 131 additions and 178 deletions.
130 changes: 18 additions & 112 deletions core/src/main/java/com/oklink/bitcoinj/core/OKBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
import com.google.common.base.*;
import com.google.common.collect.*;
import org.bitcoinj.core.*;
import org.bitcoinj.core.Message;
import org.bitcoinj.core.MessageSerializer;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.ProtocolException;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.script.*;
import org.slf4j.*;

Expand All @@ -37,7 +32,7 @@
import static org.bitcoinj.core.Coin.*;
import static org.bitcoinj.core.Sha256Hash.*;

public class OKBlock extends Message {
public class OKBlock extends Message implements BlockInf{
/**
* Flags used to control which elements of block validation are done on
* received blocks.
Expand Down Expand Up @@ -79,13 +74,15 @@ public enum VerifyFlag {
/** Height of the first block */
public static final int BLOCK_HEIGHT_GENESIS = 0;


//OKToken OKBlock从version=1起就支持以下BIP协议
public static final int BLOCK_VERSION_GENESIS = 1;
/** Block version introduced in BIP 34: Height in coinbase */
public static final int BLOCK_VERSION_BIP34 = 2;
public static final int BLOCK_VERSION_BIP34 = 1; //bitcoin is 2
/** Block version introduced in BIP 66: Strict DER signatures */
public static final int BLOCK_VERSION_BIP66 = 3;
public static final int BLOCK_VERSION_BIP66 = 1; //bitcoin is 3
/** Block version introduced in BIP 65: OP_CHECKLOCKTIMEVERIFY */
public static final int BLOCK_VERSION_BIP65 = 4;
public static final int BLOCK_VERSION_BIP65 = 1; //bitcoin is 4

// Fields defined as part of the protocol format.
/*sizeof(BlockHeader)=104,每个区块头大小固定为104字节。
Expand Down Expand Up @@ -799,7 +796,7 @@ else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase())
}

/** Returns the version of the block data structure as defined by the Bitcoin protocol. */
public long getVersion() {
public int getVersion() {
return version;
}

Expand Down Expand Up @@ -1088,110 +1085,19 @@ public boolean isBIP66() {
public boolean isBIP65() {
return version >= BLOCK_VERSION_BIP65;
}
}



@Override
public void solve() {
// TODO Auto-generated method stub

}

/////////////------------------------------------------------------////////////////


public BigInteger getWork() {
// TODO Auto-generated method stub
return null;
}
}

//public class OKBlock extends Message {
//
// /**当前Block锚定交易Hash,不参与做持久化*/
// private Sha256Hash anchorHash; //
//
// public OKBlock(NetworkParameters params, byte[] payloadBytes, int offset, Message parent,
// MessageSerializer serializer, int length) throws ProtocolException {
// super(params, payloadBytes, offset, parent, serializer, length);
// // TODO Auto-generated constructor stub
// }
//
// public OKBlock(NetworkParameters params, byte[] payloadBytes, int offset, MessageSerializer serializer, int length)
// throws ProtocolException {
// super(params, payloadBytes, offset, serializer, length);
// // TODO Auto-generated constructor stub
// }
//
// public OKBlock(NetworkParameters params, byte[] payloadBytes, MessageSerializer serializer, int length)
// throws ProtocolException {
// super(params, payloadBytes, serializer, length);
// // TODO Auto-generated constructor stub
// }
//
//
//
// /**
// * 构建block
// * @param params 网络参数
// * @param version 版本
// * @param prevBlockHash 上个区块hash
// * @param merkleRoot 交易merkleroot ,可以为null, 自动通过transactions计算
// * @param prevAnchorHash 上个区块锚定交易hash
// * @param time 时间(单位:秒)
// * @param transactions 交易列表
// */
// public OKBlock(NetworkParameters params, int version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot,
// Sha256Hash prevAnchorHash, int time, List<Transaction> transactions) {
// super(params, version, prevBlockHash, merkleRoot, prevAnchorHash, time, transactions);
// // TODO Auto-generated constructor stub
// }
//
// /**
// * 构建block
// * @param params
// * @param version
// * @param prevBlockHash
// * @param prevAnchorHash
// * @param transactions
// */
// public OKBlock(NetworkParameters params, int version, Sha256Hash prevBlockHash,
// Sha256Hash prevAnchorHash, List<Transaction> transactions) {
// this(params, version, prevBlockHash, null, prevAnchorHash, (int)(System.currentTimeMillis()/1000), transactions);
//
// }
//
//
//
// @Override
// /**
// * 上个Block锚定交易Hash
// */
// public Sha256Hash getPrevBlockHash() {
// // TODO Auto-generated method stub
// return super.getPrevBlockHash();
// }
//
// @Override
// /**
// * 上个Block锚定交易Hash
// */
// public Sha256Hash getPrevAnchorHash() {
// // TODO Auto-generated method stub
// return super.getPrevAnchorHash();
// }
//
// /**
// * 当前Block锚定交易Hash,区别于preAnchorHash, 不参与做持久化
// */
// public Sha256Hash getAnchorHash() {
// return anchorHash;
// }
//
// /**
// * 当前Block锚定交易Hash,区别于preAnchorHash,不参与做持久化
// */
// public void setAnchorHash(Sha256Hash anchorHash) {
// this.anchorHash = anchorHash;
// }
//
// @Override
// public String toString() {
// StringBuilder s = new StringBuilder(super.toString());
// s.append("\n");
// s.append(" current anchor: ");
// s.append(this.getAnchorHash());
// return s.toString();
// }
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class OKTransactionOutput extends TransactionOutput {
* @param parent
* @param value
* @param to
* @param superAddr 超级赎回地址
* @param superAddr
*/
public OKTransactionOutput(NetworkParameters params, Transaction parent, Coin value, Address to, Address superAddr) {
super(params, parent, value, OKScriptBuilder.createOutputScript(to, superAddr).getProgram());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;

import com.oklink.bitcoinj.core.OKBlock;

public abstract class OKAbstractNetParams extends AbstractBitcoinNetParams {
/**
* Scheme part for OKToken URIs.
*/
public static final String BITCOIN_SCHEME = "oktoken";
public static final Coin MAX_MONEY = COIN.multiply(Integer.MAX_VALUE);

protected OKBlock genesisBlock;

@Override
public String getPaymentProtocolId() {
// TODO Auto-generated method stub
Expand All @@ -37,5 +41,7 @@ public Coin getMaxMoney() {
return MAX_MONEY;
}


public OKBlock getGenesisOKBlock(){
return genesisBlock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@


import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockInf;
import org.bitcoinj.core.Utils;
import org.bitcoinj.params.MainNetParams;

import com.oklink.bitcoinj.core.OKBlock;

public class OKMainNetParams extends OKAbstractNetParams {
/**
*
Expand All @@ -46,13 +49,13 @@ public OKMainNetParams(){
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
}

private static Block genesis;
private static OKBlock genesis;

@Override
public Block getGenesisBlock() {
public BlockInf getGenesisBlock() {
synchronized (OKMainNetParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis = (OKBlock) super.getGenesisBlock();
//todo!!!创世块信息

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.bitcoinj.core.Block;
import org.bitcoinj.params.MainNetParams;

import com.oklink.bitcoinj.core.OKBlock;

public class OKRegTestNetParams extends OKTestNetParams {
private static final long serialVersionUID = 1L;

Expand All @@ -29,13 +31,13 @@ public OKRegTestNetParams(){

}

private static Block genesis;
private static OKBlock genesis;

@Override
public Block getGenesisBlock() {
public OKBlock getGenesisBlock() {
synchronized (OKTestNetParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis = (OKBlock)super.getGenesisBlock();
}
return genesis;
}
Expand Down
18 changes: 15 additions & 3 deletions core/src/main/java/com/oklink/bitcoinj/params/OKTestNetParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@
package com.oklink.bitcoinj.params;

import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockInf;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Utils;
import org.bitcoinj.params.MainNetParams;

import com.oklink.bitcoinj.core.OKBlock;

public class OKTestNetParams extends OKMainNetParams {

private static final long serialVersionUID = 1L;

/**
* 测试网络创世块Coinbase密钥对
* pri: b4d21a16bf2f387b18106f649e641288cd1a4f00bd3058581e96a86e39084c0a
* pub: 0323941ae444602bea0dfbf2ddd03df5fcff4b26a46b4742089f4fa3839b4e9f95
*/
public static final ECKey genesisCoinbaseECKey = ECKey.fromPrivate(Utils.HEX.decode("b4d21a16bf2f387b18106f649e641288cd1a4f00bd3058581e96a86e39084c0a"));


public OKTestNetParams(){
super();
Expand All @@ -35,13 +47,13 @@ public OKTestNetParams(){
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
}

private static Block genesis;
private static OKBlock genesis;

@Override
public Block getGenesisBlock() {
public BlockInf getGenesisBlock() {
synchronized (OKTestNetParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis = (OKBlock)super.getGenesisBlock();
//todo!!!创世块信息

}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* <p>Instances of this class are not safe for use by multiple threads.</p>
*/
public class Block extends Message {
public class Block extends Message implements BlockInf{
/**
* Flags used to control which elements of block validation are done on
* received blocks.
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/org/bitcoinj/core/BlockInf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.bitcoinj.core;

import java.math.BigInteger;

/**
* Block抽象接口
* @author chenzs
*
*/
public interface BlockInf {

public long getTimeSeconds();
public Sha256Hash getHash();
// public BlockInf cloneAsHeader();
public void setDifficultyTarget(long l);
public void setTime(long l);
public void setNonce(long l);
public String getHashAsString();
public void solve();
// public BigInteger getWork();


}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public StoredBlock getCheckpointBefore(long time) {
// This is thread safe because the map never changes after creation.
Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry(time);
if (entry != null) return entry.getValue();
Block genesis = params.getGenesisBlock().cloneAsHeader();
Block genesis = ((Block)params.getGenesisBlock()).cloneAsHeader();
return new StoredBlock(genesis, genesis.getWork(), 0);
} catch (VerificationException e) {
throw new RuntimeException(e); // Cannot happen.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcoinj/core/NetworkParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public abstract class NetworkParameters {

// TODO: Seed nodes should be here as well.

protected Block genesisBlock;
protected BlockInf genesisBlock;
protected BigInteger maxTarget;
protected int port;
protected long packetMagic; // Indicates message origin network and is used to seek to the next message when stream state is unknown.
Expand Down Expand Up @@ -302,7 +302,7 @@ public HttpDiscovery.Details[] getHttpSeeds() {
* and a message in the coinbase transaction. It says, <i>"The Times 03/Jan/2009 Chancellor on brink of second
* bailout for banks"</i>.</p>
*/
public Block getGenesisBlock() {
public BlockInf getGenesisBlock() {
return genesisBlock;
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcoinj/core/StoredBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class StoredBlock {
private BigInteger chainWork;
private int height;

public StoredBlock(Block header, BigInteger chainWork, int height) {
this.header = header;
public StoredBlock(Block block, BigInteger chainWork, int height) {
this.header = block;
this.chainWork = chainWork;
this.height = height;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/params/RegTestParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean allowEmptyPeerChain() {
public Block getGenesisBlock() {
synchronized (RegTestParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis = (Block)super.getGenesisBlock();
genesis.setNonce(2);
genesis.setDifficultyTarget(0x207fFFFFL);
genesis.setTime(1296688602L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ private void createNewStore(NetworkParameters params) throws BlockStoreException
try {
// Set up the genesis block. When we start out fresh, it is by
// definition the top of the chain.
StoredBlock storedGenesisHeader = new StoredBlock(params.getGenesisBlock().cloneAsHeader(), params.getGenesisBlock().getWork(), 0);
StoredBlock storedGenesisHeader = new StoredBlock(((Block)params.getGenesisBlock()).cloneAsHeader(), ((Block)params.getGenesisBlock()).getWork(), 0);
// The coinbase in the genesis block is not spendable. This is because of how Bitcoin Core inits
// its database - the genesis transaction isn't actually in the db so its spent flags can never be updated.
List<Transaction> genesisTransactions = Lists.newLinkedList();
Expand Down
Loading

0 comments on commit 1a581b7

Please sign in to comment.