[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
This commit is contained in:
Ori Newman 2019-03-12 18:12:24 +02:00 committed by Svarog
parent 2db55f219f
commit c9ef176f4c
3 changed files with 16 additions and 22 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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