erigon-pulse/cmd/restapi/apis/storage_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

67 lines
1.4 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 RegisterStorageAPI(router *gin.RouterGroup, e *Env) error {
router.GET("/", e.FindStorage)
return nil
}
func (e *Env) FindStorage(c *gin.Context) {
results, err := findStorageByPrefix(c.Query("prefix"), e.DB)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) //nolint:errcheck
return
}
c.JSON(http.StatusOK, results)
}
type StorageResponse struct {
Prefix string `json:"prefix"`
Value string `json:"value"`
}
func findStorageByPrefix(prefixS string, remoteDB ethdb.KV) ([]*StorageResponse, error) {
var results []*StorageResponse
prefix := common.FromHex(prefixS)
if err := remoteDB.View(context.TODO(), func(tx ethdb.Tx) error {
c := tx.Bucket(dbutils.CurrentStateBucket).Cursor().Prefix(prefix).Prefetch(200)
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if len(k) == 32 {
continue
}
if err != nil {
return err
}
results = append(results, &StorageResponse{
Prefix: fmt.Sprintf("%x\n", k),
Value: fmt.Sprintf("%x\n", v),
})
if len(results) > 200 {
results = append(results, &StorageResponse{
Prefix: "too much results",
})
return nil
}
}
return nil
}); err != nil {
return nil, err
}
return results, nil
}