Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper read lock for FieldTrie #8588

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions beacon-chain/state/field_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// FieldTrie is the representation of the representative
// trie of the particular field.
type FieldTrie struct {
*sync.Mutex
*sync.RWMutex
*reference
fieldLayers [][]*[32]byte
field fieldIndex
Expand All @@ -30,7 +30,7 @@ func NewFieldTrie(field fieldIndex, elements interface{}, length uint64) (*Field
return &FieldTrie{
field: field,
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}, nil
}
datType, ok := fieldMap[field]
Expand All @@ -47,14 +47,14 @@ func NewFieldTrie(field fieldIndex, elements interface{}, length uint64) (*Field
fieldLayers: stateutil.ReturnTrieLayer(fieldRoots, length),
field: field,
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}, nil
case compositeArray:
return &FieldTrie{
fieldLayers: stateutil.ReturnTrieLayerVariable(fieldRoots, length),
field: field,
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}, nil
default:
return nil, errors.Errorf("unrecognized data type in field map: %v", reflect.TypeOf(datType).Name())
Expand Down Expand Up @@ -106,7 +106,7 @@ func (f *FieldTrie) CopyTrie() *FieldTrie {
return &FieldTrie{
field: f.field,
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}
}
dstFieldTrie := make([][]*[32]byte, len(f.fieldLayers))
Expand All @@ -118,7 +118,7 @@ func (f *FieldTrie) CopyTrie() *FieldTrie {
fieldLayers: dstFieldTrie,
field: f.field,
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}
}

Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/state/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
b.stateFieldLeaves[fieldIndex(i)] = &FieldTrie{
field: fieldIndex(i),
reference: &reference{refs: 1},
Mutex: new(sync.Mutex),
RWMutex: new(sync.RWMutex),
}
}

Expand Down Expand Up @@ -215,11 +215,11 @@ func (b *BeaconState) FieldReferencesCount() map[string]uint64 {
}
for i, f := range b.stateFieldLeaves {
numOfRefs := uint64(f.Refs())
f.lock.RLock()
f.RLock()
if len(f.fieldLayers) != 0 {
refMap[i.String()+"_trie"] = numOfRefs
}
f.lock.RUnlock()
f.RUnlock()
}
return refMap
}
Expand Down