-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix after self review (refactor, comments, additional tests).
- Loading branch information
1 parent
5f8d03f
commit 5db19b5
Showing
4 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package state | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-go/errors" | ||
"github.com/multiversx/mx-chain-go/testscommon/state" | ||
vmcommon "github.com/multiversx/mx-chain-vm-common-go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAccountNonceProvider_SetAccountsAdapter(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("with a nil the accounts adapter", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
provider, err := NewAccountNonceProvider(nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
|
||
err = provider.SetAccountsAdapter(nil) | ||
require.Error(t, errors.ErrNilAccountsAdapter) | ||
}) | ||
|
||
t.Run("with a non-nil accounts adapter", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
provider, err := NewAccountNonceProvider(nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
|
||
err = provider.SetAccountsAdapter(&state.AccountsStub{}) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestAccountNonceProvider_GetAccountNonce(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("without a backing the accounts adapter", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
provider, err := NewAccountNonceProvider(nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
|
||
nonce, err := provider.GetAccountNonce(nil) | ||
require.Error(t, errors.ErrNilAccountsAdapter) | ||
require.Equal(t, uint64(0), nonce) | ||
}) | ||
|
||
t.Run("with a backing accounts adapter (provided in constructor)", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
userAddress := []byte("alice") | ||
accounts := &state.AccountsStub{} | ||
accounts.GetExistingAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { | ||
if bytes.Compare(address, userAddress) != 0 { | ||
return nil, fmt.Errorf("account not found: %s", address) | ||
} | ||
|
||
return &state.UserAccountStub{ | ||
Nonce: 42, | ||
Check failure on line 66 in factory/state/accountNonceProvider_test.go GitHub Actions / Build (ubuntu-latest)
Check failure on line 66 in factory/state/accountNonceProvider_test.go GitHub Actions / golangci linter
|
||
}, nil | ||
} | ||
|
||
provider, err := NewAccountNonceProvider(accounts) | ||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
|
||
nonce, err := provider.GetAccountNonce(userAddress) | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(42), nonce) | ||
|
||
nonce, err = provider.GetAccountNonce([]byte("bob")) | ||
require.ErrorContains(t, err, "account not found: bob") | ||
require.Equal(t, uint64(0), nonce) | ||
}) | ||
|
||
t.Run("with a backing accounts adapter (provided using setter)", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
userAddress := []byte("alice") | ||
accounts := &state.AccountsStub{} | ||
accounts.GetExistingAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { | ||
if bytes.Compare(address, userAddress) != 0 { | ||
return nil, fmt.Errorf("account not found: %s", address) | ||
} | ||
|
||
return &state.UserAccountStub{ | ||
Nonce: 42, | ||
Check failure on line 94 in factory/state/accountNonceProvider_test.go GitHub Actions / Build (ubuntu-latest)
Check failure on line 94 in factory/state/accountNonceProvider_test.go GitHub Actions / golangci linter
|
||
}, nil | ||
} | ||
|
||
provider, err := NewAccountNonceProvider(nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
|
||
err = provider.SetAccountsAdapter(accounts) | ||
require.NoError(t, err) | ||
|
||
nonce, err := provider.GetAccountNonce(userAddress) | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(42), nonce) | ||
|
||
nonce, err = provider.GetAccountNonce([]byte("bob")) | ||
require.ErrorContains(t, err, "account not found: bob") | ||
require.Equal(t, uint64(0), nonce) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters