Skip to content

Commit e2d8066

Browse files
authored
Merge pull request #1 from bsv-blockchain/lintFix
Add go-subtree source code
2 parents 70a7e85 + 92cbc36 commit e2d8066

26 files changed

+3860
-167
lines changed

.golangci.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@
4646
"disable": [
4747
"gochecknoglobals",
4848
"gocritic",
49-
"godot"
49+
"godot",
50+
"godox",
51+
"testifylint",
52+
"revive",
53+
"gochecknoinits",
54+
"forbidigo",
55+
"err113",
56+
"errorlint",
57+
"gosec",
58+
"unused",
59+
"gomoddirectives"
5060
],
5161
"enable": [
5262
"asasalint",

coinbase_placeholder.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package subtree
2+
3+
import (
4+
"github.com/libsv/go-bt/v2"
5+
"github.com/libsv/go-bt/v2/chainhash"
6+
)
7+
8+
var (
9+
// CoinbasePlaceholder hard code this value to avoid having to calculate it every time
10+
// to help the compiler optimize the code.
11+
CoinbasePlaceholder = [32]byte{
12+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
13+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
15+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
16+
}
17+
CoinbasePlaceholderHashValue = chainhash.Hash(CoinbasePlaceholder)
18+
CoinbasePlaceholderHash = &CoinbasePlaceholderHashValue
19+
20+
FrozenBytes = [36]byte{
21+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
22+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
23+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
24+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
25+
0xFF, 0xFF, 0xFF, 0xFF,
26+
}
27+
FrozenBytesTxBytes = FrozenBytes[0:32]
28+
FrozenBytesTxHash = chainhash.Hash(FrozenBytesTxBytes)
29+
)
30+
31+
var (
32+
CoinbasePlaceholderTx *bt.Tx
33+
coinbasePlaceholderTxHash *chainhash.Hash
34+
)
35+
36+
func init() {
37+
CoinbasePlaceholderTx = bt.NewTx()
38+
CoinbasePlaceholderTx.Version = 0xFFFFFFFF
39+
CoinbasePlaceholderTx.LockTime = 0xFFFFFFFF
40+
41+
coinbasePlaceholderTxHash = CoinbasePlaceholderTx.TxIDChainHash()
42+
}
43+
44+
func IsCoinbasePlaceHolderTx(tx *bt.Tx) bool {
45+
return tx.TxIDChainHash().IsEqual(coinbasePlaceholderTxHash)
46+
}

coinbase_placeholder_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package subtree
2+
3+
import (
4+
"testing"
5+
6+
"github.com/libsv/go-bt/v2"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCoinbasePlaceholderTx(t *testing.T) {
11+
assert.True(t, IsCoinbasePlaceHolderTx(CoinbasePlaceholderTx))
12+
assert.Equal(t, CoinbasePlaceholderTx.Version, uint32(0xFFFFFFFF))
13+
assert.Equal(t, CoinbasePlaceholderTx.LockTime, uint32(0xFFFFFFFF))
14+
assert.Equal(t, CoinbasePlaceholderTx.TxIDChainHash(), coinbasePlaceholderTxHash)
15+
assert.False(t, IsCoinbasePlaceHolderTx(bt.NewTx()))
16+
assert.Equal(t, "a8502e9c08b3c851201a71d25bf29fd38a664baedb777318b12d19242f0e46ab", CoinbasePlaceholderTx.TxIDChainHash().String())
17+
}

compare.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package subtree
2+
3+
import "golang.org/x/exp/constraints"
4+
5+
func Min[T constraints.Ordered](a, b T) T {
6+
if a < b {
7+
return a
8+
}
9+
10+
return b
11+
}
12+
13+
func Max[T constraints.Ordered](a, b T) T {
14+
if a > b {
15+
return a
16+
}
17+
18+
return b
19+
}

compare_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package subtree
2+
3+
import "testing"
4+
5+
func TestMin(t *testing.T) {
6+
tests := []struct {
7+
a, b, expected int
8+
}{
9+
{1, 2, 1},
10+
{2, 1, 1},
11+
{3, 3, 3},
12+
{-1, 1, -1},
13+
}
14+
15+
for _, tt := range tests {
16+
t.Run("", func(t *testing.T) {
17+
result := Min(tt.a, tt.b)
18+
if result != tt.expected {
19+
t.Errorf("Min(%d, %d) = %d; want %d", tt.a, tt.b, result, tt.expected)
20+
}
21+
})
22+
}
23+
}
24+
25+
func TestMax(t *testing.T) {
26+
tests := []struct {
27+
a, b, expected int
28+
}{
29+
{1, 2, 2},
30+
{2, 1, 2},
31+
{3, 3, 3},
32+
{-1, 1, 1},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run("", func(t *testing.T) {
37+
result := Max(tt.a, tt.b)
38+
if result != tt.expected {
39+
t.Errorf("Max(%d, %d) = %d; want %d", tt.a, tt.b, result, tt.expected)
40+
}
41+
})
42+
}
43+
}

examples/example.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

go.mod

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ module github.com/bsv-blockchain/go-subtree
22

33
go 1.24
44

5-
require github.com/stretchr/testify v1.10.0
5+
require (
6+
github.com/bsv-blockchain/go-safe-conversion v0.0.0-20250701040542-ca4e7b9ca0da
7+
github.com/bsv-blockchain/go-tx-map v1.0.0
8+
github.com/libsv/go-bt/v2 v2.0.0-00010101000000-000000000000
9+
github.com/stretchr/testify v1.10.0
10+
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b
11+
)
612

713
require (
814
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/dolthub/maphash v0.1.0 // indirect
16+
github.com/dolthub/swiss v0.2.1 // indirect
17+
github.com/libsv/go-bk v0.1.6 // indirect
18+
github.com/pkg/errors v0.9.1 // indirect
919
github.com/pmezard/go-difflib v1.0.0 // indirect
20+
golang.org/x/crypto v0.14.0 // indirect
21+
google.golang.org/protobuf v1.36.6 // indirect
1022
gopkg.in/yaml.v3 v3.0.1 // indirect
1123
)
24+
25+
replace github.com/libsv/go-bt/v2 => github.com/ordishs/go-bt/v2 v2.2.22

go.sum

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
1+
github.com/bsv-blockchain/go-safe-conversion v0.0.0-20250701040542-ca4e7b9ca0da h1:WYjLgrUvgq1HjGcP1mU0aXPrqZ5l0UoDB/b9ssa/nxo=
2+
github.com/bsv-blockchain/go-safe-conversion v0.0.0-20250701040542-ca4e7b9ca0da/go.mod h1:Fmat8fhPfMrdGCGv9PZ+QOkpQutD61hssbaLto4+3ks=
3+
github.com/bsv-blockchain/go-tx-map v1.0.0 h1:oeGI6et039crvzuELKHojYdlZwDNf+UCv9r1+63sZHE=
4+
github.com/bsv-blockchain/go-tx-map v1.0.0/go.mod h1:xCauj1rtF8dxuxP8WusegkFLzmcVCuLzcYZfjwXOi3Y=
15
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
26
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
8+
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
9+
github.com/dolthub/swiss v0.2.1 h1:gs2osYs5SJkAaH5/ggVJqXQxRXtWshF6uE0lgR/Y3Gw=
10+
github.com/dolthub/swiss v0.2.1/go.mod h1:8AhKZZ1HK7g18j7v7k6c5cYIGEZJcPn0ARsai8cUrh0=
11+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
12+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
14+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
15+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
16+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
17+
github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c=
18+
github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA=
19+
github.com/ordishs/go-bt/v2 v2.2.22 h1:5WmTQoX74g9FADM9hpbXZOE34uep4EqeSwpIy4CbWYE=
20+
github.com/ordishs/go-bt/v2 v2.2.22/go.mod h1:bOaZFOoazYognJH/nfcBjuDFud1XmIc05n7bp4Tvvfk=
21+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
22+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
323
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
424
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25+
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
26+
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
527
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
628
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
29+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
30+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
31+
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
32+
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
33+
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
34+
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
835
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
37+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
938
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1039
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)