-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address historical summaries feedback (#11864)
* Address historical summaries feedback * Fix build * Fix test * Move historicalSummaries Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f93b82e
commit 179252f
Showing
6 changed files
with
56 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package stateutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams" | ||
"github.com/prysmaticlabs/prysm/v3/crypto/hash" | ||
"github.com/prysmaticlabs/prysm/v3/encoding/ssz" | ||
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" | ||
) | ||
|
||
func HistoricalSummariesRoot(summaries []*ethpb.HistoricalSummary) ([32]byte, error) { | ||
max := uint64(fieldparams.HistoricalRootsLength) | ||
if uint64(len(summaries)) > max { | ||
return [32]byte{}, fmt.Errorf("historical summary exceeds max length %d", max) | ||
} | ||
|
||
hasher := hash.CustomSHA256Hasher() | ||
roots := make([][32]byte, len(summaries)) | ||
for i := 0; i < len(summaries); i++ { | ||
r, err := summaries[i].HashTreeRoot() | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not merkleize historical summary") | ||
} | ||
roots[i] = r | ||
} | ||
|
||
summariesRoot, err := ssz.BitwiseMerkleize( | ||
hasher, | ||
roots, | ||
uint64(len(roots)), | ||
fieldparams.HistoricalRootsLength, | ||
) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not compute historical summaries merkleization") | ||
} | ||
summariesLenBuf := new(bytes.Buffer) | ||
if err := binary.Write(summariesLenBuf, binary.LittleEndian, uint64(len(summaries))); err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not marshal historical summary length") | ||
} | ||
// We need to mix in the length of the slice. | ||
summariesLenRoot := make([]byte, 32) | ||
copy(summariesLenRoot, summariesLenBuf.Bytes()) | ||
res := ssz.MixInLength(summariesRoot, summariesLenRoot) | ||
return res, nil | ||
} |