Commit graph

3538 commits

Author SHA1 Message Date
Dave Collins
29eb4d4250 Add tests for new Serialize/Deserialize functions. 2013-08-05 18:08:26 -05:00
Dave Collins
088f3c923d Add Serialize/Deserialize for MsgBlock and MsgTx.
This commit introduces two new functions for MsgBlock and MsgTx named
Serialize and Deserialize.  The functions provide a stable mechanism for
serializing and deserializing blocks and transactions to and from disk
without having to worry about the protocol version.  Instead these
functions use the Version fields in the blocks and transactions.

These new functions differ from BtcEncode and BtcDecode in that the latter
functions are intended to encode/decode blocks and transaction from the
wire which technically can differ depending on the protocol version and
don't even really need to use the same format as the stored data.

Currently, there is no difference between the two, and due to how
intertwined they are in the reference implementaiton, they may not ever
diverge, but there is a difference and the goal for btcwire is to provide
a stable API that is flexible enough to deal with encoding changes.
2013-08-05 18:07:56 -05:00
John C. Vernaleo
285faf8c75 Add additional commands to ones we know return data type.
getaccount and getnewaddress
2013-08-05 17:26:59 -04:00
Dave Collins
c00de3ffd5 Fix a potential issue with removing orphan blocks.
The range loop is over a local var, so the local variable needs to be
updated and then set back into the chain instance after the range loop.
2013-08-04 13:41:05 -05:00
Dale Rahn
3b743e4cfc Cleanup after insert failure, do not leave inconsistant db.
Fix error returns in InsertBlock and FetchBlockBySha

Give up on return by name in InsertBlock() and return explicit
err one location in FetchBlockBySha to return proper error value
2013-08-03 11:15:42 -04:00
Dale Rahn
d8c3213d99 more 2013-08-03 11:09:50 -04:00
Dale Rahn
85d97d7436 block converted. 2013-08-02 18:31:21 -04:00
Dave Collins
0c2d4435ca Make use of removeChildNode function.
Rather than duplicating this code in the removeBlockNode function,
use the existing function.
2013-08-01 22:28:54 -05:00
Dave Collins
1a5683e220 Add optimized initial block index load function.
This commit adds a new optional function named GenerateInitialIndex.  It
generates the required number of initial block nodes in an optimized
fashion.  Since the memory block index is sparse and previous nodes are
dynamically loaded as needed, this function is not stricty needed.
However, during initial startup (when there are no nodes in memory yet),
dynamically loading all of the required nodes on the fly in the usual way
is much slower than preloading them.
2013-08-01 18:00:35 -05:00
Dave Collins
716075d0d0 Remove tesnet specific rules unsupported blurb.
The testnet specific rules have been supported since commit
9787f46f6a.
2013-08-01 17:10:59 -05:00
Dale Rahn
ead14e5a12 Testing code for FetchHeightRange() 2013-08-01 16:14:22 -04:00
Dale Rahn
b46d53c18e Test NewestSha() 2013-08-01 15:03:26 -04:00
Dave Collins
5b697947a7 Optimize getPrevNodeFromNode.
The recent pruning code made the parent hash for a node available directly
as a field in the node.  Make use of this in the getPrevNodeFromNode
function to avoid having to load the full block data associated with the
node to get the parent hash.
2013-08-01 12:34:59 -05:00
Dave Collins
2988289d2e Implement memory chain pruning.
This commit implements pruning for the block nodes that form the memory
chain which are no longer needed.  The validation of a block only requires
information from a set number of previous nodes.  The initial code did not
prune old nodes as they were no longer needed, but the vast majority of
the infrastructure to support it was already in place as that was always
the goal.  This commit finishes implementing the missing bits to make it
a reality.
2013-08-01 11:31:52 -05:00
Dave Collins
2d6a664d9d Greatly optimize best chain selection work values.
Previously, the code was using big rational numbers for work values which
resulted in carrying way too much precision around (and ultimately a lot
of extra memory and computation to carry that precision).  This commit
converts the work values to big integers and calculates them with integer
division.  This is acceptable because the numerator is multiplied by 2^256
which is higher than the maximum possible proof of work.  Therefore
anything after the decimal is superfluous precision for the purposes of
chain selection.

