Skip to content

Commit

Permalink
feat: remove lazy sync (#22)
Browse files Browse the repository at this point in the history
* feat: remove lazy sync

* refactor: lazysync, better error handling

Co-authored-by: jess <[email protected]>
  • Loading branch information
Jeff Woo and kjessec authored Jan 14, 2022
1 parent 6054fe8 commit 6c78d9e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 193 deletions.
36 changes: 14 additions & 22 deletions bin/v0.34.x/indexer/block/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,48 @@ package block
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/http"
"strconv"

"github.com/gorilla/mux"
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"
"net/http"
"strconv"
)

var (
EndpointGETBlocksHeight = "/index/blocks/{height}"
EndpointPOSTBlock = "/index/block"
)

var (
ErrorInvalidHeight = func(height string) string { return fmt.Sprintf("invalid height %s", height) }
ErrorBlockNotFound = func(height string) string { return fmt.Sprintf("block %s not found... yet.", height) }
)

func blockByHeightHandler(indexerDB tmdb.DB, height string) (json.RawMessage, error) {
heightInInt, err := strconv.Atoi(height)
if err != nil {
return nil, fmt.Errorf("invalid height: %v", err)
return nil, errors.New(ErrorInvalidHeight(height))
}
return indexerDB.Get(getKey(uint64(heightInInt)))
}

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)
height, ok := vars["height"]
if !ok {
writer.WriteHeader(400)
writer.Write([]byte("invalid height"))
http.Error(writer, ErrorInvalidHeight(height), 400)
return
}

if block, err := blockByHeightHandler(indexerDB, height); err != nil {
writer.WriteHeader(400)
writer.Write([]byte(err.Error()))
http.Error(writer, indexer.ErrorInternal(err), 500)
return
} else if block == nil {
// block not seen;
heightInInt, err := strconv.Atoi(height)
if err != nil {
http.Error(writer, fmt.Errorf("invalid height: %v", err).Error(), 400)
return
}
if record, lazySyncErr := LazySync(int64(heightInInt), mantlemintConfig.RPCEndpoints[0], indexerDB); lazySyncErr != nil {
http.Error(writer, lazySyncErr.Error(), 400)
return
} else {
writer.WriteHeader(200)
writer.Write(record)
}
http.Error(writer, ErrorBlockNotFound(height), 400)
return
} else {
writer.WriteHeader(200)
writer.Write(block)
Expand Down
54 changes: 0 additions & 54 deletions bin/v0.34.x/indexer/block/lazy_sync.go

This file was deleted.

48 changes: 20 additions & 28 deletions bin/v0.34.x/indexer/tx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package tx

import (
"fmt"
"github.com/pkg/errors"
"net/http"
"strconv"

"github.com/gorilla/mux"
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"
"net/http"
"strconv"
)

var (
ErrorInvalidHeight = func(height string) string { return fmt.Sprintf("invalid height %s", height) }
ErrorTxsNotFound = func(height string) string { return fmt.Sprintf("txs at height %s not found... yet.", height) }
ErrorInvalidHash = func(hash string) string { return fmt.Sprintf("invalid hash %s", hash) }
ErrorTxNotFound = func(hash string) string { return fmt.Sprintf("tx (%s) not found... yet or forever.", hash) }
)

func txByHashHandler(indexerDB tmdb.DB, txHash string) ([]byte, error) {
Expand All @@ -17,23 +25,25 @@ func txByHashHandler(indexerDB tmdb.DB, txHash string) ([]byte, error) {
func txsByHeightHandler(indexerDB tmdb.DB, height string) ([]byte, error) {
heightInInt, err := strconv.Atoi(height)
if err != nil {
return nil, fmt.Errorf("invalid height: %v", err)
return nil, errors.New(ErrorInvalidHeight(height))
}
return indexerDB.Get(getByHeightKey(uint64(heightInInt)))
}

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)
hash, ok := vars["hash"]
if !ok {
http.Error(writer, "txn not found", 400)
http.Error(writer, ErrorInvalidHash(hash), 400)
return
}

if txn, err := txByHashHandler(indexerDB, hash); err != nil {
http.Error(writer, err.Error(), 400)
http.Error(writer, indexer.ErrorInternal(err), 500)
return
} else if txn == nil {
http.Error(writer, ErrorTxNotFound(hash), 400)
return
} else {
writer.WriteHeader(200)
Expand All @@ -46,33 +56,15 @@ var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, indexer
vars := mux.Vars(request)
height, ok := vars["height"]
if !ok {
http.Error(writer, "invalid height", 400)
http.Error(writer, ErrorInvalidHeight(height), 400)
return
}

if txns, err := txsByHeightHandler(indexerDB, height); err != nil {
http.Error(writer, err.Error(), 400)
http.Error(writer, indexer.ErrorInternal(err), 400)
return
} else if txns == nil {
// block not seen;
heightInInt, err := strconv.Atoi(height)
if err != nil {
http.Error(writer, fmt.Errorf("invalid height: %v", err).Error(), 400)
return
}
if _, lazySyncErr := LazySync(int64(heightInInt), mantlemintConfig.RPCEndpoints[0], indexerDB); lazySyncErr != nil {
http.Error(writer, lazySyncErr.Error(), 400)
return
} else {
txns, err := txsByHeightHandler(indexerDB, height)
if err != nil {
http.Error(writer, err.Error(), 400)
return
} else {
writer.WriteHeader(200)
writer.Write(txns)
}
}
http.Error(writer, ErrorTxsNotFound(height), 400)
return
} else {
writer.WriteHeader(200)
Expand Down
65 changes: 0 additions & 65 deletions bin/v0.34.x/indexer/tx/lazy_sync.go

This file was deleted.

20 changes: 0 additions & 20 deletions bin/v0.34.x/indexer/tx/lazy_sync_test.go

This file was deleted.

20 changes: 16 additions & 4 deletions bin/v0.34.x/indexer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
tm "github.com/tendermint/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"github.com/terra-money/mantlemint-provider-v0.34.x/mantlemint"
"log"
"net/http"
"runtime"
)

type IndexFunc func(indexerDB tmdb.Batch, block *tm.Block, blockId *tm.BlockID, evc *mantlemint.EventCollector) error
Expand All @@ -16,10 +18,20 @@ func CreateIndexer(idf IndexFunc) IndexFunc {
return idf
}

func CreateHandler(ich ClientHandler) ClientHandler {
return ich
}

func CreateRESTRoute(registerer RESTRouteRegisterer) RESTRouteRegisterer {
return registerer
}

var (
ErrorInternal = func(err error) string {
_, fn, fl, ok := runtime.Caller(1)

if !ok {
// ...
} else {
log.Printf("ErrorInternal[%s:%d] %v\n", fn, fl, err.Error())
}

return "internal server error"
}
)

0 comments on commit 6c78d9e

Please sign in to comment.