Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lightninglabs/neutrino
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Apr 27, 2022
2 parents 9b5eb6d + 3100c28 commit 0d46727
Show file tree
Hide file tree
Showing 29 changed files with 2,070 additions and 152 deletions.
58 changes: 55 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ linters-settings:
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
maligned:
suggest-new: true
whitespace:
multi-func: true
multi-if: true

linters:
enable-all: true
Expand All @@ -22,19 +23,70 @@ linters:
# even longer by marking them as 'nolint'.
- lll

# We want to allow short variable names.
- varnamelen

# We want to allow TODOs.
- godox

# We have long functions, especially in tests. Moving or renaming those would
# trigger funlen problems that we may not want to solve at that time.
- funlen

# Disable for now as we haven't yet tuned the sensitivity to our codebase
# yet. Enabling by default for example, would also force new contributors to
# yet. Enabling by default for example, would also force new contributors to
# potentially extensively refactor code, when they want to smaller change to
# land.
- gocyclo
- gocognit
- cyclop

# Instances of table driven tests that don't pre-allocate shouldn't trigger
# the linter.
- prealloc

# Init functions are used by loggers throughout the codebase.
- gochecknoinits

# Causes stack overflow, see https://github.com/polyfloyd/go-errorlint/issues/19.
- errorlint

# Deprecated linters. See https://golangci-lint.run/usage/linters/.
- interfacer
- golint
- maligned
- scopelint

# New linters that need a code adjustment first.
- wrapcheck
- nolintlint
- paralleltest
- tparallel
- testpackage
- gofumpt
- gomoddirectives
- ireturn
- maintidx
- nlreturn
- dogsled
- gci
- containedctx
- contextcheck
- errname
- exhaustivestruct
- goerr113
- gomnd
- ifshort
- noctx
- nestif
- wsl
- exhaustive
- forcetypeassert
- nilerr
- nilnil
- stylecheck
- thelper

issues:
exclude-rules:
# Exclude gosec from running for tests so that tests with weak randomness
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2018 Lightning Labs
Copyright (c) 2017-2022 Lightning Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
42 changes: 16 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
PKG := github.com/lightninglabs/neutrino
TOOLS_DIR := tools

BTCD_PKG := github.com/btcsuite/btcd
LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
GOACC_PKG := github.com/ory/go-acc
GOIMPORTS_PKG := golang.org/x/tools/cmd/goimports
GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports

GO_BIN := ${GOPATH}/bin
LINT_BIN := $(GO_BIN)/golangci-lint
GOACC_BIN := $(GO_BIN)/go-acc

LINT_COMMIT := v1.18.0
GOACC_COMMIT := v0.2.6

DEPGET := cd /tmp && GO111MODULE=on go get -v
GOBUILD := GO111MODULE=on go build -v
GOINSTALL := GO111MODULE=on go install -v
GOTEST := GO111MODULE=on go test
GOBUILD := go build -v
GOINSTALL := go install -v
GOTEST := go test

GOLIST := go list -deps $(PKG)/... | grep '$(PKG)'
GOLIST_COVER := $$(go list -deps $(PKG)/... | grep '$(PKG)')
GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*")

BTCD_COMMIT := $(shell cat go.mod | \
grep $(BTCD_PKG) | \
head -n1 | \
awk -F " " '{ print $$2 }' | \
awk -F "/" '{ print $$1 }')

RM := rm -f
CP := cp
MAKE := make
XARGS := xargs -L 1
DOCKER_TOOLS = docker run -v $$(pwd):/build neutrino-tools

# Linting uses a lot of memory, so keep it under control by limiting the number
# of workers if requested.
ifneq ($(workers),)
LINT_WORKERS = --concurrency=$(workers)
endif

LINT = $(LINT_BIN) run -v $(LINT_WORKERS)

GREEN := "\\033[0;32m"
NC := "\\033[0m"
define print
Expand All @@ -56,19 +46,15 @@ all: build check

btcd:
@$(call print, "Installing btcd.")
$(DEPGET) $(BTCD_PKG)@$(BTCD_COMMIT)

$(LINT_BIN):
@$(call print, "Fetching linter")
$(DEPGET) $(LINT_PKG)@$(LINT_COMMIT)
cd $(TOOLS_DIR); go install -trimpath -tags=tools $(BTCD_PKG)

