apiactions/validation.go
package apiactions
import (
"net/http"
"github.com/lbryio/chainquery/auth"
"github.com/lbryio/chainquery/daemon/jobs"
"github.com/lbryio/lbry.go/v2/extras/api"
"github.com/lbryio/lbry.go/v2/extras/errors"
v "github.com/lbryio/ozzo-validation"
)
//SyncAddressBalance will synchronize the balances for all addresses in chainquery.
func SyncAddressBalance(r *http.Request) api.Response {
params := struct {
Key string
}{}
err := api.FormValues(r, ¶ms, []*v.FieldRules{
v.Field(¶ms.Key),
})
if err != nil {
return api.Response{Error: err, Status: http.StatusBadRequest}
}
if !auth.IsAuthorized(params.Key) {
return api.Response{Error: errors.Err("not authorized"), Status: http.StatusUnauthorized}
}
rowsAffected, err := jobs.SyncAddressBalances()
if err != nil {
return api.Response{Error: err}
}
return api.Response{Data: rowsAffected}
}
//SyncTransactionValue will synchronize the value of all transactions in chainquery.
func SyncTransactionValue(r *http.Request) api.Response {
params := struct {
Key string
}{}
err := api.FormValues(r, ¶ms, []*v.FieldRules{
v.Field(¶ms.Key),
})
if err != nil {
return api.Response{Error: err, Status: http.StatusBadRequest}
}
if !auth.IsAuthorized(params.Key) {
return api.Response{Error: errors.Err("not authorized"), Status: http.StatusUnauthorized}
}
rowsAffected, err := jobs.SyncTransactionValue()
if err != nil {
return api.Response{Error: err}
}
return api.Response{Data: rowsAffected}
}
// ValidateChainData validates a range of blocks ensure that the block,Txs, and the same number of outputs,inputs exist.
//If a difference in data is identified it will return an array identifying where there are differences.
func ValidateChainData(r *http.Request) api.Response {
params := struct {
From uint64
To *uint64
Key string
}{}
err := api.FormValues(r, ¶ms, []*v.FieldRules{
v.Field(¶ms.From, v.Required),
v.Field(¶ms.To),
v.Field(¶ms.Key),
})
if err != nil {
return api.Response{Error: err, Status: http.StatusBadRequest}
}
if !auth.IsAuthorized(params.Key) {
return api.Response{Error: errors.Err("not authorized"), Status: http.StatusUnauthorized}
}
var missing []jobs.BlockData
if params.To != nil {
missing, err = jobs.ValidateChainRange(¶ms.From, params.To)
} else {
missing, err = jobs.ValidateChainRange(¶ms.From, nil)
}
if err != nil {
return api.Response{Error: err}
}
return api.Response{Data: missing}
}