Skip to content

Commit

Permalink
protect-trackableDataTrie-dirtyData-map
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniaminDrasovean committed May 21, 2024
1 parent 5be0a55 commit 29ac95a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion state/trackableDataTrie/trackableDataTrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trackableDataTrie

import (
"fmt"
"sync"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
Expand Down Expand Up @@ -32,6 +33,7 @@ type trackableDataTrie struct {
marshaller marshal.Marshalizer
enableEpochsHandler common.EnableEpochsHandler
identifier []byte
mutex sync.RWMutex
}

// NewTrackableDataTrie returns an instance of trackableDataTrie
Expand Down Expand Up @@ -64,6 +66,7 @@ func NewTrackableDataTrie(
dirtyData: make(map[string]dirtyData),
identifier: identifier,
enableEpochsHandler: enableEpochsHandler,
mutex: sync.RWMutex{},
}, nil
}

Expand All @@ -72,7 +75,11 @@ func NewTrackableDataTrie(
// Data must have been retrieved from its trie
func (tdt *trackableDataTrie) RetrieveValue(key []byte) ([]byte, uint32, error) {
// search in dirty data cache
if dataEntry, found := tdt.dirtyData[string(key)]; found {
tdt.mutex.RLock()
dataEntry, found := tdt.dirtyData[string(key)]
tdt.mutex.RUnlock()

if found {
log.Trace("retrieve value from dirty data", "key", key, "value", dataEntry.value, "account", tdt.identifier)
return dataEntry.value, 0, nil
}
Expand Down Expand Up @@ -108,7 +115,9 @@ func (tdt *trackableDataTrie) SaveKeyValue(key []byte, value []byte) error {
newVersion: core.GetVersionForNewData(tdt.enableEpochsHandler),
}

tdt.mutex.Lock()
tdt.dirtyData[string(key)] = dataEntry
tdt.mutex.Unlock()
return nil
}

Expand Down Expand Up @@ -149,7 +158,9 @@ func (tdt *trackableDataTrie) MigrateDataTrieLeaves(args vmcommon.ArgsMigrateDat
return err
}

tdt.mutex.Lock()
tdt.dirtyData[string(originalKey)] = dataEntry
tdt.mutex.Unlock()
}

return nil
Expand Down Expand Up @@ -210,6 +221,9 @@ func (tdt *trackableDataTrie) DataTrie() common.DataTrieHandler {

// SaveDirtyData saved the dirty data to the trie
func (tdt *trackableDataTrie) SaveDirtyData(mainTrie common.Trie) ([]core.TrieData, error) {
tdt.mutex.Lock()
defer tdt.mutex.Unlock()

if len(tdt.dirtyData) == 0 {
return make([]core.TrieData, 0), nil
}
Expand Down

0 comments on commit 29ac95a

Please sign in to comment.