use encoder to calculate Merkle roots

parent 64a990ac
......@@ -113,13 +113,14 @@ func (b Block) ID() BlockID {
func (b Block) MerkleRoot() crypto.Hash {
tree := crypto.NewTree()
var buf bytes.Buffer
e := encoder(&buf)
for _, payout := range b.MinerPayouts {
payout.MarshalSia(&buf)
payout.MarshalSia(e)
tree.Push(buf.Bytes())
buf.Reset()
}
for _, txn := range b.Transactions {
txn.MarshalSia(&buf)
txn.MarshalSia(e)
tree.Push(buf.Bytes())
buf.Reset()
}
......
......@@ -96,9 +96,8 @@ func encoder(w io.Writer) *encHelper {
return e
}
return &encHelper{
w: w,
// 32 bytes is large enough for everything except StorageProof.Segment
buf: make([]byte, 32),
w: w,
buf: make([]byte, 64), // large enough for everything but ArbitraryData
}
}
......
......@@ -7,6 +7,7 @@ package types
// called 'UnlockConditions'.
import (
"bytes"
"errors"
"github.com/NebulousLabs/Sia/crypto"
......@@ -140,12 +141,19 @@ func Ed25519PublicKey(pk crypto.PublicKey) SiaPublicKey {
// Timelock and SignaturesRequired are both low entropy fields; they can be
// protected by having random public keys next to them.
func (uc UnlockConditions) UnlockHash() UnlockHash {
var buf bytes.Buffer
e := encoder(&buf)
tree := crypto.NewTree()
tree.PushObject(uc.Timelock)
for i := range uc.PublicKeys {
tree.PushObject(uc.PublicKeys[i])
e.WriteUint64(uint64(uc.Timelock))
tree.Push(buf.Bytes())
buf.Reset()
for _, key := range uc.PublicKeys {
key.MarshalSia(e)
tree.Push(buf.Bytes())
buf.Reset()
}
tree.PushObject(uc.SignaturesRequired)
e.WriteUint64(uc.SignaturesRequired)
tree.Push(buf.Bytes())
return UnlockHash(tree.Root())
}
......
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