Cosmos SDK Interview Questions - Hard
Hard-level Cosmos SDK interview questions covering advanced SDK internals, performance optimization, and complex module design.
Q1: How do you implement a custom ABCI application with advanced state management?
Answer:
Custom BaseApp:
1package app
2
3import (
4 "github.com/cosmos/cosmos-sdk/baseapp"
5 "github.com/cosmos/cosmos-sdk/types"
6)
7
8type CustomApp struct {
9 *baseapp.BaseApp
10 customState *CustomState
11}
12
13// CustomState manages application state
14type CustomState struct {
15 snapshots *SnapshotManager
16 checkpoints *CheckpointManager
17 cache *StateCache
18}
19
20// BeginBlock with custom logic
21func (app *CustomApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
22 // Custom pre-processing
23 app.customState.cache.BeginBlock()
24
25 // Standard BeginBlock
26 res := app.BaseApp.BeginBlock(req)
27
28 // Custom post-processing
29 app.customState.checkpoints.CreateCheckpoint(req.Height)
30
31 return res
32}
33
34// DeliverTx with custom validation
35func (app *CustomApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
36 ctx := app.NewContext(false, req.Header)
37
38 // Custom validation
39 if err := app.customState.ValidateTx(ctx, req.Tx); err != nil {
40 return abci.ResponseDeliverTx{
41 Code: uint32(sdkerrors.ErrInvalidRequest.ABCICode()),
42 Log: err.Error(),
43 }
44 }
45
46 // Standard DeliverTx
47 res := app.BaseApp.DeliverTx(req)
48
49 // Custom post-processing
50 app.customState.cache.CacheTx(req.Tx, res)
51
52 return res
53}
54
55// Commit with snapshot creation
56func (app *CustomApp) Commit() abci.ResponseCommit {
57 // Standard commit
58 res := app.BaseApp.Commit()
59
60 // Create snapshot periodically
61 if app.customState.snapshots.ShouldCreateSnapshot(app.LastBlockHeight()) {
62 app.customState.snapshots.CreateSnapshot(app.LastBlockHeight())
63 }
64
65 return res
66}
State Snapshotting:
1package state
2
3// SnapshotManager manages state snapshots
4type SnapshotManager struct {
5 store sdk.KVStore
6 interval uint64
7}
8
9func (sm *SnapshotManager) CreateSnapshot(height int64) error {
10 // Create snapshot of all stores
11 snapshot := &Snapshot{
12 Height: height,
13 Stores: make(map[string][]byte),
14 }
15
16 // Snapshot each module store
17 for _, storeKey := range sm.storeKeys {
18 store := sm.getStore(storeKey)
19 snapshot.Stores[storeKey.Name()] = sm.snapshotStore(store)
20 }
21
22 // Persist snapshot
23 return sm.persistSnapshot(snapshot)
24}
25
26func (sm *SnapshotManager) RestoreSnapshot(height int64) error {
27 snapshot, err := sm.loadSnapshot(height)
28 if err != nil {
29 return err
30 }
31
32 // Restore each store
33 for storeKey, data := range snapshot.Stores {
34 store := sm.getStore(storeKey)
35 sm.restoreStore(store, data)
36 }
37
38 return nil
39}
Q2: How do you implement advanced transaction ordering and MEV protection?
Answer:
MEV-Resistant Mempool:
1package mempool
2
3// EncryptedMempool encrypts transactions to prevent front-running
4type EncryptedMempool struct {
5 transactions map[string]*EncryptedTx
6 decryptionKey []byte
7}
8
9type EncryptedTx struct {
10 EncryptedData []byte
11 Commitment []byte
12 Timestamp time.Time
13}
14
15// Insert encrypts transaction before adding
16func (mp *EncryptedMempool) Insert(ctx sdk.Context, tx sdk.Tx) error {
17 // Encrypt transaction
18 encrypted, err := mp.encryptTx(tx)
19 if err != nil {
20 return err
21 }
22
23 // Create commitment
24 commitment := mp.createCommitment(tx)
25
26 // Store encrypted
27 mp.transactions[string(tx.Hash())] = &EncryptedTx{
28 EncryptedData: encrypted,
29 Commitment: commitment,
30 Timestamp: ctx.BlockTime(),
31 }
32
33 return nil
34}
35
36// Reveal decrypts transactions at block proposal time
37func (mp *EncryptedMempool) Reveal(ctx sdk.Context, txHashes [][]byte) ([]sdk.Tx, error) {
38 var txs []sdk.Tx
39
40 for _, hash := range txHashes {
41 encryptedTx, exists := mp.transactions[string(hash)]
42 if !exists {
43 continue
44 }
45
46 // Verify commitment
47 if !mp.verifyCommitment(encryptedTx.Commitment, encryptedTx.EncryptedData) {
48 return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid commitment")
49 }
50
51 // Decrypt
52 tx, err := mp.decryptTx(encryptedTx.EncryptedData)
53 if err != nil {
54 return nil, err
55 }
56
57 txs = append(txs, tx)
58 }
59
60 return txs, nil
61}
Fair Ordering:
1// FairOrderingMempool implements fair transaction ordering
2type FairOrderingMempool struct {
3 transactions []*PrioritizedTx
4 maxPriority int64
5}
6
7type PrioritizedTx struct {
8 Tx sdk.Tx
9 Priority int64
10 Nonce uint64
11 Sender sdk.AccAddress
12}
13
14// Insert with fair priority calculation
15func (mp *FairOrderingMempool) Insert(ctx sdk.Context, tx sdk.Tx) error {
16 // Calculate priority based on fee and time
17 priority := mp.calculatePriority(ctx, tx)
18
19 // Get sender nonce
20 sender := tx.GetSigners()[0]
21 nonce := mp.getNonce(ctx, sender)
22
23 prioritized := &PrioritizedTx{
24 Tx: tx,
25 Priority: priority,
26 Nonce: nonce,
27 Sender: sender,
28 }
29
30 // Insert maintaining order
31 mp.insertSorted(prioritized)
32
33 return nil
34}
35
36// calculatePriority calculates fair priority
37func (mp *FairOrderingMempool) calculatePriority(ctx sdk.Context, tx sdk.Tx) int64 {
38 feeTx := tx.(sdk.FeeTx)
39 fee := feeTx.GetFee()
40
41 // Base priority from fee
42 priority := fee.AmountOf("stake").Int64()
43
44 // Reduce priority for recent transactions (prevent front-running)
45 age := ctx.BlockTime().Unix() - mp.getTxTimestamp(tx).Unix()
46 if age < 60 { // Less than 1 minute
47 priority = priority / 2
48 }
49
50 return priority
51}
Q3: How do you implement cross-chain communication and IBC integration?
Answer:
IBC Module Integration:
1package keeper
2
3import (
4 "github.com/cosmos/ibc-go/v3/modules/core/keeper"
5 channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
6)
7
8type Keeper struct {
9 storeKey sdk.StoreKey
10 ibcKeeper *ibckeeper.Keeper
11 channelKeeper channelkeeper.Keeper
12}
13
14// SendPacket sends packet via IBC
15func (k Keeper) SendPacket(
16 ctx sdk.Context,
17 sourcePort,
18 sourceChannel string,
19 packetData []byte,
20 timeoutHeight clienttypes.Height,
21 timeoutTimestamp uint64,
22) error {
23 // Create packet
24 packet := channeltypes.NewPacket(
25 packetData,
26 k.getNextSequenceSend(ctx, sourcePort, sourceChannel),
27 sourcePort,
28 sourceChannel,
29 k.getCounterpartyPort(sourcePort),
30 k.getCounterpartyChannel(sourceChannel),
31 timeoutHeight,
32 timeoutTimestamp,
33 )
34
35 // Send via IBC
36 return k.channelKeeper.SendPacket(ctx, packet)
37}
38
39// OnRecvPacket handles received IBC packet
40func (k Keeper) OnRecvPacket(
41 ctx sdk.Context,
42 packet channeltypes.Packet,
43 relayer sdk.AccAddress,
44) (*sdk.Result, error) {
45 // Unmarshal packet data
46 var data MyPacketData
47 if err := k.cdc.Unmarshal(packet.GetData(), &data); err != nil {
48 return nil, err
49 }
50
51 // Process packet
52 switch data.Type {
53 case PacketTypeOrder:
54 return k.handleOrderPacket(ctx, data, relayer)
55 case PacketTypePayment:
56 return k.handlePaymentPacket(ctx, data, relayer)
57 default:
58 return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown packet type")
59 }
60}
61
62// AcknowledgePacket handles packet acknowledgment
63func (k Keeper) OnAcknowledgementPacket(
64 ctx sdk.Context,
65 packet channeltypes.Packet,
66 acknowledgement []byte,
67 relayer sdk.AccAddress,
68) (*sdk.Result, error) {
69 // Parse acknowledgement
70 var ack channeltypes.Acknowledgement
71 if err := k.cdc.Unmarshal(acknowledgement, &ack); err != nil {
72 return nil, err
73 }
74
75 if ack.Success() {
76 // Handle successful acknowledgement
77 return k.handleSuccessAck(ctx, packet, relayer)
78 } else {
79 // Handle failure
80 return k.handleFailureAck(ctx, packet, ack.Error(), relayer)
81 }
82}
Cross-Chain State Sync:
1// CrossChainStateSync synchronizes state across chains
2type CrossChainStateSync struct {
3 keeper Keeper
4 ibcKeeper *ibckeeper.Keeper
5}
6
7// SyncState syncs state to target chain
8func (ccs *CrossChainStateSync) SyncState(
9 ctx sdk.Context,
10 targetChain string,
11 stateKey string,
12) error {
13 // Get state
14 state := ccs.keeper.GetState(ctx, stateKey)
15
16 // Create sync packet
17 packetData := SyncPacketData{
18 Type: PacketTypeStateSync,
19 Key: stateKey,
20 State: state,
21 Height: ctx.BlockHeight(),
22 }
23
24 // Send via IBC
25 return ccs.keeper.SendPacket(
26 ctx,
27 "transfer",
28 ccs.getChannel(targetChain),
29 packetData,
30 clienttypes.Height{},
31 0,
32 )
33}
Q4: How do you implement advanced caching and state optimization?
Answer:
Multi-Level Cache:
1package cache
2
3// MultiLevelCache implements L1/L2 caching
4type MultiLevelCache struct {
5 l1Cache *sync.Map // In-memory
6 l2Cache *LRUCache // Persistent
7 store sdk.KVStore
8}
9
10func (mlc *MultiLevelCache) Get(ctx sdk.Context, key []byte) ([]byte, error) {
11 // Check L1 cache
12 if value, ok := mlc.l1Cache.Load(string(key)); ok {
13 return value.([]byte), nil
14 }
15
16 // Check L2 cache
17 if value, ok := mlc.l2Cache.Get(string(key)); ok {
18 // Promote to L1
19 mlc.l1Cache.Store(string(key), value)
20 return value.([]byte), nil
21 }
22
23 // Load from store
24 value := mlc.store.Get(key)
25 if value == nil {
26 return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "key not found")
27 }
28
29 // Cache in L2
30 mlc.l2Cache.Set(string(key), value)
31
32 // Cache in L1
33 mlc.l1Cache.Store(string(key), value)
34
35 return value, nil
36}
37
38// Write-through cache
39func (mlc *MultiLevelCache) Set(ctx sdk.Context, key, value []byte) {
40 // Write to store first
41 mlc.store.Set(key, value)
42
43 // Update L2 cache
44 mlc.l2Cache.Set(string(key), value)
45
46 // Update L1 cache
47 mlc.l1Cache.Store(string(key), value)
48}
State Compression:
1// CompressedStore compresses values before storing
2type CompressedStore struct {
3 store sdk.KVStore
4 compressor *Compressor
5}
6
7func (cs *CompressedStore) Set(key, value []byte) {
8 // Compress value
9 compressed := cs.compressor.Compress(value)
10
11 // Store compressed
12 cs.store.Set(key, compressed)
13}
14
15func (cs *CompressedStore) Get(key []byte) []byte {
16 // Get compressed
17 compressed := cs.store.Get(key)
18 if compressed == nil {
19 return nil
20 }
21
22 // Decompress
23 return cs.compressor.Decompress(compressed)
24}
Q5: How do you implement advanced validator set management and slashing?
Answer:
Custom Validator Set:
1package keeper
2
3// ValidatorSetManager manages custom validator set
4type ValidatorSetManager struct {
5 keeper Keeper
6}
7
8// UpdateValidatorSet updates validators based on custom rules
9func (vsm *ValidatorSetManager) UpdateValidatorSet(ctx sdk.Context) error {
10 // Get current validators
11 validators := vsm.keeper.GetAllValidators(ctx)
12
13 // Calculate new set based on:
14 // - Staking power
15 // - Performance metrics
16 // - Reputation
17 newSet := vsm.calculateNewSet(ctx, validators)
18
19 // Apply changes
20 return vsm.applyValidatorSet(ctx, newSet)
21}
22
23// calculateNewSet calculates optimal validator set
24func (vsm *ValidatorSetManager) calculateNewSet(
25 ctx sdk.Context,
26 validators []Validator,
27) []Validator {
28 // Score validators
29 scored := make([]ScoredValidator, len(validators))
30 for i, val := range validators {
31 scored[i] = ScoredValidator{
32 Validator: val,
33 Score: vsm.calculateScore(ctx, val),
34 }
35 }
36
37 // Sort by score
38 sort.Slice(scored, func(i, j int) bool {
39 return scored[i].Score > scored[j].Score
40 })
41
42 // Select top N
43 maxValidators := vsm.keeper.GetMaxValidators(ctx)
44 selected := make([]Validator, 0, maxValidators)
45 for i := 0; i < len(scored) && i < maxValidators; i++ {
46 selected = append(selected, scored[i].Validator)
47 }
48
49 return selected
50}
51
52// calculateScore calculates validator score
53func (vsm *ValidatorSetManager) calculateScore(
54 ctx sdk.Context,
55 val Validator,
56) sdk.Dec {
57 // Base score from staking power
58 baseScore := sdk.NewDecFromInt(val.Tokens)
59
60 // Performance multiplier
61 uptime := vsm.keeper.GetUptime(ctx, val.OperatorAddress)
62 performanceMultiplier := sdk.NewDec(1).Add(uptime.Mul(sdk.NewDecWithPrec(1, 1)))
63
64 // Reputation multiplier
65 reputation := vsm.keeper.GetReputation(ctx, val.OperatorAddress)
66 reputationMultiplier := sdk.NewDec(1).Add(reputation.Mul(sdk.NewDecWithPrec(5, 2)))
67
68 // Final score
69 return baseScore.Mul(performanceMultiplier).Mul(reputationMultiplier)
70}
Advanced Slashing:
1// SlashingManager implements advanced slashing
2type SlashingManager struct {
3 keeper Keeper
4}
5
6// SlashValidator slashes validator with custom logic
7func (sm *SlashingManager) SlashValidator(
8 ctx sdk.Context,
9 validator sdk.ValAddress,
10 infractionType InfractionType,
11 evidence Evidence,
12) error {
13 // Get validator
14 val, found := sm.keeper.GetValidator(ctx, validator)
15 if !found {
16 return sdkerrors.Wrap(sdkerrors.ErrNotFound, "validator not found")
17 }
18
19 // Calculate slash amount based on:
20 // - Infraction type
21 // - Validator history
22 // - Evidence strength
23 slashAmount := sm.calculateSlashAmount(ctx, val, infractionType, evidence)
24
25 // Apply slash
26 val.Tokens = val.Tokens.Sub(slashAmount)
27 sm.keeper.SetValidator(ctx, val)
28
29 // Distribute slashed tokens
30 sm.distributeSlashedTokens(ctx, slashAmount, evidence)
31
32 // Update reputation
33 sm.keeper.DecreaseReputation(ctx, validator, infractionType)
34
35 // Emit event
36 ctx.EventManager().EmitEvent(
37 sdk.NewEvent(
38 types.EventTypeSlash,
39 sdk.NewAttribute(types.AttributeKeyValidator, validator.String()),
40 sdk.NewAttribute(types.AttributeKeySlashAmount, slashAmount.String()),
41 sdk.NewAttribute(types.AttributeKeyInfractionType, string(infractionType)),
42 ),
43 )
44
45 return nil
46}
Q6: How do you implement parallel transaction processing?
Answer:
Parallel Execution:
1package execution
2
3// ParallelExecutor executes transactions in parallel
4type ParallelExecutor struct {
5 maxWorkers int
6 semaphore chan struct{}
7}
8
9func NewParallelExecutor(maxWorkers int) *ParallelExecutor {
10 return &ParallelExecutor{
11 maxWorkers: maxWorkers,
12 semaphore: make(chan struct{}, maxWorkers),
13 }
14}
15
16// ExecuteBatch executes transactions in parallel
17func (pe *ParallelExecutor) ExecuteBatch(
18 ctx sdk.Context,
19 txs []sdk.Tx,
20) ([]sdk.Result, []error) {
21 results := make([]sdk.Result, len(txs))
22 errors := make([]error, len(txs))
23
24 var wg sync.WaitGroup
25
26 for i, tx := range txs {
27 wg.Add(1)
28
29 go func(idx int, transaction sdk.Tx) {
30 defer wg.Done()
31
32 // Acquire semaphore
33 pe.semaphore <- struct{}{}
34 defer func() { <-pe.semaphore }()
35
36 // Execute transaction
37 result, err := pe.executeTx(ctx, transaction)
38 results[idx] = result
39 errors[idx] = err
40 }(i, tx)
41 }
42
43 wg.Wait()
44
45 return results, errors
46}
47
48// executeTx executes single transaction
49func (pe *ParallelExecutor) executeTx(ctx sdk.Context, tx sdk.Tx) (sdk.Result, error) {
50 // Create isolated context for parallel execution
51 isolatedCtx := ctx.WithIsolated(true)
52
53 // Execute
54 return pe.handler.DeliverTx(isolatedCtx, tx)
55}
Dependency Analysis:
1// DependencyAnalyzer analyzes transaction dependencies
2type DependencyAnalyzer struct{}
3
4// AnalyzeDependencies finds transaction dependencies
5func (da *DependencyAnalyzer) AnalyzeDependencies(txs []sdk.Tx) [][]int {
6 graph := make(map[int][]int)
7
8 // Build dependency graph
9 for i, tx := range txs {
10 deps := da.findDependencies(tx, txs)
11 graph[i] = deps
12 }
13
14 // Topological sort
15 return da.topologicalSort(graph)
16}
17
18// findDependencies finds transactions this tx depends on
19func (da *DependencyAnalyzer) findDependencies(tx sdk.Tx, allTxs []sdk.Tx) []int {
20 var deps []int
21
22 // Check if tx reads/writes same accounts
23 txAccounts := da.getAccounts(tx)
24
25 for i, otherTx := range allTxs {
26 if i == 0 {
27 continue
28 }
29
30 otherAccounts := da.getAccounts(otherTx)
31
32 // Check for overlap
33 if da.hasOverlap(txAccounts, otherAccounts) {
34 deps = append(deps, i)
35 }
36 }
37
38 return deps
39}
Q7: How do you implement advanced event processing and indexing?
Answer:
Event Indexer:
1package indexing
2
3// EventIndexer indexes events for efficient querying
4type EventIndexer struct {
5 store sdk.KVStore
6}
7
8// IndexEvent indexes an event
9func (ei *EventIndexer) IndexEvent(ctx sdk.Context, event sdk.Event) error {
10 // Index by type
11 typeKey := ei.getTypeKey(event.Type)
12 ei.addToIndex(typeKey, event)
13
14 // Index by each attribute
15 for _, attr := range event.Attributes {
16 attrKey := ei.getAttributeKey(event.Type, attr.Key, attr.Value)
17 ei.addToIndex(attrKey, event)
18 }
19
20 // Index by time range
21 timeKey := ei.getTimeKey(ctx.BlockTime(), event.Type)
22 ei.addToIndex(timeKey, event)
23
24 return nil
25}
26
27// QueryEvents queries events with filters
28func (ei *EventIndexer) QueryEvents(
29 ctx sdk.Context,
30 filters []EventFilter,
31 pagination *query.PageRequest,
32) ([]sdk.Event, *query.PageResponse, error) {
33 // Build query plan
34 plan := ei.buildQueryPlan(filters)
35
36 // Execute query
37 events, err := ei.executeQuery(ctx, plan, pagination)
38 if err != nil {
39 return nil, nil, err
40 }
41
42 return events, pagination, nil
43}
44
45// buildQueryPlan optimizes query execution
46func (ei *EventIndexer) buildQueryPlan(filters []EventFilter) *QueryPlan {
47 plan := &QueryPlan{}
48
49 // Find most selective filter
50 mostSelective := ei.findMostSelective(filters)
51 plan.PrimaryFilter = mostSelective
52
53 // Add secondary filters
54 for _, filter := range filters {
55 if filter != mostSelective {
56 plan.SecondaryFilters = append(plan.SecondaryFilters, filter)
57 }
58 }
59
60 return plan
61}
Q8: How do you implement advanced governance with quadratic voting?
Answer:
Quadratic Voting:
1package governance
2
3// QuadraticVoting implements quadratic voting
4type QuadraticVoting struct {
5 keeper Keeper
6}
7
8// Vote with quadratic weighting
9func (qv *QuadraticVoting) Vote(
10 ctx sdk.Context,
11 proposalId uint64,
12 voter sdk.AccAddress,
13 option VoteOption,
14 votingPower sdk.Int,
15) error {
16 // Calculate cost (quadratic)
17 cost := qv.calculateCost(votingPower)
18
19 // Check if voter has enough tokens
20 balance := qv.keeper.GetBalance(ctx, voter)
21 if balance.AmountOf("stake").LT(cost) {
22 return sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "insufficient voting power")
23 }
24
25 // Deduct cost
26 err := qv.keeper.SendCoinsFromAccountToModule(
27 ctx,
28 voter,
29 types.GovernanceModuleName,
30 sdk.NewCoins(sdk.NewCoin("stake", cost)),
31 )
32 if err != nil {
33 return err
34 }
35
36 // Record vote
37 vote := types.Vote{
38 ProposalId: proposalId,
39 Voter: voter.String(),
40 Option: option,
41 VotingPower: votingPower,
42 Cost: cost,
43 }
44
45 qv.keeper.SetVote(ctx, vote)
46
47 // Update proposal tally
48 qv.keeper.UpdateTally(ctx, proposalId, option, votingPower)
49
50 return nil
51}
52
53// calculateCost calculates quadratic cost
54func (qv *QuadraticVoting) calculateCost(votingPower sdk.Int) sdk.Int {
55 // Cost = votingPower^2
56 return votingPower.Mul(votingPower)
57}
Q9: How do you implement state machine replication and recovery?
Answer:
State Replication:
1package replication
2
3// StateReplicator replicates state across nodes
4type StateReplicator struct {
5 keeper Keeper
6 peers []Peer
7}
8
9// ReplicateState replicates state to peers
10func (sr *StateReplicator) ReplicateState(ctx sdk.Context) error {
11 // Get current state
12 state := sr.getStateSnapshot(ctx)
13
14 // Replicate to all peers
15 var wg sync.WaitGroup
16 errors := make(chan error, len(sr.peers))
17
18 for _, peer := range sr.peers {
19 wg.Add(1)
20 go func(p Peer) {
21 defer wg.Done()
22 if err := p.SendState(state); err != nil {
23 errors <- err
24 }
25 }(peer)
26 }
27
28 wg.Wait()
29 close(errors)
30
31 // Check for errors
32 for err := range errors {
33 if err != nil {
34 return err
35 }
36 }
37
38 return nil
39}
40
41// RecoverState recovers state from peers
42func (sr *StateReplicator) RecoverState(ctx sdk.Context, targetHeight int64) error {
43 // Request state from peers
44 states := make([]StateSnapshot, 0)
45
46 for _, peer := range sr.peers {
47 state, err := peer.RequestState(targetHeight)
48 if err != nil {
49 continue
50 }
51 states = append(states, state)
52 }
53
54 // Verify and merge states
55 verifiedState := sr.verifyAndMerge(states)
56
57 // Restore state
58 return sr.restoreState(ctx, verifiedState)
59}
Q10: How do you optimize Cosmos SDK application performance at scale?
Answer:
Performance Optimization Strategies:
- Connection Pooling:
1// ConnectionPool manages database connections
2type ConnectionPool struct {
3 pool *sql.DB
4 maxConnections int
5}
6
7func NewConnectionPool(maxConn int) *ConnectionPool {
8 db, _ := sql.Open("postgres", "...")
9 db.SetMaxOpenConns(maxConn)
10 db.SetMaxIdleConns(maxConn / 2)
11 return &ConnectionPool{pool: db, maxConnections: maxConn}
12}
- Batch Processing:
1// BatchProcessor processes operations in batches
2func (k Keeper) BatchUpdate(ctx sdk.Context, updates []Update) error {
3 batch := k.store.NewBatch()
4 defer batch.Close()
5
6 for _, update := range updates {
7 batch.Set(update.Key, update.Value)
8 }
9
10 return batch.Write()
11}
- Async Operations:
1// AsyncProcessor processes operations asynchronously
2type AsyncProcessor struct {
3 queue chan Operation
4 workers int
5}
6
7func (ap *AsyncProcessor) ProcessAsync(op Operation) {
8 ap.queue <- op
9}
10
11func (ap *AsyncProcessor) worker() {
12 for op := range ap.queue {
13 ap.process(op)
14 }
15}
- State Pruning:
1// StatePruner prunes old state
2func (k Keeper) PruneState(ctx sdk.Context, keepHeight int64) error {
3 currentHeight := ctx.BlockHeight()
4
5 if currentHeight-keepHeight < 100 {
6 return nil // Don't prune recent state
7 }
8
9 // Prune old state
10 return k.pruneOldState(ctx, currentHeight-keepHeight)
11}
Related Snippets
- Bitcoin (Nakamoto) Consensus Interview Questions
Bitcoin consensus algorithm interview questions covering Proof-of-Work (PoW) and … - Byzantine Fault Tolerance (BFT) Consensus Interview Questions
Byzantine Fault Tolerance (BFT) consensus algorithm interview questions covering … - Cardano Interview Questions - Easy
Easy-level Cardano interview questions covering blockchain basics, Plutus, and … - Cardano Interview Questions - Hard
Hard-level Cardano interview questions covering advanced optimization and formal … - Cardano Interview Questions - Medium
Medium-level Cardano interview questions covering advanced Plutus development … - Consensus Algorithms Comparison Interview Questions
Consensus algorithm comparison and general implementation interview questions. … - Cosmos Chain Operations Interview Questions - Easy
Easy-level Cosmos chain operation interview questions covering chain operations, … - Cosmos Chain Operations Interview Questions - Hard
Hard-level Cosmos chain operation questions covering advanced algorithms, … - Cosmos Chain Operations Interview Questions - Medium
Medium-level Cosmos chain operation questions covering advanced chain … - Cosmos SDK Interview Questions - Easy
Easy-level Cosmos SDK interview questions covering chain code, SDK basics, and … - Cosmos SDK Interview Questions - Medium
Medium-level Cosmos SDK interview questions covering advanced module … - Ethereum Proof-of-Stake Consensus Interview Questions
Ethereum Proof-of-Stake consensus algorithm interview questions covering Casper … - Ouroboros (Cardano) Consensus Interview Questions
Ouroboros consensus algorithm interview questions covering Cardano's … - Paxos Consensus Interview Questions
Paxos consensus algorithm interview questions covering the classic distributed … - Polkadot (NPoS) Consensus Interview Questions
Polkadot consensus algorithm interview questions covering Nominated … - Polkadot Interview Questions - Easy
Easy-level Polkadot interview questions covering blockchain basics, Substrate, … - Polkadot Interview Questions - Hard
Hard-level Polkadot interview questions covering advanced optimization and … - Polkadot Interview Questions - Medium
Medium-level Polkadot interview questions covering advanced Substrate … - Solana Interview Questions - Easy
Easy-level Solana interview questions covering blockchain basics, programs, and … - Solana Interview Questions - Hard
Hard-level Solana interview questions covering advanced optimization, security, … - Solana Interview Questions - Medium
Medium-level Solana interview questions covering advanced program development, … - Solana Proof of History Consensus Interview Questions
Solana consensus algorithm interview questions covering Proof of History (PoH) … - Tendermint Consensus Interview Questions
Tendermint consensus algorithm interview questions covering the Byzantine Fault … - Web3 Interview Questions - Easy
Easy-level Web3 interview questions covering blockchain fundamentals, Ethereum, … - Web3 Interview Questions - Hard
Hard-level Web3 interview questions covering MEV, zero-knowledge proofs, … - Web3 Interview Questions - Medium
Medium-level Web3 interview questions covering DeFi, advanced Solidity, …