Add getters/setters and a solve method to the Block object. Use this in a new proof of work unit test.

Introduce a unit test NetworkParameters that has very easy difficulty.
Correct some assertTrue(false) type checks to use fail() instead.
Make ALLOWED_TIME_DRIFT non-public until I have a good explanation in the javadocs of what it's for.
Simplify Block.equals
This commit is contained in:
Mike Hearn
2011-03-08 15:23:06 +00:00
parent 38bf9a8a80
commit 4003eee3fe
11 changed files with 533 additions and 124 deletions

View File

@@ -73,9 +73,9 @@ function windowTitle()
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#fields_inherited_from_class_com.google.bitcoin.core.Message">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
@@ -124,14 +124,6 @@ A block is the foundation of the BitCoin system. It records a set of <A HREF="..
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Field Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>static&nbsp;long</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#ALLOWED_TIME_DRIFT">ALLOWED_TIME_DRIFT</A></B></CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
</TABLE>
&nbsp;<A NAME="fields_inherited_from_class_com.google.bitcoin.core.Message"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
@@ -186,6 +178,22 @@ A block is the foundation of the BitCoin system. It records a set of <A HREF="..
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;long</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getDifficultyTarget()">getDifficultyTarget</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the difficulty of the proof of work that this block should meet encoded in compact form.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.math.BigInteger</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getDifficultyTargetBI()">getDifficultyTargetBI</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the difficulty target as a 256 bit value that can be compared to a SHA-256 hash.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;byte[]</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getHash()">getHash</A></B>()</CODE>
@@ -211,9 +219,90 @@ A block is the foundation of the BitCoin system. It records a set of <A HREF="..
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;long</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getNonce()">getNonce</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the nonce, an arbitrary value that exists only to make the hash of the block header fall below the
difficulty target.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;byte[]</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getPrevBlockHash()">getPrevBlockHash</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the hash of the previous block in the chain, as defined by the block header.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;long</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getTime()">getTime</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the time at which the block was solved and broadcast, according to the clock of the solving node.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;long</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#getVersion()">getVersion</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the version of the block data structure as defined by the BitCoin protocol.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#hashCode()">hashCode</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setDifficultyTarget(long)">setDifficultyTarget</A></B>(long&nbsp;compactForm)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setMerkleRoot(byte[])">setMerkleRoot</A></B>(byte[]&nbsp;value)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setNonce(long)">setNonce</A></B>(long&nbsp;nonce)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setPrevBlockHash(byte[])">setPrevBlockHash</A></B>(byte[]&nbsp;prevBlockHash)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setTime(long)">setTime</A></B>(long&nbsp;time)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/Block.html#setVersion(long)">setVersion</A></B>(long&nbsp;version)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
@@ -255,25 +344,6 @@ A block is the foundation of the BitCoin system. It records a set of <A HREF="..
&nbsp;
<P>
<!-- ============ FIELD DETAIL =========== -->
<A NAME="field_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Field Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="ALLOWED_TIME_DRIFT"><!-- --></A><H3>
ALLOWED_TIME_DRIFT</H3>
<PRE>
public static final long <B>ALLOWED_TIME_DRIFT</B></PRE>
<DL>
<DL>
<DT><B>See Also:</B><DD><A HREF="../../../../constant-values.html#com.google.bitcoin.core.Block.ALLOWED_TIME_DRIFT">Constant Field Values</A></DL>
</DL>
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<A NAME="constructor_detail"><!-- --></A>
@@ -414,6 +484,17 @@ public byte[] <B>getMerkleRoot</B>()</PRE>
</DL>
<HR>
<A NAME="setMerkleRoot(byte[])"><!-- --></A><H3>
setMerkleRoot</H3>
<PRE>
public void <B>setMerkleRoot</B>(byte[]&nbsp;value)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="addTransaction(com.google.bitcoin.core.Transaction)"><!-- --></A><H3>
addTransaction</H3>
<PRE>
@@ -425,6 +506,142 @@ public void <B>addTransaction</B>(<A HREF="../../../../com/google/bitcoin/core/T
</DL>
</DD>
</DL>
<HR>
<A NAME="getVersion()"><!-- --></A><H3>
getVersion</H3>
<PRE>
public long <B>getVersion</B>()</PRE>
<DL>
<DD>Returns the version of the block data structure as defined by the BitCoin protocol.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="setVersion(long)"><!-- --></A><H3>
setVersion</H3>
<PRE>
public void <B>setVersion</B>(long&nbsp;version)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getPrevBlockHash()"><!-- --></A><H3>
getPrevBlockHash</H3>
<PRE>
public byte[] <B>getPrevBlockHash</B>()</PRE>
<DL>
<DD>Returns the hash of the previous block in the chain, as defined by the block header.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="setPrevBlockHash(byte[])"><!-- --></A><H3>
setPrevBlockHash</H3>
<PRE>
public void <B>setPrevBlockHash</B>(byte[]&nbsp;prevBlockHash)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getTime()"><!-- --></A><H3>
getTime</H3>
<PRE>
public long <B>getTime</B>()</PRE>
<DL>
<DD>Returns the time at which the block was solved and broadcast, according to the clock of the solving node.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="setTime(long)"><!-- --></A><H3>
setTime</H3>
<PRE>
public void <B>setTime</B>(long&nbsp;time)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getDifficultyTarget()"><!-- --></A><H3>
getDifficultyTarget</H3>
<PRE>
public long <B>getDifficultyTarget</B>()</PRE>
<DL>
<DD>Returns the difficulty of the proof of work that this block should meet encoded in compact form. The
<A HREF="../../../../com/google/bitcoin/core/BlockChain.html" title="class in com.google.bitcoin.core"><CODE>BlockChain</CODE></A> verifies that this is not too easy by looking at the length of the chain when the block is
added. To find the actual value the hash should be compared against, use getDifficultyTargetBI.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getDifficultyTargetBI()"><!-- --></A><H3>
getDifficultyTargetBI</H3>
<PRE>
public java.math.BigInteger <B>getDifficultyTargetBI</B>()</PRE>
<DL>
<DD>Returns the difficulty target as a 256 bit value that can be compared to a SHA-256 hash.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="setDifficultyTarget(long)"><!-- --></A><H3>
setDifficultyTarget</H3>
<PRE>
public void <B>setDifficultyTarget</B>(long&nbsp;compactForm)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="getNonce()"><!-- --></A><H3>
getNonce</H3>
<PRE>
public long <B>getNonce</B>()</PRE>
<DL>
<DD>Returns the nonce, an arbitrary value that exists only to make the hash of the block header fall below the
difficulty target.
<P>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="setNonce(long)"><!-- --></A><H3>
setNonce</H3>
<PRE>
public void <B>setNonce</B>(long&nbsp;nonce)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<!-- ========= END OF CLASS DATA ========= -->
<HR>
@@ -476,9 +693,9 @@ public void <B>addTransaction</B>(<A HREF="../../../../com/google/bitcoin/core/T
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#fields_inherited_from_class_com.google.bitcoin.core.Message">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>

View File

@@ -162,6 +162,14 @@ A NetworkConnection handles talking to a remote BitCoin peer at a low level. It
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;java.lang.String</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/NetworkConnection.html#toString()">toString</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../../../com/google/bitcoin/core/NetworkConnection.html#writeMessage(java.lang.String, com.google.bitcoin.core.Message)">writeMessage</A></B>(java.lang.String&nbsp;tag,
<A HREF="../../../../com/google/bitcoin/core/Message.html" title="class in com.google.bitcoin.core">Message</A>&nbsp;message)</CODE>
@@ -176,7 +184,7 @@ A NetworkConnection handles talking to a remote BitCoin peer at a low level. It
<TH ALIGN="left"><B>Methods inherited from class java.lang.Object</B></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait</CODE></TD>
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait</CODE></TD>
</TR>
</TABLE>
&nbsp;
@@ -253,19 +261,35 @@ public void <B>shutdown</B>()
</DL>
<HR>
<A NAME="toString()"><!-- --></A><H3>
toString</H3>
<PRE>
public java.lang.String <B>toString</B>()</PRE>
<DL>
<DD><DL>
<DT><B>Overrides:</B><DD><CODE>toString</CODE> in class <CODE>java.lang.Object</CODE></DL>
</DD>
<DD><DL>
</DL>
</DD>
</DL>
<HR>
<A NAME="readMessage()"><!-- --></A><H3>
readMessage</H3>
<PRE>
public <A HREF="../../../../com/google/bitcoin/core/Message.html" title="class in com.google.bitcoin.core">Message</A> <B>readMessage</B>()
throws <A HREF="../../../../com/google/bitcoin/core/ProtocolException.html" title="class in com.google.bitcoin.core">ProtocolException</A></PRE>
throws java.io.IOException,
<A HREF="../../../../com/google/bitcoin/core/ProtocolException.html" title="class in com.google.bitcoin.core">ProtocolException</A></PRE>
<DL>
<DD>Reads a network message from the wire, blocking until the message is fully received.
<P>
<DD><DL>
<DT><B>Returns:</B><DD>An instance of a Message subclass.
<DT><B>Returns:</B><DD>An instance of a Message subclass
<DT><B>Throws:</B>
<DD><CODE><A HREF="../../../../com/google/bitcoin/core/ProtocolException.html" title="class in com.google.bitcoin.core">ProtocolException</A></CODE> - if the message is badly formatted, failed checksum or there was a protocol failure.</DL>
<DD><CODE><A HREF="../../../../com/google/bitcoin/core/ProtocolException.html" title="class in com.google.bitcoin.core">ProtocolException</A></CODE> - if the message is badly formatted, failed checksum or there was a TCP failure.
<DD><CODE>java.io.IOException</CODE></DL>
</DD>
</DL>
<HR>