Skip to content

Commit 0928176

Browse files
authored
[vms/avm] fix linter error in benchmark : Use of weak random number generator (#3023)
1 parent 138ce14 commit 0928176

File tree

4 files changed

+70
-63
lines changed

4 files changed

+70
-63
lines changed

config/config_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/ava-labs/avalanchego/subnets"
2323
)
2424

25+
const chainConfigFilenameExtention = ".ex"
26+
2527
func TestGetChainConfigsFromFiles(t *testing.T) {
2628
tests := map[string]struct {
2729
configs map[string]string
@@ -72,11 +74,11 @@ func TestGetChainConfigsFromFiles(t *testing.T) {
7274
// Create custom configs
7375
for key, value := range test.configs {
7476
chainDir := filepath.Join(chainsDir, key)
75-
setupFile(t, chainDir, chainConfigFileName+".ex", value) //nolint:goconst
77+
setupFile(t, chainDir, chainConfigFileName+chainConfigFilenameExtention, value)
7678
}
7779
for key, value := range test.upgrades {
7880
chainDir := filepath.Join(chainsDir, key)
79-
setupFile(t, chainDir, chainUpgradeFileName+".ex", value)
81+
setupFile(t, chainDir, chainUpgradeFileName+chainConfigFilenameExtention, value)
8082
}
8183

8284
v := setupViper(configFile)
@@ -161,7 +163,7 @@ func TestSetChainConfigDefaultDir(t *testing.T) {
161163
require.Equal(defaultChainConfigDir, v.GetString(ChainConfigDirKey))
162164

163165
chainsDir := filepath.Join(defaultChainConfigDir, "C")
164-
setupFile(t, chainsDir, chainConfigFileName+".ex", "helloworld")
166+
setupFile(t, chainsDir, chainConfigFileName+chainConfigFilenameExtention, "helloworld")
165167
chainConfigs, err := getChainConfigs(v)
166168
require.NoError(err)
167169
expected := map[string]chains.ChainConfig{"C": {Config: []byte("helloworld"), Upgrade: []byte(nil)}}

config/keys.go

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,64 @@
33

44
package config
55

6-
// #nosec G101
6+
// the HTTPWriteTimeoutKey was moved here so that it would not generate the
7+
// false-positive linter error "G101: Potential hardcoded credentials" when running golangci-lint.
8+
const HTTPWriteTimeoutKey = "http-write-timeout" // #nosec G101
9+
710
const (
8-
DataDirKey = "data-dir"
9-
ConfigFileKey = "config-file"
10-
ConfigContentKey = "config-file-content"
11-
ConfigContentTypeKey = "config-file-content-type"
12-
VersionKey = "version"
13-
GenesisFileKey = "genesis-file"
14-
GenesisFileContentKey = "genesis-file-content"
15-
NetworkNameKey = "network-id"
16-
ACPSupportKey = "acp-support"
17-
ACPObjectKey = "acp-object"
18-
TxFeeKey = "tx-fee"
19-
CreateAssetTxFeeKey = "create-asset-tx-fee"
20-
CreateSubnetTxFeeKey = "create-subnet-tx-fee"
21-
TransformSubnetTxFeeKey = "transform-subnet-tx-fee"
22-
CreateBlockchainTxFeeKey = "create-blockchain-tx-fee"
23-
AddPrimaryNetworkValidatorFeeKey = "add-primary-network-validator-fee"
24-
AddPrimaryNetworkDelegatorFeeKey = "add-primary-network-delegator-fee"
25-
AddSubnetValidatorFeeKey = "add-subnet-validator-fee"
26-
AddSubnetDelegatorFeeKey = "add-subnet-delegator-fee"
27-
UptimeRequirementKey = "uptime-requirement"
28-
MinValidatorStakeKey = "min-validator-stake"
29-
MaxValidatorStakeKey = "max-validator-stake"
30-
MinDelegatorStakeKey = "min-delegator-stake"
31-
MinDelegatorFeeKey = "min-delegation-fee"
32-
MinStakeDurationKey = "min-stake-duration"
33-
MaxStakeDurationKey = "max-stake-duration"
34-
StakeMaxConsumptionRateKey = "stake-max-consumption-rate"
35-
StakeMinConsumptionRateKey = "stake-min-consumption-rate"
36-
StakeMintingPeriodKey = "stake-minting-period"
37-
StakeSupplyCapKey = "stake-supply-cap"
38-
DBTypeKey = "db-type"
39-
DBReadOnlyKey = "db-read-only"
40-
DBPathKey = "db-dir"
41-
DBConfigFileKey = "db-config-file"
42-
DBConfigContentKey = "db-config-file-content"
43-
PublicIPKey = "public-ip"
44-
PublicIPResolutionFreqKey = "public-ip-resolution-frequency"
45-
PublicIPResolutionServiceKey = "public-ip-resolution-service"
46-
HTTPHostKey = "http-host"
47-
HTTPPortKey = "http-port"
48-
HTTPSEnabledKey = "http-tls-enabled"
49-
HTTPSKeyFileKey = "http-tls-key-file"
50-
HTTPSKeyContentKey = "http-tls-key-file-content"
51-
HTTPSCertFileKey = "http-tls-cert-file"
52-
HTTPSCertContentKey = "http-tls-cert-file-content"
53-
HTTPAllowedOrigins = "http-allowed-origins"
54-
HTTPAllowedHostsKey = "http-allowed-hosts"
55-
HTTPShutdownTimeoutKey = "http-shutdown-timeout"
56-
HTTPShutdownWaitKey = "http-shutdown-wait"
57-
HTTPReadTimeoutKey = "http-read-timeout"
58-
HTTPReadHeaderTimeoutKey = "http-read-header-timeout"
59-
HTTPWriteTimeoutKey = "http-write-timeout"
11+
DataDirKey = "data-dir"
12+
ConfigFileKey = "config-file"
13+
ConfigContentKey = "config-file-content"
14+
ConfigContentTypeKey = "config-file-content-type"
15+
VersionKey = "version"
16+
GenesisFileKey = "genesis-file"
17+
GenesisFileContentKey = "genesis-file-content"
18+
NetworkNameKey = "network-id"
19+
ACPSupportKey = "acp-support"
20+
ACPObjectKey = "acp-object"
21+
TxFeeKey = "tx-fee"
22+
CreateAssetTxFeeKey = "create-asset-tx-fee"
23+
CreateSubnetTxFeeKey = "create-subnet-tx-fee"
24+
TransformSubnetTxFeeKey = "transform-subnet-tx-fee"
25+
CreateBlockchainTxFeeKey = "create-blockchain-tx-fee"
26+
AddPrimaryNetworkValidatorFeeKey = "add-primary-network-validator-fee"
27+
AddPrimaryNetworkDelegatorFeeKey = "add-primary-network-delegator-fee"
28+
AddSubnetValidatorFeeKey = "add-subnet-validator-fee"
29+
AddSubnetDelegatorFeeKey = "add-subnet-delegator-fee"
30+
UptimeRequirementKey = "uptime-requirement"
31+
MinValidatorStakeKey = "min-validator-stake"
32+
MaxValidatorStakeKey = "max-validator-stake"
33+
MinDelegatorStakeKey = "min-delegator-stake"
34+
MinDelegatorFeeKey = "min-delegation-fee"
35+
MinStakeDurationKey = "min-stake-duration"
36+
MaxStakeDurationKey = "max-stake-duration"
37+
StakeMaxConsumptionRateKey = "stake-max-consumption-rate"
38+
StakeMinConsumptionRateKey = "stake-min-consumption-rate"
39+
StakeMintingPeriodKey = "stake-minting-period"
40+
StakeSupplyCapKey = "stake-supply-cap"
41+
DBTypeKey = "db-type"
42+
DBReadOnlyKey = "db-read-only"
43+
DBPathKey = "db-dir"
44+
DBConfigFileKey = "db-config-file"
45+
DBConfigContentKey = "db-config-file-content"
46+
PublicIPKey = "public-ip"
47+
PublicIPResolutionFreqKey = "public-ip-resolution-frequency"
48+
PublicIPResolutionServiceKey = "public-ip-resolution-service"
49+
HTTPHostKey = "http-host"
50+
HTTPPortKey = "http-port"
51+
HTTPSEnabledKey = "http-tls-enabled"
52+
HTTPSKeyFileKey = "http-tls-key-file"
53+
HTTPSKeyContentKey = "http-tls-key-file-content"
54+
HTTPSCertFileKey = "http-tls-cert-file"
55+
HTTPSCertContentKey = "http-tls-cert-file-content"
56+
57+
HTTPAllowedOrigins = "http-allowed-origins"
58+
HTTPAllowedHostsKey = "http-allowed-hosts"
59+
HTTPShutdownTimeoutKey = "http-shutdown-timeout"
60+
HTTPShutdownWaitKey = "http-shutdown-wait"
61+
HTTPReadTimeoutKey = "http-read-timeout"
62+
HTTPReadHeaderTimeoutKey = "http-read-header-timeout"
63+
6064
HTTPIdleTimeoutKey = "http-idle-timeout"
6165
StateSyncIPsKey = "state-sync-ips"
6266
StateSyncIDsKey = "state-sync-ids"

vms/avm/vm_benchmark_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func BenchmarkLoadUser(b *testing.B) {
6565
}
6666

6767
// GetAllUTXOsBenchmark is a helper func to benchmark the GetAllUTXOs depending on the size
68-
func GetAllUTXOsBenchmark(b *testing.B, utxoCount int) {
68+
func getAllUTXOsBenchmark(b *testing.B, utxoCount int, randSrc rand.Source) {
6969
require := require.New(b)
7070

7171
env := setup(b, &envConfig{fork: latest})
@@ -76,12 +76,11 @@ func GetAllUTXOsBenchmark(b *testing.B, utxoCount int) {
7676

7777
addr := ids.GenerateTestShortID()
7878

79-
// #nosec G404
8079
for i := 0; i < utxoCount; i++ {
8180
utxo := &avax.UTXO{
8281
UTXOID: avax.UTXOID{
8382
TxID: ids.GenerateTestID(),
84-
OutputIndex: rand.Uint32(),
83+
OutputIndex: uint32(randSrc.Int63()),
8584
},
8685
Asset: avax.Asset{ID: env.vm.ctx.AVAXAssetID},
8786
Out: &secp256k1fx.TransferOutput{
@@ -129,9 +128,10 @@ func BenchmarkGetUTXOs(b *testing.B) {
129128
},
130129
}
131130

132-
for _, count := range tests {
131+
for testIdx, count := range tests {
132+
randSrc := rand.NewSource(int64(testIdx))
133133
b.Run(count.name, func(b *testing.B) {
134-
GetAllUTXOsBenchmark(b, count.utxoCount)
134+
getAllUTXOsBenchmark(b, count.utxoCount, randSrc)
135135
})
136136
}
137137
}

vms/platformvm/service_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ func TestGetTxStatus(t *testing.T) {
130130
sm := m.NewSharedMemory(service.vm.ctx.ChainID)
131131
peerSharedMemory := m.NewSharedMemory(service.vm.ctx.XChainID)
132132

133-
// #nosec G404
133+
randSrc := rand.NewSource(0)
134+
134135
utxo := &avax.UTXO{
135136
UTXOID: avax.UTXOID{
136137
TxID: ids.GenerateTestID(),
137-
OutputIndex: rand.Uint32(),
138+
OutputIndex: uint32(randSrc.Int63()),
138139
},
139140
Asset: avax.Asset{ID: service.vm.ctx.AVAXAssetID},
140141
Out: &secp256k1fx.TransferOutput{

0 commit comments

Comments
 (0)