Commit 8a7a0ffd authored by David Vorick's avatar David Vorick

fix #302, make block merkle root implicit

parent 004e1f4b
......@@ -114,13 +114,6 @@ func (s *State) validHeader(b Block) (err error) {
return
}
// Check that the transaction merkle root matches the transactions
// included into the block.
if b.MerkleRoot != b.TransactionMerkleRoot() {
err = errors.New("merkle root does not match transactions sent.")
return
}
return
}
......
......@@ -25,7 +25,6 @@ type Block struct {
Timestamp Timestamp
Nonce uint64
MinerAddress CoinAddress
MerkleRoot hash.Hash
Transactions []Transaction
}
......@@ -120,10 +119,8 @@ func CalculateCoinbase(height BlockHeight) Currency {
func (b Block) ID() BlockID {
return BlockID(hash.HashBytes(encoding.MarshalAll(
b.ParentBlockID,
b.Timestamp,
b.Nonce,
b.MinerAddress,
b.MerkleRoot,
b.MerkleRoot(),
)))
}
......@@ -135,12 +132,14 @@ func (b Block) CheckTarget(target Target) bool {
// ExpectedTransactionMerkleRoot() returns the expected transaction
// merkle root of the block.
func (b Block) TransactionMerkleRoot() hash.Hash {
var transactionHashes []hash.Hash
for _, transaction := range b.Transactions {
transactionHashes = append(transactionHashes, hash.HashBytes(encoding.Marshal(transaction)))
func (b Block) MerkleRoot() hash.Hash {
var hashes []hash.Hash
hashes = append(hashes, hash.HashObject(b.Timestamp))
hashes = append(hashes, hash.HashObject(b.MinerAddress))
for _, txn := range b.Transactions {
hashes = append(hashes, hash.HashObject(txn))
}
return hash.MerkleRoot(transactionHashes)
return hash.MerkleRoot(hashes)
}
// SubisdyID() returns the id of the output created by the block subsidy.
......
......@@ -42,6 +42,9 @@ func randomHash(t *testing.T) (h hash.Hash) {
// TestTypeMarshalling tries to marshal and unmarshal all types, verifying that
// the marshalling is consistent with the unmarshalling. Right now block is the
// only type implemented, more may be added later.
//
// TODO: there are no transactions that we test the marshalling of, need to add
// transactions.
func TestTypeMarshalling(t *testing.T) {
// Create a block full of random values.
originalBlock := Block{
......@@ -49,8 +52,6 @@ func TestTypeMarshalling(t *testing.T) {
Timestamp: Timestamp(randomInt64(t)),
Nonce: randomUint64(t),
MinerAddress: CoinAddress(randomHash(t)),
MerkleRoot: randomHash(t),
// Transactions to be added from... input?
}
marshalledBlock := encoding.Marshal(originalBlock)
......@@ -72,7 +73,4 @@ func TestTypeMarshalling(t *testing.T) {
if a.MinerAddress != b.MinerAddress {
t.Error("MinerAddress marshalling problems.")
}
if a.MerkleRoot != b.MerkleRoot {
t.Error("MerkleRoot marshalling problems.")
}
}
......@@ -30,7 +30,6 @@ func (m *Miner) blockForWork() (b consensus.Block) {
// of randomly generated arbitrary data. This will provide entropy to
// the block even though the timestamp isn't changing at all.
}
b.MerkleRoot = b.TransactionMerkleRoot()
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment