Skip to content

Commit

Permalink
merge master 6-24-24
Browse files Browse the repository at this point in the history
  • Loading branch information
gitteri committed Jun 24, 2024
2 parents 38285ce + f6db48a commit 36bc15a
Show file tree
Hide file tree
Showing 155 changed files with 6,125 additions and 1,279 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ btcutil/psbt/coverage.txt

# vim
*.swp
*.swo
/.vim

# Binaries produced by "make build"
/addblock
Expand Down
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ Changes in 0.8.0-beta (Sun May 25 2014)
recent reference client changes
(https://github.com/conformal/btcd/issues/100)
- Raise the maximum signature script size to support standard 15-of-15
multi-signature pay-to-sript-hash transactions with compressed pubkeys
multi-signature pay-to-script-hash transactions with compressed pubkeys
to remain compatible with the reference client
(https://github.com/conformal/btcd/issues/128)
- Reduce max bytes allowed for a standard nulldata transaction to 40 for
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# docker build . -t yourregistry/btcd
#
# You can use the following command to buid an arm64v8 container:
# You can use the following command to build an arm64v8 container:
#
# docker build . -t yourregistry/btcd --build-arg ARCH=arm64v8
#
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ define print
echo $(GREEN)$1$(NC)
endef

#? default: Run `make build`
default: build

#? all: Run `make build` and `make check`
all: build check

# ============
Expand All @@ -55,6 +57,7 @@ $(GOACC_BIN):
@$(call print, "Fetching go-acc")
$(DEPGET) $(GOACC_PKG)@$(GOACC_COMMIT)

#? goimports: Install goimports
goimports:
@$(call print, "Installing goimports.")
$(DEPGET) $(GOIMPORTS_PKG)
Expand All @@ -63,6 +66,7 @@ goimports:
# INSTALLATION
# ============

#? build: Build all binaries, place them in project directory
build:
@$(call print, "Building all binaries")
$(GOBUILD) $(PKG)
Expand All @@ -71,6 +75,7 @@ build:
$(GOBUILD) $(PKG)/cmd/findcheckpoint
$(GOBUILD) $(PKG)/cmd/addblock

#? install: Install all binaries, place them in $GOPATH/bin
install:
@$(call print, "Installing all binaries")
$(GOINSTALL) $(PKG)
Expand All @@ -79,6 +84,7 @@ install:
$(GOINSTALL) $(PKG)/cmd/findcheckpoint
$(GOINSTALL) $(PKG)/cmd/addblock

#? release-install: Install btcd and btcctl release binaries, place them in $GOPATH/bin
release-install:
@$(call print, "Installing btcd and btcctl release binaries")
env CGO_ENABLED=0 $(GOINSTALL) -trimpath -ldflags="-s -w -buildid=" $(PKG)
Expand All @@ -88,15 +94,18 @@ release-install:
# TESTING
# =======

#? check: Run `make unit`
check: unit

#? unit: Run unit tests
unit:
@$(call print, "Running unit tests.")
$(GOTEST_DEV) ./... -test.timeout=20m
cd btcec; $(GOTEST_DEV) ./... -test.timeout=20m
cd btcutil; $(GOTEST_DEV) ./... -test.timeout=20m
cd btcutil/psbt; $(GOTEST_DEV) ./... -test.timeout=20m

#? unit-cover: Run unit coverage tests
unit-cover: $(GOACC_BIN)
@$(call print, "Running unit coverage tests.")
$(GOACC_BIN) ./...
Expand All @@ -109,6 +118,7 @@ unit-cover: $(GOACC_BIN)

cd btcutil/psbt; $(GOACC_BIN) ./...

#? unit-race: Run unit race tests
unit-race:
@$(call print, "Running unit race tests.")
env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" $(GOTEST) -race -test.timeout=20m ./...
Expand All @@ -120,19 +130,27 @@ unit-race:
# UTILITIES
# =========

#? fmt: Fix imports and formatting source
fmt: goimports
@$(call print, "Fixing imports.")
goimports -w $(GOFILES_NOVENDOR)
@$(call print, "Formatting source.")
gofmt -l -w -s $(GOFILES_NOVENDOR)

#? lint: Lint source
lint: $(LINT_BIN)
@$(call print, "Linting source.")
$(LINT)

#? clean: Clean source
clean:
@$(call print, "Cleaning source.$(NC)")
$(RM) coverage.txt btcec/coverage.txt btcutil/coverage.txt btcutil/psbt/coverage.txt

#? tidy-module: Run 'go mod tidy' for all modules
tidy-module:
echo "Running 'go mod tidy' for all modules"
scripts/tidy_modules.sh

.PHONY: all \
default \
Expand All @@ -144,3 +162,10 @@ clean:
fmt \
lint \
clean

#? help: Get more info on make commands
help: Makefile
@echo " Choose a command run in btcd:"
@sed -n 's/^#?//p' $< | column -t -s ':' | sort | sed -e 's/^/ /'

.PHONY: help
34 changes: 25 additions & 9 deletions addrmgr/addrmanager_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package addrmgr

import (
"io/ioutil"
"math/rand"
"net"
"os"
Expand All @@ -12,7 +11,7 @@ import (
)

// randAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6
// address.
// address. Some of the returned addresses may not be routable.
func randAddr(t *testing.T) *wire.NetAddressV2 {
t.Helper()

Expand Down Expand Up @@ -40,6 +39,23 @@ func randAddr(t *testing.T) *wire.NetAddressV2 {
)
}

// routableRandAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6
// address that is always routable.
func routableRandAddr(t *testing.T) *wire.NetAddressV2 {
t.Helper()

var addr *wire.NetAddressV2

// If the address is not routable, try again.
routable := false
for !routable {
addr = randAddr(t)
routable = IsRoutable(addr)
}

return addr
}

// assertAddr ensures that the two addresses match. The timestamp is not
// checked as it does not affect uniquely identifying a specific address.
func assertAddr(t *testing.T, got, expected *wire.NetAddressV2) {
Expand Down Expand Up @@ -91,7 +107,7 @@ func TestAddrManagerSerialization(t *testing.T) {

// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := ioutil.TempDir("", "addrmgr")
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
Expand All @@ -104,9 +120,9 @@ func TestAddrManagerSerialization(t *testing.T) {

expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
for i := 0; i < numAddrs; i++ {
addr := randAddr(t)
addr := routableRandAddr(t)
expectedAddrs[NetAddressKey(addr)] = addr
addrMgr.AddAddress(addr, randAddr(t))
addrMgr.AddAddress(addr, routableRandAddr(t))
}

// Now that the addresses have been added, we should be able to retrieve
Expand All @@ -131,7 +147,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {

// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := ioutil.TempDir("", "addrmgr")
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
Expand All @@ -149,9 +165,9 @@ func TestAddrManagerV1ToV2(t *testing.T) {

expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
for i := 0; i < numAddrs; i++ {
addr := randAddr(t)
addr := routableRandAddr(t)
expectedAddrs[NetAddressKey(addr)] = addr
addrMgr.AddAddress(addr, randAddr(t))
addrMgr.AddAddress(addr, routableRandAddr(t))
}

// Then, we'll persist these addresses to disk and restart the address
Expand All @@ -168,7 +184,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {
addrMgr.loadPeers()
addrs := addrMgr.getAddresses()
if len(addrs) != len(expectedAddrs) {
t.Fatalf("expected to find %d adddresses, found %d",
t.Fatalf("expected to find %d addresses, found %d",
len(expectedAddrs), len(addrs))
}
for _, addr := range addrs {
Expand Down
8 changes: 5 additions & 3 deletions blockchain/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, flags BehaviorFlags)
// Notify the caller that the new block was accepted into the block
// chain. The caller would typically want to react by relaying the
// inventory to other peers.
b.chainLock.Unlock()
b.sendNotification(NTBlockAccepted, block)
b.chainLock.Lock()
func() {
b.chainLock.Unlock()
defer b.chainLock.Lock()
b.sendNotification(NTBlockAccepted, block)
}()

return isMainChain, nil
}
36 changes: 36 additions & 0 deletions blockchain/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ func newBlockNode(blockHeader *wire.BlockHeader, parent *blockNode) *blockNode {
return &node
}

// Equals compares all the fields of the block node except for the parent and
// ancestor and returns true if they're equal.
func (node *blockNode) Equals(other *blockNode) bool {
return node.hash == other.hash &&
node.workSum.Cmp(other.workSum) == 0 &&
node.height == other.height &&
node.version == other.version &&
node.bits == other.bits &&
node.nonce == other.nonce &&
node.timestamp == other.timestamp &&
node.merkleRoot == other.merkleRoot &&
node.status == other.status
}

// Header constructs a block header from the node and returns it.
//
// This function is safe for concurrent access.
Expand Down Expand Up @@ -260,6 +274,28 @@ func (node *blockNode) RelativeAncestorCtx(distance int32) HeaderCtx {
return ancestor
}

// IsAncestor returns if the other node is an ancestor of this block node.
func (node *blockNode) IsAncestor(otherNode *blockNode) bool {
// Return early as false if the otherNode is nil.
if otherNode == nil {
return false
}

ancestor := node.Ancestor(otherNode.height)
if ancestor == nil {
return false
}

// If the otherNode has the same height as me, then the returned
// ancestor will be me. Return false since I'm not an ancestor of me.
if node.height == ancestor.height {
return false
}

// Return true if the fetched ancestor is other node.
return ancestor.Equals(otherNode)
}

// RelativeAncestor returns the ancestor block node a relative 'distance' blocks
// before this node. This is equivalent to calling Ancestor with the node's
// height minus provided distance.
Expand Down
Loading

0 comments on commit 36bc15a

Please sign in to comment.