From 3ef9c7fbe1bd4358bf76047cc6db1b45ce80a049 Mon Sep 17 00:00:00 2001 From: jstr1121 Date: Thu, 26 Oct 2023 20:22:14 +0800 Subject: [PATCH] add custom marshaler for witness --- trie/trienode/witness.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/trie/trienode/witness.go b/trie/trienode/witness.go index ac328a3f15..bbd0c71475 100644 --- a/trie/trienode/witness.go +++ b/trie/trienode/witness.go @@ -16,7 +16,12 @@ package trienode -import "github.com/ethereum/go-ethereum/common" +import ( + "encoding/hex" + "encoding/json" + + "github.com/ethereum/go-ethereum/common" +) // Witness is the set of nodes retrieved from the database for executing a state // transition. It can be considered as the previous state for the state transition, @@ -26,6 +31,20 @@ type Witness struct { Nodes map[string][]byte } +func (u *Witness) MarshalJSON() ([]byte, error) { + nodes := make(map[string]string) + for key, node := range u.Nodes { + nodes[key] = hex.EncodeToString(node) + } + return json.Marshal(&struct { + Owner common.Hash + Nodes map[string]string + }{ + Owner: u.Owner, + Nodes: nodes, + }) +} + // NewWitness constructs a witness structure. func NewWitness(owner common.Hash) *Witness { return &Witness{Owner: owner, Nodes: make(map[string][]byte)}