From 4af3f656880705e877453a68f70859cbe6f30b66 Mon Sep 17 00:00:00 2001 From: Jesse Chung Date: Mon, 20 Dec 2021 20:52:15 +0900 Subject: [PATCH] fix: resurrect indexer endpoints --- bin/v0.34.x/indexer/block/client.go | 47 +-------------------- bin/v0.34.x/indexer/indexer.go | 11 +++-- bin/v0.34.x/indexer/tx/client.go | 63 +---------------------------- bin/v0.34.x/indexer/types.go | 4 +- bin/v0.34.x/sync.go | 5 ++- 5 files changed, 13 insertions(+), 117 deletions(-) diff --git a/bin/v0.34.x/indexer/block/client.go b/bin/v0.34.x/indexer/block/client.go index d5251b3..8c4ff64 100644 --- a/bin/v0.34.x/indexer/block/client.go +++ b/bin/v0.34.x/indexer/block/client.go @@ -4,11 +4,9 @@ import ( "encoding/json" "fmt" "github.com/gorilla/mux" - tmjson "github.com/tendermint/tendermint/libs/json" tmdb "github.com/tendermint/tm-db" "github.com/terra-money/mantlemint-provider-v0.34.x/config" "github.com/terra-money/mantlemint-provider-v0.34.x/indexer" - "io/ioutil" "net/http" "strconv" ) @@ -26,7 +24,7 @@ func blockByHeightHandler(indexerDB tmdb.DB, height string) (json.RawMessage, er return indexerDB.Get(getKey(uint64(heightInInt))) } -var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, postRouter *mux.Router, indexerDB tmdb.DB) { +var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, indexerDB tmdb.DB) { var mantlemintConfig = config.NewConfig() router.HandleFunc(EndpointGETBlocksHeight, func(writer http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) @@ -61,47 +59,4 @@ var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, postRou return } }).Methods("GET") - - postRouter.HandleFunc(EndpointPOSTBlock, func(writer http.ResponseWriter, request *http.Request) { - bz, err := ioutil.ReadAll(request.Body) - if err != nil { - http.Error(writer, "error reading body", 400) - return - } - record := BlockRecord{} - if err := tmjson.Unmarshal(bz, &record); err != nil { - http.Error(writer, "error unmarshaling block record", 400) - return - } - - // prevent rewrite - injectedHeight := record.Block.Height - data, err := indexerDB.Get(getKey(uint64(injectedHeight))) - if err != nil { - http.Error(writer, err.Error(), 400) - return - } - - if data != nil { - writer.WriteHeader(204) - writer.Write([]byte("already committed")) - return - } - - batch := indexerDB.NewBatch() - if err := IndexBlock(batch, record.Block, record.BlockID, nil); err != nil { - http.Error(writer, err.Error(), 400) - return - } - - if err := batch.WriteSync(); err != nil { - http.Error(writer, err.Error(), 400) - return - } - - if err := batch.Close(); err != nil { - http.Error(writer, err.Error(), 400) - return - } - }).Methods("POST") }) diff --git a/bin/v0.34.x/indexer/indexer.go b/bin/v0.34.x/indexer/indexer.go index 5631e37..ae4fe37 100644 --- a/bin/v0.34.x/indexer/indexer.go +++ b/bin/v0.34.x/indexer/indexer.go @@ -11,9 +11,9 @@ import ( ) type Indexer struct { - db tmdb.DB - indexerTags []string - indexers []IndexFunc + db tmdb.DB + indexerTags []string + indexers []IndexFunc } func NewIndexer(dbName, path string) (*Indexer, error) { @@ -58,7 +58,6 @@ func (idx *Indexer) Run(block *tm.Block, blockId *tm.BlockID, evc *mantlemint.Ev return nil } -func (idx *Indexer) RegisterRESTRoute(router *mux.Router, postRouter *mux.Router, registerer RESTRouteRegisterer) { - registerer(router, postRouter, idx.db) +func (idx *Indexer) RegisterRESTRoute(router *mux.Router, registerer RESTRouteRegisterer) { + registerer(router, idx.db) } - diff --git a/bin/v0.34.x/indexer/tx/client.go b/bin/v0.34.x/indexer/tx/client.go index d83edd9..09213c2 100644 --- a/bin/v0.34.x/indexer/tx/client.go +++ b/bin/v0.34.x/indexer/tx/client.go @@ -3,15 +3,9 @@ package tx import ( "fmt" "github.com/gorilla/mux" - "github.com/pkg/errors" - abci "github.com/tendermint/tendermint/abci/types" - tmjson "github.com/tendermint/tendermint/libs/json" - tm "github.com/tendermint/tendermint/types" tmdb "github.com/tendermint/tm-db" "github.com/terra-money/mantlemint-provider-v0.34.x/config" "github.com/terra-money/mantlemint-provider-v0.34.x/indexer" - "github.com/terra-money/mantlemint-provider-v0.34.x/mantlemint" - "io/ioutil" "net/http" "strconv" ) @@ -28,7 +22,7 @@ func txsByHeightHandler(indexerDB tmdb.DB, height string) ([]byte, error) { return indexerDB.Get(getByHeightKey(uint64(heightInInt))) } -var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, postRouter *mux.Router, indexerDB tmdb.DB) { +var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, indexerDB tmdb.DB) { var mantlemintConfig = config.NewConfig() router.HandleFunc("/index/tx/by_hash/{hash}", func(writer http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) @@ -86,59 +80,4 @@ var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, postRou return } }).Methods("GET") - - // expected input is from RPC - // { block, txRecords } - postRouter.HandleFunc("/index/txs", func(writer http.ResponseWriter, request *http.Request) { - body, err := request.GetBody() - if err != nil { - http.Error(writer, err.Error(), 400) - return - } - - bz, err := ioutil.ReadAll(body) - if err != nil { - http.Error(writer, err.Error(), 400) - return - } - - input := struct { - Block *tm.Block `json:"block"` - TxRecords []TxRecord `json:"tx_records"` - }{ - Block: nil, - TxRecords: make([]TxRecord, 0), - } - - if err := tmjson.Unmarshal(bz, &input); err != nil { - http.Error(writer, err.Error(), 400) - return - } - - batch := indexerDB.NewBatch() - evc := mantlemint.NewMantlemintEventCollector() - - for _, txRecord := range input.TxRecords { - deliverTx := abci.ResponseDeliverTx{} - if err := tmjson.Unmarshal(txRecord.TxResponse, &deliverTx); err != nil { - http.Error(writer, errors.Wrapf(err, "failed unmarshaling tx response").Error(), 400) - } - evc.ResponseDeliverTxs = append(evc.ResponseDeliverTxs, &deliverTx) - } - - if err := IndexTx(batch, input.Block, nil, evc); err != nil { - http.Error(writer, err.Error(), 400) - return - } - - if err := batch.WriteSync(); err != nil { - http.Error(writer, err.Error(), 400) - return - } - - if err := batch.Close(); err != nil { - http.Error(writer, err.Error(), 400) - return - } - }) }) diff --git a/bin/v0.34.x/indexer/types.go b/bin/v0.34.x/indexer/types.go index 20e28b4..114d989 100644 --- a/bin/v0.34.x/indexer/types.go +++ b/bin/v0.34.x/indexer/types.go @@ -10,7 +10,7 @@ import ( type IndexFunc func(indexerDB tmdb.Batch, block *tm.Block, blockId *tm.BlockID, evc *mantlemint.EventCollector) error type ClientHandler func(w http.ResponseWriter, r *http.Request) error -type RESTRouteRegisterer func(router *mux.Router, postRouter *mux.Router, indexerDB tmdb.DB) +type RESTRouteRegisterer func(router *mux.Router, indexerDB tmdb.DB) func CreateIndexer(idf IndexFunc) IndexFunc { return idf @@ -22,4 +22,4 @@ func CreateHandler(ich ClientHandler) ClientHandler { func CreateRESTRoute(registerer RESTRouteRegisterer) RESTRouteRegisterer { return registerer -} \ No newline at end of file +} diff --git a/bin/v0.34.x/sync.go b/bin/v0.34.x/sync.go index 3fbb55a..a994843 100644 --- a/bin/v0.34.x/sync.go +++ b/bin/v0.34.x/sync.go @@ -188,7 +188,10 @@ func main() { // callback for registering custom routers; primarily for indexers // default: noop, // todo: make this part injectable - func(router *mux.Router) {}, + func(router *mux.Router) { + indexerInstance.RegisterRESTRoute(router, tx.RegisterRESTRoute) + indexerInstance.RegisterRESTRoute(router, block.RegisterRESTRoute) + }, // inject flag checker for synced blockFeed.IsSynced,