Skip to content

Commit

Permalink
Merge pull request #215 from kcalvinalvin/2024-11-13-remove-serialize…
Browse files Browse the repository at this point in the history
…-no-acc

wire, indexers, main: remove SerializeNoAcc for udata
  • Loading branch information
kcalvinalvin authored Nov 13, 2024
2 parents 81c4487 + 40d89a1 commit 779bd90
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 227 deletions.
17 changes: 5 additions & 12 deletions blockchain/indexers/flatutreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (idx *FlatUtreexoProofIndex) getUndoData(block *btcutil.Block) (uint64, []u
)

if !idx.config.Pruned {
ud, err := idx.FetchUtreexoProof(block.Height(), false)
ud, err := idx.FetchUtreexoProof(block.Height())
if err != nil {
return 0, nil, nil, err
}
Expand Down Expand Up @@ -605,7 +605,7 @@ func (idx *FlatUtreexoProofIndex) PruneBlock(_ database.Tx, _ *chainhash.Hash, l
}

// FetchUtreexoProof returns the Utreexo proof data for the given block height.
func (idx *FlatUtreexoProofIndex) FetchUtreexoProof(height int32, excludeAccProof bool) (
func (idx *FlatUtreexoProofIndex) FetchUtreexoProof(height int32) (
*wire.UData, error) {

if height == 0 {
Expand All @@ -626,16 +626,9 @@ func (idx *FlatUtreexoProofIndex) FetchUtreexoProof(height int32, excludeAccProo
r := bytes.NewReader(proofBytes)

ud := new(wire.UData)
if excludeAccProof {
err = ud.DeserializeCompactNoAccProof(r)
if err != nil {
return nil, err
}
} else {
err = ud.DeserializeCompact(r)
if err != nil {
return nil, err
}
err = ud.DeserializeCompact(r)
if err != nil {
return nil, err
}

return ud, nil
Expand Down
10 changes: 5 additions & 5 deletions blockchain/indexers/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func compareUtreexoIdx(start, end int32, pruned bool, chain *blockchain.BlockCha
case *FlatUtreexoProofIndex:
var err error
if !idxType.config.Pruned {
flatUD, err = idxType.FetchUtreexoProof(b, false)
flatUD, err = idxType.FetchUtreexoProof(b)
if err != nil {
return err
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func syncCsnChain(start, end int32, chainToSyncFrom, csnChain *blockchain.BlockC
for _, indexer := range indexes {
switch idxType := indexer.(type) {
case *FlatUtreexoProofIndex:
flatUD, err = idxType.FetchUtreexoProof(b, false)
flatUD, err = idxType.FetchUtreexoProof(b)
if err != nil {
return err
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func testUtreexoProof(block *btcutil.Block, chain *blockchain.BlockChain, indexe
switch idxType := indexer.(type) {
case *FlatUtreexoProofIndex:
var err error
flatUD, err = idxType.FetchUtreexoProof(block.Height(), false)
flatUD, err = idxType.FetchUtreexoProof(block.Height())
if err != nil {
return err
}
Expand Down Expand Up @@ -833,7 +833,7 @@ func TestBridgeNodePruneUndoDataGen(t *testing.T) {
switch idxType := indexer.(type) {
case *FlatUtreexoProofIndex:
for height := int32(1); height <= maxHeight; height++ {
_, err := idxType.FetchUtreexoProof(height, false)
_, err := idxType.FetchUtreexoProof(height)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -888,7 +888,7 @@ func TestBridgeNodePruneUndoDataGen(t *testing.T) {
for _, indexer := range indexes {
switch idxType := indexer.(type) {
case *FlatUtreexoProofIndex:
_, err := idxType.FetchUtreexoProof(maxHeight, false)
_, err := idxType.FetchUtreexoProof(maxHeight)
if err == nil {
t.Fatalf("expected an error when trying to" +
"fetch proofs from pruned bridges")
Expand Down
2 changes: 1 addition & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3402,7 +3402,7 @@ func handleGetUtreexoProof(s *rpcServer, cmd interface{}, closeChan <-chan struc
}
}
} else {
udata, err = s.cfg.FlatUtreexoProofIndex.FetchUtreexoProof(height, false)
udata, err = s.cfg.FlatUtreexoProofIndex.FetchUtreexoProof(height)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCMisc,
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ func (s *server) pushBlockMsg(sp *serverPeer, hash *chainhash.Hash, doneChan cha
}
return err
}
ud, err = s.flatUtreexoProofIndex.FetchUtreexoProof(height, false)
ud, err = s.flatUtreexoProofIndex.FetchUtreexoProof(height)
if err != nil {
peerLog.Debugf("Unable to fetch requested utreexo data for block hash %v: %v",
hash, err)
Expand Down
84 changes: 0 additions & 84 deletions wire/udata.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,90 +265,6 @@ func (ud *UData) DeserializeCompact(r io.Reader) error {
return nil
}

// SerializeSizeCompactNoAccProof returns the number of bytes it would take to
// serialize the utreexo data without the accumulator proof.
func (ud *UData) SerializeSizeCompactNoAccProof() int {
size := VarIntSerializeSize(uint64(len(ud.AccProof.Targets)))
for _, target := range ud.AccProof.Targets {
size += VarIntSerializeSize(target)
}

return size + ud.SerializeUxtoDataSizeCompact()
}

// SerializeCompactNoAccProof will serialize the utreexo data with the compact
// utreexo data format but will leave out the accumulator proof.
func (ud *UData) SerializeCompactNoAccProof(w io.Writer) error {
// Serialize the targets.
bp := ud.AccProof
err := WriteVarInt(w, 0, uint64(len(bp.Targets)))
if err != nil {
return err
}

for _, t := range bp.Targets {
err = WriteVarInt(w, 0, t)
if err != nil {
return err
}
}

// Write all the leafDatas.
err = WriteVarInt(w, 0, uint64(len(ud.LeafDatas)))
if err != nil {
return err
}
for _, ld := range ud.LeafDatas {
err := ld.SerializeCompact(w)
if err != nil {
return err
}
}

return nil
}

// DeserializeCompactNoAccProof will deserialize the utreexo data that has been
// serialized without the accumulator proof.
//
// NOTE: this function should only be called on udata that has been compactly serialized
// without the accumulator proof.
func (ud *UData) DeserializeCompactNoAccProof(r io.Reader) error {
// Deserialize the targets.
targetCount, err := ReadVarInt(r, 0)
if err != nil {
return err
}
targets := make([]uint64, targetCount)
for i := range targets {
target, err := ReadVarInt(r, 0)
if err != nil {
return err
}

targets[i] = target
}
ud.AccProof = utreexo.Proof{Targets: targets}

// Grab the count for the udatas
udCount, err := ReadVarInt(r, 0)
if err != nil {
return err
}
ud.LeafDatas = make([]LeafData, udCount)
for i := range ud.LeafDatas {
err := ud.LeafDatas[i].DeserializeCompact(r)
if err != nil {
str := fmt.Sprintf("targetCount:%d, LeafDatas[%d], err:%s\n",
len(ud.AccProof.Targets), i, err.Error())
returnErr := messageError("Deserialize leaf datas", str)
return returnErr
}
}

return nil
}

// HashesFromLeafDatas hashes the passed in leaf datas. Returns an error if a
// leaf data is compact as you can't generate the correct hash.
func HashesFromLeafDatas(leafDatas []LeafData) ([]utreexo.Hash, error) {
Expand Down
138 changes: 14 additions & 124 deletions wire/udata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ type testData struct {
height int32
leavesPerBlock []LeafData

size int
sizeCompact int
sizeCompactNoAcc int
size int
sizeCompact int
}

func getTestDatas() []testData {
Expand Down Expand Up @@ -58,9 +57,8 @@ var mainNetBlock104773 = testData{
IsCoinBase: false,
},
},
size: 217,
sizeCompact: 83,
sizeCompactNoAcc: 82,
size: 217,
sizeCompact: 83,
}

var testNetBlock383 = testData{
Expand Down Expand Up @@ -112,9 +110,8 @@ var testNetBlock383 = testData{
IsCoinBase: true,
},
},
size: 461,
sizeCompact: 193,
sizeCompactNoAcc: 192,
size: 461,
sizeCompact: 193,
}

func checkUDEqual(ud, checkUData *UData, isCompact bool, name string) error {
Expand Down Expand Up @@ -199,11 +196,10 @@ func TestUDataSerializeSize(t *testing.T) {
t.Parallel()

type test struct {
name string
ud UData
size int
sizeCompact int
sizeCompactNoAcc int
name string
ud UData
size int
sizeCompact int
}

testDatas := getTestDatas()
Expand Down Expand Up @@ -236,11 +232,10 @@ func TestUDataSerializeSize(t *testing.T) {

// Append to the tests.
tests = append(tests, test{
name: testData.name,
ud: *ud,
size: testData.size,
sizeCompact: testData.sizeCompact,
sizeCompactNoAcc: testData.sizeCompactNoAcc,
name: testData.name,
ud: *ud,
size: testData.size,
sizeCompact: testData.sizeCompact,
})
}

Expand Down Expand Up @@ -301,27 +296,6 @@ func TestUDataSerializeSize(t *testing.T) {
t.Fatal(err)
}

gotSize = test.ud.SerializeSizeCompactNoAccProof()
if gotSize != test.sizeCompactNoAcc {
t.Errorf("%s: UData serialize size compact no accumulator proof fail. "+
"expect %d, got %d", test.name,
test.sizeCompactNoAcc, gotSize)
continue
}

// Sanity check. Actually serialize the data and compare against our hardcoded number.
buf.Reset()
err = test.ud.SerializeCompactNoAccProof(&buf)
if err != nil {
t.Fatal(err)
}
if len(buf.Bytes()) != test.sizeCompactNoAcc {
t.Errorf("%s: UData serialize size compact no accumulator proof fail. "+
"serialized %d, hardcoded %d", test.name,
len(buf.Bytes()), test.sizeCompactNoAcc)
continue
}

// Test that SerializeUxtoDataSizeCompact and SerializeUxtoDataSizeCompact
// sums up to the entire thing.
totals := test.ud.SerializeUxtoDataSizeCompact() + test.ud.SerializeAccSizeCompact()
Expand Down Expand Up @@ -478,90 +452,6 @@ func TestUDataSerializeCompact(t *testing.T) {
}
}

func TestSerializeNoAccProof(t *testing.T) {
t.Parallel()

type test struct {
name string
isForTx bool
leafCount int
ud UData
before []byte
after []byte
}

testDatas := getTestDatas()
tests := make([]test, 0, len(testDatas))

for _, testData := range testDatas {
// New forest object.
p := utreexo.NewAccumulator()

// Create hashes to add from the stxo data.
addHashes := make([]utreexo.Leaf, 0, len(testData.leavesPerBlock))
for i, ld := range testData.leavesPerBlock {
addHashes = append(addHashes, utreexo.Leaf{
Hash: ld.LeafHash(),
// Just half and half.
Remember: i%2 == 0,
})
}
// Add to the accumulator.
err := p.Modify(addHashes, nil, utreexo.Proof{})
if err != nil {
t.Fatal(err)
}

// Generate Proof.
ud, err := GenerateUData(testData.leavesPerBlock, &p)
if err != nil {
t.Fatal(err)
}

// Append to the tests.
tests = append(tests, test{
name: testData.name,
isForTx: false,
ud: *ud,
})
}

for _, test := range tests {
ud := test.ud
// Serialize
writer := &bytes.Buffer{}
err := ud.SerializeCompactNoAccProof(writer)
if err != nil {
t.Fatal(err)
}
test.before = writer.Bytes()

// Deserialize
checkUData := new(UData)
err = checkUData.DeserializeCompactNoAccProof(writer)
if err != nil {
t.Fatal(err)
}

err = checkUDEqual(&ud, checkUData, true, test.name)
if err != nil {
t.Error(err)
}

// Re-serialize
afterWriter := &bytes.Buffer{}
checkUData.SerializeCompactNoAccProof(afterWriter)
test.after = afterWriter.Bytes()

// Check if before and after match.
if !bytes.Equal(test.before, test.after) {
t.Errorf("%s: UData serialize/deserialize fail. "+
"Before len %d, after len %d", test.name,
len(test.before), len(test.after))
}
}
}

func TestGenerateUData(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 779bd90

Please sign in to comment.