|
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"time" |
|
|
|
"github.com/btcsuite/btcd/rpcclient" |
|
log "github.com/sirupsen/logrus" |
|
prefixed "github.com/x-cray/logrus-prefixed-formatter" |
|
) |
|
|
|
const PRE_HALVING_SUBSIDY = 1250000000 |
|
|
|
// GetWire initializes a an RPC client to communicate with bitcoind |
|
func GetWire(host string, user string, pass string, tls bool) *rpcclient.Client { |
|
connCfg := &rpcclient.ConnConfig{ |
|
Host: host, |
|
User: user, |
|
Pass: pass, |
|
HTTPPostMode: true, |
|
DisableTLS: !tls, |
|
} |
|
// The notification parameter is nil since notifications are not |
|
// supported in HTTP POST mode. |
|
client, err := rpcclient.New(connCfg, nil) |
|
if client == nil || err != nil { |
|
log.WithFields(log.Fields{ |
|
"host": host, |
|
"user": user, |
|
"TLS": tls, |
|
}).Fatal("Failed to initialize RPC client.") |
|
} |
|
|
|
info, err := client.GetBlockChainInfo() |
|
|
|
if info == nil || err != nil { |
|
log.WithFields(log.Fields{ |
|
"host": host, |
|
"user": user, |
|
"TLS": tls, |
|
}).Fatal("Failed to connect to RPC server.") |
|
} |
|
|
|
log.WithFields(log.Fields{ |
|
"chain": info.Chain, |
|
}).Info("RPC connection established") |
|
|
|
return client |
|
} |
|
|
|
func main() { |
|
log.SetFormatter(&prefixed.TextFormatter{ |
|
TimestampFormat: "2006/01/02 - 15:04:05", |
|
FullTimestamp: true, |
|
QuoteEmptyFields: true, |
|
SpacePadding: 30, |
|
}) |
|
|
|
wire := GetWire( |
|
os.Getenv("RPC_HOST"), |
|
os.Getenv("RPC_USER"), |
|
os.Getenv("RPC_PASSWORD"), |
|
os.Getenv("RPC_ENABLE_TLS") == "true", |
|
) |
|
defer wire.Shutdown() |
|
|
|
for true { |
|
info, err := wire.GetBlockChainInfo() |
|
if err != nil { |
|
log.Fatal("Could not execute RPC: getblockchaininfo") |
|
} |
|
stats, err := wire.GetBlockStats(info.BestBlockHash, &[]string{"subsidy"}) |
|
if err != nil { |
|
log.Fatal("Could not execute RPC: getblockstats") |
|
} |
|
|
|
remainingBlocks := 630000 - info.Blocks |
|
eta := remainingBlocks * 10 |
|
|
|
log.WithFields(log.Fields{ |
|
"height": info.Blocks, |
|
"subsidy": stats.Subsidy, |
|
"remainingBlocks": remainingBlocks, |
|
"ETA": fmt.Sprintf("%dh%dm", eta/60, eta%60), |
|
}).Info("Get current block") |
|
|
|
if stats.Subsidy == PRE_HALVING_SUBSIDY>>1 { |
|
log.WithFields(log.Fields{ |
|
"subsidy": stats.Subsidy, |
|
"blocks": info.Blocks, |
|
"bestblockhash": info.BestBlockHash, |
|
}).Info("Halving detected!") |
|
break |
|
} |
|
time.Sleep(5 * time.Second) |
|
} |
|
} |