Also, add a check for negative difficulty values when calculating the work
value.  Negative values won't occur in practice with valid blocks, but
it's possible an invalid block could trigger the code path, so be safe and
check for it.
2013-07-31 13:07:06 -05:00
Dale Rahn
f205ff5ac0 External API functions need to grab locks before performing internal database operations.
Untangle internal and external functions, to prevent recursive locks
2013-07-31 11:14:19 -04:00
Dave Collins
455c92a716 Correct reg test proof of work limit comment. 2013-07-31 09:20:11 -05:00
Dave Collins
72feef5194 Clarify reg test proof of work limit comment. 2013-07-31 09:14:02 -05:00
Owain G. Ainsworth
118014a5b4 gofmt. 2013-07-31 01:31:27 +01:00
Owain G. Ainsworth
927d846f06 Handle bad input in sighash single.
The bitcoinj test suite actually uses it (the only thing i've seen that
does). Specifically it uses it with a bad input which returns a hash of
1.
2013-07-31 01:31:13 +01:00
Dave Collins
bb7f5da609 Print transaction hash in script val errors.
The txsha variable is already a pointer to the hash, so print the
variable, not the address of it.
2013-07-30 18:04:58 -05:00
Dave Collins
357160257c Allow checkpoints to work with all networks.
Previously the main network checkpoints were being used for unrecognized
networks.  This commit changes the code so that no checkpoints are used in
that scenario.
2013-07-30 15:29:15 -05:00
Dave Collins
1e891b4e0b Optimize proof of work limit bits handling.
Rather than converting the proof of work limit to its compact
representation multiple times during operation, do it once at package
initialization time and export it via the chain parameters.
2013-07-30 12:59:50 -05:00
Dave Collins
cef901ebad Export chain parameters.
This commit exports the chain parameters used depending on the specific
bitcoin network so callers can have access to this information if desired.
2013-07-30 12:38:27 -05:00
Dave Collins
0e7791058a Add logging for chain forks and reorgs.
This commit adds info level log statements when a block causes a chain
fork, extends a side chain (fork), or causes a reorganize.  When a reorg
happens, the new and old chain heads along with the fork point are logged.
2013-07-29 22:32:12 -05:00
Dave Collins
e5eaaee91c Remove a couple of error logs that are not needed. 2013-07-29 22:31:05 -05:00
Dave Collins
829f187e47 Use network-specific genesis hash.
The test in checkConnectBlock for whether or not the node is the genesis
block needs to account for the genesis block of the specified network.
2013-07-29 20:21:24 -05:00
Dave Collins
04d88d01ec Clarify mismatched merkle root error message. 2013-07-29 19:50:00 -05:00
Dave Collins
077e1ec336 Improve second coinbase error message.
Include the index at which the second coinbase transaction was found.
2013-07-29 17:02:42 -05:00
Dave Collins
882d1c1687 Ensure height is set on block as well as the node.
Even though the code currently makes heavy use of nodes which will have
the appropriate height, blocks which did not come from the database will
not have a height set.  As a result, this could possibly result in a
height of 0 being used when unintended.  By setting the block height in
the block as soon as its known (regardless of the source), we can future
proof against bugs that would likely be hard to track down.
2013-07-29 16:58:48 -05:00
Dave Collins
6409879e14 Modify count sigops calls for new btcscript API.
The btcscript API for GetSigOpCount and GetPreciseSigOpCount
recently changed to no longer return an error.  This change was necessary
to mirror the behavior of bitcoind signature operations counting.  This
commit modifies the code accordingly to use the new API.
2013-07-29 16:21:33 -05:00
Owain G. Ainsworth
22c29c4316 Perform pushed length checks at exec time, not parse.
To match bitcoind behaviour. Fix tests to agree with this direction.
2013-07-29 22:00:57 +01:00
Owain G. Ainsworth
fe2233c851 Make count sigops always count up to the point of parse failure.
We do this by allowing the parser to return the list of parsed objects
(it is an internal only api anyway) and then we use this in the sigops
counting and ignore the error. This change due to bitcoind
compatability.
2013-07-29 22:00:11 +01:00
Dave Collins
c3b330e42d Allow tx inputs to use txns earlier in same block.
It is acceptable for a transaction input in a block to reference the
output of another transaction in the same block only if the referenced
transaction comes before the transaction referencing it.
2013-07-29 15:48:52 -05:00
Dave Collins
dc3a2dbac0 Correct example usage.
The variable to insert is 'genesis', not 'block'.
2013-07-29 12:41:30 -05:00
Dave Collins
e82c97be3b Update examples for recent btcdb changes.
Since creating a new database with btcdb no longer automatically inserts
the main network genesis block, update the examples accordingly.
2013-07-29 12:35:01 -05:00
Dave Collins
25684a2ccb Don't add a genesis block by default.
This commit modifies the way initial database creation is handled.

