GGYU
체인코드 본문
HashInterface.java
public interface HashInterface {
String dowork(String preHash);
void upateHash(String PreHash);
}
HashAbstract.java
public abstract class HashAbstract implements HashInterface{
Public HashInterface node=null;
public final void updateHash(String preHash)
{
preHash=dowork(preHash);
if(node!=null)
node.update(preHash);
}
}
BlockInfo.java
public class BlockInfo extends HashAbstract{
private String blockHash;
private String previousBlockHash;
private int nonce; //hash 값 찾을때까지의 연산 횟수
pricate int bits; //난이도
private String time;
pricate String transactionHash; //Mukle Root
private int transactionCut=0;
pricate ArrayList<TransactionInfo> trList = new ArrayList();
public BlockInfo(String previousBlockHash,int bits)
{
this.previousBlockHash=previousBlockHash;
this.bits=bits;
long curr= System.currentTimeMillis();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy:MM:dd-hh:mm:ss");
time=sdf2.format(new Date(cur));
}
@Override
public String dowork(String o)
{
if(!o.isEmpty()){
previousBlockHash=o;
}
calcMukleTree();
String tmpHash="";
while(!(tmpHash=HashUtil.genHash(previousBlockHash+bits+nonce+time+transactionHash+transactionCount)).substring(0,bits).matches("^[0]*$") && previousBlockHash.compareTo(tmpHash)>0) //previous가 더큰경우
{
nonce++;
}
this.blockHash=tmpHash;
return blockHash;
}
private void calcMukleTree()
{
//transaction 의 모든 해쉬를 합하여 전체 해쉬를 함. 구한값을 머클트리 루트에 대임
}
public void addTransaction(TransactionInfo ti)
{
//transactionList.add 호출
//블럭 인서트 조건시 블럭 hash 값 생성
}
}
BlockChain.java
public class BlockChain{
private LinkedHashMap<Integer,BlockInfo> chain = new LinkedHashMap<>();
private int index=0;
private static BlockChain instance;
static{
try{
System.out.println("aaa");
instance=new BlockChain();
}
catch(Exception e)
{
//
}
}
public static BlockChain getInstance()
{
return instance;
}
public void addBlock(BlockInfo bi)
{
if(0 <=index -1){
BlockInfo b= chain.get(index-1);
b.setNextNode(bi);
}
chain.put(index++,bi);
}
}
'프로그래밍 > JAVA' 카테고리의 다른 글
object 전송 (0) | 2018.07.19 |
---|---|
collection sort (0) | 2018.07.19 |
sha512 예제 (0) | 2018.07.19 |
base64 사용법 (0) | 2018.07.19 |
서버/클라이언트 예제 (0) | 2018.07.19 |