github.com/gochain-io/gochain@v2.2.26+incompatible/core/types/derive_sha.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package types 18 19 import ( 20 "bytes" 21 "context" 22 23 "go.opencensus.io/trace" 24 25 "github.com/gochain-io/gochain/common" 26 "github.com/gochain-io/gochain/rlp" 27 "github.com/gochain-io/gochain/trie" 28 ) 29 30 type DerivableList interface { 31 Len() int 32 GetRlp(i int) []byte 33 } 34 35 func DeriveShaCtx(ctx context.Context, list DerivableList) common.Hash { 36 ctx, span := trace.StartSpan(ctx, "DeriveShaCtx") 37 defer span.End() 38 return DeriveSha(list) 39 } 40 41 func DeriveSha(list DerivableList) common.Hash { 42 keybuf := new(bytes.Buffer) 43 trie := new(trie.Trie) 44 for i := 0; i < list.Len(); i++ { 45 keybuf.Reset() 46 rlp.Encode(keybuf, uint(i)) 47 trie.Update(keybuf.Bytes(), list.GetRlp(i)) 48 } 49 return trie.Hash() 50 }