github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/state/mpt_root.go (about)

     1  package state
     2  
     3  import (
     4  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
     5  	"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
     6  	"github.com/nspcc-dev/neo-go/pkg/io"
     7  	"github.com/nspcc-dev/neo-go/pkg/util"
     8  )
     9  
    10  // MPTRoot represents the storage state root together with sign info.
    11  type MPTRoot struct {
    12  	Version byte                  `json:"version"`
    13  	Index   uint32                `json:"index"`
    14  	Root    util.Uint256          `json:"roothash"`
    15  	Witness []transaction.Witness `json:"witnesses"`
    16  }
    17  
    18  // Hash returns the hash of s.
    19  func (s *MPTRoot) Hash() util.Uint256 {
    20  	buf := io.NewBufBinWriter()
    21  	s.EncodeBinaryUnsigned(buf.BinWriter)
    22  	return hash.Sha256(buf.Bytes())
    23  }
    24  
    25  // DecodeBinaryUnsigned decodes the hashable part of the state root.
    26  func (s *MPTRoot) DecodeBinaryUnsigned(r *io.BinReader) {
    27  	s.Version = r.ReadB()
    28  	s.Index = r.ReadU32LE()
    29  	s.Root.DecodeBinary(r)
    30  }
    31  
    32  // EncodeBinaryUnsigned encodes the hashable part of the state root.
    33  func (s *MPTRoot) EncodeBinaryUnsigned(w *io.BinWriter) {
    34  	w.WriteB(s.Version)
    35  	w.WriteU32LE(s.Index)
    36  	s.Root.EncodeBinary(w)
    37  }
    38  
    39  // DecodeBinary implements io.Serializable.
    40  func (s *MPTRoot) DecodeBinary(r *io.BinReader) {
    41  	s.DecodeBinaryUnsigned(r)
    42  	r.ReadArray(&s.Witness, 1)
    43  }
    44  
    45  // EncodeBinary implements io.Serializable.
    46  func (s *MPTRoot) EncodeBinary(w *io.BinWriter) {
    47  	s.EncodeBinaryUnsigned(w)
    48  	w.WriteArray(s.Witness)
    49  }