From ed07ef6b5fbd68370d02b7423534b47f95aa9945 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 2 Aug 2024 11:50:47 -0300 Subject: [PATCH] t8n/evm: add new cli command Signed-off-by: Ignacio Hagopian --- cmd/evm/internal/t8ntool/transition.go | 79 ++++++++++++++++++-------- cmd/evm/main.go | 9 +++ 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index d525a68c06507..9ce288d6307a3 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -548,13 +548,65 @@ func VerkleKeys(ctx *cli.Context) error { } } + vkt, err := genVktFromAlloc(alloc) + if err != nil { + return fmt.Errorf("error generating vkt: %w", err) + } + + collector := make(map[common.Hash]hexutil.Bytes) + it, err := vkt.NodeIterator(nil) + if err != nil { + panic(err) + } + for it.Next(true) { + if it.Leaf() { + collector[common.BytesToHash(it.LeafKey())] = it.LeafBlob() + } + } + + output, err := json.MarshalIndent(collector, "", "") + if err != nil { + return fmt.Errorf("error outputting tree: %w", err) + } + + fmt.Println(string(output)) + + return nil +} + +// VerkleRoot computes the root of a VKT from a genesis alloc. +func VerkleRoot(ctx *cli.Context) error { + var allocStr = ctx.String(InputAllocFlag.Name) + var alloc core.GenesisAlloc + if allocStr == stdinSelector { + decoder := json.NewDecoder(os.Stdin) + if err := decoder.Decode(&alloc); err != nil { + return NewError(ErrorJson, fmt.Errorf("failed unmarshaling stdin: %v", err)) + } + } + if allocStr != stdinSelector { + if err := readFile(allocStr, "alloc", &alloc); err != nil { + return err + } + } + + vkt, err := genVktFromAlloc(alloc) + if err != nil { + return fmt.Errorf("error generating vkt: %w", err) + } + fmt.Println(vkt.Hash()) + + return nil +} + +func genVktFromAlloc(alloc core.GenesisAlloc) (*trie.VerkleTrie, error) { vkt := trie.NewVerkleTrie(verkle.New(), trie.NewDatabase(rawdb.NewMemoryDatabase()), utils.NewPointCache(), true) for addr, acc := range alloc { for slot, value := range acc.Storage { err := vkt.UpdateStorage(addr, slot.Bytes(), value.Big().Bytes()) if err != nil { - return fmt.Errorf("error inserting storage: %w", err) + return nil, fmt.Errorf("error inserting storage: %w", err) } } @@ -566,34 +618,15 @@ func VerkleKeys(ctx *cli.Context) error { } err := vkt.UpdateAccount(addr, account) if err != nil { - return fmt.Errorf("error inserting account: %w", err) + return nil, fmt.Errorf("error inserting account: %w", err) } err = vkt.UpdateContractCode(addr, common.BytesToHash(account.CodeHash), acc.Code) if err != nil { - return fmt.Errorf("error inserting code: %w", err) - } - } - - collector := make(map[common.Hash]hexutil.Bytes) - it, err := vkt.NodeIterator(nil) - if err != nil { - panic(err) - } - for it.Next(true) { - if it.Leaf() { - collector[common.BytesToHash(it.LeafKey())] = it.LeafBlob() + return nil, fmt.Errorf("error inserting code: %w", err) } } - - output, err := json.MarshalIndent(collector, "", "") - if err != nil { - return fmt.Errorf("error outputting tree: %w", err) - } - - fmt.Println(string(output)) - - return nil + return vkt, nil } // VerkleCodeChunkKey computes the tree key of a code-chunk for a given address. diff --git a/cmd/evm/main.go b/cmd/evm/main.go index b2f70f0930f26..465148e8358d5 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -188,6 +188,15 @@ var verkleCommand = &cli.Command{ Usage: "chunkify a given bytecode", Action: t8ntool.VerkleChunkifyCode, }, + { + Name: "state-root", + Aliases: []string{"VR"}, + Usage: "compute the state-root of a verkle tree for the given alloc", + Action: t8ntool.VerkleRoot, + Flags: []cli.Flag{ + t8ntool.InputAllocFlag, + }, + }, }, }