Skip to content

Commit

Permalink
Add Unjail Validator endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lazynina committed Jan 29, 2024
1 parent a5e3c15 commit d93feec
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,13 @@ func (fes *APIServer) NewRouter() *muxtrace.Router {
fes.UnregisterAsValidator,
PublicAccess,
},
{
"UnjailValidator",
[]string{"POST", "OPTIONS"},
RoutePathValidators + "/unjail",
fes.UnjailValidator,
PublicAccess,
},
{
"GetValidatorByPublicKeyBase58Check",
[]string{"GET"},
Expand Down
81 changes: 81 additions & 0 deletions routes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ type UnregisterAsValidatorRequest struct {
TransactionFees []TransactionFee `safeForLogging:"true"`
}

type UnjailValidatorRequest struct {
TransactorPublicKeyBase58Check string `safeForLogging:"true"`
ExtraData map[string]string `safeForLogging:"true"`
MinFeeRateNanosPerKB uint64 `safeForLogging:"true"`
TransactionFees []TransactionFee `safeForLogging:"true"`
}

type ValidatorTxnResponse struct {
SpendAmountNanos uint64
TotalInputNanos uint64
Expand Down Expand Up @@ -229,6 +236,80 @@ func (fes *APIServer) UnregisterAsValidator(ww http.ResponseWriter, req *http.Re
}
}

func (fes *APIServer) UnjailValidator(ww http.ResponseWriter, req *http.Request) {
// Decode request body.
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := UnjailValidatorRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, "UnjailValidator: problem parsing request body")
return
}

// Convert TransactorPublicKeyBase58Check to TransactorPublicKeyBytes.
if requestData.TransactorPublicKeyBase58Check == "" {
_AddBadRequestError(ww, "UnjailValidator: must provide a TransactorPublicKeyBase58Check")
return
}
transactorPublicKeyBytes, err := GetPubKeyBytesFromBase58Check(requestData.TransactorPublicKeyBase58Check)
if err != nil {
_AddInternalServerError(ww, "UnjailValidator: problem getting public key for the transactor")
return
}

// Parse ExtraData.
extraData, err := EncodeExtraDataMap(requestData.ExtraData)
if err != nil {
_AddBadRequestError(ww, "UnjailValidator: invalid ExtraData provided")
return
}

// Compute the additional transaction fees as specified
// by the request body and the node-level fees.
additionalOutputs, err := fes.getTransactionFee(
lib.TxnTypeUnjailValidator,
transactorPublicKeyBytes,
requestData.TransactionFees,
)
if err != nil {
_AddBadRequestError(ww, "UnjailValidator: specified TransactionFees are invalid")
return
}

// Create transaction.
txn, totalInput, changeAmount, fees, err := fes.blockchain.CreateUnjailValidatorTxn(
transactorPublicKeyBytes,
&lib.UnjailValidatorMetadata{},
extraData,
requestData.MinFeeRateNanosPerKB,
fes.backendServer.GetMempool(),
additionalOutputs,
)
if err != nil {
_AddInternalServerError(ww, fmt.Sprintf("UnjailValidator: problem creating txn: %v", err))
return
}

// Construct response.
txnBytes, err := txn.ToBytes(true)
if err != nil {
_AddInternalServerError(ww, "UnjailValidator: problem encoding txn to bytes")
return
}
res := ValidatorTxnResponse{
SpendAmountNanos: totalInput - changeAmount - fees,
TotalInputNanos: totalInput,
ChangeAmountNanos: changeAmount,
FeeNanos: fees,
Transaction: txn,
TransactionHex: hex.EncodeToString(txnBytes),
TxnHashHex: txn.Hash().String(),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, "UnjailValidator: problem encoding response as JSON")
return
}
}

func (fes *APIServer) GetValidatorByPublicKeyBase58Check(ww http.ResponseWriter, req *http.Request) {
// Parse ValidatorPublicKeyBase58Check from URL.
vars := mux.Vars(req)
Expand Down

0 comments on commit d93feec

Please sign in to comment.