How to use Pending method of types Package

Best Ginkgo code snippet using types.Pending

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ledger_pending.go

Source: ledger_pending.go Github

copy
1package ledger
2
3import (
4	"fmt"
5
6	"github.com/qlcchain/go-qlc/common/storage"
7	"github.com/qlcchain/go-qlc/common/types"
8)
9
10type PendingStore interface {
11	GetPending(pendingKey *types.PendingKey) (*types.PendingInfo, error)
12	GetPendings(fn func(pendingKey *types.PendingKey, pendingInfo *types.PendingInfo) error) error
13	GetPendingsByAddress(address types.Address, fn func(key *types.PendingKey, value *types.PendingInfo) error) error
14	GetPendingsByToken(account types.Address, token types.Hash, fn func(key *types.PendingKey, value *types.PendingInfo) error) error
15	PendingAmount(address types.Address, token types.Hash) (types.Balance, error)
16	AddPending(key *types.PendingKey, value *types.PendingInfo, c storage.Cache) error
17	DeletePending(key *types.PendingKey, c storage.Cache) error
18}
19
20func (l *Ledger) AddPending(key *types.PendingKey, value *types.PendingInfo, c storage.Cache) error {
21	k, err := storage.GetKeyOfParts(storage.KeyPrefixPending, key)
22	if err != nil {
23		return err
24	}
25	if err := c.Put(k, value); err != nil {
26		return err
27	}
28	return l.rcache.UpdateAccountPending(key, value, true)
29}
30
31func (l *Ledger) DeletePending(key *types.PendingKey, c storage.Cache) error {
32	k, err := storage.GetKeyOfParts(storage.KeyPrefixPending, key)
33	if err != nil {
34		return err
35	}
36	info, err := l.GetPending(key)
37	if err == nil {
38		if err := c.Delete(k); err != nil {
39			return err
40		}
41		return l.rcache.UpdateAccountPending(key, info, false)
42	}
43	return nil
44}
45
46func (l *Ledger) GetPending(pendingKey *types.PendingKey) (*types.PendingInfo, error) {
47	k, err := storage.GetKeyOfParts(storage.KeyPrefixPending, pendingKey)
48	if err != nil {
49		return nil, err
50	}
51
52	r, err := l.getFromCache(k)
53	if r != nil {
54		return r.(*types.PendingInfo), nil
55	} else {
56		if err == ErrKeyDeleted {
57			return nil, ErrPendingNotFound
58		}
59	}
60
61	v, err := l.store.Get(k)
62	if err != nil {
63		if err == storage.KeyNotFound {
64			return nil, ErrPendingNotFound
65		}
66		return nil, err
67	}
68	meta := new(types.PendingInfo)
69	if err := meta.Deserialize(v); err != nil {
70		return nil, err
71	}
72	return meta, nil
73}
74
75func (l *Ledger) GetPendings(fn func(pendingKey *types.PendingKey, pendingInfo *types.PendingInfo) error) error {
76	prefix, _ := storage.GetKeyOfParts(storage.KeyPrefixPending)
77	return l.store.Iterator(prefix, nil, func(key []byte, val []byte) error {
78		pendingKey := new(types.PendingKey)
79		if err := pendingKey.Deserialize(key[1:]); err != nil {
80			return fmt.Errorf("pendingKey deserialize: %s", err)
81		}
82		pendingInfo := new(types.PendingInfo)
83		if err := pendingInfo.Deserialize(val); err != nil {
84			return fmt.Errorf("pendingInfo deserialize: %s", err)
85		}
86		if err := fn(pendingKey, pendingInfo); err != nil {
87			return fmt.Errorf("pending func: %s", err)
88		}
89		return nil
90	})
91}
92
93func (l *Ledger) GetPendingsByAddress(address types.Address, fn func(key *types.PendingKey, value *types.PendingInfo) error) error {
94	pre := make([]byte, 0)
95	pre = append(pre, byte(storage.KeyPrefixPending))
96	pre = append(pre, address.Bytes()...)
97	return l.store.Iterator(pre, nil, func(key []byte, val []byte) error {
98		pendingKey := new(types.PendingKey)
99		if err := pendingKey.Deserialize(key[1:]); err != nil {
100			return err
101		}
102		pendingInfo := new(types.PendingInfo)
103		if err := pendingInfo.Deserialize(val); err != nil {
104			return err
105		}
106		if err := fn(pendingKey, pendingInfo); err != nil {
107			return fmt.Errorf("process pending: %s", err)
108		}
109		return nil
110	})
111}
112
113func (l *Ledger) GetPendingsByToken(account types.Address, token types.Hash, fn func(key *types.PendingKey, value *types.PendingInfo) error) error {
114	err := l.GetPendingsByAddress(account, func(key *types.PendingKey, value *types.PendingInfo) error {
115		if value.Type == token {
116			return fn(key, value)
117		}
118		return nil
119	})
120	if err != nil {
121		return fmt.Errorf("process pending by token: %s", err)
122	}
123	return nil
124}
125
126func (l *Ledger) PendingAmount(address types.Address, token types.Hash) (types.Balance, error) {
127	b, err := l.rcache.GetAccountPending(address, token)
128	if err == nil {
129		return b, nil
130	}
131	pendingAmount := types.ZeroBalance
132	if err := l.GetPendingsByToken(address, token, func(pk *types.PendingKey, pv *types.PendingInfo) error {
133		pendingAmount = pendingAmount.Add(pv.Amount)
134		return nil
135	}); err != nil {
136		return types.ZeroBalance, err
137	}
138	if err := l.rcache.AddAccountPending(address, token, pendingAmount); err != nil {
139		return types.ZeroBalance, err
140	}
141	return pendingAmount, nil
142}
143
Full Screen

