github.com/klaytn/klaytn@v1.12.1/storage/statedb/node_enc.go (about)

     1  // Modifications Copyright 2023 The klaytn Authors
     2  // Copyright 2022 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from trie/node_enc.go (2023/09/19).
    19  // Modified and improved for the klaytn development.
    20  
    21  package statedb
    22  
    23  import (
    24  	"github.com/klaytn/klaytn/rlp"
    25  )
    26  
    27  func nodeToBytes(n node) []byte {
    28  	w := rlp.NewEncoderBuffer(nil)
    29  	n.encode(w)
    30  	result := w.ToBytes()
    31  	w.Flush()
    32  	return result
    33  }
    34  
    35  func (n *fullNode) encode(w rlp.EncoderBuffer) {
    36  	offset := w.List()
    37  	for _, c := range n.Children {
    38  		if c != nil {
    39  			c.encode(w)
    40  		} else {
    41  			w.Write(rlp.EmptyString)
    42  		}
    43  	}
    44  	w.ListEnd(offset)
    45  }
    46  
    47  func (n *shortNode) encode(w rlp.EncoderBuffer) {
    48  	offset := w.List()
    49  	w.WriteBytes(n.Key)
    50  	if n.Val != nil {
    51  		n.Val.encode(w)
    52  	} else {
    53  		w.Write(rlp.EmptyString)
    54  	}
    55  	w.ListEnd(offset)
    56  }
    57  
    58  func (n hashNode) encode(w rlp.EncoderBuffer) {
    59  	w.WriteBytes(n)
    60  }
    61  
    62  func (n valueNode) encode(w rlp.EncoderBuffer) {
    63  	w.WriteBytes(n)
    64  }
    65  
    66  func (n rawFullNode) encode(w rlp.EncoderBuffer) {
    67  	offset := w.List()
    68  	for _, c := range n {
    69  		if c != nil {
    70  			c.encode(w)
    71  		} else {
    72  			w.Write(rlp.EmptyString)
    73  		}
    74  	}
    75  	w.ListEnd(offset)
    76  }
    77  
    78  func (n *rawShortNode) encode(w rlp.EncoderBuffer) {
    79  	offset := w.List()
    80  	w.WriteBytes(n.Key)
    81  	if n.Val != nil {
    82  		n.Val.encode(w)
    83  	} else {
    84  		w.Write(rlp.EmptyString)
    85  	}
    86  	w.ListEnd(offset)
    87  }
    88  
    89  func (n rawNode) encode(w rlp.EncoderBuffer) {
    90  	w.Write(n)
    91  }