gitlab.com/jokerrs1/Sia@v1.3.2/node/api/consensus.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/NebulousLabs/Sia/types"
     8  
     9  	"github.com/julienschmidt/httprouter"
    10  )
    11  
    12  // ConsensusGET contains general information about the consensus set, with tags
    13  // to support idiomatic json encodings.
    14  type ConsensusGET struct {
    15  	Synced       bool              `json:"synced"`
    16  	Height       types.BlockHeight `json:"height"`
    17  	CurrentBlock types.BlockID     `json:"currentblock"`
    18  	Target       types.Target      `json:"target"`
    19  	Difficulty   types.Currency    `json:"difficulty"`
    20  }
    21  
    22  // consensusHandler handles the API calls to /consensus.
    23  func (api *API) consensusHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    24  	cbid := api.cs.CurrentBlock().ID()
    25  	currentTarget, _ := api.cs.ChildTarget(cbid)
    26  	WriteJSON(w, ConsensusGET{
    27  		Synced:       api.cs.Synced(),
    28  		Height:       api.cs.Height(),
    29  		CurrentBlock: cbid,
    30  		Target:       currentTarget,
    31  		Difficulty:   currentTarget.Difficulty(),
    32  	})
    33  }
    34  
    35  // consensusValidateTransactionsetHandler handles the API calls to
    36  // /consensus/validate/transactionset.
    37  func (api *API) consensusValidateTransactionsetHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    38  	var txnset []types.Transaction
    39  	err := json.NewDecoder(req.Body).Decode(&txnset)
    40  	if err != nil {
    41  		WriteError(w, Error{"could not decode transaction set: " + err.Error()}, http.StatusBadRequest)
    42  		return
    43  	}
    44  	_, err = api.cs.TryTransactionSet(txnset)
    45  	if err != nil {
    46  		WriteError(w, Error{"transaction set validation failed: " + err.Error()}, http.StatusBadRequest)
    47  		return
    48  	}
    49  	WriteSuccess(w)
    50  }