diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index b95b96d7..bd6f1519 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -9,7 +9,6 @@ on: permissions: contents: read - pull-requests: read jobs: golangci: @@ -21,6 +20,4 @@ jobs: with: go-version: 1.23.x - name: golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - only-new-issues: ${{ github.event_name == 'pull_request' && 'true' || 'false' }} + uses: golangci/golangci-lint-action@v7 diff --git a/.golangci.yml b/.golangci.yml index 87667c0b..1b3d3e70 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,35 +1,71 @@ -issues: - exclude-dirs: - - docs - max-issues-per-linter: 0 - max-same-issues: 0 +version: "2" +run: + issues-exit-code: 1 + tests: false linters: + enable: + - asciicheck + - bidichk + - bodyclose + - contextcheck + - copyloopvar + - durationcheck + - errchkjson + - errorlint + - exhaustive + - fatcontext + - gocheckcompilerdirectives + - gochecksumtype + - gomodguard + - gosec + - gosmopolitan + - loggercheck + - makezero + - musttag + - nilerr + - nilnesserr + - noctx + - perfsprint + - prealloc + - protogetter + - reassign + - rowserrcheck + - spancheck + - sqlclosecheck + - testifylint + - usestdlibvars + - whitespace + - zerologlint disable: - asasalint - depguard - recvcheck - unparam + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - docs + - third_party$ + - builtin$ + - examples$ +issues: + max-issues-per-linter: 0 + max-same-issues: 0 +formatters: enable: - - errcheck - - gosimple - - govet - - ineffassign - - staticcheck - - unused - # Defaults above ours below - - copyloopvar - - usestdlibvars - - whitespace - presets: - - bugs - - format - - import - - performance - - unused -#linters-settings: -# errcheck: -# check-type-assertions: true -run: - issues-exit-code: 1 - tests: false - timeout: 10m + - gci + - gofmt + - gofumpt + - goimports + exclusions: + generated: lax + paths: + - docs + - third_party$ + - builtin$ + - examples$ diff --git a/cbor/tags.go b/cbor/tags.go index 6d4cca59..56dd855c 100644 --- a/cbor/tags.go +++ b/cbor/tags.go @@ -121,7 +121,7 @@ func (r *Rat) UnmarshalCBOR(cborData []byte) error { } // Create new big.Rat with num/denom set to big.Int values above r.Rat = new(big.Rat) - r.Rat.SetFrac(tmpNum, tmpDenom) + r.SetFrac(tmpNum, tmpDenom) return nil } diff --git a/cmd/gouroboros/chainsync.go b/cmd/gouroboros/chainsync.go index 73100199..07a755fe 100644 --- a/cmd/gouroboros/chainsync.go +++ b/cmd/gouroboros/chainsync.go @@ -267,7 +267,7 @@ func chainSyncRollForwardHandler( blockHash := v.Hash().Bytes() var err error if oConn == nil { - return errors.New("empty ouroboros connection, aborting!") + return errors.New("empty ouroboros connection, aborting") } block, err = oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash)) if err != nil { diff --git a/connection.go b/connection.go index e73cc709..58a2e5ad 100644 --- a/connection.go +++ b/connection.go @@ -249,7 +249,7 @@ func (c *Connection) setupConnection() error { // Check network magic value if c.networkMagic == 0 { return fmt.Errorf( - "invalid network magic value provided: %d\n", + "invalid network magic value provided: %d", c.networkMagic, ) } diff --git a/ledger/allegra/allegra.go b/ledger/allegra/allegra.go index e81969e1..bd883bd6 100644 --- a/ledger/allegra/allegra.go +++ b/ledger/allegra/allegra.go @@ -326,7 +326,7 @@ func (t *AllegraTransaction) Cbor() []byte { func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) { var allegraBlock AllegraBlock if _, err := cbor.Decode(data, &allegraBlock); err != nil { - return nil, fmt.Errorf("Allegra block decode error: %w", err) + return nil, fmt.Errorf("decode Allegra block error: %w", err) } return &allegraBlock, nil } @@ -334,7 +334,7 @@ func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) { func NewAllegraBlockHeaderFromCbor(data []byte) (*AllegraBlockHeader, error) { var allegraBlockHeader AllegraBlockHeader if _, err := cbor.Decode(data, &allegraBlockHeader); err != nil { - return nil, fmt.Errorf("Allegra block header decode error: %w", err) + return nil, fmt.Errorf("decode Allegra block header error: %w", err) } return &allegraBlockHeader, nil } @@ -344,7 +344,7 @@ func NewAllegraTransactionBodyFromCbor( ) (*AllegraTransactionBody, error) { var allegraTx AllegraTransactionBody if _, err := cbor.Decode(data, &allegraTx); err != nil { - return nil, fmt.Errorf("Allegra transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Allegra transaction body error: %w", err) } return &allegraTx, nil } @@ -352,7 +352,7 @@ func NewAllegraTransactionBodyFromCbor( func NewAllegraTransactionFromCbor(data []byte) (*AllegraTransaction, error) { var allegraTx AllegraTransaction if _, err := cbor.Decode(data, &allegraTx); err != nil { - return nil, fmt.Errorf("Allegra transaction decode error: %w", err) + return nil, fmt.Errorf("decode Allegra transaction error: %w", err) } return &allegraTx, nil } diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index 0450a96c..5d95ce3f 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -528,7 +528,7 @@ func (t *AlonzoTransaction) Utxorpc() *utxorpc.Tx { func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) { var alonzoBlock AlonzoBlock if _, err := cbor.Decode(data, &alonzoBlock); err != nil { - return nil, fmt.Errorf("Alonzo block decode error: %w", err) + return nil, fmt.Errorf("decode Alonzo block error: %w", err) } return &alonzoBlock, nil } @@ -536,7 +536,7 @@ func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) { func NewAlonzoBlockHeaderFromCbor(data []byte) (*AlonzoBlockHeader, error) { var alonzoBlockHeader AlonzoBlockHeader if _, err := cbor.Decode(data, &alonzoBlockHeader); err != nil { - return nil, fmt.Errorf("Alonzo block header decode error: %w", err) + return nil, fmt.Errorf("decode Alonzo block header error: %w", err) } return &alonzoBlockHeader, nil } @@ -546,7 +546,7 @@ func NewAlonzoTransactionBodyFromCbor( ) (*AlonzoTransactionBody, error) { var alonzoTx AlonzoTransactionBody if _, err := cbor.Decode(data, &alonzoTx); err != nil { - return nil, fmt.Errorf("Alonzo transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Alonzo transaction body error: %w", err) } return &alonzoTx, nil } @@ -554,7 +554,7 @@ func NewAlonzoTransactionBodyFromCbor( func NewAlonzoTransactionFromCbor(data []byte) (*AlonzoTransaction, error) { var alonzoTx AlonzoTransaction if _, err := cbor.Decode(data, &alonzoTx); err != nil { - return nil, fmt.Errorf("Alonzo transaction decode error: %w", err) + return nil, fmt.Errorf("decode Alonzo transaction error: %w", err) } return &alonzoTx, nil } @@ -565,7 +565,7 @@ func NewAlonzoTransactionOutputFromCbor( var alonzoTxOutput AlonzoTransactionOutput if _, err := cbor.Decode(data, &alonzoTxOutput); err != nil { return nil, fmt.Errorf( - "Alonzo transaction output decode error: %w", + "decode Alonzo transaction output error: %w", err, ) } diff --git a/ledger/babbage/babbage.go b/ledger/babbage/babbage.go index d0daf88f..311a3f2b 100644 --- a/ledger/babbage/babbage.go +++ b/ledger/babbage/babbage.go @@ -684,7 +684,7 @@ func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx { func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) { var babbageBlock BabbageBlock if _, err := cbor.Decode(data, &babbageBlock); err != nil { - return nil, fmt.Errorf("Babbage block decode error: %w", err) + return nil, fmt.Errorf("decode Babbage block error: %w", err) } return &babbageBlock, nil } @@ -692,7 +692,7 @@ func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) { func NewBabbageBlockHeaderFromCbor(data []byte) (*BabbageBlockHeader, error) { var babbageBlockHeader BabbageBlockHeader if _, err := cbor.Decode(data, &babbageBlockHeader); err != nil { - return nil, fmt.Errorf("Babbage block header decode error: %w", err) + return nil, fmt.Errorf("decode Babbage block header error: %w", err) } return &babbageBlockHeader, nil } @@ -702,7 +702,7 @@ func NewBabbageTransactionBodyFromCbor( ) (*BabbageTransactionBody, error) { var babbageTx BabbageTransactionBody if _, err := cbor.Decode(data, &babbageTx); err != nil { - return nil, fmt.Errorf("Babbage transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Babbage transaction body error: %w", err) } return &babbageTx, nil } @@ -710,7 +710,7 @@ func NewBabbageTransactionBodyFromCbor( func NewBabbageTransactionFromCbor(data []byte) (*BabbageTransaction, error) { var babbageTx BabbageTransaction if _, err := cbor.Decode(data, &babbageTx); err != nil { - return nil, fmt.Errorf("Babbage transaction decode error: %w", err) + return nil, fmt.Errorf("decode Babbage transaction error: %w", err) } return &babbageTx, nil } @@ -721,7 +721,7 @@ func NewBabbageTransactionOutputFromCbor( var babbageTxOutput BabbageTransactionOutput if _, err := cbor.Decode(data, &babbageTxOutput); err != nil { return nil, fmt.Errorf( - "Babbage transaction output decode error: %w", + "decode Babbage transaction output error: %w", err, ) } diff --git a/ledger/byron/byron.go b/ledger/byron/byron.go index 1200748f..45b6a9e9 100644 --- a/ledger/byron/byron.go +++ b/ledger/byron/byron.go @@ -683,7 +683,7 @@ func NewByronEpochBoundaryBlockFromCbor( ) (*ByronEpochBoundaryBlock, error) { var byronEbbBlock ByronEpochBoundaryBlock if _, err := cbor.Decode(data, &byronEbbBlock); err != nil { - return nil, fmt.Errorf("Byron EBB block decode error: %w", err) + return nil, fmt.Errorf("decode Byron EBB block error: %w", err) } return &byronEbbBlock, nil } @@ -693,7 +693,7 @@ func NewByronEpochBoundaryBlockHeaderFromCbor( ) (*ByronEpochBoundaryBlockHeader, error) { var byronEbbBlockHeader ByronEpochBoundaryBlockHeader if _, err := cbor.Decode(data, &byronEbbBlockHeader); err != nil { - return nil, fmt.Errorf("Byron EBB block header decode error: %w", err) + return nil, fmt.Errorf("decode Byron EBB block header error: %w", err) } return &byronEbbBlockHeader, nil } @@ -701,7 +701,7 @@ func NewByronEpochBoundaryBlockHeaderFromCbor( func NewByronMainBlockFromCbor(data []byte) (*ByronMainBlock, error) { var byronMainBlock ByronMainBlock if _, err := cbor.Decode(data, &byronMainBlock); err != nil { - return nil, fmt.Errorf("Byron main block decode error: %w", err) + return nil, fmt.Errorf("decode Byron main block error: %w", err) } return &byronMainBlock, nil } @@ -711,7 +711,7 @@ func NewByronMainBlockHeaderFromCbor( ) (*ByronMainBlockHeader, error) { var byronMainBlockHeader ByronMainBlockHeader if _, err := cbor.Decode(data, &byronMainBlockHeader); err != nil { - return nil, fmt.Errorf("Byron main block header decode error: %w", err) + return nil, fmt.Errorf("decode Byron main block header error: %w", err) } return &byronMainBlockHeader, nil } @@ -719,7 +719,7 @@ func NewByronMainBlockHeaderFromCbor( func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) { var byronTx ByronTransaction if _, err := cbor.Decode(data, &byronTx); err != nil { - return nil, fmt.Errorf("Byron transaction decode error: %w", err) + return nil, fmt.Errorf("decode Byron transaction error: %w", err) } return &byronTx, nil } diff --git a/ledger/common/address.go b/ledger/common/address.go index 4ec5c702..29a79bd9 100644 --- a/ledger/common/address.go +++ b/ledger/common/address.go @@ -301,14 +301,12 @@ func (a Address) ByronType() uint64 { // PaymentAddress returns a new Address with only the payment address portion. This will return nil for anything other than payment and script addresses func (a Address) PaymentAddress() *Address { var addrType uint8 - if a.addressType == AddressTypeKeyKey || - a.addressType == AddressTypeKeyNone { + switch a.addressType { + case AddressTypeKeyKey, AddressTypeKeyNone: addrType = AddressTypeKeyNone - } else if a.addressType == AddressTypeScriptKey || - a.addressType == AddressTypeScriptNone || - a.addressType == AddressTypeScriptScript { + case AddressTypeScriptKey, AddressTypeScriptNone, AddressTypeScriptScript: addrType = AddressTypeScriptNone - } else { + default: // Unsupported address type return nil } @@ -332,13 +330,12 @@ func (a *Address) PaymentKeyHash() Blake2b224 { // StakeAddress returns a new Address with only the stake key portion. This will return nil if the address is not a payment/staking key pair func (a Address) StakeAddress() *Address { var addrType uint8 - if a.addressType == AddressTypeKeyKey || - a.addressType == AddressTypeScriptKey { + switch a.addressType { + case AddressTypeKeyKey, AddressTypeScriptKey: addrType = AddressTypeNoneKey - } else if a.addressType == AddressTypeScriptScript || - a.addressType == AddressTypeNoneScript { + case AddressTypeScriptScript, AddressTypeNoneScript: addrType = AddressTypeNoneScript - } else { + default: // Unsupported address type return nil } diff --git a/ledger/common/genesis.go b/ledger/common/genesis.go index e0c44162..1c9593d0 100644 --- a/ledger/common/genesis.go +++ b/ledger/common/genesis.go @@ -26,7 +26,7 @@ type GenesisRat struct { func (r *GenesisRat) UnmarshalJSON(data []byte) error { r.Rat = new(big.Rat) - if _, ok := r.Rat.SetString(string(data)); !ok { + if _, ok := r.SetString(string(data)); !ok { return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data) } return nil diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index 8e761605..bfd440fa 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -490,7 +490,7 @@ func (t *ConwayTransaction) Utxorpc() *utxorpc.Tx { func NewConwayBlockFromCbor(data []byte) (*ConwayBlock, error) { var conwayBlock ConwayBlock if _, err := cbor.Decode(data, &conwayBlock); err != nil { - return nil, fmt.Errorf("Conway block decode error: %w", err) + return nil, fmt.Errorf("decode Conway block error: %w", err) } return &conwayBlock, nil } @@ -498,7 +498,7 @@ func NewConwayBlockFromCbor(data []byte) (*ConwayBlock, error) { func NewConwayBlockHeaderFromCbor(data []byte) (*ConwayBlockHeader, error) { var conwayBlockHeader ConwayBlockHeader if _, err := cbor.Decode(data, &conwayBlockHeader); err != nil { - return nil, fmt.Errorf("Conway block header decode error: %w", err) + return nil, fmt.Errorf("decode Conway block header error: %w", err) } return &conwayBlockHeader, nil } @@ -508,7 +508,7 @@ func NewConwayTransactionBodyFromCbor( ) (*ConwayTransactionBody, error) { var conwayTx ConwayTransactionBody if _, err := cbor.Decode(data, &conwayTx); err != nil { - return nil, fmt.Errorf("Conway transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Conway transaction body error: %w", err) } return &conwayTx, nil } @@ -516,7 +516,7 @@ func NewConwayTransactionBodyFromCbor( func NewConwayTransactionFromCbor(data []byte) (*ConwayTransaction, error) { var conwayTx ConwayTransaction if _, err := cbor.Decode(data, &conwayTx); err != nil { - return nil, fmt.Errorf("Conway transaction decode error: %w", err) + return nil, fmt.Errorf("decode Conway transaction error: %w", err) } return &conwayTx, nil } diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index 2324e398..257ea199 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -414,7 +414,7 @@ func (v *MaryTransactionOutputValue) MarshalCBOR() ([]byte, error) { func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) { var maryBlock MaryBlock if _, err := cbor.Decode(data, &maryBlock); err != nil { - return nil, fmt.Errorf("Mary block decode error: %w", err) + return nil, fmt.Errorf("decode Mary block error: %w", err) } return &maryBlock, nil } @@ -422,7 +422,7 @@ func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) { func NewMaryBlockHeaderFromCbor(data []byte) (*MaryBlockHeader, error) { var maryBlockHeader MaryBlockHeader if _, err := cbor.Decode(data, &maryBlockHeader); err != nil { - return nil, fmt.Errorf("Mary block header decode error: %w", err) + return nil, fmt.Errorf("decode Mary block header error: %w", err) } return &maryBlockHeader, nil } @@ -430,7 +430,7 @@ func NewMaryBlockHeaderFromCbor(data []byte) (*MaryBlockHeader, error) { func NewMaryTransactionBodyFromCbor(data []byte) (*MaryTransactionBody, error) { var maryTx MaryTransactionBody if _, err := cbor.Decode(data, &maryTx); err != nil { - return nil, fmt.Errorf("Mary transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Mary transaction body error: %w", err) } return &maryTx, nil } @@ -438,7 +438,7 @@ func NewMaryTransactionBodyFromCbor(data []byte) (*MaryTransactionBody, error) { func NewMaryTransactionFromCbor(data []byte) (*MaryTransaction, error) { var maryTx MaryTransaction if _, err := cbor.Decode(data, &maryTx); err != nil { - return nil, fmt.Errorf("Mary transaction decode error: %w", err) + return nil, fmt.Errorf("decode Mary transaction error: %w", err) } return &maryTx, nil } @@ -448,7 +448,7 @@ func NewMaryTransactionOutputFromCbor( ) (*MaryTransactionOutput, error) { var maryTxOutput MaryTransactionOutput if _, err := cbor.Decode(data, &maryTxOutput); err != nil { - return nil, fmt.Errorf("Mary transaction output decode error: %w", err) + return nil, fmt.Errorf("decode Mary transaction output error: %w", err) } return &maryTxOutput, nil } diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index ba13b494..aa1222a2 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -684,7 +684,7 @@ func (t *ShelleyTransaction) Cbor() []byte { func NewShelleyBlockFromCbor(data []byte) (*ShelleyBlock, error) { var shelleyBlock ShelleyBlock if _, err := cbor.Decode(data, &shelleyBlock); err != nil { - return nil, fmt.Errorf("Shelley block decode error: %w", err) + return nil, fmt.Errorf("decode Shelley block error: %w", err) } return &shelleyBlock, nil } @@ -692,7 +692,7 @@ func NewShelleyBlockFromCbor(data []byte) (*ShelleyBlock, error) { func NewShelleyBlockHeaderFromCbor(data []byte) (*ShelleyBlockHeader, error) { var shelleyBlockHeader ShelleyBlockHeader if _, err := cbor.Decode(data, &shelleyBlockHeader); err != nil { - return nil, fmt.Errorf("Shelley block header decode error: %w", err) + return nil, fmt.Errorf("decode Shelley block header error: %w", err) } return &shelleyBlockHeader, nil } @@ -702,7 +702,7 @@ func NewShelleyTransactionBodyFromCbor( ) (*ShelleyTransactionBody, error) { var shelleyTx ShelleyTransactionBody if _, err := cbor.Decode(data, &shelleyTx); err != nil { - return nil, fmt.Errorf("Shelley transaction body decode error: %w", err) + return nil, fmt.Errorf("decode Shelley transaction body error: %w", err) } return &shelleyTx, nil } @@ -710,7 +710,7 @@ func NewShelleyTransactionBodyFromCbor( func NewShelleyTransactionFromCbor(data []byte) (*ShelleyTransaction, error) { var shelleyTx ShelleyTransaction if _, err := cbor.Decode(data, &shelleyTx); err != nil { - return nil, fmt.Errorf("Shelley transaction decode error: %w", err) + return nil, fmt.Errorf("decode Shelley transaction error: %w", err) } return &shelleyTx, nil } @@ -721,7 +721,7 @@ func NewShelleyTransactionOutputFromCbor( var shelleyTxOutput ShelleyTransactionOutput if _, err := cbor.Decode(data, &shelleyTxOutput); err != nil { return nil, fmt.Errorf( - "Shelley transaction output decode error: %w", + "decode Shelley transaction output error: %w", err, ) } diff --git a/ledger/tx.go b/ledger/tx.go index 93b5fe1b..35a4aa83 100644 --- a/ledger/tx.go +++ b/ledger/tx.go @@ -56,7 +56,7 @@ func NewTransactionBodyFromCbor( ) (TransactionBody, error) { switch txType { case TxTypeByron: - return nil, errors.New("Byron transactions do not contain a body") + return nil, errors.New("no body for Byron transactions") case TxTypeShelley: return NewShelleyTransactionBodyFromCbor(data) case TxTypeAllegra: diff --git a/ledger/verify_block.go b/ledger/verify_block.go index c5d349be..bcb258f6 100644 --- a/ledger/verify_block.go +++ b/ledger/verify_block.go @@ -27,6 +27,7 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" ) +//nolint:staticcheck func VerifyBlock(block BlockHexCbor) (error, bool, string, uint64, uint64) { headerCborHex := block.HeaderCbor epochNonceHex := block.Eta0 diff --git a/protocol/blockfetch/client.go b/protocol/blockfetch/client.go index 113246d6..23f1091a 100644 --- a/protocol/blockfetch/client.go +++ b/protocol/blockfetch/client.go @@ -93,7 +93,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() close(c.blockChan) close(c.startBatchResultChan) }() @@ -141,7 +141,7 @@ func (c *Client) GetBlockRange(start common.Point, end common.Point) error { err, ok := <-c.startBatchResultChan if !ok { c.busyMutex.Unlock() - return protocol.ProtocolShuttingDownError + return protocol.ErrProtocolShuttingDown } if err != nil { c.busyMutex.Unlock() @@ -171,7 +171,7 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) { err, ok := <-c.startBatchResultChan if !ok { c.busyMutex.Unlock() - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown } if err != nil { c.busyMutex.Unlock() @@ -180,7 +180,7 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) { block, ok := <-c.blockChan if !ok { c.busyMutex.Unlock() - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown } return block, nil } @@ -216,8 +216,8 @@ func (c *Client) handleStartBatch() error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } c.startBatchResultChan <- nil @@ -234,8 +234,8 @@ func (c *Client) handleNoBlocks() error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } err := errors.New("block(s) not found") @@ -270,8 +270,8 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error { } // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } // We use the callback when requesting ranges and the internal channel for a single block diff --git a/protocol/blockfetch/server.go b/protocol/blockfetch/server.go index c33df4ec..e51f3d0e 100644 --- a/protocol/blockfetch/server.go +++ b/protocol/blockfetch/server.go @@ -167,8 +167,8 @@ func (s *Server) handleClientDone() error { "connection_id", s.callbackContext.ConnectionId.String(), ) // Restart protocol - s.Protocol.Stop() + s.Stop() s.initProtocol() - s.Protocol.Start() + s.Start() return nil } diff --git a/protocol/chainsync/client.go b/protocol/chainsync/client.go index 01d0e1f7..b5c24acb 100644 --- a/protocol/chainsync/client.go +++ b/protocol/chainsync/client.go @@ -129,7 +129,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() close(c.readyForNextBlockChan) }() }) @@ -184,7 +184,7 @@ func (c *Client) GetCurrentTip() (*Tip, error) { return } select { - case <-c.Protocol.DoneChan(): + case <-c.DoneChan(): case tip := <-currentTipChan: requestResultChan <- tip } @@ -195,9 +195,9 @@ func (c *Client) GetCurrentTip() (*Tip, error) { for { select { - case <-c.Protocol.DoneChan(): + case <-c.DoneChan(): done.Store(true) - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown case waitingForCurrentTipChan <- waitingResultChan: // The request is being handled by another request, wait for the result. waitingForCurrentTipChan = nil @@ -321,7 +321,7 @@ func (c *Client) GetAvailableBlockRange( for { select { case <-c.DoneChan(): - return start, end, protocol.ProtocolShuttingDownError + return start, end, protocol.ErrProtocolShuttingDown case tip := <-currentTipChan: currentTipChan = nil end = tip.Point @@ -414,8 +414,8 @@ func (c *Client) Sync(intersectPoints []common.Point) error { return err } select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown case result := <-intersectResultChan: if result.error != nil { return result.error @@ -491,7 +491,7 @@ func (c *Client) wantCurrentTip() (<-chan Tip, func()) { ch := make(chan Tip, 1) select { - case <-c.Protocol.DoneChan(): + case <-c.DoneChan(): return nil, func() {} case c.wantCurrentTipChan <- ch: return ch, func() { @@ -509,7 +509,7 @@ func (c *Client) wantFirstBlock() (<-chan clientPointResult, func()) { ch := make(chan clientPointResult, 1) select { - case <-c.Protocol.DoneChan(): + case <-c.DoneChan(): return nil, func() {} case c.wantFirstBlockChan <- ch: return ch, func() { @@ -527,7 +527,7 @@ func (c *Client) wantIntersectFound() (<-chan clientPointResult, func()) { ch := make(chan clientPointResult, 1) select { - case <-c.Protocol.DoneChan(): + case <-c.DoneChan(): return nil, func() {} case c.wantIntersectFoundChan <- ch: return ch, func() { @@ -550,8 +550,8 @@ func (c *Client) requestFindIntersect( } select { - case <-c.Protocol.DoneChan(): - return clientPointResult{error: protocol.ProtocolShuttingDownError} + case <-c.DoneChan(): + return clientPointResult{error: protocol.ErrProtocolShuttingDown} case result := <-resultChan: return result } @@ -716,7 +716,7 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error { } } if callbackErr != nil { - if errors.Is(callbackErr, StopSyncProcessError) { + if errors.Is(callbackErr, ErrStopSyncProcess) { // Signal that we're cancelling the sync c.readyForNextBlockChan <- false return nil @@ -747,7 +747,7 @@ func (c *Client) handleRollBackward(msg protocol.Message) error { } // Call the user callback function if callbackErr := c.config.RollBackwardFunc(c.callbackContext, msgRollBackward.Point, msgRollBackward.Tip); callbackErr != nil { - if errors.Is(callbackErr, StopSyncProcessError) { + if errors.Is(callbackErr, ErrStopSyncProcess) { // Signal that we're cancelling the sync c.readyForNextBlockChan <- false return nil @@ -793,7 +793,7 @@ func (c *Client) handleIntersectNotFound(msgGeneric protocol.Message) error { select { case ch := <-c.wantIntersectFoundChan: - ch <- clientPointResult{tip: msgIntersectNotFound.Tip, error: IntersectNotFoundError} + ch <- clientPointResult{tip: msgIntersectNotFound.Tip, error: ErrIntersectNotFound} default: } return nil diff --git a/protocol/chainsync/client_test.go b/protocol/chainsync/client_test.go index 309757b3..575ef920 100644 --- a/protocol/chainsync/client_test.go +++ b/protocol/chainsync/client_test.go @@ -128,11 +128,11 @@ func TestIntersectNotFound(t *testing.T) { if err == nil { t.Fatalf("did not receive expected error") } - if !errors.Is(err, chainsync.IntersectNotFoundError) { + if !errors.Is(err, chainsync.ErrIntersectNotFound) { t.Fatalf( "did not receive expected error\n got: %s\n wanted: %s", err, - chainsync.IntersectNotFoundError, + chainsync.ErrIntersectNotFound, ) } }, diff --git a/protocol/chainsync/error.go b/protocol/chainsync/error.go index 88ba1a82..c7709c2d 100644 --- a/protocol/chainsync/error.go +++ b/protocol/chainsync/error.go @@ -16,8 +16,8 @@ package chainsync import "errors" -var IntersectNotFoundError = errors.New("chain intersection not found") +var ErrIntersectNotFound = errors.New("chain intersection not found") // StopChainSync is used as a special return value from a RollForward or RollBackward handler function // to signify that the sync process should be stopped -var StopSyncProcessError = errors.New("stop sync process") +var ErrStopSyncProcess = errors.New("stop sync process") diff --git a/protocol/chainsync/server.go b/protocol/chainsync/server.go index b04f796f..39f36944 100644 --- a/protocol/chainsync/server.go +++ b/protocol/chainsync/server.go @@ -210,7 +210,7 @@ func (s *Server) handleFindIntersect(msg protocol.Message) error { msgFindIntersect.Points, ) if err != nil { - if errors.Is(err, IntersectNotFoundError) { + if errors.Is(err, ErrIntersectNotFound) { msgResp := NewMsgIntersectNotFound(tip) if err := s.SendMessage(msgResp); err != nil { return err @@ -235,8 +235,8 @@ func (s *Server) handleDone() error { "connection_id", s.callbackContext.ConnectionId.String(), ) // Restart protocol - s.Protocol.Stop() + s.Stop() s.initProtocol() - s.Protocol.Start() + s.Start() return nil } diff --git a/protocol/error.go b/protocol/error.go index 08a0606e..47417f73 100644 --- a/protocol/error.go +++ b/protocol/error.go @@ -16,4 +16,4 @@ package protocol import "errors" -var ProtocolShuttingDownError = errors.New("protocol is shutting down") +var ErrProtocolShuttingDown = errors.New("protocol is shutting down") diff --git a/protocol/keepalive/client.go b/protocol/keepalive/client.go index 65898f86..6446c104 100644 --- a/protocol/keepalive/client.go +++ b/protocol/keepalive/client.go @@ -78,7 +78,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() // Stop any existing timer c.timerMutex.Lock() if c.timer != nil { diff --git a/protocol/localstatequery/client.go b/protocol/localstatequery/client.go index 155bb582..dbbba9c4 100644 --- a/protocol/localstatequery/client.go +++ b/protocol/localstatequery/client.go @@ -106,7 +106,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() close(c.queryResultChan) close(c.acquireResultChan) }() @@ -877,8 +877,8 @@ func (c *Client) handleAcquired() error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } c.acquired = true @@ -897,8 +897,8 @@ func (c *Client) handleFailure(msg protocol.Message) error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } msgFailure := msg.(*MsgFailure) @@ -923,8 +923,8 @@ func (c *Client) handleResult(msg protocol.Message) error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } msgResult := msg.(*MsgResult) @@ -962,7 +962,7 @@ func (c *Client) acquire(acquireTarget AcquireTarget) error { } err, ok := <-c.acquireResultChan if !ok { - return protocol.ProtocolShuttingDownError + return protocol.ErrProtocolShuttingDown } return err } @@ -989,7 +989,7 @@ func (c *Client) runQuery(query interface{}, result interface{}) error { } resultCbor, ok := <-c.queryResultChan if !ok { - return protocol.ProtocolShuttingDownError + return protocol.ErrProtocolShuttingDown } if _, err := cbor.Decode(resultCbor, result); err != nil { return err diff --git a/protocol/localtxmonitor/client.go b/protocol/localtxmonitor/client.go index abfa5b90..f119dd97 100644 --- a/protocol/localtxmonitor/client.go +++ b/protocol/localtxmonitor/client.go @@ -93,7 +93,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() close(c.acquireResultChan) close(c.hasTxResultChan) close(c.nextTxResultChan) @@ -172,7 +172,7 @@ func (c *Client) HasTx(txId []byte) (bool, error) { } result, ok := <-c.hasTxResultChan if !ok { - return false, protocol.ProtocolShuttingDownError + return false, protocol.ErrProtocolShuttingDown } return result, nil } @@ -199,7 +199,7 @@ func (c *Client) NextTx() ([]byte, error) { } tx, ok := <-c.nextTxResultChan if !ok { - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown } return tx, nil } @@ -226,7 +226,7 @@ func (c *Client) GetSizes() (uint32, uint32, uint32, error) { } result, ok := <-c.getSizesResultChan if !ok { - return 0, 0, 0, protocol.ProtocolShuttingDownError + return 0, 0, 0, protocol.ErrProtocolShuttingDown } return result.Capacity, result.Size, result.NumberOfTxs, nil } @@ -314,7 +314,7 @@ func (c *Client) acquire() error { // Wait for reply _, ok := <-c.acquireResultChan if !ok { - return protocol.ProtocolShuttingDownError + return protocol.ErrProtocolShuttingDown } return nil } diff --git a/protocol/localtxsubmission/client.go b/protocol/localtxsubmission/client.go index f2c02fb2..be32008a 100644 --- a/protocol/localtxsubmission/client.go +++ b/protocol/localtxsubmission/client.go @@ -82,7 +82,7 @@ func (c *Client) Start() { c.Protocol.Start() // Start goroutine to cleanup resources on protocol shutdown go func() { - <-c.Protocol.DoneChan() + <-c.DoneChan() close(c.submitResultChan) }() }) @@ -125,7 +125,7 @@ func (c *Client) SubmitTx(eraId uint16, tx []byte) error { } err, ok := <-c.submitResultChan if !ok { - return protocol.ProtocolShuttingDownError + return protocol.ErrProtocolShuttingDown } return err } @@ -157,8 +157,8 @@ func (c *Client) handleAcceptTx() error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } c.submitResultChan <- nil @@ -175,8 +175,8 @@ func (c *Client) handleRejectTx(msg protocol.Message) error { ) // Check for shutdown select { - case <-c.Protocol.DoneChan(): - return protocol.ProtocolShuttingDownError + case <-c.DoneChan(): + return protocol.ErrProtocolShuttingDown default: } msgRejectTx := msg.(*MsgRejectTx) diff --git a/protocol/peersharing/client.go b/protocol/peersharing/client.go index eb867361..9e5feb97 100644 --- a/protocol/peersharing/client.go +++ b/protocol/peersharing/client.go @@ -80,7 +80,7 @@ func (c *Client) GetPeers(amount uint8) ([]PeerAddress, error) { } peers, ok := <-c.sharePeersChan if !ok { - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown } return peers, nil } diff --git a/protocol/peersharing/messages.go b/protocol/peersharing/messages.go index 27cd0d62..8432e144 100644 --- a/protocol/peersharing/messages.go +++ b/protocol/peersharing/messages.go @@ -125,7 +125,8 @@ func (p *PeerAddress) UnmarshalCBOR(cborData []byte) error { if err != nil { return err } - if cborListLen == 8 { + switch cborListLen { + case 8: // V11-12 tmpPeer := struct { cbor.StructAsArray @@ -147,7 +148,7 @@ func (p *PeerAddress) UnmarshalCBOR(cborData []byte) error { binary.LittleEndian.PutUint32(p.IP[8:], tmpPeer.Address3) binary.LittleEndian.PutUint32(p.IP[12:], tmpPeer.Address4) p.Port = tmpPeer.Port - } else if cborListLen == 6 { + case 6: // V13+ tmpPeer := struct { cbor.StructAsArray @@ -167,11 +168,11 @@ func (p *PeerAddress) UnmarshalCBOR(cborData []byte) error { binary.LittleEndian.PutUint32(p.IP[8:], tmpPeer.Address3) binary.LittleEndian.PutUint32(p.IP[12:], tmpPeer.Address4) p.Port = tmpPeer.Port - } else { + default: return fmt.Errorf("invalid peer address length: %d", cborListLen) } default: - return fmt.Errorf("unknown peer type: %d\n", peerType) + return fmt.Errorf("unknown peer type: %d", peerType) } return nil } diff --git a/protocol/peersharing/server.go b/protocol/peersharing/server.go index 75ba4d65..713d3e09 100644 --- a/protocol/peersharing/server.go +++ b/protocol/peersharing/server.go @@ -115,8 +115,8 @@ func (s *Server) handleDone(msg protocol.Message) error { "connection_id", s.callbackContext.ConnectionId.String(), ) // Restart protocol - s.Protocol.Stop() + s.Stop() s.initProtocol() - s.Protocol.Start() + s.Start() return nil } diff --git a/protocol/txsubmission/server.go b/protocol/txsubmission/server.go index 7f319ca9..72abd45c 100644 --- a/protocol/txsubmission/server.go +++ b/protocol/txsubmission/server.go @@ -113,7 +113,7 @@ func (s *Server) RequestTxIds( // Wait for result select { case <-s.DoneChan(): - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown case txIds := <-s.requestTxIdsResultChan: // Update ack count for next call s.ackCount = len(txIds) @@ -146,7 +146,7 @@ func (s *Server) RequestTxs(txIds []TxId) ([]TxBody, error) { // Wait for result select { case <-s.DoneChan(): - return nil, protocol.ProtocolShuttingDownError + return nil, protocol.ErrProtocolShuttingDown case txs := <-s.requestTxsResultChan: return txs, nil } @@ -208,7 +208,7 @@ func (s *Server) handleDone() error { "connection_id", s.callbackContext.ConnectionId.String(), ) // Restart protocol - s.Protocol.Stop() + s.Stop() s.initProtocol() s.requestTxIdsResultChan = make(chan []TxIdAndSize) s.requestTxsResultChan = make(chan []TxBody)