diff --git a/difficulty_test.go b/difficulty_test.go new file mode 100644 index 00000000..b29271f2 --- /dev/null +++ b/difficulty_test.go @@ -0,0 +1,70 @@ +// Copyright (c) 2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package btcchain_test + +import ( + "github.com/conformal/btcchain" + "math/big" + "testing" +) + +func TestBigToCompact(t *testing.T) { + tests := []struct { + in int64 + out uint32 + }{ + {0, 0}, + {-1, 25231360}, + } + + for x, test := range tests { + n := big.NewInt(test.in) + r := btcchain.BigToCompact(n) + if r != test.out { + t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n", + x, r, test.out) + return + } + } +} + +func TestCompactToBig(t *testing.T) { + tests := []struct { + in uint32 + out int64 + }{ + {10000000, 0}, + } + + for x, test := range tests { + n := btcchain.CompactToBig(test.in) + want := big.NewInt(test.out) + if n.Cmp(want) != 0 { + t.Errorf("TestCompactToBig test #%d failed: got %d want %d\n", + x, n.Int64(), want.Int64()) + return + } + } +} + +func TestCalcWork(t *testing.T) { + tests := []struct { + in uint32 + out int64 + }{ + {10000000, 0}, + } + + for x, test := range tests { + bits := uint32(test.in) + + r := btcchain.CalcWork(bits) + if r.Int64() != test.out { + t.Errorf("TestCalcWork test #%d failed: got %v want %d\n", + x, r.Int64(), test.out) + return + } + } +} diff --git a/test_coverage.txt b/test_coverage.txt index ca3ea7e1..a160aa2c 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,22 +1,25 @@ github.com/conformal/btcchain/validate.go checkSerializedHeight 100.00% (17/17) github.com/conformal/btcchain/chain.go BlockChain.removeOrphanBlock 100.00% (16/16) +github.com/conformal/btcchain/difficulty.go BigToCompact 100.00% (16/16) github.com/conformal/btcchain/txlookup.go disconnectTransactions 100.00% (13/13) +github.com/conformal/btcchain/difficulty.go CompactToBig 100.00% (12/12) github.com/conformal/btcchain/validate.go CountSigOps 100.00% (9/9) github.com/conformal/btcchain/validate.go BlockChain.CheckConnectBlock 100.00% (7/7) github.com/conformal/btcchain/checkpoints.go init 100.00% (6/6) -github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5) github.com/conformal/btcchain/merkle.go hashMerkleBranches 100.00% (5/5) github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 100.00% (5/5) +github.com/conformal/btcchain/difficulty.go CalcWork 100.00% (5/5) +github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5) github.com/conformal/btcchain/merkle.go nextPowerOfTwo 100.00% (4/4) github.com/conformal/btcchain/chain.go newBlockNode 100.00% (3/3) github.com/conformal/btcchain/process.go BlockChain.blockExists 100.00% (3/3) github.com/conformal/btcchain/chain.go New 100.00% (2/2) github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2) -github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1) +github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1) github.com/conformal/btcchain/chain.go BlockChain.HaveBlock 100.00% (1/1) github.com/conformal/btcchain/log.go init 100.00% (1/1) -github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1) +github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1) github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1) github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1) github.com/conformal/btcchain/scriptval.go newTxValidator 100.00% (1/1) @@ -35,17 +38,14 @@ github.com/conformal/btcchain/chain.go BlockChain.connectBestChain 85.29% (2 github.com/conformal/btcchain/chain.go BlockChain.reorganizeChain 85.29% (29/34) github.com/conformal/btcchain/process.go BlockChain.processOrphans 84.21% (16/19) github.com/conformal/btcchain/chain.go BlockChain.connectBlock 83.33% (10/12) -github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17) github.com/conformal/btcchain/validate.go BlockChain.checkBIP0030 82.35% (14/17) +github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17) github.com/conformal/btcchain/chain.go BlockChain.isMajorityVersion 80.00% (8/10) -github.com/conformal/btcchain/difficulty.go CalcWork 80.00% (4/5) github.com/conformal/btcchain/chain.go BlockChain.addOrphanBlock 77.78% (14/18) github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromBlock 77.78% (7/9) github.com/conformal/btcchain/chain.go BlockChain.GenerateInitialIndex 77.27% (17/22) github.com/conformal/btcchain/chain.go BlockChain.disconnectBlock 76.92% (10/13) github.com/conformal/btcchain/txlookup.go BlockChain.fetchInputTransactions 75.00% (18/24) -github.com/conformal/btcchain/difficulty.go BigToCompact 75.00% (12/16) -github.com/conformal/btcchain/difficulty.go CompactToBig 75.00% (9/12) github.com/conformal/btcchain/validate.go isTransactionSpent 75.00% (3/4) github.com/conformal/btcchain/validate.go BlockChain.checkConnectBlock 71.15% (37/52) github.com/conformal/btcchain/validate.go CheckBlockSanity 67.44% (29/43) @@ -53,20 +53,20 @@ github.com/conformal/btcchain/validate.go isNullOutpoint 66.67% (2/3) github.com/conformal/btcchain/validate.go CheckTransactionInputs 65.12% (28/43) github.com/conformal/btcchain/txlookup.go connectTransactions 61.54% (8/13) github.com/conformal/btcchain/validate.go CheckTransactionSanity 61.11% (22/36) -github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5) github.com/conformal/btcchain/params.go ChainParams 60.00% (3/5) +github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5) github.com/conformal/btcchain/scriptval.go txValidator.validateHandler 59.26% (16/27) +github.com/conformal/btcchain/chain.go BlockChain.loadBlockNode 53.85% (14/26) github.com/conformal/btcchain/validate.go checkProofOfWork 53.33% (8/15) github.com/conformal/btcchain/process.go BlockChain.ProcessBlock 52.27% (23/44) -github.com/conformal/btcchain/chain.go BlockChain.loadBlockNode 52.00% (13/25) github.com/conformal/btcchain/accept.go BlockChain.maybeAcceptBlock 51.28% (40/78) github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromNode 50.00% (4/8) -github.com/conformal/btcchain/checkpoints.go BlockChain.LatestCheckpoint 50.00% (2/4) github.com/conformal/btcchain/notifications.go BlockChain.sendNotification 50.00% (2/4) +github.com/conformal/btcchain/checkpoints.go BlockChain.LatestCheckpoint 50.00% (2/4) github.com/conformal/btcchain/chain.go BlockChain.pruneBlockNodes 41.18% (7/17) github.com/conformal/btcchain/validate.go IsFinalizedTransaction 28.57% (4/14) github.com/conformal/btcchain/checkpoints.go BlockChain.verifyCheckpoint 22.22% (2/9) -github.com/conformal/btcchain/difficulty.go BlockChain.calcNextRequiredDifficulty 17.50% (7/40) +github.com/conformal/btcchain/difficulty.go BlockChain.calcNextRequiredDifficulty 17.07% (7/41) github.com/conformal/btcchain/checkpoints.go BlockChain.findPreviousCheckpoint 4.88% (2/41) github.com/conformal/btcchain/blocklocator.go BlockChain.BlockLocatorFromHash 0.00% (0/39) github.com/conformal/btcchain/checkpoints.go BlockChain.IsCheckpointCandidate 0.00% (0/32) @@ -83,14 +83,14 @@ github.com/conformal/btcchain/blocklocator.go BlockChain.LatestBlockLocator 0 github.com/conformal/btcchain/txlookup.go BlockChain.FetchTransactionStore 0.00% (0/6) github.com/conformal/btcchain/checkpoints.go isNonstandardTransaction 0.00% (0/5) github.com/conformal/btcchain/checkpoints.go BlockChain.checkpointData 0.00% (0/4) -github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3) -github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3) github.com/conformal/btcchain/checkpoints.go BlockChain.Checkpoints 0.00% (0/3) -github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1) +github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3) +github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3) +github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1) github.com/conformal/btcchain/process.go RuleError.Error 0.00% (0/1) +github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1) github.com/conformal/btcchain/difficulty.go BlockChain.CalcNextRequiredDifficulty 0.00% (0/1) github.com/conformal/btcchain/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1) -github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1) -github.com/conformal/btcchain ------------------------------------- 55.94% (697/1246) +github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1) +github.com/conformal/btcchain ------------------------------------- 56.57% (706/1248)