Skip to content

Commit

Permalink
Merge pull request #189 from kcalvinalvin/2024-06-19-no-unconfirmed-m…
Browse files Browse the repository at this point in the history
…arker

wire, main, indexers: remove unconfirmed marker
  • Loading branch information
kcalvinalvin committed Jun 27, 2024
2 parents 593dd81 + 24f29d8 commit 496c40a
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 269 deletions.
16 changes: 8 additions & 8 deletions blockchain/indexers/flatutreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (idx *FlatUtreexoProofIndex) Init(chain *blockchain.BlockChain) error {
}
r := bytes.NewReader(proofBytes)
ud := new(wire.UData)
err = ud.DeserializeCompact(r, udataSerializeBool, 0)
err = ud.DeserializeCompact(r)
if err != nil {
return err
}
Expand Down Expand Up @@ -827,7 +827,7 @@ func (idx *FlatUtreexoProofIndex) FetchUtreexoProof(height int32, excludeAccProo
return nil, err
}
} else {
err = ud.DeserializeCompact(r, udataSerializeBool, 0)
err = ud.DeserializeCompact(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -923,7 +923,7 @@ func (idx *FlatUtreexoProofIndex) FetchMultiUtreexoProof(height int32) (
// Deserialize the utreexo data that will provide the proof for the upcoming
// blocks in the interval.
multiUd := new(wire.UData)
err = multiUd.DeserializeCompact(r, udataSerializeBool, 0)
err = multiUd.DeserializeCompact(r)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -967,7 +967,7 @@ func (idx *FlatUtreexoProofIndex) FetchRemembers(height int32) ([]uint32, error)
// storeProof serializes and stores the utreexo data in the proof state.
func (idx *FlatUtreexoProofIndex) storeProof(height int32, excludeAccProof bool, ud *wire.UData) error {
if excludeAccProof {
bytesBuf := bytes.NewBuffer(make([]byte, 0, ud.SerializeUxtoDataSizeCompact(udataSerializeBool)))
bytesBuf := bytes.NewBuffer(make([]byte, 0, ud.SerializeUxtoDataSizeCompact()))
err := ud.SerializeCompactNoAccProof(bytesBuf)
if err != nil {
return err
Expand All @@ -978,8 +978,8 @@ func (idx *FlatUtreexoProofIndex) storeProof(height int32, excludeAccProof bool,
return fmt.Errorf("store proof err. %v", err)
}
} else {
bytesBuf := bytes.NewBuffer(make([]byte, 0, ud.SerializeSizeCompact(udataSerializeBool)))
err := ud.SerializeCompact(bytesBuf, udataSerializeBool)
bytesBuf := bytes.NewBuffer(make([]byte, 0, ud.SerializeSizeCompact()))
err := ud.SerializeCompact(bytesBuf)
if err != nil {
return err
}
Expand All @@ -1000,7 +1000,7 @@ func (idx *FlatUtreexoProofIndex) storeMultiBlockProof(height int32, ud, multiUd
dels []utreexo.Hash) error {

size := ud.SerializeSizeCompactNoAccProof()
size += multiUd.SerializeSizeCompact(udataSerializeBool)
size += multiUd.SerializeSizeCompact()
size += (len(dels) * chainhash.HashSize) + 4 // 4 for uint32 size

bytesBuf := bytes.NewBuffer(make([]byte, 0, size))
Expand All @@ -1010,7 +1010,7 @@ func (idx *FlatUtreexoProofIndex) storeMultiBlockProof(height int32, ud, multiUd
}

multiUd.LeafDatas = []wire.LeafData{}
err = multiUd.SerializeCompact(bytesBuf, udataSerializeBool)
err = multiUd.SerializeCompact(bytesBuf)
if err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions blockchain/indexers/utreexobackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const (
nodesDBDirName = "nodes"
cachedLeavesDBDirName = "cachedleaves"
defaultUtreexoFileName = "forest.dat"

// udataSerializeBool defines the argument that should be passed to the
// serialize and deserialize functions for udata.
udataSerializeBool = false
)

// UtreexoConfig is a descriptor which specifies the Utreexo state instance configuration.
Expand Down
8 changes: 4 additions & 4 deletions blockchain/indexers/utreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (idx *UtreexoProofIndex) Init(chain *blockchain.BlockChain) error {
}
r := bytes.NewReader(proofBytes)

err = ud.DeserializeCompact(r, udataSerializeBool, 0)
err = ud.DeserializeCompact(r)
if err != nil {
return err
}
Expand Down Expand Up @@ -399,7 +399,7 @@ func (idx *UtreexoProofIndex) FetchUtreexoProof(hash *chainhash.Hash) (*wire.UDa
}
r := bytes.NewReader(proofBytes)

err = ud.DeserializeCompact(r, udataSerializeBool, 0)
err = ud.DeserializeCompact(r)
if err != nil {
return err
}
Expand Down Expand Up @@ -606,10 +606,10 @@ func DropUtreexoProofIndex(db database.DB, dataDir string, interrupt <-chan stru
// TODO Use the compact serialization.
func dbStoreUtreexoProof(dbTx database.Tx, hash *chainhash.Hash, ud *wire.UData) error {
// Pre-allocated the needed buffer.
udSize := ud.SerializeSizeCompact(udataSerializeBool)
udSize := ud.SerializeSizeCompact()
buf := bytes.NewBuffer(make([]byte, 0, udSize))

err := ud.SerializeCompact(buf, udataSerializeBool)
err := ud.SerializeCompact(buf)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/indexers/utreexoproofstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ps *proofStats) UpdateUDStats(excludeAccProof bool, ud *wire.UData) {
ps.TgCount += uint64(len(ud.AccProof.Targets))

// Update leaf data size.
ps.LdSize += uint64(ud.SerializeUxtoDataSizeCompact(false))
ps.LdSize += uint64(ud.SerializeUxtoDataSizeCompact())
ps.LdCount += uint64(len(ud.LeafDatas))

// Update proof size if the proof is to be included.
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2722,7 +2722,7 @@ func (s *server) GetProofSizeforTx(msgTx *wire.MsgTx) (int, int, error) {
return 0, 0, err
}

return ud.SerializeAccSizeCompact(), ud.SerializeUxtoDataSizeCompact(true), nil
return ud.SerializeAccSizeCompact(), ud.SerializeUxtoDataSizeCompact(), nil
}

// UpdateProofBytesRead updates the bytes for utreexo proofs that would have
Expand All @@ -2739,7 +2739,7 @@ func (s *server) UpdateProofBytesRead(msgTx *wire.MsgTx) error {

} else if s.chain.IsUtreexoViewActive() {
if msgTx.UData != nil {
s.addProofBytesReceived(uint64(msgTx.UData.SerializeSizeCompact(true)))
s.addProofBytesReceived(uint64(msgTx.UData.SerializeSizeCompact()))
s.addAccBytesReceived(uint64(msgTx.UData.SerializeAccSizeCompact()))
}
}
Expand All @@ -2761,7 +2761,7 @@ func (s *server) UpdateProofBytesWritten(msgTx *wire.MsgTx) error {

} else if s.chain.IsUtreexoViewActive() {
if msgTx.UData != nil {
s.addProofBytesSent(uint64(msgTx.UData.SerializeSizeCompact(true)))
s.addProofBytesSent(uint64(msgTx.UData.SerializeSizeCompact()))
s.addAccBytesSent(uint64(msgTx.UData.SerializeAccSizeCompact()))
}
}
Expand Down
77 changes: 12 additions & 65 deletions wire/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package wire

import (
"bytes"
"crypto/sha512"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -325,21 +324,6 @@ func (l *LeafData) Deserialize(r io.Reader) error {
// pkscript length VLQ variable
// pkscript []byte variable
//
// The serialized format for a transaction is:
// [<unconfirmed marker><header code><amount><pkscript len><pkscript>]
//
// All other fields with the exception of 'unconfirmed marker' is the same as
// the serialization for a block. The unconfirmed marker is represented in
// the struct as height = -1.
//
// Field Type Size
// unconfirmed marker byte 1
// header code int32 4
// amount int64 8
// pkType byte 1
// pkscript length VLQ variable
// pkscript []byte variable
//
// -----------------------------------------------------------------------------

// PkType is a list of different pkScript types that can be reconstructed.
Expand Down Expand Up @@ -443,43 +427,24 @@ func PkScriptDeserializeCompact(r io.Reader) (PkType, []byte, error) {

// SerializeSizeCompact returns the number of bytes it would take to serialize the
// LeafData in the compact serialization format.
func (l *LeafData) SerializeSizeCompact(isForTx bool) int {
if isForTx {
if l.IsUnconfirmed() {
// If the leaf data corresponds to an unconfirmed tx, we only
// send a byte.
return 1
} else {
// header code 4 bytes + amount 8 bytes + unconfirmed marker + pkscript.
return 13 + PkScriptSerializeSizeCompact(
l.ReconstructablePkType, l.PkScript)
}
func (l *LeafData) SerializeSizeCompact() int {
// If the leaf data corresponds to an unconfirmed tx, we don't
// serialize it.
if l.IsUnconfirmed() {
return 0
}

// header code 4 bytes + amount 8 bytes + pkscript.
return 12 + PkScriptSerializeSizeCompact(
l.ReconstructablePkType, l.PkScript)
}

// SerializeCompact encodes the LeafData to w using the compact leaf data serialization format.
func (l *LeafData) SerializeCompact(w io.Writer, isForTx bool) error {
if isForTx {
// If the tx is unconfirmed, write the unconfirmed marker and
// return immediately.
if l.IsUnconfirmed() {
_, err := w.Write([]byte{0x1})
if err != nil {
return err
}

// Return if unconfirmed.
return nil
}

// Write 0 to mark that this transaction is confirmed.
_, err := w.Write([]byte{0x0})
if err != nil {
return err
}
func (l *LeafData) SerializeCompact(w io.Writer) error {
// If the tx is unconfirmed, write the unconfirmed marker and
// return immediately.
if l.IsUnconfirmed() {
return nil
}

bs := newSerializer()
Expand Down Expand Up @@ -508,25 +473,7 @@ func (l *LeafData) SerializeCompact(w io.Writer, isForTx bool) error {
}

// DeserializeCompact encodes the LeafData to w using the compact leaf serialization format.
func (l *LeafData) DeserializeCompact(r io.Reader, isForTx bool) error {
if isForTx {
// Read unconfirmed marker.
unconfirmed := make([]byte, 1)
_, err := io.ReadFull(r, unconfirmed)
if err != nil {
return err
}

// 1 means that the LeafData corresponds to an uncomfirmed tx.
// Set IsUnconfirmed as true and return.
if bytes.Equal(unconfirmed, []byte{0x1}) {
l.SetUnconfirmed()

// Return immediately here if the tx is unconfirmed.
return nil
}
}

func (l *LeafData) DeserializeCompact(r io.Reader) error {
bs := newSerializer()
defer bs.free()

Expand Down
Loading

0 comments on commit 496c40a

Please sign in to comment.