API Coordinator Refactor

This commit is contained in:
ToniRamirezM
2020-10-27 17:28:23 +01:00
parent e5d1143a7e
commit b521a77fc5
8 changed files with 175 additions and 166 deletions

View File

@@ -1060,17 +1060,17 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
return txn.Commit()
}
// GetCoordinator returns a coordinator by its bidderAddr
func (hdb *HistoryDB) GetCoordinator(bidderAddr ethCommon.Address) (*HistoryCoordinator, error) {
coordinator := &HistoryCoordinator{}
// GetCoordinatorAPI returns a coordinator by its bidderAddr
func (hdb *HistoryDB) GetCoordinatorAPI(bidderAddr ethCommon.Address) (*CoordinatorAPI, error) {
coordinator := &CoordinatorAPI{}
err := meddler.QueryRow(
hdb.db, coordinator, `SELECT * FROM coordinator WHERE bidder_addr = $1;`, bidderAddr,
)
return coordinator, err
}
// GetCoordinators returns a list of coordinators from the DB and pagination info
func (hdb *HistoryDB) GetCoordinators(fromItem, limit *uint, order string) ([]HistoryCoordinator, *db.Pagination, error) {
// GetCoordinatorsAPI returns a list of coordinators from the DB and pagination info
func (hdb *HistoryDB) GetCoordinatorsAPI(fromItem, limit *uint, order string) ([]CoordinatorAPI, *db.Pagination, error) {
var query string
var args []interface{}
queryStr := `SELECT coordinator.*,
@@ -1096,14 +1096,14 @@ func (hdb *HistoryDB) GetCoordinators(fromItem, limit *uint, order string) ([]Hi
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
query = hdb.db.Rebind(queryStr)
coordinators := []*HistoryCoordinator{}
coordinators := []*CoordinatorAPI{}
if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
return nil, nil, err
}
if len(coordinators) == 0 {
return nil, nil, sql.ErrNoRows
}
return db.SlicePtrsToSlice(coordinators).([]HistoryCoordinator), &db.Pagination{
return db.SlicePtrsToSlice(coordinators).([]CoordinatorAPI), &db.Pagination{
TotalItems: coordinators[0].TotalItems,
FirstItem: coordinators[0].FirstItem,
LastItem: coordinators[0].LastItem,

View File

@@ -120,17 +120,17 @@ type HistoryExit struct {
TokenUSDUpdate *time.Time `meddler:"usd_update"`
}
// HistoryCoordinator is a representation of a coordinator with additional information
// CoordinatorAPI is a representation of a coordinator with additional information
// required by the API
type HistoryCoordinator struct {
ItemID int `meddler:"item_id"`
Bidder ethCommon.Address `meddler:"bidder_addr"`
Forger ethCommon.Address `meddler:"forger_addr"`
EthBlockNum int64 `meddler:"eth_block_num"`
URL string `meddler:"url"`
TotalItems int `meddler:"total_items"`
FirstItem int `meddler:"first_item"`
LastItem int `meddler:"last_item"`
type CoordinatorAPI struct {
ItemID int `json:"itemId" meddler:"item_id"`
Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
URL string `json:"URL" meddler:"url"`
TotalItems int `json:"-" meddler:"total_items"`
FirstItem int `json:"-" meddler:"first_item"`
LastItem int `json:"-" meddler:"last_item"`
}
// BatchAPI is a representation of a batch with additional information
@@ -157,10 +157,10 @@ type BatchAPI struct {
// Network define status of the network
type Network struct {
LastBlock int64 `json:"lastBlock"`
LastBatch BatchAPI `json:"lastBatch"`
CurrentSlot int64 `json:"currentSlot"`
NextForgers []HistoryCoordinator `json:"nextForgers"`
LastBlock int64 `json:"lastBlock"`
LastBatch BatchAPI `json:"lastBatch"`
CurrentSlot int64 `json:"currentSlot"`
NextForgers []CoordinatorAPI `json:"nextForgers"`
}
// Metrics define metrics of the network