prysm-pulse/tools/pcli/main.go
Preston Van Loon 49a0d3caf0
Refactor dependencies, make Prysm "go gettable" (#6053)
* Fix a few deps to work with go.mod, check in generated files

* Update Gossipsub to 1.1 (#5998)

* update libs

* add new validators

* add new deps

* new set of deps

* tls

* further fix gossip update

* get everything to build

* clean up

* gaz

* fix build

* fix all tests

* add deps to images

* imports

Co-authored-by: rauljordan <raul@prysmaticlabs.com>

* Beacon chain builds with go build

* fix bazel

* fix dep

* lint

* Add github action for testing go

* on PR for any branch

* fix libp2p test failure

* Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test

* Revert "Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test"

This reverts commit 43676894ab01f03fe90a9b8ee3ecfbc2ec1ec4e4.

* Compute and set proposer index instead of hard code

* Add back go mod/sum, fix deps

* go build ./...

* Temporarily skip two tests

* Fix kafka confluent patch

* Fix kafka confluent patch

* fix kafka build

* fix kafka

* Add info in DEPENDENCIES. Added a stub link for Why Bazel? until https://github.com/prysmaticlabs/documentation/issues/138

* Update fuzz ssz files as well

* Update fuzz ssz files as well

* getting closer

* rollback rules_go and gazelle

* fix gogo protobuf

* install librdkafka-dev as part of github actions

* Update kafka to a recent version where librkafkfa is not required for go modules

* clarify comment

* fix kafka build

* disable go tests

* comment

* Fix geth dependencies for end to end

* rename word

* lint

* fix docker

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: rauljordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-31 14:44:34 +08:00

150 lines
4.3 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"strings"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/version"
log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"gopkg.in/d4l3k/messagediff.v1"
"github.com/urfave/cli/v2"
)
func main() {
var blockPath string
var preStatePath string
var expectedPostStatePath string
customFormatter := new(prefixed.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
log.SetFormatter(customFormatter)
app := cli.App{}
app.Name = "pcli"
app.Usage = "A command line utility to run eth2 specific commands"
app.Version = version.GetVersion()
app.Commands = []*cli.Command{{
Name: "state-transition",
Category: "state-transition",
Usage: "Subcommand to run manual state transitions",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "block-path",
Usage: "Path to block file(ssz)",
Destination: &blockPath,
},
&cli.StringFlag{
Name: "pre-state-path",
Usage: "Path to pre state file(ssz)",
Destination: &preStatePath,
},
&cli.StringFlag{
Name: "expected-post-state-path",
Usage: "Path to expected post state file(ssz)",
Destination: &expectedPostStatePath,
},
},
Action: func(c *cli.Context) error {
if blockPath == "" {
log.Info("Block path not provided for state transition. " +
"Please provide path")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
if text = strings.Replace(text, "\n", "", -1); text == "" {
log.Fatal("Empty block path given")
}
blockPath = text
}
block := &ethpb.SignedBeaconBlock{}
if err := dataFetcher(blockPath, block); err != nil {
log.Fatal(err)
}
blkRoot, err := stateutil.BlockRoot(block.Block)
if err != nil {
log.Fatal(err)
}
if preStatePath == "" {
log.Info("Pre State path not provided for state transition. " +
"Please provide path")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
if text = strings.Replace(text, "\n", "", -1); text == "" {
log.Fatal("Empty state path given")
}
preStatePath = text
}
preState := &pb.BeaconState{}
if err := dataFetcher(preStatePath, preState); err != nil {
log.Fatal(err)
}
stateObj, err := stateTrie.InitializeFromProto(preState)
if err != nil {
log.Fatal(err)
}
preStateRoot, err := stateObj.HashTreeRoot(context.Background())
if err != nil {
log.Fatal(err)
}
log.WithFields(log.Fields{
"blockSlot": fmt.Sprintf("%d", block.Block.Slot),
"preStateSlot": fmt.Sprintf("%d", stateObj.Slot()),
}).Infof(
"Performing state transition with a block root of %#x and pre state root of %#x",
blkRoot,
preStateRoot,
)
postState, err := state.ExecuteStateTransition(context.Background(), stateObj, block)
if err != nil {
log.Fatal(err)
}
postRoot, err := postState.HashTreeRoot(context.Background())
log.Infof("Finished state transition with post state root of %#x", postRoot)
// Diff the state if a post state is provided.
if expectedPostStatePath != "" {
expectedState := &pb.BeaconState{}
if err := dataFetcher(expectedPostStatePath, expectedState); err != nil {
log.Fatal(err)
}
if !ssz.DeepEqual(expectedState, postState.InnerStateUnsafe()) {
diff, _ := messagediff.PrettyDiff(expectedState, postState.InnerStateUnsafe())
log.Errorf("Derived state differs from provided post state: %s", diff)
}
}
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
// dataFetcher fetches and unmarshals data from file to provided data structure.
func dataFetcher(fPath string, data interface{}) error {
rawFile, err := ioutil.ReadFile(fPath)
if err != nil {
return err
}
return ssz.Unmarshal(rawFile, data)
}