add custom marshaler for witness

This commit is contained in:
jstr1121 2023-10-26 20:22:14 +08:00
parent 2530c4a3e6
commit 3ef9c7fbe1

View file

@ -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)}