From 88ceb1b807a2b3f1f4f34d1f969c09faf3edf9b4 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Wed, 23 Oct 2024 18:34:17 +0900 Subject: [PATCH 1/4] main: update utreexo lib --- go.mod | 4 ++-- go.sum | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 28fe8fc6..99aa10aa 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 github.com/stretchr/testify v1.7.0 github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 - github.com/utreexo/utreexo v0.1.5 + github.com/utreexo/utreexo v0.2.1 golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd ) @@ -31,4 +31,4 @@ require ( gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) -go 1.18 +go 1.21 diff --git a/go.sum b/go.sum index 54e6682f..2d4e62a0 100644 --- a/go.sum +++ b/go.sum @@ -97,6 +97,8 @@ github.com/utreexo/utreexo v0.1.4 h1:jygjZscJEzab7woP7aB6YWUKjhsV5Mrbjj873inhwR8 github.com/utreexo/utreexo v0.1.4/go.mod h1:RT9JpZADhLr2YJVBgp48tmUxVeAHaAbOSr6p6nAEJpI= github.com/utreexo/utreexo v0.1.5 h1:nnG2VvwDYPkXCSRicV15eAbh2vvTp/g4Pot3vlseGdQ= github.com/utreexo/utreexo v0.1.5/go.mod h1:RT9JpZADhLr2YJVBgp48tmUxVeAHaAbOSr6p6nAEJpI= +github.com/utreexo/utreexo v0.2.1 h1:xycdaHK+HhDZO5MY6Clq7YeXEDguzar1GrAYqIJ1gvs= +github.com/utreexo/utreexo v0.2.1/go.mod h1:xIu3cTtT0jNdntc1qJhVyQV/RX03Sk9gFD3UAzALvuU= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= From 930f84d49b47bc2e3ee4d5b3414e772cd0a509af Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Wed, 23 Oct 2024 18:43:47 +0900 Subject: [PATCH 2/4] wire: don't init unless if the count is bigger than 0 For reflect.DeepEqual, we nil and len() == 0 is not equal. To account for this, keep slices nil when we can. --- wire/batchproof.go | 33 ++++++++++++++++++++++----------- wire/udata.go | 11 ++++++++--- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/wire/batchproof.go b/wire/batchproof.go index 9b640625..a7a6aee8 100644 --- a/wire/batchproof.go +++ b/wire/batchproof.go @@ -103,30 +103,41 @@ func BatchProofDeserialize(r io.Reader) (*utreexo.Proof, error) { return nil, err } - targets := make([]uint64, targetCount) - for i := range targets { - target, err := ReadVarInt(r, 0) - if err != nil { - return nil, err - } + proof := new(utreexo.Proof) - targets[i] = target + if targetCount > 0 { + targets := make([]uint64, 0, targetCount) + for i := 0; i < int(targetCount); i++ { + target, err := ReadVarInt(r, 0) + if err != nil { + return nil, err + } + + targets = append(targets, target) + } + proof.Targets = targets } proofCount, err := ReadVarInt(r, 0) if err != nil { return nil, err } + if proofCount == 0 { + return proof, nil + } - proofs := make([]utreexo.Hash, proofCount) - for i := range proofs { - _, err = io.ReadFull(r, proofs[i][:]) + proofs := make([]utreexo.Hash, 0, proofCount) + for i := 0; i < int(proofCount); i++ { + var hash utreexo.Hash + _, err = io.ReadFull(r, hash[:]) if err != nil { return nil, err } + proofs = append(proofs, hash) } + proof.Proof = proofs - return &utreexo.Proof{Targets: targets, Proof: proofs}, nil + return proof, nil } // BatchProofToString converts a batchproof into a human-readable string. Note diff --git a/wire/udata.go b/wire/udata.go index 173cc354..d5a59c9a 100644 --- a/wire/udata.go +++ b/wire/udata.go @@ -111,16 +111,21 @@ func (ud *UData) Deserialize(r io.Reader) error { if err != nil { return err } + if udCount == 0 { + return nil + } - ud.LeafDatas = make([]LeafData, udCount) - for i := range ud.LeafDatas { - err = ud.LeafDatas[i].Deserialize(r) + ud.LeafDatas = make([]LeafData, 0, udCount) + for i := 0; i < int(udCount); i++ { + ld := LeafData{} + err = ld.Deserialize(r) if err != nil { str := fmt.Sprintf("targetCount:%d, Stxos[%d], err:%s\n", len(ud.AccProof.Targets), i, err.Error()) returnErr := messageError("Deserialize stxos", str) return returnErr } + ud.LeafDatas = append(ud.LeafDatas, ld) } return nil From 68c8197739b14bea24e4dfd5b68ba49e558a5a91 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Wed, 23 Oct 2024 19:09:06 +0900 Subject: [PATCH 3/4] main, .github: update go Since the utreexo lib now requires v1.21.0, we update the CI accordingly. --- .github/workflows/go.yml | 4 ++-- goclean.sh | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5d7671d4..5e164f32 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go: ["1.20", "1.21"] + go: ["1.21"] rust: - version: stable clippy: true @@ -36,7 +36,7 @@ jobs: run: make all - name: Install Linters - run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2 + run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0 - name: Test run: make test diff --git a/goclean.sh b/goclean.sh index e5b819bb..df945c6b 100755 --- a/goclean.sh +++ b/goclean.sh @@ -10,9 +10,9 @@ set -ex go test -short -race -tags="rpctest" -tags="bdkwallet" ./... # Automatic checks -golangci-lint run --deadline=10m --disable-all \ +golangci-lint run --disable-all \ --enable=gofmt \ ---enable=vet \ +--enable=govet \ --enable=gosimple \ --enable=unconvert \ ---skip-dirs=bdkwallet/bdkgo # these are generated files +--exclude-dirs=bdkwallet/bdkgo # these are generated files From c1bc2686feebb85b548846d67663f7f939d44940 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Wed, 23 Oct 2024 19:14:03 +0900 Subject: [PATCH 4/4] blockchain, btcjson, wire: make linter happy --- blockchain/utreexoviewpoint.go | 4 ++-- btcjson/help.go | 2 +- wire/batchproof_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/blockchain/utreexoviewpoint.go b/blockchain/utreexoviewpoint.go index 235257a4..a4ba94b4 100644 --- a/blockchain/utreexoviewpoint.go +++ b/blockchain/utreexoviewpoint.go @@ -883,7 +883,7 @@ func (b *BlockChain) VerifyUData(ud *wire.UData, txIns []*wire.TxIn, remember bo str += fmt.Sprintf("%s\n", txIn.PreviousOutPoint.String()) } - return fmt.Errorf(str) + return fmt.Errorf("%v", str) } // Make a slice of hashes from LeafDatas. These are the hash commitments @@ -954,7 +954,7 @@ func (b *BlockChain) VerifyUData(ud *wire.UData, txIns []*wire.TxIn, remember bo ud.LeafDatas[i].String(), hex.EncodeToString(leafHash[:])) } str += fmt.Sprintf("err: %s", err.Error()) - return fmt.Errorf(str) + return fmt.Errorf("%v", str) } if remember { diff --git a/btcjson/help.go b/btcjson/help.go index 2cc55b84..bad10443 100644 --- a/btcjson/help.go +++ b/btcjson/help.go @@ -269,7 +269,7 @@ func resultTypeHelp(xT descLookupFunc, rt reflect.Type, fieldDescKey string) str w.Init(&formatted, 0, 4, 1, ' ', 0) for i, text := range results { if i == len(results)-1 { - fmt.Fprintf(w, text) + fmt.Fprint(w, text) } else { fmt.Fprintln(w, text) } diff --git a/wire/batchproof_test.go b/wire/batchproof_test.go index 4a6bfefe..d38e0a67 100644 --- a/wire/batchproof_test.go +++ b/wire/batchproof_test.go @@ -137,7 +137,7 @@ func TestBatchProofSerializeSize(t *testing.T) { if BatchProofSerializeSize(&test.bp) != test.size { err := fmt.Errorf("TestBatchProofSerializeSize \"%s\": expected size of %d but got %d", test.name, test.size, BatchProofSerializeSize(&test.bp)) - t.Errorf(err.Error()) + t.Error(err) } var buf bytes.Buffer @@ -149,7 +149,7 @@ func TestBatchProofSerializeSize(t *testing.T) { if test.size != len(buf.Bytes()) { err := fmt.Errorf("TestBatchProofSerializeSize \"%s\": serialized bytes len of %d but serialize size of %d", test.name, len(buf.Bytes()), BatchProofSerializeSize(&test.bp)) - t.Errorf(err.Error()) + t.Error(err) } } }