[NOD-173] Add UTXO commitment to block header (#297)

This commit is contained in:
Ori Newman 2019-05-13 16:23:28 +03:00 committed by Svarog
parent 3b72aafbc6
commit d7a2ab52a1
34 changed files with 206 additions and 82 deletions

View File

@ -104,6 +104,7 @@ type blockNode struct {
hashMerkleRoot *daghash.Hash
idMerkleRoot *daghash.Hash
acceptedIDMerkleRoot *daghash.Hash
utxoCommitment *daghash.Hash
// status is a bitfield representing the validation state of the block. The
// status field, unlike the other fields, may be written to and so should
@ -132,6 +133,7 @@ func initBlockNode(node *blockNode, blockHeader *wire.BlockHeader, parents block
node.hashMerkleRoot = blockHeader.HashMerkleRoot
node.idMerkleRoot = blockHeader.IDMerkleRoot
node.acceptedIDMerkleRoot = blockHeader.AcceptedIDMerkleRoot
node.utxoCommitment = blockHeader.UTXOCommitment
} else {
node.hash = &daghash.ZeroHash
}
@ -183,6 +185,7 @@ func (node *blockNode) Header() *wire.BlockHeader {
HashMerkleRoot: node.hashMerkleRoot,
IDMerkleRoot: node.idMerkleRoot,
AcceptedIDMerkleRoot: node.acceptedIDMerkleRoot,
UTXOCommitment: node.utxoCommitment,
Timestamp: time.Unix(node.timestamp, 0),
Bits: node.bits,
Nonce: node.nonce,

View File

@ -207,6 +207,7 @@ func newTestNode(parents blockSet, blockVersion int32, bits uint32, timestamp ti
HashMerkleRoot: &daghash.ZeroHash,
IDMerkleRoot: &daghash.ZeroHash,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
}
return newBlockNode(header, parents, phantomK)
}

View File

@ -192,10 +192,10 @@ func TestHaveBlock(t *testing.T) {
{hash: dagconfig.SimNetParams.GenesisHash.String(), want: true},
// Block 3b should be present (as a second child of Block 2).
{hash: "3a81c2f7064370dddaeaf7bc7e4d282702cefb2b6e11c84942c3791c76cdd3e8", want: true},
{hash: "0a20af2fa5ce154a60faca96e9fa125fc52e0ca8f98484708d0413203626edaf", want: true},
// Block 100000 should be present (as an orphan).
{hash: "746deb238f38dfc82ea2e1dbd85c079ceb581fc2912aae37d6c3675a12545df4", want: true},
{hash: "18bcf45b8c0dbccd7690a728f3486c6d5fc84971688f89f4554297b6a278e554", want: true},
// Random hashes should not be available.
{hash: "123", want: false},
@ -558,6 +558,7 @@ func chainedNodes(parents blockSet, numNodes int) []*blockNode {
IDMerkleRoot: &daghash.ZeroHash,
HashMerkleRoot: &daghash.ZeroHash,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
}
header.ParentHashes = tips.hashes()
nodes[i] = newBlockNode(&header, tips, dagconfig.SimNetParams.K)
@ -908,6 +909,7 @@ func TestValidateFeeTransaction(t *testing.T) {
HashMerkleRoot: BuildHashMerkleTreeStore(utilTxs).Root(),
IDMerkleRoot: BuildIDMerkleTreeStore(utilTxs).Root(),
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
},
Transactions: transactions,
}

View File

@ -625,6 +625,7 @@ func (dag *BlockDAG) deserializeBlockNode(blockRow []byte) (*blockNode, error) {
hashMerkleRoot: header.HashMerkleRoot,
idMerkleRoot: header.IDMerkleRoot,
acceptedIDMerkleRoot: header.AcceptedIDMerkleRoot,
utxoCommitment: header.UTXOCommitment,
}
node.children = newSet()

View File

@ -67,5 +67,5 @@ func ExampleBlockDAG_ProcessBlock() {
fmt.Printf("Block accepted. Is it an orphan?: %v", isOrphan)
// Output:
// Failed to process block: already have block 4aa46622896ef2f0c59aaff0c05f87984a3e640a8e79f9167f4c1b959fd14706
// Failed to process block: already have block 5804d0207bdbfd88dd035271fab95944585eb57017b0709d5a0a10a7edb37795
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -234,6 +234,12 @@ func TestCheckBlockSanity(t *testing.T) {
0x4e, 0x06, 0xba, 0x64, 0xd7, 0x61, 0xda, 0x25,
0x1a, 0x0e, 0x21, 0xd4, 0x64, 0x49, 0x02, 0xa2,
},
UTXOCommitment: &daghash.Hash{
0x80, 0xf7, 0x00, 0xe3, 0x16, 0x3d, 0x04, 0x95,
0x5b, 0x7e, 0xaf, 0x84, 0x7e, 0x1b, 0x6b, 0x06,
0x4e, 0x06, 0xba, 0x64, 0xd7, 0x61, 0xda, 0x25,
0x1a, 0x0e, 0x21, 0xd4, 0x64, 0x49, 0x02, 0xa2,
},
Timestamp: time.Unix(0x5cd18053, 0),
Bits: 0x207fffff,
Nonce: 0x0,
@ -520,7 +526,7 @@ func TestCheckSerializedHeight(t *testing.T) {
msgTx := coinbaseTx.Copy()
msgTx.TxIn[0].SignatureScript = test.sigScript
msgBlock := wire.NewMsgBlock(wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0))
msgBlock := wire.NewMsgBlock(wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0))
msgBlock.AddTransaction(msgTx)
block := util.NewBlock(msgBlock)
block.SetHeight(test.wantHeight)
@ -627,7 +633,12 @@ func TestValidateParents(t *testing.T) {
b := generateNode(a)
c := generateNode(genesisNode)
fakeBlockHeader := &wire.BlockHeader{IDMerkleRoot: &daghash.ZeroHash, HashMerkleRoot: &daghash.ZeroHash, AcceptedIDMerkleRoot: &daghash.ZeroHash}
fakeBlockHeader := &wire.BlockHeader{
IDMerkleRoot: &daghash.ZeroHash,
HashMerkleRoot: &daghash.ZeroHash,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
}
// Check direct parents relation
err := validateParents(fakeBlockHeader, setFromSlice(a, b))
@ -771,9 +782,10 @@ var Block100000 = wire.MsgBlock{
0x81, 0xb8, 0xa0, 0x68, 0x77, 0xc4, 0x02, 0x1e,
0x3c, 0xb1, 0x16, 0x8f, 0x5f, 0x6b, 0x45, 0x87,
},
Timestamp: time.Unix(0x5c404bc3, 0),
Bits: 0x207fffff,
Nonce: 0xdffffffffffffff9,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: time.Unix(0x5c404bc3, 0),
Bits: 0x207fffff,
Nonce: 0xdffffffffffffff9,
},
Transactions: []*wire.MsgTx{
{
@ -1068,6 +1080,12 @@ var BlockWithWrongTxOrder = wire.MsgBlock{
0x0b, 0x79, 0xf5, 0x29, 0x6d, 0x1c, 0xaa, 0x90,
0x2f, 0x01, 0xd4, 0x83, 0x9b, 0x2a, 0x04, 0x5e,
},
UTXOCommitment: &daghash.Hash{
0x00, 0x69, 0x2d, 0x16, 0xb5, 0xd7, 0xe4, 0xf3,
0xcd, 0xc7, 0xc9, 0xaf, 0xfb, 0xd2, 0x1b, 0x85,
0x0b, 0x79, 0xf5, 0x29, 0x6d, 0x1c, 0xaa, 0x90,
0x2f, 0x01, 0xd4, 0x83, 0x9b, 0x2a, 0x04, 0x5e,
},
Timestamp: time.Unix(0x5cd16eaa, 0),
Bits: 0x207fffff,
Nonce: 0x2,

View File

@ -41,10 +41,10 @@ var genesisCoinbaseTx = wire.NewNativeMsgTx(1, genesisTxIns, genesisTxOuts)
// genesisHash is the hash of the first block in the block chain for the main
// network (genesis block).
var genesisHash = daghash.Hash([daghash.HashSize]byte{
0x06, 0x47, 0xd1, 0x9f, 0x95, 0x1b, 0x4c, 0x7f,
0x16, 0xf9, 0x79, 0x8e, 0x0a, 0x64, 0x3e, 0x4a,
0x98, 0x87, 0x5f, 0xc0, 0xf0, 0xaf, 0x9a, 0xc5,
0xf0, 0xf2, 0x6e, 0x89, 0x22, 0x66, 0xa4, 0x4a,
0x95, 0x77, 0xb3, 0xed, 0xa7, 0x10, 0x0a, 0x5a,
0x9d, 0x70, 0xb0, 0x17, 0x70, 0xb5, 0x5e, 0x58,
0x44, 0x59, 0xb9, 0xfa, 0x71, 0x52, 0x03, 0xdd,
0x88, 0xfd, 0xdb, 0x7b, 0x20, 0xd0, 0x04, 0x58,
})
// genesisMerkleRoot is the hash of the first transaction in the genesis block
@ -65,9 +65,10 @@ var genesisBlock = wire.MsgBlock{
HashMerkleRoot: &genesisMerkleRoot,
IDMerkleRoot: &genesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.Hash{},
Timestamp: time.Unix(0x5cd00a98, 0),
UTXOCommitment: &daghash.Hash{},
Timestamp: time.Unix(0x5cd83da0, 0),
Bits: 0x207fffff,
Nonce: 0x0,
Nonce: 0x1,
},
Transactions: []*wire.MsgTx{genesisCoinbaseTx},
}
@ -118,10 +119,10 @@ var devNetGenesisCoinbaseTx = genesisCoinbaseTx
// devGenesisHash is the hash of the first block in the block chain for the development
// network (genesis block).
var devNetGenesisHash = daghash.Hash([daghash.HashSize]byte{
0x90, 0x9c, 0x51, 0x90, 0x39, 0x02, 0x5e, 0x11,
0x52, 0xa6, 0x54, 0xff, 0xc8, 0x40, 0xdf, 0x67,
0x2a, 0xe8, 0x20, 0x72, 0xed, 0x6a, 0x5e, 0x3f,
0xf4, 0x8e, 0xf8, 0xdb, 0x02, 0x02, 0x00, 0x00,
0x53, 0x68, 0x95, 0xd4, 0xf7, 0xc7, 0x3b, 0x94,
0x64, 0xfa, 0x98, 0xc7, 0xcb, 0x92, 0x53, 0x71,
0x5e, 0x14, 0xd1, 0x83, 0x01, 0xd4, 0x1e, 0x17,
0xd4, 0xc4, 0xd3, 0x50, 0xa7, 0x64, 0x00, 0x00,
})
// devNetGenesisMerkleRoot is the hash of the first transaction in the genesis block
@ -137,9 +138,10 @@ var devNetGenesisBlock = wire.MsgBlock{
HashMerkleRoot: &devNetGenesisMerkleRoot,
IDMerkleRoot: &devNetGenesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.Hash{},
Timestamp: time.Unix(0x5cd00a98, 0),
UTXOCommitment: &daghash.Hash{},
Timestamp: time.Unix(0x5cd83da0, 0),
Bits: 0x1e7fffff,
Nonce: 0x2f0e8,
Nonce: 0xfe2a,
},
Transactions: []*wire.MsgTx{devNetGenesisCoinbaseTx},
}

View File

@ -133,9 +133,13 @@ var genesisBlockBytes = []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x0a, 0xd0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x3d, 0xd8,
0x5c, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

View File

@ -175,5 +175,5 @@ func Example_blockStorageAndRetrieval() {
fmt.Printf("Serialized block size: %d bytes\n", len(loadedBlockBytes))
// Output:
// Serialized block size: 225 bytes
// Serialized block size: 257 bytes
}

View File

@ -16,7 +16,7 @@ import (
func TestDeleteFile(t *testing.T) {
testBlock := util.NewBlock(wire.NewMsgBlock(
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
tests := []struct {
fileNum uint32
@ -69,7 +69,7 @@ func TestDeleteFile(t *testing.T) {
// and makes sure no panic occurs, as well as ensures the writeCursor was updated correctly.
func TestHandleRollbackErrors(t *testing.T) {
testBlock := util.NewBlock(wire.NewMsgBlock(
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
testBlockSize := uint32(testBlock.MsgBlock().SerializeSize())
tests := []struct {

View File

@ -553,7 +553,7 @@ func TestForEachBucket(t *testing.T) {
// TestStoreBlockErrors tests all error-cases in *tx.StoreBlock().
// The non-error-cases are tested in the more general tests.
func TestStoreBlockErrors(t *testing.T) {
testBlock := util.NewBlock(wire.NewMsgBlock(wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
testBlock := util.NewBlock(wire.NewMsgBlock(wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)))
tests := []struct {
name string
@ -716,7 +716,7 @@ func TestWritePendingAndCommitErrors(t *testing.T) {
rollbackCalled = false
err = pdb.Update(func(dbTx database.Tx) error {
return dbTx.StoreBlock(util.NewBlock(wire.NewMsgBlock(
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0))))
wire.NewBlockHeader(1, []*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0))))
})
if err == nil {
t.Errorf("No error returned when blockIdx.Put() should have returned an error")

Binary file not shown.

View File

@ -49,6 +49,7 @@ func generateBlock(parent *wire.MsgBlock) *wire.MsgBlock {
HashMerkleRoot: &genesisMerkleRoot,
IDMerkleRoot: &daghash.ZeroHash,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: time.Unix(0x5b28c4c8, 0), // 2018-06-19 08:54:32 +0000 UTC
Bits: 0x2e00ffff, // 503382015 [000000ffff000000000000000000000000000000000000000000000000000000]
Nonce: 0xc0192550, // 2148484547

View File

@ -191,6 +191,7 @@ func CreateBlock(parentBlock *util.Block, inclusionTxs []*util.Tx,
HashMerkleRoot: hashMerkleTree.Root(),
IDMerkleRoot: idMerkleTree.Root(),
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: ts,
Bits: net.PowLimitBits,
}

View File

@ -70,7 +70,12 @@ func (eft *estimateFeeTester) newBlock(txs []*wire.MsgTx) {
eft.height++
block := util.NewBlock(&wire.MsgBlock{
Header: wire.BlockHeader{IDMerkleRoot: &daghash.ZeroHash, HashMerkleRoot: &daghash.ZeroHash, AcceptedIDMerkleRoot: &daghash.ZeroHash},
Header: wire.BlockHeader{
IDMerkleRoot: &daghash.ZeroHash,
HashMerkleRoot: &daghash.ZeroHash,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
},
Transactions: txs,
})
block.SetHeight(eft.height)

View File

@ -660,6 +660,7 @@ func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress util.Address) (*BlockTe
HashMerkleRoot: hashMerkleTree.Root(),
IDMerkleRoot: idMerkleTree.Root(),
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: ts,
Bits: reqDifficulty,
}

View File

@ -37,7 +37,7 @@ func parseBlock(template *btcjson.GetBlockTemplateResult) (*util.Block, error) {
bits := uint32(bitsInt64)
// parse rest of block
msgBlock := wire.NewMsgBlock(wire.NewBlockHeader(template.Version, parentHashes, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, uint32(bits), 0))
msgBlock := wire.NewMsgBlock(wire.NewBlockHeader(template.Version, parentHashes, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, uint32(bits), 0))
for i, txResult := range append([]btcjson.GetBlockTemplateResultTx{*template.CoinbaseTxn}, template.Transactions...) {
reader := hex.NewDecoder(strings.NewReader(txResult.Data))

View File

@ -494,7 +494,7 @@ func TestPeerListeners(t *testing.T) {
{
"OnBlock",
wire.NewMsgBlock(wire.NewBlockHeader(1,
[]*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 1, 1)),
[]*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 1, 1)),
},
{
"OnInv",
@ -560,7 +560,7 @@ func TestPeerListeners(t *testing.T) {
{
"OnMerkleBlock",
wire.NewMsgMerkleBlock(wire.NewBlockHeader(1,
[]*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 1, 1)),
[]*daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 1, 1)),
},
// only one version message is allowed
// only one verack message is allowed

View File

@ -38,7 +38,7 @@ func TestBlock(t *testing.T) {
}
// Hash for block 100,000.
wantHashStr := "c4bb55d2eaae7c7505b199097ef163ab940aa41228bbdb6f8b6d8a5b71bc432c"
wantHashStr := "df1b3cc747f665f36df84bdaa54a9ea695fbe12e102156fc1fc61d89366bc2d7"
wantHash, err := daghash.NewHashFromStr(wantHashStr)
if err != nil {
t.Errorf("NewHashFromStr: %v", err)
@ -147,10 +147,10 @@ func TestBlock(t *testing.T) {
// Transaction offsets and length for the transaction in Block100000.
wantTxLocs := []wire.TxLoc{
{TxStart: 186, TxLen: 163},
{TxStart: 349, TxLen: 287},
{TxStart: 636, TxLen: 285},
{TxStart: 921, TxLen: 253},
{TxStart: 218, TxLen: 163},
{TxStart: 381, TxLen: 287},
{TxStart: 668, TxLen: 285},
{TxStart: 953, TxLen: 253},
}
// Ensure the transaction location information is accurate.
@ -259,7 +259,7 @@ func TestBlockErrors(t *testing.T) {
}
// Truncate the block byte buffer to force errors.
shortBytes := block100000Bytes[:186]
shortBytes := block100000Bytes[:218]
_, err = util.NewBlockFromBytes(shortBytes)
if err != io.EOF {
t.Errorf("NewBlockFromBytes: did not get expected error - "+
@ -337,6 +337,12 @@ var Block100000 = wire.MsgBlock{
0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0,
0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22,
},
UTXOCommitment: &daghash.Hash{
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
},
Timestamp: time.Unix(1529483563, 0), // 2018-06-20 08:32:43 +0000 UTC
Bits: 0x1e00ffff, // 503382015
Nonce: 0x000ae53f, // 714047

View File

@ -543,6 +543,10 @@ func TestFilterInsertP2PubKeyOnly(t *testing.T) {
0x3C, 0xE3, 0x70, 0xD9, 0x5F, 0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, // AcceptedIDMerkleRoot
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63, 0x65, 0x9C, 0x79,
0x7B, 0x3C, 0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x76, 0x38, 0x1B, 0x4D, 0x00, 0x00, 0x00, 0x00, // Time
0x4C, 0x86, 0x04, 0x1B, // Bits
0x55, 0x4B, 0x85, 0x29, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce

View File

@ -31,6 +31,10 @@ func TestMerkleBlock3(t *testing.T) {
0x3C, 0xE3, 0x70, 0xD9, 0x5F, 0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, // AcceptedIDMerkleRoot
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63, 0x65, 0x9C, 0x79,
0x7B, 0x3C, 0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x67, 0x29, 0x1B, 0x4D, 0x00, 0x00, 0x00, 0x00, //Time
0x4C, 0x86, 0x04, 0x1B, // Bits
0x8F, 0xA4, 0x5D, 0x63, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -88,7 +92,11 @@ func TestMerkleBlock3(t *testing.T) {
0xd9, 0x5f, 0x09, 0x3b, 0xc7, 0xe3, 0x67, 0x11,
0x7f, 0x16, 0xc5, 0x96, 0x2e, 0x8b, 0xd9, 0x63,
0x65, 0x9c, 0x79, 0x7b, 0x3c, 0x30, 0xc1, 0xf8,
0xfd, 0xd0, 0xd9, 0x72, 0x87, 0x67, 0x29, 0x1b,
0xfd, 0xd0, 0xd9, 0x72, 0x87, 0x10, 0x3b, 0xc7,
0xe3, 0x67, 0x11, 0x7b, 0x3c, 0x30, 0xc1, 0xf8,
0xfd, 0xd0, 0xd9, 0x72, 0x87, 0x7f, 0x16, 0xc5,
0x96, 0x2e, 0x8b, 0xd9, 0x63, 0x65, 0x9c, 0x79,
0x3c, 0xe3, 0x70, 0xd9, 0x5f, 0x67, 0x29, 0x1b,
0x4d, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x86, 0x04,
0x1b, 0x8f, 0xa4, 0x5d, 0x63, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0xe2, 0x03,

View File

@ -426,7 +426,7 @@ func BenchmarkDecodeHeaders(b *testing.B) {
}
parentHashes[i] = hash
}
m.AddBlockHeader(NewBlockHeader(1, parentHashes, hash, hash, hash, 0, uint64(i)))
m.AddBlockHeader(NewBlockHeader(1, parentHashes, hash, hash, hash, hash, 0, uint64(i)))
}
// Serialize it so the bytes are available to test the decode below.
@ -572,7 +572,7 @@ func BenchmarkDecodeMerkleBlock(b *testing.B) {
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.Header = *NewBlockHeader(1, []*daghash.Hash{hash}, hash, hash, hash, 0, uint64(10000))
m.Header = *NewBlockHeader(1, []*daghash.Hash{hash}, hash, hash, hash, hash, 0, uint64(10000))
for i := 0; i < 105; i++ {
hash, err := daghash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {

View File

@ -16,10 +16,10 @@ import (
// not including the list of parent block headers.
// Version 4 bytes + Timestamp 8 bytes + Bits 4 bytes + Nonce 8 bytes +
// + NumParentBlocks 1 byte + HashMerkleRoot hash + IDMerkleRoot hash +
// + AcceptedIDMerkleRoot hash.
// + AcceptedIDMerkleRoot hash + UTXOCommitment hash.
// To get total size of block header len(ParentHashes) * daghash.HashSize should be
// added to this value
const BaseBlockHeaderPayload = 25 + 3*(daghash.HashSize)
const BaseBlockHeaderPayload = 25 + 4*(daghash.HashSize)
// MaxNumParentBlocks is the maximum number of parent blocks a block can reference.
// Currently set to 255 as the maximum number NumParentBlocks can be due to it being a byte
@ -48,6 +48,9 @@ type BlockHeader struct {
// accepted form the block.Blues
AcceptedIDMerkleRoot *daghash.Hash
// UTXOCommitment is an ECMH UTXO commitment to the block UTXO.
UTXOCommitment *daghash.Hash
// Time the block was created.
Timestamp time.Time
@ -127,6 +130,7 @@ func (h *BlockHeader) SerializeSize() int {
// block with defaults or calclulated values for the remaining fields.
func NewBlockHeader(version int32, parentHashes []*daghash.Hash, hashMerkleRoot *daghash.Hash,
idMerkleRoot *daghash.Hash, acceptedIDMerkleRoot *daghash.Hash,
utxoCommitment *daghash.Hash,
bits uint32, nonce uint64) *BlockHeader {
// Limit the timestamp to one second precision since the protocol
@ -137,6 +141,7 @@ func NewBlockHeader(version int32, parentHashes []*daghash.Hash, hashMerkleRoot
HashMerkleRoot: hashMerkleRoot,
IDMerkleRoot: idMerkleRoot,
AcceptedIDMerkleRoot: acceptedIDMerkleRoot,
UTXOCommitment: utxoCommitment,
Timestamp: time.Unix(time.Now().Unix(), 0),
Bits: bits,
Nonce: nonce,
@ -165,8 +170,9 @@ func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
bh.HashMerkleRoot = &daghash.Hash{}
bh.IDMerkleRoot = &daghash.Hash{}
bh.AcceptedIDMerkleRoot = &daghash.Hash{}
bh.UTXOCommitment = &daghash.Hash{}
return readElements(r, bh.HashMerkleRoot, bh.IDMerkleRoot,
bh.AcceptedIDMerkleRoot, (*int64Time)(&bh.Timestamp),
bh.AcceptedIDMerkleRoot, bh.UTXOCommitment, (*int64Time)(&bh.Timestamp),
&bh.Bits, &bh.Nonce)
}
@ -184,5 +190,5 @@ func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
}
}
return writeElements(w, bh.HashMerkleRoot, bh.IDMerkleRoot,
bh.AcceptedIDMerkleRoot, sec, bh.Bits, bh.Nonce)
bh.AcceptedIDMerkleRoot, bh.UTXOCommitment, sec, bh.Bits, bh.Nonce)
}

View File

@ -29,7 +29,7 @@ func TestBlockHeader(t *testing.T) {
acceptedIDMerkleRoot := exampleAcceptedIDMerkleRoot
bits := uint32(0x1d00ffff)
bh := NewBlockHeader(1, hashes, merkleHash, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, exampleUTXOCommitment, bits, nonce)
// Ensure we get the same data back out.
if !reflect.DeepEqual(bh.ParentHashes, hashes) {
@ -64,6 +64,7 @@ func TestBlockHeaderWire(t *testing.T) {
HashMerkleRoot: mainNetGenesisMerkleRoot,
IDMerkleRoot: exampleIDMerkleRoot,
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
UTXOCommitment: exampleUTXOCommitment,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
@ -93,6 +94,10 @@ func TestBlockHeaderWire(t *testing.T) {
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x29, 0xab, 0x5f, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -180,6 +185,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
HashMerkleRoot: mainNetGenesisMerkleRoot,
IDMerkleRoot: exampleIDMerkleRoot,
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
UTXOCommitment: exampleUTXOCommitment,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Bits: bits,
Nonce: nonce,
@ -209,6 +215,10 @@ func TestBlockHeaderSerialize(t *testing.T) {
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x29, 0xab, 0x5f, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -269,6 +279,7 @@ func TestBlockHeaderSerializeSize(t *testing.T) {
HashMerkleRoot: mainNetGenesisMerkleRoot,
IDMerkleRoot: mainNetGenesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
@ -280,6 +291,7 @@ func TestBlockHeaderSerializeSize(t *testing.T) {
HashMerkleRoot: mainNetGenesisMerkleRoot,
IDMerkleRoot: mainNetGenesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.ZeroHash,
UTXOCommitment: &daghash.ZeroHash,
Timestamp: timestamp,
Bits: bits,
Nonce: nonce,
@ -289,10 +301,10 @@ func TestBlockHeaderSerializeSize(t *testing.T) {
size int // Expected serialized size
}{
// Block with no transactions.
{genesisBlockHdr, 121},
{genesisBlockHdr, 153},
// First block in the mainnet block DAG.
{baseBlockHdr, 185},
{baseBlockHdr, 217},
}
t.Logf("Running %d tests", len(tests))

View File

@ -56,6 +56,13 @@ var exampleAcceptedIDMerkleRoot = &daghash.Hash{
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
}
var exampleUTXOCommitment = &daghash.Hash{
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
}
// TestElementWire tests wire encode and decode for various element types. This
// is mainly to test the "fast" paths in readElement and writeElement which use
// type assertions to avoid reflection when possible.

View File

@ -68,7 +68,7 @@ func TestMessage(t *testing.T) {
msgFilterAdd := NewMsgFilterAdd([]byte{0x01})
msgFilterClear := NewMsgFilterClear()
msgFilterLoad := NewMsgFilterLoad([]byte{0x01}, 10, 0, BloomUpdateNone)
bh := NewBlockHeader(1, []*daghash.Hash{mainNetGenesisHash, simNetGenesisHash}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)
bh := NewBlockHeader(1, []*daghash.Hash{mainNetGenesisHash, simNetGenesisHash}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, &daghash.Hash{}, 0, 0)
msgMerkleBlock := NewMsgMerkleBlock(bh)
msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
msgGetCFilters := NewMsgGetCFilters(GCSFilterExtended, 0, &daghash.Hash{})
@ -91,7 +91,7 @@ func TestMessage(t *testing.T) {
{msgGetAddr, msgGetAddr, pver, MainNet, 26},
{msgAddr, msgAddr, pver, MainNet, 27},
{msgGetBlocks, msgGetBlocks, pver, MainNet, 61},
{msgBlock, msgBlock, pver, MainNet, 372},
{msgBlock, msgBlock, pver, MainNet, 404},
{msgInv, msgInv, pver, MainNet, 25},
{msgGetData, msgGetData, pver, MainNet, 25},
{msgNotFound, msgNotFound, pver, MainNet, 25},
@ -107,7 +107,7 @@ func TestMessage(t *testing.T) {
{msgFilterAdd, msgFilterAdd, pver, MainNet, 26},
{msgFilterClear, msgFilterClear, pver, MainNet, 24},
{msgFilterLoad, msgFilterLoad, pver, MainNet, 35},
{msgMerkleBlock, msgMerkleBlock, pver, MainNet, 215},
{msgMerkleBlock, msgMerkleBlock, pver, MainNet, 247},
{msgReject, msgReject, pver, MainNet, 79},
{msgGetCFilters, msgGetCFilters, pver, MainNet, 65},
{msgGetCFHeaders, msgGetCFHeaders, pver, MainNet, 65},

View File

@ -27,10 +27,11 @@ func TestBlock(t *testing.T) {
hashMerkleRoot := blockOne.Header.HashMerkleRoot
idMerkleRoot := blockOne.Header.IDMerkleRoot
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
utxoCommitment := blockOne.Header.UTXOCommitment
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
// Ensure the command is expected value.
wantCmd := "block"
@ -76,7 +77,7 @@ func TestBlock(t *testing.T) {
// TestBlockHash tests the ability to generate the hash of a block accurately.
func TestBlockHash(t *testing.T) {
// Block 1 hash.
hashStr := "d632f80af4f507bed094a424902e37cc4b1e447e4ede731f0f4b446edebaf381"
hashStr := "8a3a27a37c9e3342b4b38ae9ef29e59fe37b59618adafbf36a106b6d62f80654"
wantHash, err := daghash.NewHashFromStr(hashStr)
if err != nil {
t.Errorf("NewHashFromStr: %v", err)
@ -224,16 +225,18 @@ func TestBlockWireErrors(t *testing.T) {
{&blockOne, blockOneBytes, pver, 101, io.ErrShortWrite, io.EOF},
// Force error in accepted ID merkle root.
{&blockOne, blockOneBytes, pver, 133, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
// Force error in utxo commitment.
{&blockOne, blockOneBytes, pver, 165, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
{&blockOne, blockOneBytes, pver, 197, io.ErrShortWrite, io.EOF},
// Force error in difficulty bits.
{&blockOne, blockOneBytes, pver, 173, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, pver, 205, io.ErrShortWrite, io.EOF},
// Force error in header nonce.
{&blockOne, blockOneBytes, pver, 177, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, pver, 209, io.ErrShortWrite, io.EOF},
// Force error in transaction count.
{&blockOne, blockOneBytes, pver, 185, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, pver, 217, io.ErrShortWrite, io.EOF},
// Force error in transactions.
{&blockOne, blockOneBytes, pver, 186, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, pver, 218, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
@ -350,16 +353,18 @@ func TestBlockSerializeErrors(t *testing.T) {
{&blockOne, blockOneBytes, 101, io.ErrShortWrite, io.EOF},
// Force error in accepted ID merkle root.
{&blockOne, blockOneBytes, 133, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
// Force error in utxo commitment.
{&blockOne, blockOneBytes, 165, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
{&blockOne, blockOneBytes, 197, io.ErrShortWrite, io.EOF},
// Force error in difficulty bits.
{&blockOne, blockOneBytes, 173, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, 205, io.ErrShortWrite, io.EOF},
// Force error in header nonce.
{&blockOne, blockOneBytes, 177, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, 209, io.ErrShortWrite, io.EOF},
// Force error in transaction count.
{&blockOne, blockOneBytes, 185, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, 217, io.ErrShortWrite, io.EOF},
// Force error in transactions.
{&blockOne, blockOneBytes, 186, io.ErrShortWrite, io.EOF},
{&blockOne, blockOneBytes, 218, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
@ -431,6 +436,10 @@ func TestBlockOverflowErrors(t *testing.T) {
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -483,7 +492,7 @@ func TestBlockSerializeSize(t *testing.T) {
size int // Expected serialized size
}{
// Block with no transactions.
{noTxBlock, 186},
{noTxBlock, 218},
// First block in the mainnet block chain.
{&blockOne, len(blockOneBytes)},
@ -509,6 +518,7 @@ var blockOne = MsgBlock{
HashMerkleRoot: mainNetGenesisMerkleRoot,
IDMerkleRoot: exampleIDMerkleRoot,
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
UTXOCommitment: exampleUTXOCommitment,
Timestamp: time.Unix(0x4966bc61, 0), // 2009-01-08 20:54:25 -0600 CST
Bits: 0x1d00ffff, // 486604799
Nonce: 0x9962e301, // 2573394689
@ -572,6 +582,10 @@ var blockOneBytes = []byte{
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -608,5 +622,5 @@ var blockOneBytes = []byte{
// Transaction location information for block one transactions.
var blockOneTxLocs = []TxLoc{
{TxStart: 186, TxLen: 162},
{TxStart: 218, TxLen: 162},
}

View File

@ -29,7 +29,7 @@ func TestHeaders(t *testing.T) {
// Ensure max payload is expected value for latest protocol version.
// Num headers (varInt) + max allowed headers (header length + 1 byte
// for the number of transactions which is always 0).
wantPayload := uint32(16564009)
wantPayload := uint32(16628009)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
@ -65,10 +65,11 @@ func TestHeadersWire(t *testing.T) {
hashMerkleRoot := blockOne.Header.HashMerkleRoot
idMerkleRoot := blockOne.Header.IDMerkleRoot
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
utxoCommitment := blockOne.Header.UTXOCommitment
bits := uint32(0x1d00ffff)
nonce := uint64(0x9962e301)
bh := NewBlockHeader(1, hashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
bh.Version = blockOne.Header.Version
bh.Timestamp = blockOne.Header.Timestamp
@ -105,6 +106,10 @@ func TestHeadersWire(t *testing.T) {
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -175,10 +180,11 @@ func TestHeadersWireErrors(t *testing.T) {
hashMerkleRoot := blockOne.Header.HashMerkleRoot
idMerkleRoot := blockOne.Header.IDMerkleRoot
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
utxoCommitment := blockOne.Header.UTXOCommitment
bits := uint32(0x1d00ffff)
nonce := uint64(0x9962e301)
bh := NewBlockHeader(1, hashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
bh.Version = blockOne.Header.Version
bh.Timestamp = blockOne.Header.Timestamp
@ -205,6 +211,14 @@ func TestHeadersWireErrors(t *testing.T) {
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -225,7 +239,7 @@ func TestHeadersWireErrors(t *testing.T) {
// Intentionally invalid block header that has a transaction count used
// to force errors.
bhTrans := NewBlockHeader(1, hashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
bhTrans.Version = blockOne.Header.Version
bhTrans.Timestamp = blockOne.Header.Timestamp
@ -255,6 +269,10 @@ func TestHeadersWireErrors(t *testing.T) {
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce
@ -277,7 +295,7 @@ func TestHeadersWireErrors(t *testing.T) {
// Force error with greater than max headers.
{maxHeaders, maxHeadersEncoded, pver, 3, wireErr, wireErr},
// Force error with number of transactions.
{transHeader, transHeaderEncoded, pver, 178, io.ErrShortWrite, io.EOF},
{transHeader, transHeaderEncoded, pver, 210, io.ErrShortWrite, io.EOF},
// Force error with included transactions.
{transHeader, transHeaderEncoded, pver, len(transHeaderEncoded), nil, wireErr},
}

View File

@ -25,10 +25,11 @@ func TestMerkleBlock(t *testing.T) {
hashMerkleRoot := blockOne.Header.HashMerkleRoot
idMerkleRoot := blockOne.Header.IDMerkleRoot
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
utxoCommitment := blockOne.Header.UTXOCommitment
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
// Ensure the command is expected value.
wantCmd := "merkleblock"
@ -120,10 +121,11 @@ func TestMerkleBlockCrossProtocol(t *testing.T) {
hashMerkleRoot := blockOne.Header.HashMerkleRoot
idMerkleRoot := blockOne.Header.IDMerkleRoot
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
utxoCommitment := blockOne.Header.UTXOCommitment
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, idMerkleRoot,
acceptedIDMerkleRoot, bits, nonce)
acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
msg := NewMsgMerkleBlock(bh)
@ -209,22 +211,24 @@ func TestMerkleBlockWireErrors(t *testing.T) {
{&merkleBlockOne, merkleBlockOneBytes, pver, 101, io.ErrShortWrite, io.EOF},
// Force error in accepted ID merkle root.
{&merkleBlockOne, merkleBlockOneBytes, pver, 133, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
// Force error in utxo commitment.
{&merkleBlockOne, merkleBlockOneBytes, pver, 165, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
{&merkleBlockOne, merkleBlockOneBytes, pver, 197, io.ErrShortWrite, io.EOF},
// Force error in difficulty bits.
{&merkleBlockOne, merkleBlockOneBytes, pver, 173, io.ErrShortWrite, io.EOF},
{&merkleBlockOne, merkleBlockOneBytes, pver, 205, io.ErrShortWrite, io.EOF},
// Force error in header nonce.
{&merkleBlockOne, merkleBlockOneBytes, pver, 177, io.ErrShortWrite, io.EOF},
{&merkleBlockOne, merkleBlockOneBytes, pver, 209, io.ErrShortWrite, io.EOF},
// Force error in transaction count.
{&merkleBlockOne, merkleBlockOneBytes, pver, 185, io.ErrShortWrite, io.EOF},
{&merkleBlockOne, merkleBlockOneBytes, pver, 217, io.ErrShortWrite, io.EOF},
// Force error in num hashes.
{&merkleBlockOne, merkleBlockOneBytes, pver, 189, io.ErrShortWrite, io.EOF},
{&merkleBlockOne, merkleBlockOneBytes, pver, 221, io.ErrShortWrite, io.EOF},
// Force error in hashes.
{&merkleBlockOne, merkleBlockOneBytes, pver, 190, io.ErrShortWrite, io.EOF},
// Force error in num flag bytes.
{&merkleBlockOne, merkleBlockOneBytes, pver, 222, io.ErrShortWrite, io.EOF},
// Force error in num flag bytes.
{&merkleBlockOne, merkleBlockOneBytes, pver, 254, io.ErrShortWrite, io.EOF},
// Force error in flag bytes.
{&merkleBlockOne, merkleBlockOneBytes, pver, 223, io.ErrShortWrite, io.EOF},
{&merkleBlockOne, merkleBlockOneBytes, pver, 255, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
@ -281,16 +285,17 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
// allowed tx hashes.
var buf bytes.Buffer
WriteVarInt(&buf, maxTxPerBlock+1)
numHashesOffset := 189
numHashesOffset := 221
exceedMaxHashes := make([]byte, numHashesOffset)
copy(exceedMaxHashes, merkleBlockOneBytes[:numHashesOffset])
spew.Dump(exceedMaxHashes)
exceedMaxHashes = append(exceedMaxHashes, buf.Bytes()...)
// Create bytes for a merkle block that claims to have more than the max
// allowed flag bytes.
buf.Reset()
WriteVarInt(&buf, maxFlagsPerMerkleBlock+1)
numFlagBytesOffset := 222
numFlagBytesOffset := 254
exceedMaxFlagBytes := make([]byte, numFlagBytesOffset)
copy(exceedMaxFlagBytes, merkleBlockOneBytes[:numFlagBytesOffset])
exceedMaxFlagBytes = append(exceedMaxFlagBytes, buf.Bytes()...)
@ -334,6 +339,7 @@ var merkleBlockOne = MsgMerkleBlock{
},
IDMerkleRoot: exampleIDMerkleRoot,
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
UTXOCommitment: exampleUTXOCommitment,
Timestamp: time.Unix(0x4966bc61, 0), // 2009-01-08 20:54:25 -0600 CST
Bits: 0x1d00ffff, // 486604799
Nonce: 0x9962e301, // 2573394689
@ -363,7 +369,7 @@ var merkleBlockOneBytes = []byte{
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, // MerkleRoot
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, // HashMerkleRoot
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e,
@ -375,6 +381,10 @@ var merkleBlockOneBytes = []byte{
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
0xff, 0xff, 0x00, 0x1d, // Bits
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce. TODO: (Ori) Replace to a real nonce