Skip to content

Commit

Permalink
test link reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtinms committed Sep 5, 2024
1 parent dd9faee commit a91b40f
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
160 changes: 160 additions & 0 deletions cmd/util/cmd/link-reporter/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package link_reporter

import (
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/onflow/flow-go/cmd/util/ledger/migrations"
"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
)

var (
flagPayloads string
flagState string
flagStateCommitment string
flagChain string
)

var Cmd = &cobra.Command{
Use: "report-links",
Short: "reports links",
Run: run,
}

func init() {

Cmd.Flags().StringVar(
&flagPayloads,
"payloads",
"",
"Input payload file name",
)

Cmd.Flags().StringVar(
&flagState,
"state",
"",
"Input state file name",
)
Cmd.Flags().StringVar(
&flagStateCommitment,
"state-commitment",
"",
"Input state commitment",
)

Cmd.Flags().StringVar(
&flagChain,
"chain",
"",
"Chain name",
)
_ = Cmd.MarkFlagRequired("chain")
}

func run(*cobra.Command, []string) {

if flagPayloads == "" && flagState == "" {
log.Fatal().Msg("Either --payloads or --state must be provided")
} else if flagPayloads != "" && flagState != "" {
log.Fatal().Msg("Only one of --payloads or --state must be provided")
}
if flagState != "" && flagStateCommitment == "" {
log.Fatal().Msg("--state-commitment must be provided when --state is provided")
}

chainID := flow.ChainID(flagChain)
// Validate chain ID
_ = chainID.Chain()

var payloads []*ledger.Payload
var err error

if flagPayloads != "" {
_, payloads, err = util.ReadPayloadFile(log.Logger, flagPayloads)
} else {
log.Info().Msg("reading trie")

stateCommitment := util.ParseStateCommitment(flagStateCommitment)
payloads, err = util.ReadTrie(flagState, stateCommitment)
}
if err != nil {
log.Fatal().Err(err).Msg("failed to read payloads")
}

log.Info().Msgf("creating registers from payloads (%d)", len(payloads))

registersByAccount, err := registers.NewByAccountFromPayloads(payloads)
if err != nil {
log.Fatal().Err(err)
}
log.Info().Msgf(
"created %d registers from payloads (%d accounts)",
registersByAccount.Count(),
registersByAccount.AccountCount(),
)

mr, err := migrations.NewInterpreterMigrationRuntime(
registersByAccount,
chainID,
migrations.InterpreterMigrationRuntimeConfig{},
)
if err != nil {
panic(err)
}

err = registersByAccount.ForEachAccount(func(accountRegisters *registers.AccountRegisters) error {

address := common.MustBytesToAddress([]byte(accountRegisters.Owner()))

log.Info().Msgf("account: %s", address)

publicStorage := mr.Storage.GetStorageMap(
address,
common.PathDomainPublic.Identifier(),
false,
)

iterator := publicStorage.Iterator(nil)
for {
k, v := iterator.Next()

if k == nil || v == nil {
break
}

key, ok := k.(interpreter.StringAtreeValue)
if !ok {
log.Fatal().Msgf("unexpected key type: %T", k)
}

identifier := string(key)

value := interpreter.MustConvertUnmeteredStoredValue(v)

switch link := value.(type) {
case interpreter.PathLinkValue:
targetPath := link.TargetPath
linkType := link.Type

log.Info().Msgf("%s: %s -> %s = %s", address, identifier, targetPath, linkType.ID())

case interpreter.AccountLinkValue:
// ignore
continue
default:
log.Fatal().Msgf("unexpected value type: %T", value)

}
}
return nil
})
if err != nil {
log.Fatal().Err(err)
}
}
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
extractpayloads "github.com/onflow/flow-go/cmd/util/cmd/extract-payloads-by-address"
find_inconsistent_result "github.com/onflow/flow-go/cmd/util/cmd/find-inconsistent-result"
find_trie_root "github.com/onflow/flow-go/cmd/util/cmd/find-trie-root"
link_reporter "github.com/onflow/flow-go/cmd/util/cmd/link-reporter"
read_badger "github.com/onflow/flow-go/cmd/util/cmd/read-badger/cmd"
read_execution_state "github.com/onflow/flow-go/cmd/util/cmd/read-execution-state"
read_hotstuff "github.com/onflow/flow-go/cmd/util/cmd/read-hotstuff/cmd"
Expand Down Expand Up @@ -114,6 +115,7 @@ func addCommands() {
rootCmd.AddCommand(atree_inlined_status.Cmd)
rootCmd.AddCommand(find_trie_root.Cmd)
rootCmd.AddCommand(run_script.Cmd)
rootCmd.AddCommand(link_reporter.Cmd)
}

func initConfig() {
Expand Down

0 comments on commit a91b40f

Please sign in to comment.