Skip to content

Commit

Permalink
extend checks with txindex + estimate smart fee
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviaVir committed Dec 13, 2024
1 parent 5673f8b commit 10292a2
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 55 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Docker Compose Test

on: push

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
continue-on-error: false

- name: Set up Docker Compose
run: |
cd contrib/docker
docker-compose up -d
- name: Wait for service to be available
run: |
TIMEOUT=30
RETRY_INTERVAL=5
SUCCESS=false
for ((i=0;i<TIMEOUT;i+=RETRY_INTERVAL)); do
if curl -s --head http://localhost:8090 | grep "200 OK" > /dev/null; then
SUCCESS=true
break
fi
sleep $RETRY_INTERVAL
done
if [ "$SUCCESS" = false ]; then
echo "Service did not start successfully."
exit 1
fi
- name: Tear down Docker Compose
if: always()
run: |
docker-compose -f docker-compose.yml down
22 changes: 22 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Go Test

on: push

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: go test ./...
5 changes: 4 additions & 1 deletion contrib/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3'
services:
regtest_node:
image: blockstream/bitcoind:latest
Expand All @@ -9,6 +8,8 @@ services:
- "18443:18443"
entrypoint:
- "bash"
environment:
- TXINDEX_ENABLED=1
command:
- "-c"
- "/entrypoint.sh"
Expand All @@ -22,6 +23,8 @@ services:
- RPC_COOKIE_PATH=/shared/regtest/.cookie
- CACHE_EXPIRE_SECONDS=14
- PORT=8090
- TXINDEX_ENABLED=true
- FEE_ESTIMATION_ENABLED=false # not a thing in regtest
volumes:
- shared_cookie:/shared
depends_on:
Expand Down
9 changes: 5 additions & 4 deletions contrib/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Fail if anything fails!
set -e
BITCOIN_DIR=/root/.bitcoin
BITCOIND_ARGS="-regtest -datadir=$BITCOIN_DIR -fallbackfee=0.0002 -rpcallowip=0.0.0.0/0 -server=1 -rpcbind=0.0.0.0"
BITCOIND_ARGS="-regtest -datadir=$BITCOIN_DIR -fallbackfee=0.0002 -rpcallowip=0.0.0.0/0 -server=1 -rpcbind=0.0.0.0 -txindex=$TXINDEX_ENABLED"
BITCOINCLI_ARGS="-regtest -datadir=$BITCOIN_DIR"

# Start bitcoind pointing into a temporary directory
Expand All @@ -13,9 +13,10 @@ bitcoind $BITCOIND_ARGS &
# Wait for it to work!
while ! bitcoin-cli $BITCOINCLI_ARGS help > /dev/null 2>&1; do sleep 1; done

# Keep mining every time there's something in the mempool.
# Create a default wallet
bitcoin-cli $BITCOINCLI_ARGS createwallet "default" || true
bitcoin-cli $BITCOINCLI_ARGS loadwallet "default" || true

while sleep 5; do
if [ $(bitcoin-cli $BITCOINCLI_ARGS getrawmempool | wc -l) -gt 2 ]; then
bitcoin-cli $BITCOINCLI_ARGS -generate 1
fi
done
30 changes: 30 additions & 0 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

type BlockChainInfoGetter interface {
GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error)
GetIndexInfo() (*btcjson.GetIndexInfoResult, error)
EstimateSmartFee(int64, *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error)
}

func getBlockChainInfo(client BlockChainInfoGetter) (*float64, error) {
Expand All @@ -16,3 +18,31 @@ func getBlockChainInfo(client BlockChainInfoGetter) (*float64, error) {

return &info.VerificationProgress, nil
}

func getIndexInfo(client BlockChainInfoGetter) (*float64, error) {
info, err := client.GetIndexInfo()
if err != nil || info == nil {
return nil, err
}

var result float64
result = 0.0
if info.Synced {
result = 1.0
}
return &result, nil
}

func getFeeEstimation(client BlockChainInfoGetter) (*float64, error) {
info, err := client.EstimateSmartFee(1, nil)
if err != nil || info == nil {
return nil, err
}

var result float64
result = 0.0
if info.FeeRate != nil {
result = 1.0
}
return &result, nil
}
45 changes: 45 additions & 0 deletions getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func (m *MockClient) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, erro
return args.Get(0).(*btcjson.GetBlockChainInfoResult), args.Error(1)
}

func (m *MockClient) GetIndexInfo() (*btcjson.GetIndexInfoResult, error) {
args := m.Called()
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*btcjson.GetIndexInfoResult), args.Error(1)
}

func (m *MockClient) EstimateSmartFee(blocks int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) {
args := m.Called()
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*btcjson.EstimateSmartFeeResult), args.Error(1)
}

func TestGetBlockChainInfo(t *testing.T) {
log.SetOutput(nil)
mockClient := new(MockClient)
Expand All @@ -37,4 +53,33 @@ func TestGetBlockChainInfo(t *testing.T) {
result, err = getBlockChainInfo(mockClient)
assert.NotNil(t, err)
assert.Nil(t, result)

mockClient.On("GetIndexInfo").Return(&btcjson.GetIndexInfoResult{TxIndex: &btcjson.TxIndex{Synced: true, BestBlockHeight: 0}}, nil).Once()
result, err = getIndexInfo(mockClient)
assert.NotNil(t, result)
assert.Nil(t, err)
assert.Equal(t, 1.0, *result)

mockClient.On("GetIndexInfo").Return(nil, errors.New("Failed to fetch")).Once()
result, err = getIndexInfo(mockClient)
assert.NotNil(t, err)
assert.Nil(t, result)

fee := 0.04094321
mockClient.On("EstimateSmartFee").Return(&btcjson.EstimateSmartFeeResult{FeeRate: &fee}, nil).Once()
result, err = getFeeEstimation(mockClient)
assert.NotNil(t, result)
assert.Nil(t, err)
assert.Equal(t, 1.0, *result)

mockClient.On("EstimateSmartFee").Return(&btcjson.EstimateSmartFeeResult{}, nil).Once()
result, err = getFeeEstimation(mockClient)
assert.NotNil(t, result)
assert.Nil(t, err)
assert.Equal(t, 0.0, *result)

mockClient.On("EstimateSmartFee").Return(nil, errors.New("Failed to fetch")).Once()
result, err = getFeeEstimation(mockClient)
assert.NotNil(t, err)
assert.Nil(t, result)
}
22 changes: 13 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ module github.com/deviavir/bitcoind-healthcheck

go 1.21

// `go get github.com/deviavir/btcd@fffd8b2def3be6238c9bdfcf9bebdde1c41de312`
// to get and replace using a specific commit version
replace github.com/btcsuite/btcd => github.com/deviavir/btcd v0.0.0-20241213141921-f9d94bc301ba

require (
github.com/btcsuite/btcd v0.23.4
github.com/stretchr/testify v1.7.0
github.com/btcsuite/btcd v0.24.2
github.com/stretchr/testify v1.8.4
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect
github.com/btcsuite/btcd/btcutil v1.1.0 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 10292a2

Please sign in to comment.