Skip to content

Commit

Permalink
improve formating for decoders.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 12, 2024
1 parent 42d1389 commit 42bf24d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 51 deletions.
24 changes: 15 additions & 9 deletions x/crosschain/simulation/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,50 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
var cctxA, cctxB types.CrossChainTx
cdc.MustUnmarshal(kvA.Value, &cctxA)
cdc.MustUnmarshal(kvB.Value, &cctxB)
return fmt.Sprintf("cctx key %v\n%v", cctxA, cctxB)
return fmt.Sprintf("key %s value A %v value B %v", types.CCTXKey, cctxA, cctxB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.LastBlockHeightKey)):
var lastBlockHeightA, lastBlockHeightB types.LastBlockHeight
cdc.MustUnmarshal(kvA.Value, &lastBlockHeightA)
cdc.MustUnmarshal(kvB.Value, &lastBlockHeightB)
return fmt.Sprintf("last block height key %v\n%v", lastBlockHeightA, lastBlockHeightB)
return fmt.Sprintf("key %s value A %v value B %v", types.LastBlockHeightKey, lastBlockHeightA, lastBlockHeightB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.FinalizedInboundsKey)):
var finalizedInboundsA, finalizedInboundsB []byte
finalizedInboundsA = kvA.Value
finalizedInboundsB = kvB.Value
return fmt.Sprintf("finalized inbounds key %v\n%v", finalizedInboundsA, finalizedInboundsB)
return fmt.Sprintf("key %s value A %v value B %v", types.FinalizedInboundsKey, finalizedInboundsA, finalizedInboundsB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.GasPriceKey)):
var gasPriceA, gasPriceB types.GasPrice
cdc.MustUnmarshal(kvA.Value, &gasPriceA)
cdc.MustUnmarshal(kvB.Value, &gasPriceB)
return fmt.Sprintf("gas price key %v\n%v", gasPriceA, gasPriceB)
return fmt.Sprintf("key %s value A %v value B %v", types.GasPriceKey, gasPriceA, gasPriceB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.OutboundTrackerKeyPrefix)):
var outboundTrackerA, outboundTrackerB types.OutboundTracker
cdc.MustUnmarshal(kvA.Value, &outboundTrackerA)
cdc.MustUnmarshal(kvB.Value, &outboundTrackerB)
return fmt.Sprintf("outbound trackers key %v\n%v", outboundTrackerA, outboundTrackerB)
return fmt.Sprintf("key %s value A %v value B %v", types.OutboundTrackerKeyPrefix, outboundTrackerA, outboundTrackerB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.InboundTrackerKeyPrefix)):
var inboundTrackerA, inboundTrackerB types.InboundTracker
cdc.MustUnmarshal(kvA.Value, &inboundTrackerA)
cdc.MustUnmarshal(kvB.Value, &inboundTrackerB)
return fmt.Sprintf("inbound trackers key %v\n%v", inboundTrackerA, inboundTrackerB)
return fmt.Sprintf("key %s value A %v value B %v", types.InboundTrackerKeyPrefix, inboundTrackerA, inboundTrackerB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.ZetaAccountingKey)):
var zetaAccountingA, zetaAccountingB types.ZetaAccounting
cdc.MustUnmarshal(kvA.Value, &zetaAccountingA)
cdc.MustUnmarshal(kvB.Value, &zetaAccountingB)
return fmt.Sprintf("zeta accounting key %v\n%v", zetaAccountingA, zetaAccountingB)
return fmt.Sprintf("key %s value A %v value B %v", types.ZetaAccountingKey, zetaAccountingA, zetaAccountingB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.RateLimiterFlagsKey)):
var rateLimiterFlagsA, rateLimiterFlagsB types.RateLimiterFlags
cdc.MustUnmarshal(kvA.Value, &rateLimiterFlagsA)
cdc.MustUnmarshal(kvB.Value, &rateLimiterFlagsB)
return fmt.Sprintf("rate limiter flags key %v\n%v", rateLimiterFlagsA, rateLimiterFlagsB)
return fmt.Sprintf("key %s value A %v value B %v", types.RateLimiterFlagsKey, rateLimiterFlagsA, rateLimiterFlagsB)
default:
panic(fmt.Sprintf("invalid crosschain key prefix %X", kvA.Key[:1]))
panic(
fmt.Sprintf(
"invalid crosschain key prefix %X (first 8 bytes: %X)",
kvA.Key[:1],
kvA.Key[:min(8, len(kvA.Key))],
),
)
}
}
}
16 changes: 8 additions & 8 deletions x/crosschain/simulation/decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func TestDecodeStore(t *testing.T) {
name string
expectedLog string
}{
{"CrossChainTx", fmt.Sprintf("cctx key %v\n%v", *cctx, *cctx)},
{"LastBlockHeight", fmt.Sprintf("last block height key %v\n%v", *lastBlockHeight, *lastBlockHeight)},
{"GasPrice", fmt.Sprintf("gas price key %v\n%v", *gasPrice, *gasPrice)},
{"OutboundTracker", fmt.Sprintf("outbound trackers key %v\n%v", outboundTracker, outboundTracker)},
{"InboundTracker", fmt.Sprintf("inbound trackers key %v\n%v", inboundTracker, inboundTracker)},
{"ZetaAccounting", fmt.Sprintf("zeta accounting key %v\n%v", zetaAccounting, zetaAccounting)},
{"RateLimiterFlags", fmt.Sprintf("rate limiter flags key %v\n%v", rateLimiterFlags, rateLimiterFlags)},
{"FinalizedInbounds", fmt.Sprintf("finalized inbounds key %v\n%v", []byte{1}, []byte{1})},
{"CrossChainTx", fmt.Sprintf("key %s value A %v value B %v", types.CCTXKey, *cctx, *cctx)},
{"LastBlockHeight", fmt.Sprintf("key %s value A %v value B %v", types.LastBlockHeightKey, *lastBlockHeight, *lastBlockHeight)},
{"GasPrice", fmt.Sprintf("key %s value A %v value B %v", types.GasPriceKey, *gasPrice, *gasPrice)},
{"OutboundTracker", fmt.Sprintf("key %s value A %v value B %v", types.OutboundTrackerKeyPrefix, outboundTracker, outboundTracker)},
{"InboundTracker", fmt.Sprintf("key %s value A %v value B %v", types.InboundTrackerKeyPrefix, inboundTracker, inboundTracker)},
{"ZetaAccounting", fmt.Sprintf("key %s value A %v value B %v", types.ZetaAccountingKey, zetaAccounting, zetaAccounting)},
{"RateLimiterFlags", fmt.Sprintf("key %s value A %v value B %v", types.RateLimiterFlagsKey, rateLimiterFlags, rateLimiterFlags)},
{"FinalizedInbounds", fmt.Sprintf("key %s value A %v value B %v", types.FinalizedInboundsKey, []byte{1}, []byte{1})},
}

