Skip to content

Commit

Permalink
Merge pull request #6679 from multiversx/fix-state-changes-collector-…
Browse files Browse the repository at this point in the history
…store-check

Fix state changes collector store check
  • Loading branch information
ssd04 authored Dec 16, 2024
2 parents 2b41727 + dd49376 commit e7c81c9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
19 changes: 13 additions & 6 deletions state/stateChanges/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"sync"

"github.com/multiversx/mx-chain-core-go/core/check"
data "github.com/multiversx/mx-chain-core-go/data/stateChange"
"github.com/multiversx/mx-chain-core-go/data/transaction"
logger "github.com/multiversx/mx-chain-logger-go"
Expand Down Expand Up @@ -43,6 +44,11 @@ func NewCollector(opts ...CollectorOption) *collector {
c.cachedTxs = make(map[string]*transaction.Transaction)
}

log.Debug("created new state changes collector",
"withRead", c.collectRead,
"withWrite", c.collectWrite,
)

return c
}

Expand Down Expand Up @@ -116,7 +122,8 @@ func (c *collector) Publish() (map[string]*data.StateChanges, error) {

// Store will store the collected state changes if it has been configured with a storer
func (c *collector) Store() error {
if c.storer != nil {
// TODO: evaluate adding a more explicit field check here
if check.IfNil(c.storer) {
return nil
}

Expand Down Expand Up @@ -201,11 +208,6 @@ func (c *collector) RevertToIndex(index int) error {
return nil
}

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

func (c *collector) getStateChangesForTxs() ([]StateChangesForTx, error) {
c.stateChangesMut.Lock()
defer c.stateChangesMut.Unlock()
Expand Down Expand Up @@ -265,3 +267,8 @@ func (c *collector) getDataAnalysisStateChangesForTxs() ([]dataAnalysisStateChan

return dataAnalysisStateChangesForTxs, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (c *collector) IsInterfaceNil() bool {
return c == nil
}
44 changes: 44 additions & 0 deletions state/stateChanges/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,47 @@ func TestDataAnalysisStateChangesCollector_Reset(t *testing.T) {
c.Reset()
require.Equal(t, 0, len(c.GetStateChanges()))
}

func TestDataAnalysisStateChangesCollector_Store(t *testing.T) {
t.Parallel()

t.Run("with storer", func(t *testing.T) {
t.Parallel()

putCalled := false
storer := &mock.PersisterStub{
PutCalled: func(key, val []byte) error {
putCalled = true
return nil
},
}

c := NewCollector(WithCollectWrite(), WithStorer(storer))

numStateChanges := 10
for i := 0; i < numStateChanges; i++ {
c.AddStateChange(getWriteStateChange())
}
c.AddTxHashToCollectedStateChanges([]byte("txHash1"), &transaction.Transaction{})

err := c.Store()
require.Nil(t, err)

require.True(t, putCalled)
})

t.Run("without storer, should return nil directly", func(t *testing.T) {
t.Parallel()

c := NewCollector(WithCollectWrite())

numStateChanges := 10
for i := 0; i < numStateChanges; i++ {
c.AddStateChange(getWriteStateChange())
}
c.AddTxHashToCollectedStateChanges([]byte("txHash1"), &transaction.Transaction{})

err := c.Store()
require.Nil(t, err)
})
}

0 comments on commit e7c81c9

Please sign in to comment.