erigon-pulse/cmd/restapi/apis/intermediate_hash_api.go
Alex Sharov 916a1f8b83
[wip] Lmdb: AbstractKV and DB interfaces (#589)
* resetIH from scratch if needed

* lmdb

* add AbstractKV to loader, added new Object accessor around AbstractKV

* add lmdb cli flag

* add requirement of k!=nil on error in docs

* add Size method for compatibility

* read after put tests

* fix multiput nils

* simplify loops

* increase mmap size

* better error messages

* better error messages

* fix tests

* better error messages

* cleanup

* avoid bolt usage in test

* move hardcoded bucket name to dbutils

* register more buckets

* register more buckets

* fix test
2020-05-30 09:12:21 +01:00

65 lines
1.5 KiB
Go

package apis
import (
"context"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/ethdb"
)
func RegisterIntermediateHashAPI(router *gin.RouterGroup, e *Env) error {
router.GET("/", e.FindIntermediateHash)
return nil
}
func (e *Env) FindIntermediateHash(c *gin.Context) {
results, err := findIntermediateHashByPrefix(c.Query("prefix"), e.DB)
if err != nil {
c.Error(err) //nolint:errcheck
return
}
c.JSON(http.StatusOK, results)
}
type IntermediateHashResponse struct {
Prefix string `json:"prefix"`
Value string `json:"value"`
}
func findIntermediateHashByPrefix(prefixS string, remoteDB ethdb.KV) ([]*IntermediateHashResponse, error) {
var results []*IntermediateHashResponse
prefix := common.FromHex(prefixS)
if err := remoteDB.View(context.TODO(), func(tx ethdb.Tx) error {
interBucket := tx.Bucket(dbutils.IntermediateTrieHashBucket)
c := interBucket.Cursor().Prefix(prefix)
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
results = append(results, &IntermediateHashResponse{
Prefix: fmt.Sprintf("%x\n", k),
Value: fmt.Sprintf("%x\n", v),
})
if len(results) > 50 {
results = append(results, &IntermediateHashResponse{
Prefix: "too much results",
})
return nil
}
}
return nil
}); err != nil {
return nil, err
}
return results, nil
}