mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 09:14:28 +00:00
a5998fab2f
* checkpoint all work * add new fixes * fixes * gaz * terence's review * terence's review Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
161 lines
4.8 KiB
Go
161 lines
4.8 KiB
Go
package fieldtrie
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
|
|
)
|
|
|
|
// FieldTrie is the representation of the representative
|
|
// trie of the particular field.
|
|
type FieldTrie struct {
|
|
*sync.RWMutex
|
|
reference *stateutil.Reference
|
|
fieldLayers [][]*[32]byte
|
|
field types.FieldIndex
|
|
dataType types.DataType
|
|
length uint64
|
|
}
|
|
|
|
// NewFieldTrie is the constructor for the field trie data structure. It creates the corresponding
|
|
// trie according to the given parameters. Depending on whether the field is a basic/composite array
|
|
// which is either fixed/variable length, it will appropriately determine the trie.
|
|
func NewFieldTrie(field types.FieldIndex, dataType types.DataType, elements interface{}, length uint64) (*FieldTrie, error) {
|
|
if elements == nil {
|
|
return &FieldTrie{
|
|
field: field,
|
|
dataType: dataType,
|
|
reference: stateutil.NewRef(1),
|
|
RWMutex: new(sync.RWMutex),
|
|
length: length,
|
|
}, nil
|
|
}
|
|
fieldRoots, err := fieldConverters(field, []uint64{}, elements, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateElements(field, elements, length); err != nil {
|
|
return nil, err
|
|
}
|
|
switch dataType {
|
|
case types.BasicArray:
|
|
return &FieldTrie{
|
|
fieldLayers: stateutil.ReturnTrieLayer(fieldRoots, length),
|
|
field: field,
|
|
dataType: dataType,
|
|
reference: stateutil.NewRef(1),
|
|
RWMutex: new(sync.RWMutex),
|
|
length: length,
|
|
}, nil
|
|
case types.CompositeArray:
|
|
return &FieldTrie{
|
|
fieldLayers: stateutil.ReturnTrieLayerVariable(fieldRoots, length),
|
|
field: field,
|
|
dataType: dataType,
|
|
reference: stateutil.NewRef(1),
|
|
RWMutex: new(sync.RWMutex),
|
|
length: length,
|
|
}, nil
|
|
default:
|
|
return nil, errors.Errorf("unrecognized data type in field map: %v", reflect.TypeOf(dataType).Name())
|
|
}
|
|
|
|
}
|
|
|
|
// RecomputeTrie rebuilds the affected branches in the trie according to the provided
|
|
// changed indices and elements. This recomputes the trie according to the particular
|
|
// field the trie is based on.
|
|
func (f *FieldTrie) RecomputeTrie(indices []uint64, elements interface{}) ([32]byte, error) {
|
|
f.Lock()
|
|
defer f.Unlock()
|
|
var fieldRoot [32]byte
|
|
if len(indices) == 0 {
|
|
return f.TrieRoot()
|
|
}
|
|
fieldRoots, err := fieldConverters(f.field, indices, elements, false)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
if err := f.validateIndices(indices); err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
switch f.dataType {
|
|
case types.BasicArray:
|
|
fieldRoot, f.fieldLayers, err = stateutil.RecomputeFromLayer(fieldRoots, indices, f.fieldLayers)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
return fieldRoot, nil
|
|
case types.CompositeArray:
|
|
fieldRoot, f.fieldLayers, err = stateutil.RecomputeFromLayerVariable(fieldRoots, indices, f.fieldLayers)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
return stateutil.AddInMixin(fieldRoot, uint64(len(f.fieldLayers[0])))
|
|
default:
|
|
return [32]byte{}, errors.Errorf("unrecognized data type in field map: %v", reflect.TypeOf(f.dataType).Name())
|
|
}
|
|
}
|
|
|
|
// CopyTrie copies the references to the elements the trie
|
|
// is built on.
|
|
func (f *FieldTrie) CopyTrie() *FieldTrie {
|
|
if f.fieldLayers == nil {
|
|
return &FieldTrie{
|
|
field: f.field,
|
|
dataType: f.dataType,
|
|
reference: stateutil.NewRef(1),
|
|
RWMutex: new(sync.RWMutex),
|
|
length: f.length,
|
|
}
|
|
}
|
|
dstFieldTrie := make([][]*[32]byte, len(f.fieldLayers))
|
|
for i, layer := range f.fieldLayers {
|
|
dstFieldTrie[i] = make([]*[32]byte, len(layer))
|
|
copy(dstFieldTrie[i], layer)
|
|
}
|
|
return &FieldTrie{
|
|
fieldLayers: dstFieldTrie,
|
|
field: f.field,
|
|
dataType: f.dataType,
|
|
reference: stateutil.NewRef(1),
|
|
RWMutex: new(sync.RWMutex),
|
|
length: f.length,
|
|
}
|
|
}
|
|
|
|
// TrieRoot returns the corresponding root of the trie.
|
|
func (f *FieldTrie) TrieRoot() ([32]byte, error) {
|
|
switch f.dataType {
|
|
case types.BasicArray:
|
|
return *f.fieldLayers[len(f.fieldLayers)-1][0], nil
|
|
case types.CompositeArray:
|
|
trieRoot := *f.fieldLayers[len(f.fieldLayers)-1][0]
|
|
return stateutil.AddInMixin(trieRoot, uint64(len(f.fieldLayers[0])))
|
|
default:
|
|
return [32]byte{}, errors.Errorf("unrecognized data type in field map: %v", reflect.TypeOf(f.dataType).Name())
|
|
}
|
|
}
|
|
|
|
// FieldReference returns the underlying field reference
|
|
// object for the trie.
|
|
func (f *FieldTrie) FieldReference() *stateutil.Reference {
|
|
return f.reference
|
|
}
|
|
|
|
// Empty checks whether the underlying field trie is
|
|
// empty or not.
|
|
func (f *FieldTrie) Empty() bool {
|
|
return f == nil || len(f.fieldLayers) == 0
|
|
}
|
|
|
|
// InsertFieldLayer manually inserts a field layer. This method
|
|
// bypasses the normal method of field computation, it is only
|
|
// meant to be used in tests.
|
|
func (f *FieldTrie) InsertFieldLayer(layer [][]*[32]byte) {
|
|
f.fieldLayers = layer
|
|
}
|