From 03b155ecd93c58bdde7e6080c0bf6511b1561cb4 Mon Sep 17 00:00:00 2001 From: Wisdom Arerosuoghene Date: Sun, 22 Sep 2019 20:09:35 +0100 Subject: [PATCH] modify generateTicketPurchaseInfo to return error --- controllers/main.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/controllers/main.go b/controllers/main.go index 76fb282d..ffb0cc53 100644 --- a/controllers/main.go +++ b/controllers/main.go @@ -300,7 +300,8 @@ func (controller *MainController) APIAddress(c web.C, r *http.Request) ([]string return nil, codes.InvalidArgument, "address error", err } - if purchaseInfo := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr); purchaseInfo == nil { + if _, err := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr); err != nil { + log.Errorf("generateTicketPurchaseInfo: %v", err) return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands") } @@ -350,11 +351,13 @@ func (controller *MainController) APIPurchaseTicket(c web.C, r *http.Request) (* // load saved user info from db to get the user id user = models.GetUserByEmail(dbMap, userPubKeyAddr) - if purchaseInfo := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr); purchaseInfo != nil { - return purchaseInfo, codes.OK, "APIPurchaseTicket: purchaseinfo generated for userPubKeyAddr", nil + purchaseInfo, err := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr) + if err != nil { + log.Errorf("generateTicketPurchaseInfo: %v", err) + return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands") } - return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands") + return purchaseInfo, codes.OK, "APIPurchaseTicket: purchaseinfo generated for userPubKeyAddr", nil } // APIPurchaseInfo fetches and returns the user's info or an error @@ -741,58 +744,53 @@ func validateUserPubKeyAddr(pubKeyAddr string) (dcrutil.Address, error) { } func (controller *MainController) generateTicketPurchaseInfo(dbMap *gorp.DbMap, user *models.User, - userPubKeyAddr string) *poolapi.PurchaseInfo { + userPubKeyAddr string) (*poolapi.PurchaseInfo, error) { // Get the ticket address for this user pooladdress, err := controller.TicketAddressForUserID(int(user.Id)) if err != nil { - log.Errorf("unable to derive ticket address: %v", err) - return nil + return nil, fmt.Errorf("unable to derive ticket address: %v", err) } // From new address (pkh), get pubkey address poolValidateAddress, err := controller.Cfg.StakepooldServers.ValidateAddress(pooladdress) if err != nil { - log.Errorf("unable to validate address: %v", err) - return nil + return nil, fmt.Errorf("unable to validate address: %v", err) } if !poolValidateAddress.IsMine { - log.Errorf("unable to validate ismine for pool ticket address: %s", + return nil, fmt.Errorf("unable to validate ismine for pool ticket address: %s", pooladdress.String()) - return nil } poolPubKeyAddr := poolValidateAddress.PubKeyAddr // Get back Address from pool's new pubkey address if _, err = dcrutil.DecodeAddress(poolPubKeyAddr); err != nil { - return nil + return nil, fmt.Errorf("unable to decode pool address: %v", err) } // Create the the multisig script. Result includes a P2SH and redeem script. createMultiSig, err := controller.Cfg.StakepooldServers.CreateMultisig([]string{poolPubKeyAddr, userPubKeyAddr}) if err != nil { - return nil + return nil, fmt.Errorf("create multisig error: %v", err) } // Serialize the redeem script (hex string -> []byte) serializedScript, err := hex.DecodeString(createMultiSig.RedeemScript) if err != nil { - return nil + return nil, fmt.Errorf("unable to serialize redeem script hex: %v", err) } // Import the redeem script importedHeight, err := controller.Cfg.StakepooldServers.ImportNewScript(serializedScript) if err != nil { - log.Warnf("unexpected error importing multisig redeem script: %v", err) - return nil + return nil, fmt.Errorf("unexpected error importing multisig redeem script: %v", err) } // Get the pool fees address for this user userFeeAddr, err := controller.FeeAddressForUserID(int(user.Id)) if err != nil { - log.Errorf("unexpected error deriving fee addr: %s", err.Error()) - return nil + return nil, fmt.Errorf("unexpected error deriving fee addr: %s", err.Error()) } // Update the user's DB entry with multisig, user and pool pubkey @@ -813,7 +811,7 @@ func (controller *MainController) generateTicketPurchaseInfo(dbMap *gorp.DbMap, Script: createMultiSig.RedeemScript, TicketAddress: createMultiSig.Address, VoteBits: uint16(user.VoteBits), - } + }, nil } // AddressPost is address form submit route. @@ -844,7 +842,8 @@ func (controller *MainController) AddressPost(c web.C, r *http.Request) (string, return controller.Address(c, r) } - if purchaseInfo := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr); purchaseInfo == nil { + if _, err := controller.generateTicketPurchaseInfo(dbMap, user, userPubKeyAddr); err != nil { + log.Errorf("generateTicketPurchaseInfo: %v", err) return "/error", http.StatusSeeOther }