pending_account.go

Source: pending_account.go Github

copy
1package keeper
2
3import (
4	"github.com/cosmos/cosmos-sdk/store/prefix"
5	sdk "github.com/cosmos/cosmos-sdk/types"
6	"github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
7)
8
9// SetPendingAccount set a specific pendingAccount in the store from its index.
10func (k Keeper) SetPendingAccount(ctx sdk.Context, pendingAccount types.PendingAccount) {
11	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PendingAccountKeyPrefix))
12	b := k.cdc.MustMarshal(&pendingAccount)
13	store.Set(types.PendingAccountKey(
14		pendingAccount.GetAddress(),
15	), b)
16}
17
18// GetPendingAccount returns a pendingAccount from its index.
19func (k Keeper) GetPendingAccount(
20	ctx sdk.Context,
21	address sdk.AccAddress,
22) (val types.PendingAccount, found bool) {
23	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PendingAccountKeyPrefix))
24
25	b := store.Get(types.PendingAccountKey(
26		address,
27	))
28	if b == nil {
29		return val, false
30	}
31
32	k.cdc.MustUnmarshal(b, &val)
33
34	return val, true
35}
36
37// Check if the Account record associated with an address is present in the store or not.
38func (k Keeper) IsPendingAccountPresent(ctx sdk.Context, address sdk.AccAddress) bool {
39	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PendingAccountKeyPrefix))
40
41	return store.Has(types.PendingAccountKey(
42		address,
43	))
44}
45
46// RemovePendingAccount removes a pendingAccount from the store.
47func (k Keeper) RemovePendingAccount(
48	ctx sdk.Context,
49	address sdk.AccAddress,
50) {
51	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PendingAccountKeyPrefix))
52	store.Delete(types.PendingAccountKey(
53		address,
54	))
55}
56
57// GetAllPendingAccount returns all pendingAccount.
58func (k Keeper) GetAllPendingAccount(ctx sdk.Context) (list []types.PendingAccount) {
59	k.IteratePendingAccounts(ctx, func(acc types.PendingAccount) (stop bool) {
60		list = append(list, acc)
61
62		return false
63	})
64
65	return
66}
67
68func (k Keeper) IteratePendingAccounts(ctx sdk.Context, cb func(account types.PendingAccount) (stop bool)) {
69	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PendingAccountKeyPrefix))
70	iterator := sdk.KVStorePrefixIterator(store, []byte{})
71
72	defer iterator.Close()
73
74	for ; iterator.Valid(); iterator.Next() {
75		var val types.PendingAccount
76		k.cdc.MustUnmarshal(iterator.Value(), &val)
77		if cb(val) {
78			break
79		}
80	}
81}
82
Full Screen

store_test.go

Source: store_test.go Github

