Skip to content

Commit

Permalink
t8n/evm: add new cli command
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign committed Aug 2, 2024
1 parent 8ff9410 commit 8eb1368
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
79 changes: 56 additions & 23 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions cmd/evm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ var verkleCommand = &cli.Command{
Usage: "chunkify a given bytecode",
Action: t8ntool.VerkleChunkifyCode,
},
{
Name: "root",
Aliases: []string{"VR"},
Usage: "compute the root of a verkle tree for the given alloc",
Action: t8ntool.VerkleRoot,
Flags: []cli.Flag{
t8ntool.InputAllocFlag,
},
},
},
}

Expand Down

0 comments on commit 8eb1368

Please sign in to comment.