Skip to content

Commit

Permalink
fixes after first review
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Oct 2, 2024
1 parent ef0d3b0 commit f42f45d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 53 deletions.
3 changes: 3 additions & 0 deletions process/receiptslog/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ var ErrNilTrieInteractor = errors.New("trie interactor is nil")

// ErrNilReceiptsDataSyncer signals that a nil receipt data syncer has been provided
var ErrNilReceiptsDataSyncer = errors.New("receipts data syncer is nil")

// ErrReceiptTrieRootHashDoesNotMatch signal that the receipts trie root hash does not match
var ErrReceiptTrieRootHashDoesNotMatch = errors.New("receipts trie root hash does not match")
2 changes: 1 addition & 1 deletion process/receiptslog/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Interactor interface {
Save() ([]byte, error)
GetSerializedNode(nodeHash []byte) ([]byte, error)
RecreateTrieFromDB(rootHash []byte, db storage.Persister) (common.Trie, error)
SaveNewTrie(localTrie common.Trie) error
SaveNewTrie(localTrie common.Trie) ([]byte, error)
IsInterfaceNil() bool
}

Expand Down
37 changes: 25 additions & 12 deletions process/receiptslog/receiptsManager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package receiptslog

import (
"bytes"
"context"
"encoding/hex"
"fmt"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/state"
Expand Down Expand Up @@ -38,7 +41,7 @@ func NewReceiptsManager(args ArgsReceiptsManager) (*receiptsManager, error) {
if check.IfNil(args.TrieHandler) {
return nil, ErrNilTrieInteractor
}
if args.ReceiptsDataSyncer == nil {
if check.IfNil(args.ReceiptsDataSyncer) {
return nil, ErrNilReceiptsDataSyncer
}

Expand Down Expand Up @@ -98,19 +101,23 @@ func (rm *receiptsManager) SyncReceiptsTrie(receiptsRootHash []byte) error {
return err
}

return rm.trieInteractor.SaveNewTrie(newTrie)
newTrieRootHash, err := rm.trieInteractor.SaveNewTrie(newTrie)
if err != nil {
return err
}

if !bytes.Equal(newTrieRootHash, receiptsRootHash) {
return fmt.Errorf("%v , expected=%s, actual=%s", ErrReceiptTrieRootHashDoesNotMatch, hex.EncodeToString(receiptsRootHash), hex.EncodeToString(newTrieRootHash))
}

return nil
}

func (rm *receiptsManager) syncBranchNodesData(receiptsRootHash []byte) (map[string][]byte, error) {
err := rm.receiptsDataSyncer.SyncReceiptsDataFor([][]byte{receiptsRootHash}, context.Background())
if err != nil {
return nil, err
}
receiptsDataMap, err := rm.receiptsDataSyncer.GetReceiptsData()
receiptsDataMap, err := rm.syncData([][]byte{receiptsRootHash})
if err != nil {
return nil, err
}
rm.receiptsDataSyncer.ClearFields()

receiptTrieBranchNodesBytes := receiptsDataMap[string(receiptsRootHash)]

Expand All @@ -123,16 +130,22 @@ func (rm *receiptsManager) syncBranchNodesData(receiptsRootHash []byte) (map[str
return serializedNodes.SerializedNodes, nil
}

func (rm *receiptsManager) syncLeafNodesAndPutInStorer(hashes [][]byte, db storage.Persister) error {
func (rm *receiptsManager) syncData(hashes [][]byte) (map[string][]byte, error) {
err := rm.receiptsDataSyncer.SyncReceiptsDataFor(hashes, context.Background())
if err != nil {
return err
return nil, err
}
leafNodesMap, err := rm.receiptsDataSyncer.GetReceiptsData()

defer rm.receiptsDataSyncer.ClearFields()

return rm.receiptsDataSyncer.GetReceiptsData()
}

func (rm *receiptsManager) syncLeafNodesAndPutInStorer(hashes [][]byte, db storage.Persister) error {
leafNodesMap, err := rm.syncData(hashes)
if err != nil {
return err
}
rm.receiptsDataSyncer.ClearFields()

for leafHash, leafBytes := range leafNodesMap {
err = db.Put([]byte(leafHash), leafBytes)
Expand Down
3 changes: 3 additions & 0 deletions process/receiptslog/storageManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type storageManagerOnlyGet struct {
db storage.Persister
}

// NewStorageManagerOnlyGet will create a new instance of storageManagerOnlyGet
func NewStorageManagerOnlyGet(db storage.Persister) (*storageManagerOnlyGet, error) {
return &storageManagerOnlyGet{
db: db,
Expand Down Expand Up @@ -61,6 +62,7 @@ func (s storageManagerOnlyGet) GetFromCurrentEpoch(_ []byte) ([]byte, error) {
return nil, nil
}

// PutInEpochWithoutCache -
func (s storageManagerOnlyGet) PutInEpochWithoutCache(_ []byte, _ []byte, _ uint32) error {
return nil
}
Expand Down Expand Up @@ -97,6 +99,7 @@ func (s storageManagerOnlyGet) RemoveFromAllActiveEpochs(_ []byte) error {
return nil
}

// SetEpochForPutOperation -
func (s storageManagerOnlyGet) SetEpochForPutOperation(_ uint32) {
}

Expand Down
33 changes: 24 additions & 9 deletions process/receiptslog/trieInteractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/common/holders"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
Expand Down Expand Up @@ -65,12 +66,10 @@ func (ti *trieInteractor) CreateNewTrie() error {
}

// SaveNewTrie will save in storage the synced trie
func (ti *trieInteractor) SaveNewTrie(localTrie common.Trie) error {
func (ti *trieInteractor) SaveNewTrie(localTrie common.Trie) ([]byte, error) {
ti.localTrie = localTrie

_, err := ti.Save()

return err
return ti.Save()
}

// AddReceiptData will add receipt data in local trie
Expand Down Expand Up @@ -152,11 +151,6 @@ func (ti *trieInteractor) saveReceiptTxHashLeafKey(leafHash []byte, leafData []b
return ti.storage.Put(receiptData.TxHash, leafHash)
}

// IsInterfaceNil returns true if there is no value under the interface
func (ti *trieInteractor) IsInterfaceNil() bool {
return ti == nil
}

func checkArgs(args ArgsTrieInteractor) error {
if check.IfNil(args.EnableEpochsHandler) {
return process.ErrNilEnableEpochsHandler
Expand All @@ -174,6 +168,22 @@ func checkArgs(args ArgsTrieInteractor) error {
return nil
}

// RecreateTrieFromDB will recreate the trie from the provided storer
func (ti *trieInteractor) RecreateTrieFromDB(rootHash []byte, db storage.Persister) (common.Trie, error) {
storageManager, err := NewStorageManagerOnlyGet(db)
if err != nil {
return nil, err
}

localTrie, err := trie.NewTrie(storageManager, ti.marshaller, ti.hasher, ti.enableEpochsHandler, maxTrieLevelInMemory)
if err != nil {
return nil, err
}

rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash)
return localTrie.Recreate(rootHashHolder)
}

func (ti *trieInteractor) saveNodeData(currentNodeData *trie.CurrentNodeInfo, serializedNodes *state.SerializedNodeMap) error {
if currentNodeData.Type != trie.LeafNodeType {
serializedNodes.SerializedNodes[string(currentNodeData.Hash)] = currentNodeData.SerializedNode
Expand All @@ -187,3 +197,8 @@ func (ti *trieInteractor) saveNodeData(currentNodeData *trie.CurrentNodeInfo, se

return ti.saveReceiptTxHashLeafKey(currentNodeData.Hash, currentNodeData.Value)
}

// IsInterfaceNil returns true if there is no value under the interface
func (ti *trieInteractor) IsInterfaceNil() bool {
return ti == nil
}
24 changes: 0 additions & 24 deletions process/receiptslog/trieRecreate.go

This file was deleted.

1 change: 1 addition & 0 deletions testscommon/receiptsDataSyncerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testscommon

import "context"

// ReceiptsDataSyncerStub -
type ReceiptsDataSyncerStub struct {
GetReceiptsDataCalled func() (map[string][]byte, error)
SyncReceiptsDataForCalled func(hashes [][]byte, ctx context.Context) error
Expand Down
24 changes: 17 additions & 7 deletions trie/receiptsTrie.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package trie

import (
"fmt"

"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/storage"
Expand All @@ -22,14 +20,12 @@ func GetLeafHashesAndPutNodesInRamStorage(
return nil, err
}

hashes := decodedNode.getChildrenHashes()
if len(hashes) == 0 {
childrenHashes := decodedNode.getChildrenHashes()
if len(childrenHashes) == 0 {
continue
}

leafHashes = append(leafHashes, hashes...)

fmt.Println("here", len(nodeHash), len([]byte(nodeHash)))
leafHashes = append(leafHashes, getLeafHashesFromChildrenHashes(childrenHashes, branchNodesMap)...)

err = db.Put([]byte(nodeHash), branchNodeSerialized)
if err != nil {
Expand All @@ -39,3 +35,17 @@ func GetLeafHashesAndPutNodesInRamStorage(

return leafHashes, nil
}

func getLeafHashesFromChildrenHashes(childrenHashes [][]byte, nodesMap map[string][]byte) [][]byte {
leafHashes := make([][]byte, 0)
for _, childHash := range childrenHashes {
_, isBranchNodeOrExtensionNode := nodesMap[string(childHash)]
if isBranchNodeOrExtensionNode {
continue
}

leafHashes = append(leafHashes, childHash)
}

return leafHashes
}

0 comments on commit f42f45d

Please sign in to comment.