$(GOACC_BIN):
@$(call print, "Fetching go-acc")
$(DEPGET) $(GOACC_PKG)@$(GOACC_COMMIT)
cd $(TOOLS_DIR); go install -trimpath -tags=tools $(GOACC_PKG)

goimports:
@$(call print, "Installing goimports.")
$(DEPGET) $(GOIMPORTS_PKG)
cd $(TOOLS_DIR); go install -trimpath -tags=tools $(GOIMPORTS_PKG)

# ============
# INSTALLATION
Expand Down Expand Up @@ -100,15 +86,19 @@ unit-race: btcd
# UTILITIES
# =========

docker-tools:
@$(call print, "Building tools docker image.")
docker build -q -t neutrino-tools $(TOOLS_DIR)

fmt: goimports
@$(call print, "Fixing imports.")
goimports -w $(GOFILES_NOVENDOR)
gosimports -w $(GOFILES_NOVENDOR)
@$(call print, "Formatting source.")
gofmt -l -w -s $(GOFILES_NOVENDOR)

lint: $(LINT_BIN)
lint: docker-tools
@$(call print, "Linting source.")
$(LINT)
$(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)

clean:
@$(call print, "Cleaning source.$(NC)")
Expand Down
30 changes: 16 additions & 14 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ type blockManager struct { // nolint:maligned
// newBlockManager returns a new bitcoin block manager. Use Start to begin
// processing asynchronous block and inv updates.
func newBlockManager(cfg *blockManagerCfg) (*blockManager, error) {

targetTimespan := int64(cfg.ChainParams.TargetTimespan / time.Second)
targetTimePerBlock := int64(cfg.ChainParams.TargetTimePerBlock / time.Second)
adjustmentFactor := cfg.ChainParams.RetargetAdjustmentFactor
Expand Down Expand Up @@ -509,8 +508,8 @@ waitForHeaders:
case <-b.quit:
b.newHeadersSignal.L.Unlock()
return
default:

default:
}

// Re-acquire the lock in order to check for the filter header
Expand Down Expand Up @@ -547,7 +546,6 @@ waitForHeaders:
// from all peers. We can go on and just request the cfheaders.
var goodCheckpoints []*chainhash.Hash
for len(goodCheckpoints) == 0 && lastHeight >= wire.CFCheckptInterval {

// Quit if requested.
select {
case <-b.quit:
Expand Down Expand Up @@ -1272,10 +1270,10 @@ func (b *blockManager) writeCFHeadersMsg(msg *wire.MsgCFHeaders,

// rollBackToHeight rolls back all blocks until it hits the specified height.
// It sends notifications along the way.
func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, error) {
func (b *blockManager) rollBackToHeight(height uint32) error {
header, headerHeight, err := b.cfg.BlockHeaders.ChainTip()
if err != nil {
return nil, err
return err
}
bs := &headerfs.BlockStamp{
Height: int32(headerHeight),
Expand All @@ -1285,13 +1283,13 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er

_, regHeight, err := b.cfg.RegFilterHeaders.ChainTip()
if err != nil {
return nil, err
return err
}

for uint32(bs.Height) > height {
header, headerHeight, err := b.cfg.BlockHeaders.FetchHeader(&bs.Hash)
if err != nil {
return nil, err
return err
}

newTip := &header.PrevBlock
Expand All @@ -1300,14 +1298,14 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er
if uint32(bs.Height) <= regHeight {
newFilterTip, err := b.cfg.RegFilterHeaders.RollbackLastBlock(newTip)
if err != nil {
return nil, err
return err
}
regHeight = uint32(newFilterTip.Height)
}

bs, err = b.cfg.BlockHeaders.RollbackLastBlock()
if err != nil {
return nil, err
return err
}

// Notifications are asynchronous, so we include the previous
Expand All @@ -1316,15 +1314,16 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er
// can't read it before it's deleted from the store.
prevHeader, _, err := b.cfg.BlockHeaders.FetchHeader(newTip)
if err != nil {
return nil, err
return err
}

// Now we send the block disconnected notifications.
b.onBlockDisconnected(
*header, headerHeight, *prevHeader,
)
}
return bs, nil

return nil
}

// minCheckpointHeight returns the height of the last filter checkpoint for the
Expand Down Expand Up @@ -1858,6 +1857,7 @@ func (b *blockManager) fetchFilterFromAllPeers(
// Ignore this message.
if blockHash != response.BlockHash ||
filterType != response.FilterType {

return
}

Expand Down Expand Up @@ -1903,6 +1903,7 @@ func (b *blockManager) getCheckpts(lastHash *chainhash.Hash,
if isCheckpoint {
if m.FilterType == fType &&
m.StopHash == *lastHash {

checkpoints[sp.Addr()] = m.FilterHeaders
close(peerQuit)
}
Expand Down Expand Up @@ -2592,7 +2593,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
b.syncPeerMutex.Lock()
b.syncPeer = hmsg.peer
b.syncPeerMutex.Unlock()
_, err = b.rollBackToHeight(backHeight)
err = b.rollBackToHeight(backHeight)
if err != nil {
panic(fmt.Sprintf("Rollback failed: %s", err))
// Should we panic here?
Expand Down Expand Up @@ -2644,7 +2645,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
prevCheckpoint.Height,
prevCheckpoint.Hash)

_, err := b.rollBackToHeight(uint32(
err := b.rollBackToHeight(uint32(
prevCheckpoint.Height),
)
if err != nil {
Expand Down Expand Up @@ -2708,6 +2709,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
// checkHeaderSanity checks the PoW, and timestamp of a block header.
func (b *blockManager) checkHeaderSanity(blockHeader *wire.BlockHeader,
maxTimestamp time.Time, reorgAttempt bool) error {

diff, err := b.calcNextRequiredDifficulty(
blockHeader.Timestamp, reorgAttempt)
if err != nil {
Expand Down Expand Up @@ -2848,7 +2850,7 @@ func (b *blockManager) findPrevTestNetDifficulty(hList headerlist.Chain) (uint32
iterNode := &startNode.Header
iterHeight := startNode.Height
for iterNode != nil && iterHeight%b.blocksPerRetarget != 0 &&
iterNode.Bits == b.cfg.ChainParams.PowLimitBits {
iterNode.Bits == b.cfg.ChainParams.PowLimitBits { // nolint

// Get the previous block node. This function is used over
// simply accessing iterNode.parent directly as it will
Expand Down
4 changes: 1 addition & 3 deletions blockmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func generateResponses(msgs []wire.Message,

var prevFilterHeader chainhash.Hash
switch q.StartHeight {

// If the start height is 1 the prevFilterHeader is set to the
// genesis header.
case 1:
Expand Down Expand Up @@ -426,7 +425,6 @@ func TestBlockManagerInitialInterval(t *testing.T) {
"%v", index, testDesc)
return
}

}
errChan <- nil
}()
Expand Down Expand Up @@ -657,7 +655,6 @@ func TestBlockManagerInvalidInterval(t *testing.T) {

errChan := make(chan error, 1)
go func() {

// Check that the success of the callback match what we
// expect.
for i := range responses {
Expand Down Expand Up @@ -726,6 +723,7 @@ func buildNonPushScriptFilter(block *wire.MsgBlock) (*gcs.Filter, error) {
// had a push-only script.
if txOut.PkScript[0] == txscript.OP_RETURN &&
txscript.IsPushOnlyScript(txOut.PkScript[1:]) {

continue
}

Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module github.com/lightninglabs/neutrino

require (
github.com/btcsuite/btcd v0.22.0-beta.0.20220207191057-4dc4ff7963b4
github.com/btcsuite/btcd/btcec/v2 v2.1.0
github.com/btcsuite/btcd/btcutil v1.1.0
github.com/btcsuite/btcd v0.22.0-beta.0.20220413172512-bf64c8bdbbbf
github.com/btcsuite/btcd/btcec/v2 v2.1.3
github.com/btcsuite/btcd/btcutil v1.1.1
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/btcsuite/btcwallet/wallet/txauthor v1.2.1
github.com/btcsuite/btcwallet/wallet/txauthor v1.2.3
github.com/btcsuite/btcwallet/walletdb v1.3.5
github.com/btcsuite/btcwallet/wtxmgr v1.5.0
github.com/davecgh/go-spew v1.1.1
github.com/lightningnetwork/lnd/queue v1.0.1
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.7.0
)

go 1.13
Loading

0 comments on commit 0d46727

Please sign in to comment.