From c9ef176f4c028ac6b1d8727d7723cb2c4cace800 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Tue, 12 Mar 2019 18:12:24 +0200 Subject: [PATCH] [NOD-35] Refactor fmt.Sprintf to always have the template argument as string literal * [NOD-35] Refactor fmt.Sprintf to always have the template argument as string literal * [NOD-35] Remove redundant fmt.Sprintf's --- blockdag/validate.go | 8 ++------ database/ffldb/blockio.go | 23 +++++++++++------------ util/random/random_test.go | 7 +++---- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/blockdag/validate.go b/blockdag/validate.go index 30d8aebc1..830be2e57 100644 --- a/blockdag/validate.go +++ b/blockdag/validate.go @@ -621,7 +621,6 @@ func ExtractCoinbaseHeight(coinbaseTx *util.Tx) (int32, error) { str := "the coinbase signature script" + "must start with the " + "length of the serialized block height" - str = fmt.Sprintf(str) return 0, ruleError(ErrMissingCoinbaseHeight, str) } @@ -642,7 +641,6 @@ func ExtractCoinbaseHeight(coinbaseTx *util.Tx) (int32, error) { str := "the coinbase signature script " + "must start with the " + "serialized block height" - str = fmt.Sprintf(str, serializedLen) return 0, ruleError(ErrMissingCoinbaseHeight, str) } @@ -727,8 +725,7 @@ func validateMedianTime(header *wire.BlockHeader, bluestParent *blockNode) error // median time of the last several blocks (medianTimeBlocks). medianTime := bluestParent.PastMedianTime() if header.Timestamp.Before(medianTime) { - str := "block timestamp of %s is not after expected %s" - str = fmt.Sprintf(str, header.Timestamp, medianTime) + str := fmt.Sprintf("block timestamp of %s is not after expected %s", header.Timestamp, medianTime) return ruleError(ErrTimeTooOld, str) } } @@ -747,8 +744,7 @@ func (dag *BlockDAG) validateDifficulty(header *wire.BlockHeader, bluestParent * } blockDifficulty := header.Bits if blockDifficulty != expectedDifficulty { - str := "block difficulty of %d is not the expected value of %d" - str = fmt.Sprintf(str, blockDifficulty, expectedDifficulty) + str := fmt.Sprintf("block difficulty of %d is not the expected value of %d", blockDifficulty, expectedDifficulty) return ruleError(ErrUnexpectedDifficulty, str) } diff --git a/database/ffldb/blockio.go b/database/ffldb/blockio.go index 4169b925f..d7c8b3856 100644 --- a/database/ffldb/blockio.go +++ b/database/ffldb/blockio.go @@ -23,17 +23,6 @@ import ( ) const ( - // The Bitcoin protocol encodes block height as int32, so max number of - // blocks is 2^31. Max block size per the protocol is 32MiB per block. - // So the theoretical max at the time this comment was written is 64PiB - // (pebibytes). With files @ 512MiB each, this would require a maximum - // of 134,217,728 files. Thus, choose 9 digits of precision for the - // filenames. An additional benefit is 9 digits provides 10^9 files @ - // 512MiB each for a total of ~476.84PiB (roughly 7.4 times the current - // theoretical max), so there is room for the max block size to grow in - // the future. - blockFilenameTemplate = "%09d.fdb" - // maxOpenFiles is the max number of open files to maintain in the // open blocks cache. Note that this does not include the current // write file, so there will typically be one more than this value open. @@ -225,7 +214,17 @@ func serializeBlockLoc(loc blockLocation) []byte { // blockFilePath return the file path for the provided block file number. func blockFilePath(dbPath string, fileNum uint32) string { - fileName := fmt.Sprintf(blockFilenameTemplate, fileNum) + // The Bitcoin protocol encodes block height as int32, so max number of + // blocks is 2^31. Max block size per the protocol is 32MiB per block. + // So the theoretical max at the time this comment was written is 64PiB + // (pebibytes). With files @ 512MiB each, this would require a maximum + // of 134,217,728 files. Thus, choose 9 digits of precision for the + // filenames. An additional benefit is 9 digits provides 10^9 files @ + // 512MiB each for a total of ~476.84PiB (roughly 7.4 times the current + // theoretical max), so there is room for the max block size to grow in + // the future. + + fileName := fmt.Sprintf("%09d.fdb", fileNum) return filepath.Join(dbPath, fileName) } diff --git a/util/random/random_test.go b/util/random/random_test.go index 66186afd3..8e85d092a 100644 --- a/util/random/random_test.go +++ b/util/random/random_test.go @@ -33,9 +33,6 @@ func TestRandomUint64(t *testing.T) { tries := 1 << 8 // 2^8 watermark := uint64(1 << 56) // 2^56 maxHits := 5 - badRNG := "The random number generator on this system is clearly " + - "terrible since we got %d values less than %d in %d runs " + - "when only %d was expected" numHits := 0 for i := 0; i < tries; i++ { @@ -49,7 +46,9 @@ func TestRandomUint64(t *testing.T) { numHits++ } if numHits > maxHits { - str := fmt.Sprintf(badRNG, numHits, watermark, tries, maxHits) + str := fmt.Sprintf("The random number generator on this system is clearly "+ + "terrible since we got %d values less than %d in %d runs "+ + "when only %d was expected", numHits, watermark, tries, maxHits) t.Errorf("Random Uint64 iteration %d failed - %v %v", i, str, numHits) return