github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/stateutil/eth1_root.go (about) 1 package stateutil 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 7 "github.com/pkg/errors" 8 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 9 "github.com/prysmaticlabs/prysm/shared/bytesutil" 10 "github.com/prysmaticlabs/prysm/shared/hashutil" 11 "github.com/prysmaticlabs/prysm/shared/htrutils" 12 "github.com/prysmaticlabs/prysm/shared/params" 13 ) 14 15 // Eth1DataEncKey returns the encoded key in bytes of input `eth1Data`, 16 // the returned key bytes can be used for caching purposes. 17 func Eth1DataEncKey(eth1Data *ethpb.Eth1Data) []byte { 18 enc := make([]byte, 0, 96) 19 if eth1Data != nil { 20 if len(eth1Data.DepositRoot) > 0 { 21 depRoot := bytesutil.ToBytes32(eth1Data.DepositRoot) 22 enc = append(enc, depRoot[:]...) 23 } 24 eth1DataCountBuf := make([]byte, 8) 25 binary.LittleEndian.PutUint64(eth1DataCountBuf, eth1Data.DepositCount) 26 eth1CountRoot := bytesutil.ToBytes32(eth1DataCountBuf) 27 enc = append(enc, eth1CountRoot[:]...) 28 if len(eth1Data.BlockHash) > 0 { 29 blockHash := bytesutil.ToBytes32(eth1Data.BlockHash) 30 enc = append(enc, blockHash[:]...) 31 } 32 } 33 return enc 34 } 35 36 // Eth1DataRootWithHasher returns the hash tree root of input `eth1Data`. 37 func Eth1DataRootWithHasher(hasher htrutils.HashFn, eth1Data *ethpb.Eth1Data) ([32]byte, error) { 38 if eth1Data == nil { 39 return [32]byte{}, errors.New("nil eth1 data") 40 } 41 42 fieldRoots := make([][]byte, 3) 43 for i := 0; i < len(fieldRoots); i++ { 44 fieldRoots[i] = make([]byte, 32) 45 } 46 if len(eth1Data.DepositRoot) > 0 { 47 depRoot := bytesutil.ToBytes32(eth1Data.DepositRoot) 48 fieldRoots[0] = depRoot[:] 49 } 50 eth1DataCountBuf := make([]byte, 8) 51 binary.LittleEndian.PutUint64(eth1DataCountBuf, eth1Data.DepositCount) 52 eth1CountRoot := bytesutil.ToBytes32(eth1DataCountBuf) 53 fieldRoots[1] = eth1CountRoot[:] 54 if len(eth1Data.BlockHash) > 0 { 55 blockHash := bytesutil.ToBytes32(eth1Data.BlockHash) 56 fieldRoots[2] = blockHash[:] 57 } 58 root, err := htrutils.BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots))) 59 if err != nil { 60 return [32]byte{}, err 61 } 62 return root, nil 63 } 64 65 // Eth1DatasEncKey returns the encoded key in bytes of input `eth1Data`s, 66 // the returned key bytes can be used for caching purposes. 67 func Eth1DatasEncKey(eth1Datas []*ethpb.Eth1Data) ([32]byte, error) { 68 hasher := hashutil.CustomSHA256Hasher() 69 enc := make([]byte, len(eth1Datas)*32) 70 for i := 0; i < len(eth1Datas); i++ { 71 eth1, err := Eth1DataRootWithHasher(hasher, eth1Datas[i]) 72 if err != nil { 73 return [32]byte{}, errors.Wrap(err, "could not compute eth1data merkleization") 74 } 75 copy(enc[(i*32):(i+1)*32], eth1[:]) 76 } 77 hashKey := hashutil.FastSum256(enc) 78 return hashKey, nil 79 } 80 81 // Eth1DatasRoot returns the hash tree root of input `eth1Datas`. 82 func Eth1DatasRoot(eth1Datas []*ethpb.Eth1Data) ([32]byte, error) { 83 hasher := hashutil.CustomSHA256Hasher() 84 eth1VotesRoots := make([][]byte, 0) 85 for i := 0; i < len(eth1Datas); i++ { 86 eth1, err := Eth1DataRootWithHasher(hasher, eth1Datas[i]) 87 if err != nil { 88 return [32]byte{}, errors.Wrap(err, "could not compute eth1data merkleization") 89 } 90 eth1VotesRoots = append(eth1VotesRoots, eth1[:]) 91 } 92 eth1Chunks, err := htrutils.Pack(eth1VotesRoots) 93 if err != nil { 94 return [32]byte{}, errors.Wrap(err, "could not chunk eth1 votes roots") 95 } 96 97 eth1VotesRootsRoot, err := htrutils.BitwiseMerkleize( 98 hasher, 99 eth1Chunks, 100 uint64(len(eth1Chunks)), 101 uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))), 102 ) 103 if err != nil { 104 return [32]byte{}, errors.Wrap(err, "could not compute eth1data votes merkleization") 105 } 106 eth1VotesRootBuf := new(bytes.Buffer) 107 if err := binary.Write(eth1VotesRootBuf, binary.LittleEndian, uint64(len(eth1Datas))); err != nil { 108 return [32]byte{}, errors.Wrap(err, "could not marshal eth1data votes length") 109 } 110 // We need to mix in the length of the slice. 111 eth1VotesRootBufRoot := make([]byte, 32) 112 copy(eth1VotesRootBufRoot, eth1VotesRootBuf.Bytes()) 113 root := htrutils.MixInLength(eth1VotesRootsRoot, eth1VotesRootBufRoot) 114 115 return root, nil 116 }