add verify preimages rpc method

pull/4514/head
Diego Nava 1 year ago
parent cdcae9ac12
commit 9586553f5e
No known key found for this signature in database
GPG Key ID: 61AFC8738DA8B8B1
  1. 47
      core/preimages.go
  2. 6
      rpc/preimages.go

@ -7,13 +7,16 @@ import (
"os" "os"
"strconv" "strconv"
"github.com/ethereum/go-ethereum/common"
ethCommon "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/harmony-one/harmony/api/service/prometheus" "github.com/harmony-one/harmony/api/service/prometheus"
"github.com/harmony-one/harmony/block"
"github.com/harmony-one/harmony/core/rawdb" "github.com/harmony-one/harmony/core/rawdb"
"github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/pkg/errors"
prom "github.com/prometheus/client_golang/prometheus" prom "github.com/prometheus/client_golang/prometheus"
) )
@ -316,3 +319,47 @@ func FindMissingRange(
} }
return 0, 0 return 0, 0
} }
func VerifyPreimages(header *block.Header, chain BlockChain) (uint64, error) {
var existingPreimages uint64
parentRoot := chain.GetBlockByHash(
header.ParentHash(),
).Root() // for examining MPT at this root, should exist
db, err := chain.StateAt(parentRoot)
if err != nil {
return 0, err
}
trie, err := db.Database().OpenTrie(parentRoot)
if err != nil {
return 0, err
}
diskDB := db.Database().DiskDB()
// start the iteration
accountIterator := trie.NodeIterator(nil)
for accountIterator.Next(true) {
// leaf means leaf node of the MPT, which is an account
// the leaf key is the address
if accountIterator.Leaf() {
key := accountIterator.LeafKey()
preimage := rawdb.ReadPreimage(diskDB, common.BytesToHash(key))
if len(preimage) == 0 {
return 0, errors.New(
fmt.Sprintf(
"cannot find preimage for %x", key,
),
)
}
address := common.BytesToAddress(preimage)
// skip blank address
if address == (common.Address{}) {
continue
}
existingPreimages++
}
}
return existingPreimages, nil
}

@ -27,3 +27,9 @@ func (s *PreimagesService) Export(ctx context.Context, path string) error {
// these are by default not blocking // these are by default not blocking
return core.ExportPreimages(s.hmy.BlockChain, path) return core.ExportPreimages(s.hmy.BlockChain, path)
} }
func (s *PreimagesService) Verify(ctx context.Context) (uint64, error) {
currentBlock := s.hmy.CurrentBlock()
// these are by default not blocking
return core.VerifyPreimages(currentBlock.Header(), s.hmy.BlockChain)
}

Loading…
Cancel
Save