github.com/ava-labs/subnet-evm@v0.6.4/internal/blocktest/test_hash.go (about)

     1  // (c) 2024, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2023 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  // Package utesting provides a standalone replacement for package testing.
    28  //
    29  // This package exists because package testing cannot easily be embedded into a
    30  // standalone go program. It provides an API that mirrors the standard library
    31  // testing API.
    32  
    33  package blocktest
    34  
    35  import (
    36  	"hash"
    37  
    38  	"github.com/ethereum/go-ethereum/common"
    39  	"golang.org/x/crypto/sha3"
    40  )
    41  
    42  // testHasher is the helper tool for transaction/receipt list hashing.
    43  // The original hasher is trie, in order to get rid of import cycle,
    44  // use the testing hasher instead.
    45  type testHasher struct {
    46  	hasher hash.Hash
    47  }
    48  
    49  // NewHasher returns a new testHasher instance.
    50  func NewHasher() *testHasher {
    51  	return &testHasher{hasher: sha3.NewLegacyKeccak256()}
    52  }
    53  
    54  // Reset resets the hash state.
    55  func (h *testHasher) Reset() {
    56  	h.hasher.Reset()
    57  }
    58  
    59  // Update updates the hash state with the given key and value.
    60  func (h *testHasher) Update(key, val []byte) error {
    61  	h.hasher.Write(key)
    62  	h.hasher.Write(val)
    63  	return nil
    64  }
    65  
    66  // Hash returns the hash value.
    67  func (h *testHasher) Hash() common.Hash {
    68  	return common.BytesToHash(h.hasher.Sum(nil))
    69  }