Skip to content

Commit

Permalink
More graceful shutdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
galt-tr committed Feb 8, 2024
1 parent b42d6b1 commit 246666b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 33 deletions.
50 changes: 24 additions & 26 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
Open BSV License version 4
Open BSV License Version 5 – granted by BSV Association, authorised licensee

Copyright (c) 2023 BSV Blockchain Association ("BA")
For the purposes of this license, the definitions below have the following meanings:
“Bitcoin Protocol” means the protocol implementation, cryptographic rules, network protocols, and consensus mechanisms in the Bitcoin White Paper as described here https://protocol.bsvblockchain.org.
“Bitcoin White Paper” means the paper entitled ‘Bitcoin: A Peer-to-Peer Electronic Cash System’ published by ‘Satoshi Nakamoto’ in October 2008.
“BSV Blockchains” means:
(a) the Bitcoin blockchain containing block height #556767 with the hash "000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and that contains the longest honest persistent chain of blocks which has been produced in a manner which is consistent with the rules set forth in the Network Access Rules; and
(b) the test blockchains that contain the longest honest persistent chains of blocks which has been produced in a manner which is consistent with the rules set forth in the Network Access Rules.
“Network Access Rules” or “Rules” means the set of rules regulating the relationship between BSV Association and the nodes on BSV based on the Bitcoin Protocol rules and those set out in the Bitcoin White Paper, and available here https://bsvblockchain.org/network-access-rules.
“Software” means the software the subject of this licence, including any/all intellectual property rights therein and associated documentation files.
BSV Association grants permission, free of charge and on a non-exclusive and revocable basis, to any person obtaining a copy of the Software to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
1 - The text “© BSV Association,” and this license shall be included in all copies or substantial portions of the Software.
2 - The Software, and any software that is derived from the Software or parts thereof, must only be used on the BSV Blockchains.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES REGARDING ENTITLEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS THEREOF BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Version 0.1.1 of the Bitcoin SV software, and prior versions of software upon which it was based, were licensed under the MIT License, which is included below.
The MIT License (MIT)
Copyright (c) 2009-2010 Satoshi Nakamoto
Copyright (c) 2009-2015 Bitcoin Developers
Copyright (c) 2009-2017 The Bitcoin Core developers
Copyright (c) 2017 The Bitcoin ABC developers
Copyright (c) 2018 Bitcoin Association for BSV
Copyright (c) 2023 BSV Association

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

1 - The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2 - The Software, and any software that is derived from the Software or parts thereof,
can only be used on the Bitcoin SV blockchains. The Bitcoin SV blockchains are defined,
for purposes of this license, as the Bitcoin blockchain containing block height #556767
with the hash "000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and
that contains the longest persistent chain of blocks accepted by this Software and which are valid under the rules set forth in the Bitcoin white paper (S. Nakamoto, Bitcoin: A Peer-to-Peer Electronic Cash System, posted online October 2008) and the latest version of this Software available in this repository or another repository designated by BA,
as well as the test blockchains that contain the longest persistent chains of blocks accepted by this Software and which are valid under the rules set forth in the Bitcoin whitepaper (S. Nakamoto, Bitcoin: A Peer-to-Peer Electronic Cash System, posted online October 2008) and the latest version of this Software available in this repository, or another repository designated by BA.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions app/api/base/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package base

import (
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/bitcoin-sv/alert-system/app"
"github.com/bitcoin-sv/alert-system/app/models"
"github.com/bitcoin-sv/alert-system/app/models/model"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// alerts will return the saved
func (a *Action) alert(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Read params
params := apirouter.GetParams(req)
if params == nil {
apiError := apirouter.ErrorFromRequest(req, "parameters is nil", "no parameters specified", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}
idStr := params.GetString("sequence")
if idStr == "" {
apiError := apirouter.ErrorFromRequest(req, "missing sequence param", "missing sequence param", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}
sequenceNumber, err := strconv.Atoi(idStr)
if err != nil {
apiError := apirouter.ErrorFromRequest(req, "sequence is invalid", "sequence is invalid", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}

// Get alert
alertModel, err := models.GetAlertMessageBySequenceNumber(req.Context(), uint32(sequenceNumber), model.WithAllDependencies(a.Config))
if err != nil {
app.APIErrorResponse(w, req, http.StatusBadRequest, err)
return
} else if alertModel == nil {
app.APIErrorResponse(w, req, http.StatusNotFound, errors.New("alert not found"))
return
}

// Return the response
_ = apirouter.ReturnJSONEncode(
w,
http.StatusOK,
json.NewEncoder(w),
alertModel, []string{"sequence_number", "raw"})
}
3 changes: 3 additions & 0 deletions app/api/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ func RegisterRoutes(router *apirouter.Router, conf *config.Config) {

// Set the get alerts request
router.HTTPRouter.GET("/alerts", action.Request(router, action.alerts))

// Set the get alert request
router.HTTPRouter.GET("/alert/:sequence", action.Request(router, action.alert))
}
4 changes: 2 additions & 2 deletions app/p2p/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (s *Server) initDHT(ctx context.Context) (*dht.IpfsDHT, error) {
}

// Connect to the chosen ipfs nodes
var connected = false
connected := false
for !connected {
select {
case <-s.quitPeerInitializationChannel:
return nil, nil
return kademliaDHT, nil
default:
var wg sync.WaitGroup
for _, peerAddr := range peers {
Expand Down
12 changes: 7 additions & 5 deletions app/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (s *Server) RunAlertProcessingCron(ctx context.Context) chan bool {
}
case <-quit:
ticker.Stop()
return
}
}
}()
Expand Down Expand Up @@ -276,8 +277,8 @@ func (s *Server) RunPeerDiscovery(ctx context.Context, routingDiscovery *droutin
}
case <-quit:
ticker.Stop()
return
}

}
}()
return quit
Expand Down Expand Up @@ -377,10 +378,11 @@ func (s *Server) discoverPeers(ctx context.Context, routingDiscovery *drouting.R

// Sync the stream thread
t := StreamThread{
config: s.config,
ctx: ctx,
peer: foundPeer.ID,
stream: stream,
config: s.config,
ctx: ctx,
peer: foundPeer.ID,
stream: stream,
quitChannel: s.quitPeerDiscoveryChannel,
}

// Sync the stream thread
Expand Down
4 changes: 4 additions & 0 deletions app/p2p/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type StreamThread struct {
myLatestSequence uint32
peer peer.ID
stream network.Stream
quitChannel chan bool
}

// LatestSequence will return the threads latest sequence
Expand Down Expand Up @@ -83,6 +84,7 @@ func (s *StreamThread) ProcessSyncMessage(ctx context.Context) error {
}
s.config.Services.Log.Debugf("failed to read sync message: %s; closing stream", err.Error())
done <- s.stream.Close()
return
}

if len(b) == 0 {
Expand Down Expand Up @@ -144,6 +146,8 @@ func (s *StreamThread) ProcessSyncMessage(ctx context.Context) error {
}
}()
select {
case <-s.quitChannel:
return nil
case err := <-done:
return err
case <-time.After(time.Minute * 1):
Expand Down

0 comments on commit 246666b

Please sign in to comment.