diff --git a/state/stateChanges/collector.go b/state/stateChanges/collector.go index 1994c8cd73a..58562fd7d9f 100644 --- a/state/stateChanges/collector.go +++ b/state/stateChanges/collector.go @@ -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" @@ -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 } @@ -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 } @@ -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() @@ -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 +} diff --git a/state/stateChanges/collector_test.go b/state/stateChanges/collector_test.go index 4c2730331a0..2e24e853c4b 100644 --- a/state/stateChanges/collector_test.go +++ b/state/stateChanges/collector_test.go @@ -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) + }) +}