mukan-sdk/x/auth/keeper/unordered_tx_test.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

326 lines
6.8 KiB
Go

package keeper_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
func TestManager(t *testing.T) {
var (
mgr keeper.AccountKeeper
ctx sdk.Context
)
encCfg := moduletestutil.MakeTestEncodingConfig()
reset := func() {
mockStoreKey := storetypes.NewKVStoreKey("test")
storeService := runtime.NewKVStoreService(mockStoreKey)
ctx = testutil.DefaultContextWithDB(t, mockStoreKey, storetypes.NewTransientStoreKey("transient_test")).Ctx
mgr = keeper.NewAccountKeeper(
encCfg.Codec,
storeService,
types.ProtoBaseAccount,
nil,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
keeper.WithUnorderedTransactions(true),
)
}
type utxSequence struct {
sender []byte
timeout time.Time
}
testCases := map[string]struct {
addFunc []utxSequence
blockTime time.Time
expectContains []utxSequence
expectNotContains []utxSequence
}{
"transactions are not removed when block time is before every utx": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
{
[]byte("cosmos3"),
time.Unix(10, 0),
},
},
blockTime: time.Unix(5, 0),
expectContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
{
[]byte("cosmos3"),
time.Unix(10, 0),
},
},
},
"transactions are removed if their timeout is equal to the block time": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
{
[]byte("cosmos3"),
time.Unix(10, 0),
},
},
blockTime: time.Unix(10, 10),
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
{
[]byte("cosmos3"),
time.Unix(10, 0),
},
},
},
"only some txs are removed": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(15, 0),
},
{
[]byte("cosmos3"),
time.Unix(20, 0),
},
},
blockTime: time.Unix(16, 10),
expectContains: []utxSequence{
{
[]byte("cosmos3"),
time.Unix(20, 0),
},
},
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(15, 0),
},
},
},
"empty state - no transactions to remove": {
addFunc: []utxSequence{},
blockTime: time.Unix(10, 0),
expectContains: []utxSequence{},
expectNotContains: []utxSequence{},
},
"multiple senders with same timestamp": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
},
blockTime: time.Unix(10, 1),
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos2"),
time.Unix(10, 0),
},
},
},
"same sender with multiple timestamps": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos1"),
time.Unix(15, 0),
},
{
[]byte("cosmos1"),
time.Unix(20, 0),
},
},
blockTime: time.Unix(16, 0),
expectContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(20, 0),
},
},
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 0),
},
{
[]byte("cosmos1"),
time.Unix(15, 0),
},
},
},
"nanosecond precision boundary test": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 999999998),
},
{
[]byte("cosmos2"),
time.Unix(10, 999999999),
},
{
[]byte("cosmos3"),
time.Unix(11, 0),
},
},
blockTime: time.Unix(10, 999999999),
expectContains: []utxSequence{
{
[]byte("cosmos3"),
time.Unix(11, 0),
},
},
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(10, 999999998),
},
{
[]byte("cosmos2"),
time.Unix(10, 999999999),
},
},
},
"zero timestamp test": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(0, 0),
},
},
blockTime: time.Unix(1, 0),
expectNotContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(0, 0),
},
},
},
"far future timestamp": {
addFunc: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(2^30, 0), // Very far in the future
},
},
blockTime: time.Unix(10, 0),
expectContains: []utxSequence{
{
[]byte("cosmos1"),
time.Unix(2^30, 0),
},
},
},
}
for name, tc := range testCases {
reset()
t.Run(name, func(t *testing.T) {
ctx = ctx.WithBlockTime(tc.blockTime)
for _, seq := range tc.addFunc {
err := mgr.TryAddUnorderedNonce(ctx, seq.sender, seq.timeout)
t.Logf("added transaction: %d/%s", seq.timeout.UnixNano(), seq.sender)
require.NoError(t, err)
}
t.Logf("removing txs. block_time: %d", tc.blockTime.UnixNano())
err := mgr.RemoveExpiredUnorderedNonces(ctx)
require.NoError(t, err)
for _, seq := range tc.expectNotContains {
has, err := mgr.ContainsUnorderedNonce(ctx, seq.sender, seq.timeout)
require.NoError(t, err)
require.False(t, has, "should not contain %s", seq.sender)
}
for _, seq := range tc.expectContains {
has, err := mgr.ContainsUnorderedNonce(ctx, seq.sender, seq.timeout)
require.NoError(t, err)
require.True(t, has, "expected to contain %d/%s", uint64(seq.timeout.UnixNano()), seq.sender)
}
})
}
}
func TestCannotAddDuplicate(t *testing.T) {
mockStoreKey := storetypes.NewKVStoreKey("test")
storeService := runtime.NewKVStoreService(mockStoreKey)
ctx := testutil.DefaultContextWithDB(t, mockStoreKey, storetypes.NewTransientStoreKey("transient_test")).Ctx
mgr := keeper.NewAccountKeeper(
moduletestutil.MakeTestEncodingConfig().Codec,
storeService,
types.ProtoBaseAccount,
nil,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
keeper.WithUnorderedTransactions(true),
)
addUser := []byte("foo")
timeout := time.Unix(10, 0)
err := mgr.TryAddUnorderedNonce(ctx, addUser, timeout)
require.NoError(t, err)
err = mgr.TryAddUnorderedNonce(ctx, addUser, timeout)
require.ErrorContains(t, err, "already used timeout")
}