Compare commits

..

1 Commits

Author SHA1 Message Date
terence tsao
dad1b91145 Add logs 2024-08-28 07:11:14 -07:00
3 changed files with 41 additions and 47 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
@@ -306,8 +307,35 @@ func (s *State) latestAncestor(ctx context.Context, blockRoot [32]byte) (state.B
// Does the state exists in DB.
if s.beaconDB.HasState(ctx, parentRoot) {
s, err := s.beaconDB.State(ctx, parentRoot)
return s, errors.Wrap(err, "failed to retrieve state from db")
st, err := s.beaconDB.State(ctx, parentRoot)
if err != nil {
return nil, err
}
blk, err := s.beaconDB.Block(ctx, parentRoot)
if err != nil {
return nil, errors.Wrap(err, "could not get block from db!")
}
if st.Slot() != blk.Block().Slot() {
return nil, errors.New("slot mismatch!")
}
stateRoot1, err := st.HashTreeRoot(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not hash tree root of state!")
}
stateRoot2 := blk.Block().StateRoot()
if stateRoot1 != stateRoot2 {
log.WithFields(logrus.Fields{
"StateRoot": stateRoot1,
"BlockStateRoot": stateRoot2,
"ParentBlockRoot": parentRoot,
"BlockSlot": blk.Block().Slot(),
"StateSlot": st.Slot(),
}).Warn("State root missmatch between block and state")
}
return st, nil
}
b, err = s.beaconDB.Block(ctx, parentRoot)

View File

@@ -53,17 +53,6 @@ func (*State) replayBlocks(
if err != nil {
return nil, err
}
htr, err := state.HashTreeRoot(ctx)
if err != nil {
return nil, err
}
if htr != signed[i].Block().StateRoot() {
log.WithFields(logrus.Fields{
"stateRoot": fmt.Sprintf("%x", htr),
"blockRoot": fmt.Sprintf("%x", signed[i].Block().StateRoot()),
"slot": signed[i].Block().Slot(),
}).Fatal("state root mismatch")
}
}
}

View File

@@ -2,20 +2,20 @@ package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/transition/interop"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/db/kv"
doublylinkedtree "github.com/prysmaticlabs/prysm/v5/beacon-chain/forkchoice/doubly-linked-tree"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state/stategen"
"github.com/prysmaticlabs/prysm/v5/config/features"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
)
var (
// Required fields
datadir = flag.String("datadir", "", "Path to data directory.")
state = flag.Uint("state", 0, "Extract state at this slot.")
)
func main() {
@@ -27,43 +27,20 @@ func main() {
if err != nil {
panic(err)
}
cfg := params.BeaconConfig()
cfg.ChurnLimitQuotient = 128 // Devnet uses this config. Without it, state transition will fail when processing epoch
params.OverrideBeaconConfig(cfg)
ctx := context.Background()
fc := doublylinkedtree.New()
s := stategen.New(d, fc)
hexString := "04caf4ff7fc7ab2b294ae83ea7a4c1f1763a3e552a08735a6a5d5755b4fd4933" // This is the finalized root, which we'll first replay to and save it as finalized root for state transition
root, err := hex.DecodeString(hexString)
slot := primitives.Slot(*state)
_, roots, err := d.BlockRootsBySlot(ctx, slot)
if err != nil {
panic(err)
}
st, err := s.StateByRoot(ctx, [32]byte(root))
if err != nil {
panic(err)
if len(roots) != 1 {
fmt.Printf("Expected 1 block root for slot %d, got %d roots", *state, len(roots))
}
_, err = s.Resume(ctx, st) // Resume saves the caches of the finalized state for state gen
s, err := d.State(ctx, roots[0])
if err != nil {
panic(err)
}
hexString = "ce3665faa64345557b47afa03ef7b81e48c24c6a712e4c3c90303cef64e88af8" // This is the target root
root, err = hex.DecodeString(hexString)
if err != nil {
panic(err)
}
st, err = s.StateByRoot(ctx, [32]byte(root))
if err != nil {
panic(err)
}
st, err = s.StateByRoot(ctx, [32]byte(root)) // Replay twice fails because start state caches are incorrect
if err != nil {
// Error: panic: state root 0xd21dac2ac7a03561f17c550bcaa7d5d5be8b9f5cbd2e23ae808fc8306e290be9
// does not match the block state root 0x3952f2c8a6eebc7ff96f05ca54fca81363a0fb720238722e00197a7c1e3c7149
panic(err)
}
interop.WriteStateToDisk(s)
fmt.Println("done")
}