Previously, the genesis for the main chain was inserted automatically upon
creation of the database.  However, that approach caused an issue since
other networks such as the test network don't use the same genesis block
as the main network.

The new approach introduced by this commit is to leave it up to the caller
to insert the desired genesis block.  In order to support this, the
InsertBlock function has been modified to allow the first (and only the
first) block to be inserted without having an existing parent.  Also, the
NewestSha function has been modified to return a zero hash, -1 for the
height, and no error when the database does not yet have any blocks.  This
allows the caller to determine the difference between no blocks and only
the genesis block (in which case the return values would be the genesis
hash and 0 for the height).
2013-07-29 11:48:10 -05:00
Dave Collins
584481280c Print actual hash on a couple of log warnings.
The blocksha variable is already a pointer.
2013-07-29 10:57:03 -05:00
Dave Collins
761f666c44 Use consistent form for Checkpoint comment. 2013-07-27 16:46:46 -05:00
Dave Collins
ace58495c8 Comment CheckpointConfirmations. 2013-07-27 16:45:59 -05:00
Dave Collins
81120958f0 Comment inventory vector constants. 2013-07-27 16:42:23 -05:00
Dave Collins
69446009b2 Move TxVersion constant definition to msgtx.go.
This moves the definition closer to the associated type and mirrors the
rest of the package.
2013-07-27 16:34:47 -05:00
Dave Collins
035a9c3dc3 Add several comments for exported constants. 2013-07-27 16:31:47 -05:00
Dave Collins
d6752d8f99 Update comments to fix typos and use proper form. 2013-07-27 16:18:13 -05:00
Dave Collins
bfbc08beed Allow var defs to infer type from right-hand side.
Found by golint.
2013-07-27 16:10:38 -05:00
Dave Collins
99b06f8bce Add format specifier in pong test error output.
The error message did not have a format specifier even though it was
passing an argument to show.  Found by go vet.
2013-07-27 15:56:17 -05:00
Dave Collins
562bde6902 Use byte literals in tests to make go vet happy.
The go vet command complains about untagged struct initializers when
defining a ShaHash directly.  This seems to be a limitation where go vet
does not exclude the warning for types which are a constant size byte array
like it does for normal constant size byte array definition.

This commit simply modifies the tests to use a constant definition cast to a
ShaHash to overcome the limitation of go vet.
2013-07-27 15:55:06 -05:00
Dave Collins
90bf9e7449 Update tests for corrected max block payload size. 2013-07-27 15:32:36 -05:00
Dave Collins
9989865fa2 Use correct max block payload size.
The maximum block payload size is actually 1000000 bytes, not 1MB.
2013-07-27 15:25:05 -05:00
Dave Collins
b282678d9a Spend outputs while checking transaction inputs.
This commit modifies the double spend detection to handle double spends
within the same block as well as side chains when doing the checks before
reorganizing the chain.
2013-07-26 11:51:13 -05:00