Extract bodies utility (#1409)

* Extract bodies

* Print headers too
This commit is contained in:
ledgerwatch 2020-12-14 12:09:59 +00:00 committed by GitHub
parent 76e244f24f
commit 71ef201c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2093,6 +2093,42 @@ func indexKeySizes(chaindata string) error {
return nil
}
func extractBodies(chaindata string, block uint64) error {
db := ethdb.MustOpen(chaindata)
defer db.Close()
tx, err := db.Begin(context.Background(), ethdb.RO)
if err != nil {
return err
}
defer tx.Rollback()
c := tx.(ethdb.HasTx).Tx().Cursor(dbutils.BlockBodyPrefix)
defer c.Close()
blockEncoded := dbutils.EncodeBlockNumber(block)
for k, _, err := c.Seek(blockEncoded); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
body := rawdb.ReadBody(db, blockHash, blockNumber)
b, err := rlp.EncodeToBytes(body)
if err != nil {
return err
}
fmt.Printf("Body %d %x: %x\n", blockNumber, blockHash, b)
header := rawdb.ReadHeader(db, blockHash, blockNumber)
b, err = rlp.EncodeToBytes(header)
if err != nil {
return err
}
fmt.Printf("Header %d %x: %x\n", blockNumber, blockHash, b)
if blockNumber > block+5 {
break
}
}
return nil
}
func main() {
flag.Parse()
@ -2277,4 +2313,9 @@ func main() {
fmt.Printf("Error: %v\n", err)
}
}
if *action == "extractBodies" {
if err := extractBodies(*chaindata, uint64(*block)); err != nil {
fmt.Printf("Error:%v\n", err)
}
}
}