copy
1package evidence
2
3import (
4	"testing"
5
6	"github.com/stretchr/testify/assert"
7	dbm "github.com/tendermint/tendermint/libs/db"
8	"github.com/tendermint/tendermint/types"
9)
10
11//-------------------------------------------
12
13func TestStoreAddDuplicate(t *testing.T) {
14	assert := assert.New(t)
15
16	db := dbm.NewMemDB()
17	store := NewEvidenceStore(db)
18
19	priority := int64(10)
20	ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
21
22	added := store.AddNewEvidence(ev, priority)
23	assert.True(added)
24
25	// cant add twice
26	added = store.AddNewEvidence(ev, priority)
27	assert.False(added)
28}
29
30func TestStoreCommitDuplicate(t *testing.T) {
31	assert := assert.New(t)
32
33	db := dbm.NewMemDB()
34	store := NewEvidenceStore(db)
35
36	priority := int64(10)
37	ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
38
39	store.MarkEvidenceAsCommitted(ev)
40
41	added := store.AddNewEvidence(ev, priority)
42	assert.False(added)
43}
44
45func TestStoreMark(t *testing.T) {
46	assert := assert.New(t)
47
48	db := dbm.NewMemDB()
49	store := NewEvidenceStore(db)
50
51	// before we do anything, priority/pending are empty
52	priorityEv := store.PriorityEvidence()
53	pendingEv := store.PendingEvidence(-1)
54	assert.Equal(0, len(priorityEv))
55	assert.Equal(0, len(pendingEv))
56
57	priority := int64(10)
58	ev := types.NewMockGoodEvidence(2, 1, []byte("val1"))
59
60	added := store.AddNewEvidence(ev, priority)
61	assert.True(added)
62
63	// get the evidence. verify. should be uncommitted
64	ei := store.GetEvidenceInfo(ev.Height(), ev.Hash())
65	assert.Equal(ev, ei.Evidence)
66	assert.Equal(priority, ei.Priority)
67	assert.False(ei.Committed)
68
69	// new evidence should be returns in priority/pending
70	priorityEv = store.PriorityEvidence()
71	pendingEv = store.PendingEvidence(-1)
72	assert.Equal(1, len(priorityEv))
73	assert.Equal(1, len(pendingEv))
74
75	// priority is now empty
76	store.MarkEvidenceAsBroadcasted(ev)
77	priorityEv = store.PriorityEvidence()
78	pendingEv = store.PendingEvidence(-1)
79	assert.Equal(0, len(priorityEv))
80	assert.Equal(1, len(pendingEv))
81
82	// priority and pending are now empty
83	store.MarkEvidenceAsCommitted(ev)
84	priorityEv = store.PriorityEvidence()
85	pendingEv = store.PendingEvidence(-1)
86	assert.Equal(0, len(priorityEv))
87	assert.Equal(0, len(pendingEv))
88
89	// evidence should show committed
90	newPriority := int64(0)
91	ei = store.GetEvidenceInfo(ev.Height(), ev.Hash())
92	assert.Equal(ev, ei.Evidence)
93	assert.Equal(newPriority, ei.Priority)
94	assert.True(ei.Committed)
95}
96
97func TestStorePriority(t *testing.T) {
98	assert := assert.New(t)
99
100	db := dbm.NewMemDB()
101	store := NewEvidenceStore(db)
102
103	// sorted by priority and then height
104	cases := []struct {
105		ev       types.MockGoodEvidence
106		priority int64
107	}{
108		{types.NewMockGoodEvidence(2, 1, []byte("val1")), 17},
109		{types.NewMockGoodEvidence(5, 2, []byte("val2")), 15},
110		{types.NewMockGoodEvidence(10, 2, []byte("val2")), 13},
111		{types.NewMockGoodEvidence(100, 2, []byte("val2")), 11},
112		{types.NewMockGoodEvidence(90, 2, []byte("val2")), 11},
113		{types.NewMockGoodEvidence(80, 2, []byte("val2")), 11},
114	}
115
116	for _, c := range cases {
117		added := store.AddNewEvidence(c.ev, c.priority)
118		assert.True(added)
119	}
120
121	evList := store.PriorityEvidence()
122	for i, ev := range evList {
123		assert.Equal(ev, cases[i].ev)
124	}
125}
126
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in

Trigger Pending code on LambdaTest Cloud Grid

Execute automation tests with Pending on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)