Add context to RPC wrapper (#53)
This commit is contained in:
parent
869b68fad5
commit
68e3fca59c
10
main.go
10
main.go
@ -102,7 +102,7 @@ func run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Ensure the wallet account for collecting fees exists and matches config.
|
||||
err = setupFeeAccount(ctx, feeWalletClient, cfg.FeeXPub)
|
||||
err = setupFeeAccount(feeWalletClient, cfg.FeeXPub)
|
||||
if err != nil {
|
||||
log.Errorf("Fee account error: %v", err)
|
||||
requestShutdown()
|
||||
@ -133,16 +133,16 @@ func run(ctx context.Context) error {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func setupFeeAccount(ctx context.Context, walletClient *rpc.FeeWalletRPC, feeXpub string) error {
|
||||
func setupFeeAccount(walletClient *rpc.FeeWalletRPC, feeXpub string) error {
|
||||
// Check if account for fee collection already exists.
|
||||
accounts, err := walletClient.ListAccounts(ctx)
|
||||
accounts, err := walletClient.ListAccounts()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ListAccounts error: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := accounts[feeAccountName]; ok {
|
||||
// Account already exists. Check xpub matches xpub from config.
|
||||
existingXPub, err := walletClient.GetMasterPubKey(ctx, feeAccountName)
|
||||
existingXPub, err := walletClient.GetMasterPubKey(feeAccountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetMasterPubKey error: %v", err)
|
||||
}
|
||||
@ -155,7 +155,7 @@ func setupFeeAccount(ctx context.Context, walletClient *rpc.FeeWalletRPC, feeXpu
|
||||
|
||||
} else {
|
||||
// Account does not exist. Create it using xpub from config.
|
||||
if err = walletClient.ImportXPub(ctx, feeAccountName, feeXpub); err != nil {
|
||||
if err = walletClient.ImportXPub(feeAccountName, feeXpub); err != nil {
|
||||
log.Errorf("ImportXPub error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ const (
|
||||
// of JSON encoding.
|
||||
type FeeWalletRPC struct {
|
||||
Caller
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// FeeWalletClient creates a new WalletRPC client instance from a caller.
|
||||
@ -48,64 +49,64 @@ func FeeWalletClient(ctx context.Context, c Caller) (*FeeWalletRPC, error) {
|
||||
|
||||
// TODO: Ensure correct network.
|
||||
|
||||
return &FeeWalletRPC{c}, nil
|
||||
return &FeeWalletRPC{c, ctx}, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) ImportXPub(ctx context.Context, account, xpub string) error {
|
||||
return c.Call(ctx, "importxpub", nil, account, xpub)
|
||||
func (c *FeeWalletRPC) ImportXPub(account, xpub string) error {
|
||||
return c.Call(c.ctx, "importxpub", nil, account, xpub)
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) GetMasterPubKey(ctx context.Context, account string) (string, error) {
|
||||
func (c *FeeWalletRPC) GetMasterPubKey(account string) (string, error) {
|
||||
var pubKey string
|
||||
err := c.Call(ctx, "getmasterpubkey", &pubKey, account)
|
||||
err := c.Call(c.ctx, "getmasterpubkey", &pubKey, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pubKey, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) ListAccounts(ctx context.Context) (map[string]float64, error) {
|
||||
func (c *FeeWalletRPC) ListAccounts() (map[string]float64, error) {
|
||||
var accounts map[string]float64
|
||||
err := c.Call(ctx, "listaccounts", &accounts)
|
||||
err := c.Call(c.ctx, "listaccounts", &accounts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) GetNewAddress(ctx context.Context, account string) (string, error) {
|
||||
func (c *FeeWalletRPC) GetNewAddress(account string) (string, error) {
|
||||
var newAddress string
|
||||
err := c.Call(ctx, "getnewaddress", &newAddress, account)
|
||||
err := c.Call(c.ctx, "getnewaddress", &newAddress, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return newAddress, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) GetBlockHeader(ctx context.Context, blockHash string) (*dcrdtypes.GetBlockHeaderVerboseResult, error) {
|
||||
func (c *FeeWalletRPC) GetBlockHeader(blockHash string) (*dcrdtypes.GetBlockHeaderVerboseResult, error) {
|
||||
verbose := true
|
||||
var blockHeader dcrdtypes.GetBlockHeaderVerboseResult
|
||||
err := c.Call(ctx, "getblockheader", &blockHeader, blockHash, verbose)
|
||||
err := c.Call(c.ctx, "getblockheader", &blockHeader, blockHash, verbose)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &blockHeader, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) GetRawTransaction(ctx context.Context, txHash string) (*dcrdtypes.TxRawResult, error) {
|
||||
func (c *FeeWalletRPC) GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, error) {
|
||||
verbose := 1
|
||||
var resp dcrdtypes.TxRawResult
|
||||
err := c.Call(ctx, "getrawtransaction", &resp, txHash, verbose)
|
||||
err := c.Call(c.ctx, "getrawtransaction", &resp, txHash, verbose)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c *FeeWalletRPC) SendRawTransaction(ctx context.Context, txHex string) (string, error) {
|
||||
func (c *FeeWalletRPC) SendRawTransaction(txHex string) (string, error) {
|
||||
allowHighFees := false
|
||||
var txHash string
|
||||
err := c.Call(ctx, "sendrawtransaction", &txHash, txHex, allowHighFees)
|
||||
err := c.Call(c.ctx, "sendrawtransaction", &txHash, txHex, allowHighFees)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ const (
|
||||
// of JSON encoding.
|
||||
type VotingWalletRPC struct {
|
||||
Caller
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// VotingWalletClient creates a new VotingWalletRPC client instance from a caller.
|
||||
@ -54,20 +55,20 @@ func VotingWalletClient(ctx context.Context, c Caller) (*VotingWalletRPC, error)
|
||||
|
||||
// TODO: Ensure correct network.
|
||||
|
||||
return &VotingWalletRPC{c}, nil
|
||||
return &VotingWalletRPC{c, ctx}, nil
|
||||
}
|
||||
|
||||
func (c *VotingWalletRPC) AddTransaction(ctx context.Context, blockHash, txHex string) error {
|
||||
return c.Call(ctx, "addtransaction", nil, blockHash, txHex)
|
||||
func (c *VotingWalletRPC) AddTransaction(blockHash, txHex string) error {
|
||||
return c.Call(c.ctx, "addtransaction", nil, blockHash, txHex)
|
||||
}
|
||||
|
||||
func (c *VotingWalletRPC) ImportPrivKey(ctx context.Context, votingWIF string) error {
|
||||
func (c *VotingWalletRPC) ImportPrivKey(votingWIF string) error {
|
||||
label := "imported"
|
||||
rescan := false
|
||||
scanFrom := 0
|
||||
return c.Call(ctx, "importprivkey", nil, votingWIF, label, rescan, scanFrom)
|
||||
return c.Call(c.ctx, "importprivkey", nil, votingWIF, label, rescan, scanFrom)
|
||||
}
|
||||
|
||||
func (c *VotingWalletRPC) SetVoteChoice(ctx context.Context, agenda, choice, ticketHash string) error {
|
||||
return c.Call(ctx, "setvotechoice", nil, agenda, choice, ticketHash)
|
||||
func (c *VotingWalletRPC) SetVoteChoice(agenda, choice, ticketHash string) error {
|
||||
return c.Call(c.ctx, "setvotechoice", nil, agenda, choice, ticketHash)
|
||||
}
|
||||
|
||||
@ -96,7 +96,7 @@ func feeAddress(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := fWalletClient.GetRawTransaction(ctx, txHash.String())
|
||||
resp, err := fWalletClient.GetRawTransaction(txHash.String())
|
||||
if err != nil {
|
||||
log.Warnf("Could not retrieve tx %s for %s: %v", txHash, c.ClientIP(), err)
|
||||
sendErrorResponse("unknown transaction", http.StatusBadRequest, c)
|
||||
@ -157,7 +157,7 @@ func feeAddress(c *gin.Context) {
|
||||
// get blockheight and sdiff which is required by
|
||||
// txrules.StakePoolTicketFee, and store them in the database
|
||||
// for processing by payfee
|
||||
blockHeader, err := fWalletClient.GetBlockHeader(ctx, resp.BlockHash)
|
||||
blockHeader, err := fWalletClient.GetBlockHeader(resp.BlockHash)
|
||||
if err != nil {
|
||||
log.Errorf("GetBlockHeader error: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
@ -165,7 +165,7 @@ func feeAddress(c *gin.Context) {
|
||||
}
|
||||
|
||||
// TODO: Generate this within dcrvsp without an RPC call?
|
||||
newAddress, err := fWalletClient.GetNewAddress(ctx, cfg.FeeAccountName)
|
||||
newAddress, err := fWalletClient.GetNewAddress(cfg.FeeAccountName)
|
||||
if err != nil {
|
||||
log.Errorf("GetNewAddress error: %v", err)
|
||||
sendErrorResponse("unable to generate fee address", http.StatusInternalServerError, c)
|
||||
|
||||
@ -146,7 +146,7 @@ findAddress:
|
||||
return
|
||||
}
|
||||
|
||||
rawTicket, err := fWalletClient.GetRawTransaction(ctx, ticketHash.String())
|
||||
rawTicket, err := fWalletClient.GetRawTransaction(ticketHash.String())
|
||||
if err != nil {
|
||||
log.Warnf("Could not retrieve tx %s for %s: %v", ticketHash.String(), c.ClientIP(), err)
|
||||
sendErrorResponse("unknown transaction", http.StatusBadRequest, c)
|
||||
@ -166,14 +166,14 @@ findAddress:
|
||||
return
|
||||
}
|
||||
|
||||
err = vWalletClient.AddTransaction(ctx, rawTicket.BlockHash, rawTicket.Hex)
|
||||
err = vWalletClient.AddTransaction(rawTicket.BlockHash, rawTicket.Hex)
|
||||
if err != nil {
|
||||
log.Errorf("AddTransaction failed: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
return
|
||||
}
|
||||
|
||||
err = vWalletClient.ImportPrivKey(ctx, votingWIF.String())
|
||||
err = vWalletClient.ImportPrivKey(votingWIF.String())
|
||||
if err != nil {
|
||||
log.Errorf("ImportPrivKey failed: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
@ -182,7 +182,7 @@ findAddress:
|
||||
|
||||
// Update vote choices on voting wallets.
|
||||
for agenda, choice := range voteChoices {
|
||||
err = vWalletClient.SetVoteChoice(ctx, agenda, choice, ticket.Hash)
|
||||
err = vWalletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("SetVoteChoice failed: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
@ -199,7 +199,7 @@ findAddress:
|
||||
return
|
||||
}
|
||||
|
||||
sendTxHash, err := fWalletClient.SendRawTransaction(ctx, hex.EncodeToString(feeTxBuf.Bytes()))
|
||||
sendTxHash, err := fWalletClient.SendRawTransaction(hex.EncodeToString(feeTxBuf.Bytes()))
|
||||
if err != nil {
|
||||
log.Errorf("SendRawTransaction failed: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
|
||||
@ -79,7 +79,7 @@ func setVoteChoices(c *gin.Context) {
|
||||
|
||||
// Update vote choices on voting wallets.
|
||||
for agenda, choice := range voteChoices {
|
||||
err = vWalletClient.SetVoteChoice(ctx, agenda, choice, ticket.Hash)
|
||||
err = vWalletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("SetVoteChoice failed: %v", err)
|
||||
sendErrorResponse("dcrwallet RPC error", http.StatusInternalServerError, c)
|
||||
|
||||
@ -142,9 +142,9 @@ func sendJSONResponse(resp interface{}, c *gin.Context) {
|
||||
sig := ed25519.Sign(cfg.SignKey, dec)
|
||||
c.Writer.Header().Set("VSP-Signature", hex.EncodeToString(sig))
|
||||
|
||||
c.JSON(http.StatusOK, resp)
|
||||
c.AbortWithStatusJSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func sendErrorResponse(errMsg string, code int, c *gin.Context) {
|
||||
c.JSON(code, gin.H{"error": errMsg})
|
||||
c.AbortWithStatusJSON(code, gin.H{"error": errMsg})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user