Skip to content

Commit

Permalink
Merge branch 'master' into v1.3.44-dev-config
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau authored Oct 25, 2022
2 parents 541cf26 + 9c8e1d4 commit 65550da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions process/txsimulator/txSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package txsimulator

import (
"encoding/hex"
"sync"

"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
Expand Down Expand Up @@ -30,6 +31,7 @@ type ArgsTxSimulator struct {
}

type transactionSimulator struct {
mutOperation sync.Mutex
txProcessor TransactionProcessor
intermProcContainer process.IntermediateProcessorContainer
addressPubKeyConverter core.PubkeyConverter
Expand Down Expand Up @@ -76,6 +78,9 @@ func NewTransactionSimulator(args ArgsTxSimulator) (*transactionSimulator, error

// ProcessTx will process the transaction in a special environment, where state-writing is not allowed
func (ts *transactionSimulator) ProcessTx(tx *transaction.Transaction) (*txSimData.SimulationResults, error) {
ts.mutOperation.Lock()
defer ts.mutOperation.Unlock()

txStatus := transaction.TxStatusPending
failReason := ""

Expand Down
34 changes: 34 additions & 0 deletions process/txsimulator/txSimulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package txsimulator
import (
"encoding/hex"
"errors"
"sync"
"testing"
"time"

"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/data"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/ElrondNetwork/elrond-go/testscommon"
"github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -228,3 +231,34 @@ func getTxSimulatorArgs() ArgsTxSimulator {
Hasher: &hashingMocks.HasherMock{},
}
}

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

numTransactionProcessorCalls := 0
args := getTxSimulatorArgs()
args.TransactionProcessor = &testscommon.TxProcessorStub{
ProcessTransactionCalled: func(transaction *transaction.Transaction) (vmcommon.ReturnCode, error) {
// deliberately not used a mutex here as to catch race conditions
numTransactionProcessorCalls++

return vmcommon.Ok, nil
},
}
txSimulator, _ := NewTransactionSimulator(args)
tx := &transaction.Transaction{Nonce: 37}

numCalls := 100
wg := sync.WaitGroup{}
wg.Add(numCalls)
for i := 0; i < numCalls; i++ {
go func(idx int) {
time.Sleep(time.Millisecond * 10)
_, _ = txSimulator.ProcessTx(tx)
wg.Done()
}(i)
}

wg.Wait()
assert.Equal(t, numCalls, numTransactionProcessorCalls)
}

0 comments on commit 65550da

Please sign in to comment.