for i, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/simulation/operation_add_outbound_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func SimulateMsgAddOutboundTracker(k keeper.Keeper) simtypes.Operation {
}

// Verify if the tracker is maxed
tracker, found := k.GetOutboundTracker(ctx, chainID, uint64(nonce))
tracker, found := k.GetOutboundTracker(ctx, chainID, uint64(nonce)) // nosec G115 - overflow is not an issue here
if found && tracker.IsMaxed() {
return simtypes.NoOpMsg(
types.ModuleName,
Expand Down Expand Up @@ -115,7 +115,7 @@ func SimulateMsgAddOutboundTracker(k keeper.Keeper) simtypes.Operation {
msg := types.MsgAddOutboundTracker{
Creator: randomObserver,
ChainId: chainID,
Nonce: uint64(nonce),
Nonce: uint64(nonce), // nosec G115 - overflow is not an issue here
TxHash: txHash.String(),
}

Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/simulation/operation_gas_price_voter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func SimulateMsgVoteGasPrice(k keeper.Keeper) simtypes.Operation {
ChainId: randomChainID,
Price: price,
PriorityFee: priorityFee,
BlockNumber: uint64(ctx.BlockHeight()) + r.Uint64()%1000,
BlockNumber: uint64(ctx.BlockHeight()) + r.Uint64()%1000, // nosec G115 - overflow is not a issue here
Supply: sdk.NewInt(r.Int63n(1e18)).String(),
}

Expand Down
9 changes: 8 additions & 1 deletion x/observer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
k.SetObserverSet(ctx, types.ObserverSet{})
}

observerCount := uint64(genState.Observers.Len())
observerCount := uint64(0)
if genState.Observers.Len() > int(^uint64(0)) {

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

constant 18446744073709551615 overflows int (typecheck)

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / lint

constant 18446744073709551615 overflows int) (typecheck)

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int

Check failure on line 21 in x/observer/genesis.go

View workflow job for this annotation

GitHub Actions / build-and-test

constant 18446744073709551615 overflows int
observerCount = ^uint64(0)
ctx.Logger().Error("Observer count exceeds maximum uint64 value")
} else {
observerCount = uint64(genState.Observers.Len())
}

if genState.LastObserverCount != nil {
k.SetLastObserverCount(ctx, genState.LastObserverCount)
} else {
Expand Down
30 changes: 15 additions & 15 deletions x/observer/simulation/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,77 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
var crosschainFlagsA, crosschainFlagsB types.CrosschainFlags
cdc.MustUnmarshal(kvA.Value, &crosschainFlagsA)
cdc.MustUnmarshal(kvB.Value, &crosschainFlagsB)
return fmt.Sprintf("%v\n%v", crosschainFlagsA, crosschainFlagsB)
return fmt.Sprintf("key %s value A %v value B %v", types.CrosschainFlagsKey, crosschainFlagsA, crosschainFlagsB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.LastBlockObserverCountKey)):
var lastBlockObserverCountA, lastBlockObserverCountB types.LastObserverCount
cdc.MustUnmarshal(kvA.Value, &lastBlockObserverCountA)
cdc.MustUnmarshal(kvB.Value, &lastBlockObserverCountB)
return fmt.Sprintf("%v\n%v", lastBlockObserverCountA, lastBlockObserverCountB)
return fmt.Sprintf("key %s value A %v value B %v", types.LastBlockObserverCountKey, lastBlockObserverCountA, lastBlockObserverCountB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.NodeAccountKey)):
var nodeAccountA, nodeAccountB types.NodeAccount
cdc.MustUnmarshal(kvA.Value, &nodeAccountA)
cdc.MustUnmarshal(kvB.Value, &nodeAccountB)
return fmt.Sprintf("%v\n%v", nodeAccountA, nodeAccountB)
return fmt.Sprintf("key %s value A %v value B %v", types.NodeAccountKey, nodeAccountA, nodeAccountB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.KeygenKey)):
var keygenA, keygenB types.Keygen
cdc.MustUnmarshal(kvA.Value, &keygenA)
cdc.MustUnmarshal(kvB.Value, &keygenB)
return fmt.Sprintf("%v\n%v", keygenA, keygenB)
return fmt.Sprintf("key %s value A %v value B %v", types.KeygenKey, keygenA, keygenB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.BallotListKey)):
var ballotListA, ballotListB types.BallotListForHeight
cdc.MustUnmarshal(kvA.Value, &ballotListA)
cdc.MustUnmarshal(kvB.Value, &ballotListB)
return fmt.Sprintf("%v\n%v", ballotListA, ballotListB)
return fmt.Sprintf("key %s value A %v value B %v", types.BallotListKey, ballotListA, ballotListB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.VoterKey)):
var voterA, voterB types.Ballot
cdc.MustUnmarshal(kvA.Value, &voterA)
cdc.MustUnmarshal(kvB.Value, &voterB)
return fmt.Sprintf("%v\n%v", voterA, voterB)
return fmt.Sprintf("key %s value A %v value B %v", types.VoterKey, voterA, voterB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.TSSKey)):
var tssA, tssB types.TSS
cdc.MustUnmarshal(kvA.Value, &tssA)
cdc.MustUnmarshal(kvB.Value, &tssB)
return fmt.Sprintf("%v\n%v", tssA, tssB)
return fmt.Sprintf("key %s value A %v value B %v", types.TSSKey, tssA, tssB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.ObserverSetKey)):
var observerSetA, observerSetB types.ObserverSet
cdc.MustUnmarshal(kvA.Value, &observerSetA)
cdc.MustUnmarshal(kvB.Value, &observerSetB)
return fmt.Sprintf("%v\n%v", observerSetA, observerSetB)
return fmt.Sprintf("key %s value A %v value B %v", types.ObserverSetKey, observerSetA, observerSetB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.AllChainParamsKey)):
var allChainParamsA, allChainParamsB types.ChainParamsList
cdc.MustUnmarshal(kvA.Value, &allChainParamsA)
cdc.MustUnmarshal(kvB.Value, &allChainParamsB)
return fmt.Sprintf("%v\n%v", allChainParamsA, allChainParamsB)
return fmt.Sprintf("key %s value A %v value B %v", types.AllChainParamsKey, allChainParamsA, allChainParamsB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.TSSHistoryKey)):
var tssHistoryA, tssHistoryB types.TSS
cdc.MustUnmarshal(kvA.Value, &tssHistoryA)
cdc.MustUnmarshal(kvB.Value, &tssHistoryB)
return fmt.Sprintf("%v\n%v", tssHistoryA, tssHistoryB)
return fmt.Sprintf("key %s value A %v value B %v", types.TSSHistoryKey, tssHistoryA, tssHistoryB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.TssFundMigratorKey)):
var tssFundMigratorA, tssFundMigratorB types.TssFundMigratorInfo
cdc.MustUnmarshal(kvA.Value, &tssFundMigratorA)
cdc.MustUnmarshal(kvB.Value, &tssFundMigratorB)
return fmt.Sprintf("%v\n%v", tssFundMigratorA, tssFundMigratorB)
return fmt.Sprintf("key %s value A %v value B %v", types.TssFundMigratorKey, tssFundMigratorA, tssFundMigratorB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.PendingNoncesKeyPrefix)):
var pendingNoncesA, pendingNoncesB types.PendingNonces
cdc.MustUnmarshal(kvA.Value, &pendingNoncesA)
cdc.MustUnmarshal(kvB.Value, &pendingNoncesB)
return fmt.Sprintf("%v\n%v", pendingNoncesA, pendingNoncesB)
return fmt.Sprintf("key %s value A %v value B %v", types.PendingNoncesKeyPrefix, pendingNoncesA, pendingNoncesB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.ChainNoncesKey)):
var chainNoncesA, chainNoncesB types.ChainNonces
cdc.MustUnmarshal(kvA.Value, &chainNoncesA)
cdc.MustUnmarshal(kvB.Value, &chainNoncesB)
return fmt.Sprintf("%v\n%v", chainNoncesA, chainNoncesB)
return fmt.Sprintf("key %s value A %v value B %v", types.ChainNoncesKey, chainNoncesA, chainNoncesB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.NonceToCctxKeyPrefix)):
var nonceToCctxA, nonceToCctxB types.NonceToCctx
cdc.MustUnmarshal(kvA.Value, &nonceToCctxA)
cdc.MustUnmarshal(kvB.Value, &nonceToCctxB)
return fmt.Sprintf("%v\n%v", nonceToCctxA, nonceToCctxB)
return fmt.Sprintf("key %s value A %v value B %v", types.NonceToCctxKeyPrefix, nonceToCctxA, nonceToCctxB)
case bytes.Equal(kvA.Key, types.KeyPrefix(types.ParamsKey)):
var paramsA, paramsB types.Params
cdc.MustUnmarshal(kvA.Value, &paramsA)
cdc.MustUnmarshal(kvB.Value, &paramsB)
return fmt.Sprintf("%v\n%v", paramsA, paramsB)
return fmt.Sprintf("key %s value A %v value B %v", types.ParamsKey, paramsA, paramsB)
default:
panic(
fmt.Sprintf(
Expand Down
30 changes: 15 additions & 15 deletions x/observer/simulation/decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ func TestNewDecodeStore(t *testing.T) {
name string
expectedLog string
}{
{"CrosschainFlags", fmt.Sprintf("%v\n%v", *crosschainFlags, *crosschainFlags)},
{"LastBlockObserverCount", fmt.Sprintf("%v\n%v", *lastBlockObserverCount, *lastBlockObserverCount)},
{"NodeAccount", fmt.Sprintf("%v\n%v", *nodeAccount, *nodeAccount)},
{"Keygen", fmt.Sprintf("%v\n%v", *keygen, *keygen)},
{"BallotList", fmt.Sprintf("%v\n%v", ballotList, ballotList)},
{"Ballot", fmt.Sprintf("%v\n%v", *ballot, *ballot)},
{"TSS", fmt.Sprintf("%v\n%v", tss, tss)},
{"TSSHistory", fmt.Sprintf("%v\n%v", tss, tss)},
{"ObserverSet", fmt.Sprintf("%v\n%v", observerSet, observerSet)},
{"ChainParamsList", fmt.Sprintf("%v\n%v", chainParamsList, chainParamsList)},
{"TssFundMigrator", fmt.Sprintf("%v\n%v", tssFundMigrator, tssFundMigrator)},
{"PendingNonces", fmt.Sprintf("%v\n%v", pendingNonce, pendingNonce)},
{"ChainNonces", fmt.Sprintf("%v\n%v", chainNonces, chainNonces)},
{"NonceToCctx", fmt.Sprintf("%v\n%v", nonceToCctx, nonceToCctx)},
{"Params", fmt.Sprintf("%v\n%v", params, params)},
{"CrosschainFlags", fmt.Sprintf("key %s value A %v value B %v", types.CrosschainFlagsKey, *crosschainFlags, *crosschainFlags)},
{"LastBlockObserverCount", fmt.Sprintf("key %s value A %v value B %v", types.LastBlockObserverCountKey, *lastBlockObserverCount, *lastBlockObserverCount)},
{"NodeAccount", fmt.Sprintf("key %s value A %v value B %v", types.NodeAccountKey, *nodeAccount, *nodeAccount)},
{"Keygen", fmt.Sprintf("key %s value A %v value B %v", types.KeygenKey, *keygen, *keygen)},
{"BallotList", fmt.Sprintf("key %s value A %v value B %v", types.BallotListKey, ballotList, ballotList)},
{"Ballot", fmt.Sprintf("key %s value A %v value B %v", types.VoterKey, *ballot, *ballot)},
{"TSS", fmt.Sprintf("key %s value A %v value B %v", types.TSSKey, tss, tss)},
{"TSSHistory", fmt.Sprintf("key %s value A %v value B %v", types.TSSHistoryKey, tss, tss)},
{"ObserverSet", fmt.Sprintf("key %s value A %v value B %v", types.ObserverSetKey, observerSet, observerSet)},
{"ChainParamsList", fmt.Sprintf("key %s value A %v value B %v", types.AllChainParamsKey, chainParamsList, chainParamsList)},
{"TssFundMigrator", fmt.Sprintf("key %s value A %v value B %v", types.TssFundMigratorKey, tssFundMigrator, tssFundMigrator)},
{"PendingNonces", fmt.Sprintf("key %s value A %v value B %v", types.PendingNoncesKeyPrefix, pendingNonce, pendingNonce)},
{"ChainNonces", fmt.Sprintf("key %s value A %v value B %v", types.ChainNoncesKey, chainNonces, chainNonces)},
{"NonceToCctx", fmt.Sprintf("key %s value A %v value B %v", types.NonceToCctxKeyPrefix, nonceToCctx, nonceToCctx)},
{"Params", fmt.Sprintf("key %s value A %v value B %v", types.ParamsKey, params, params)},
}

for i, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions x/observer/simulation/operation_update_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func SimulateUpdateObserver(k keeper.Keeper) simtypes.Operation {
"no last block count found",
), nil, nil
}
// #nosec G115 - overflow is not a concern here
if int(lastBlockCount.Count) != len(observerList) {
return simtypes.NoOpMsg(
types.ModuleName,
Expand Down

0 comments on commit 42bf24d

Please sign in to comment.