mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-05 20:32:31 +00:00
Merge remote-tracking branch 'origin/v0.1.1-dev' into v0.1.2-dev
This commit is contained in:
commit
5daab45947
@ -1,6 +1,7 @@
|
|||||||
package blockdag
|
package blockdag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"github.com/kaspanet/kaspad/database"
|
"github.com/kaspanet/kaspad/database"
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/util/daghash"
|
||||||
"github.com/kaspanet/kaspad/util/locks"
|
"github.com/kaspanet/kaspad/util/locks"
|
||||||
@ -161,9 +162,13 @@ func (diffStore *utxoDiffStore) flushToDB(dbTx database.Tx) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allocate a buffer here to avoid needless allocations/grows
|
||||||
|
// while writing each entry.
|
||||||
|
buffer := &bytes.Buffer{}
|
||||||
for hash := range diffStore.dirty {
|
for hash := range diffStore.dirty {
|
||||||
|
buffer.Reset()
|
||||||
diffData := diffStore.loaded[hash]
|
diffData := diffStore.loaded[hash]
|
||||||
err := dbStoreDiffData(dbTx, &hash, diffData)
|
err := dbStoreDiffData(dbTx, buffer, &hash, diffData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -177,12 +182,23 @@ func (diffStore *utxoDiffStore) clearDirtyEntries() {
|
|||||||
|
|
||||||
// dbStoreDiffData stores the UTXO diff data to the database.
|
// dbStoreDiffData stores the UTXO diff data to the database.
|
||||||
// This overwrites the current entry if there exists one.
|
// This overwrites the current entry if there exists one.
|
||||||
func dbStoreDiffData(dbTx database.Tx, hash *daghash.Hash, diffData *blockUTXODiffData) error {
|
func dbStoreDiffData(dbTx database.Tx, writeBuffer *bytes.Buffer, hash *daghash.Hash, diffData *blockUTXODiffData) error {
|
||||||
serializedDiffData, err := serializeBlockUTXODiffData(diffData)
|
// To avoid a ton of allocs, use the given writeBuffer
|
||||||
|
// instead of allocating one. We expect the buffer to
|
||||||
|
// already be initalized and, in most cases, to already
|
||||||
|
// be large enough to accommodate the serialized data
|
||||||
|
// without growing.
|
||||||
|
err := serializeBlockUTXODiffData(writeBuffer, diffData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bucket.Put doesn't copy on its own, so we manually
|
||||||
|
// copy here. We do so because we expect the buffer
|
||||||
|
// to be reused once we're done with it.
|
||||||
|
serializedDiffData := make([]byte, writeBuffer.Len())
|
||||||
|
copy(serializedDiffData, writeBuffer.Bytes())
|
||||||
|
|
||||||
return dbTx.Metadata().Bucket(utxoDiffsBucketName).Put(hash[:], serializedDiffData)
|
return dbTx.Metadata().Bucket(utxoDiffsBucketName).Put(hash[:], serializedDiffData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,26 +19,25 @@ import (
|
|||||||
// hasDiffChild | Boolean | Indicates if a diff child exist
|
// hasDiffChild | Boolean | Indicates if a diff child exist
|
||||||
// diffChild | Hash | The diffChild's hash. Empty if hasDiffChild is true.
|
// diffChild | Hash | The diffChild's hash. Empty if hasDiffChild is true.
|
||||||
// diff | UTXODiff | The diff data's diff
|
// diff | UTXODiff | The diff data's diff
|
||||||
func serializeBlockUTXODiffData(diffData *blockUTXODiffData) ([]byte, error) {
|
func serializeBlockUTXODiffData(w io.Writer, diffData *blockUTXODiffData) error {
|
||||||
w := &bytes.Buffer{}
|
|
||||||
hasDiffChild := diffData.diffChild != nil
|
hasDiffChild := diffData.diffChild != nil
|
||||||
err := wire.WriteElement(w, hasDiffChild)
|
err := wire.WriteElement(w, hasDiffChild)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
if hasDiffChild {
|
if hasDiffChild {
|
||||||
err := wire.WriteElement(w, diffData.diffChild.hash)
|
err := wire.WriteElement(w, diffData.diffChild.hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = serializeUTXODiff(w, diffData.diff)
|
err = serializeUTXODiff(w, diffData.diff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Bytes(), nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// utxoEntryHeaderCode returns the calculated header code to be used when
|
// utxoEntryHeaderCode returns the calculated header code to be used when
|
||||||
|
Loading…
x
Reference in New Issue
Block a user