Add apitypes to avoid parsing from/to DB

This commit is contained in:
Arnau B
2020-10-22 17:10:17 +02:00
parent 0e5aad4767
commit 7c8f380637
9 changed files with 538 additions and 2 deletions

View File

@@ -150,6 +150,15 @@ func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
return nil
}
// GetBatch return the batch with the given batchNum
func (hdb *HistoryDB) GetBatch(batchNum common.BatchNum) (HistoryBatch, error) {
var batch *common.Batch
return batch, meddler.QueryRow(
hdb.db, &batch,
"SELECT * FROM batch WHERE batch_num == $1;", batchNum,
)
}
// GetBatches retrieve batches from the DB, given a range of batch numbers defined by from and to
func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, error) {
var batches []*common.Batch

View File

@@ -6,6 +6,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/db"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree"
)
@@ -131,3 +132,24 @@ type HistoryCoordinator struct {
FirstItem int `meddler:"first_item"`
LastItem int `meddler:"last_item"`
}
// HistoryBatch is a representation of a batch with additional information
// required by the API, and extracted by joining block table
type HistoryBatch struct {
ItemID int `json:"itemId" meddler:"item_id"`
BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
CollectedFees map[common.TokenID]db.BigIntStr `json:"collectedFees" meddler:"fees_collected,json"`
TotalFeesUSD float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
StateRoot db.BigIntStr `json:"stateRoot" meddler:"state_root"`
NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
ExitRoot db.BigIntStr `json:"exitRoot" meddler:"exit_root"`
ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
SlotNum int64 `json:"slotNum" meddler:"slot_num"`
TotalItems int `json:"-" meddler:"total_items"`
FirstItem int `json:"-" meddler:"first_item"`
LastItem int `json:"-" meddler:"last_item"`
}

View File

@@ -16,7 +16,8 @@ CREATE TABLE coordinator (
);
CREATE TABLE batch (
batch_num BIGINT PRIMARY KEY,
item_id SERIAL PRIMARY KEY,
batch_num BIGINT NOT NULL,
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
fees_collected BYTEA NOT NULL,

View File

@@ -192,3 +192,11 @@ type Paginationer interface {
GetPagination() *Pagination
Len() int
}
// BigIntStr is used to Marshal *big.Int directly into strings
type BigIntStr big.Int
func (b BigIntStr) MarshalText() ([]byte, error) {
bigInt := big.Int(b)
return []byte((&bigInt).String()), nil
}