github.com/klaytn/klaytn@v1.10.2/blockchain/types/derivesha/orig.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 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 core/types/derive_sha.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package derivesha
    22  
    23  import (
    24  	"github.com/klaytn/klaytn/blockchain/types"
    25  	"github.com/klaytn/klaytn/common"
    26  	"github.com/klaytn/klaytn/rlp"
    27  	"github.com/klaytn/klaytn/storage/statedb"
    28  )
    29  
    30  type DeriveShaOrig struct{}
    31  
    32  func (d DeriveShaOrig) DeriveSha(list types.DerivableList) common.Hash {
    33  	trie := statedb.NewStackTrie(nil)
    34  	trie.Reset()
    35  	var buf []byte
    36  
    37  	// StackTrie requires values to be inserted in increasing
    38  	// hash order, which is not the order that `list` provides
    39  	// hashes in. This insertion sequence ensures that the
    40  	// order is correct.
    41  	for i := 1; i < list.Len() && i <= 0x7f; i++ {
    42  		buf = rlp.AppendUint64(buf[:0], uint64(i))
    43  		trie.Update(buf, list.GetRlp(i))
    44  	}
    45  	if list.Len() > 0 {
    46  		buf = rlp.AppendUint64(buf[:0], 0)
    47  		trie.Update(buf, list.GetRlp(0))
    48  	}
    49  	for i := 0x80; i < list.Len(); i++ {
    50  		buf = rlp.AppendUint64(buf[:0], uint64(i))
    51  		trie.Update(buf, list.GetRlp(i))
    52  	}
    53  	return trie.Hash()
    54  }