Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resurrect indexer endpoints #15

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions bin/v0.34.x/indexer/block/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
})
11 changes: 5 additions & 6 deletions bin/v0.34.x/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

63 changes: 1 addition & 62 deletions bin/v0.34.x/indexer/tx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
})
})
4 changes: 2 additions & 2 deletions bin/v0.34.x/indexer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,4 +22,4 @@ func CreateHandler(ich ClientHandler) ClientHandler {

func CreateRESTRoute(registerer RESTRouteRegisterer) RESTRouteRegisterer {
return registerer
}
}
5 changes: 4 additions & 1 deletion bin/v0.34.x/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down