Replace map[a]bool with map[a]struct{}

The later uses no memory storage for values and provides the same
functionality.
This commit is contained in:
Tomás Senart 2014-07-02 17:56:22 +02:00
parent 933cdf50e8
commit e29f40274d
3 changed files with 15 additions and 15 deletions

View file

@ -57,14 +57,14 @@ func TestReorganization(t *testing.T) {
chain.DisableCheckpoints(true) chain.DisableCheckpoints(true)
btcchain.TstSetCoinbaseMaturity(1) btcchain.TstSetCoinbaseMaturity(1)
expectedOrphans := map[int]bool{5: true, 6: true} expectedOrphans := map[int]struct{}{5: struct{}{}, 6: struct{}{}}
for i := 1; i < len(blocks); i++ { for i := 1; i < len(blocks); i++ {
isOrphan, err := chain.ProcessBlock(blocks[i], btcchain.BFNone) isOrphan, err := chain.ProcessBlock(blocks[i], btcchain.BFNone)
if err != nil { if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err) t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return return
} }
if isOrphan && !expectedOrphans[i] { if _, ok := expectedOrphans[i]; !ok && isOrphan {
t.Errorf("ProcessBlock incorrectly returned block %v "+ t.Errorf("ProcessBlock incorrectly returned block %v "+
"is an orphan\n", i) "is an orphan\n", i)
} }

View file

@ -100,7 +100,7 @@ func disconnectTransactions(txStore TxStore, block *btcutil.Block) error {
// transactions from the point of view of the end of the main chain. It takes // transactions from the point of view of the end of the main chain. It takes
// a flag which specifies whether or not fully spent transaction should be // a flag which specifies whether or not fully spent transaction should be
// included in the results. // included in the results.
func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]bool, includeSpent bool) TxStore { func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]struct{}, includeSpent bool) TxStore {
// Just return an empty store now if there are no requested hashes. // Just return an empty store now if there are no requested hashes.
txStore := make(TxStore) txStore := make(TxStore)
if len(txSet) == 0 { if len(txSet) == 0 {
@ -160,7 +160,7 @@ func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]bool, includeSpent
// chain). Another scenario is where a transaction exists from the point of // chain). Another scenario is where a transaction exists from the point of
// view of the main chain, but doesn't exist in a side chain that branches // view of the main chain, but doesn't exist in a side chain that branches
// before the block that contains the transaction on the main chain. // before the block that contains the transaction on the main chain.
func (b *BlockChain) fetchTxStore(node *blockNode, txSet map[btcwire.ShaHash]bool) (TxStore, error) { func (b *BlockChain) fetchTxStore(node *blockNode, txSet map[btcwire.ShaHash]struct{}) (TxStore, error) {
// Get the previous block node. This function is used over simply // Get the previous block node. This function is used over simply
// accessing node.parent directly as it will dynamically create previous // accessing node.parent directly as it will dynamically create previous
// block nodes as needed. This helps allow only the pieces of the chain // block nodes as needed. This helps allow only the pieces of the chain
@ -245,7 +245,7 @@ func (b *BlockChain) fetchInputTransactions(node *blockNode, block *btcutil.Bloc
// Loop through all of the transaction inputs (except for the coinbase // Loop through all of the transaction inputs (except for the coinbase
// which has no inputs) collecting them into sets of what is needed and // which has no inputs) collecting them into sets of what is needed and
// what is already known (in-flight). // what is already known (in-flight).
txNeededSet := make(map[btcwire.ShaHash]bool) txNeededSet := make(map[btcwire.ShaHash]struct{})
txStore := make(TxStore) txStore := make(TxStore)
for i, tx := range transactions[1:] { for i, tx := range transactions[1:] {
for _, txIn := range tx.MsgTx().TxIn { for _, txIn := range tx.MsgTx().TxIn {
@ -274,7 +274,7 @@ func (b *BlockChain) fetchInputTransactions(node *blockNode, block *btcutil.Bloc
txD.Spent = make([]bool, len(originTx.MsgTx().TxOut)) txD.Spent = make([]bool, len(originTx.MsgTx().TxOut))
txD.Err = nil txD.Err = nil
} else { } else {
txNeededSet[*originHash] = true txNeededSet[*originHash] = struct{}{}
} }
} }
} }
@ -302,10 +302,10 @@ func (b *BlockChain) FetchTransactionStore(tx *btcutil.Tx) (TxStore, error) {
// Create a set of needed transactions from the transactions referenced // Create a set of needed transactions from the transactions referenced
// by the inputs of the passed transaction. Also, add the passed // by the inputs of the passed transaction. Also, add the passed
// transaction itself as a way for the caller to detect duplicates. // transaction itself as a way for the caller to detect duplicates.
txNeededSet := make(map[btcwire.ShaHash]bool) txNeededSet := make(map[btcwire.ShaHash]struct{})
txNeededSet[*tx.Sha()] = true txNeededSet[*tx.Sha()] = struct{}{}
for _, txIn := range tx.MsgTx().TxIn { for _, txIn := range tx.MsgTx().TxIn {
txNeededSet[txIn.PreviousOutpoint.Hash] = true txNeededSet[txIn.PreviousOutpoint.Hash] = struct{}{}
} }
// Request the input transactions from the point of view of the end of // Request the input transactions from the point of view of the end of

View file

@ -243,13 +243,13 @@ func CheckTransactionSanity(tx *btcutil.Tx) error {
} }
// Check for duplicate transaction inputs. // Check for duplicate transaction inputs.
existingTxOut := make(map[btcwire.OutPoint]bool) existingTxOut := make(map[btcwire.OutPoint]struct{})
for _, txIn := range msgTx.TxIn { for _, txIn := range msgTx.TxIn {
if _, exists := existingTxOut[txIn.PreviousOutpoint]; exists { if _, exists := existingTxOut[txIn.PreviousOutpoint]; exists {
return ruleError(ErrDuplicateTxInputs, "transaction "+ return ruleError(ErrDuplicateTxInputs, "transaction "+
"contains duplicate inputs") "contains duplicate inputs")
} }
existingTxOut[txIn.PreviousOutpoint] = true existingTxOut[txIn.PreviousOutpoint] = struct{}{}
} }
// Coinbase script length must be between min and max length. // Coinbase script length must be between min and max length.
@ -518,7 +518,7 @@ func checkBlockSanity(block *btcutil.Block, powLimit *big.Int, flags BehaviorFla
// Check for duplicate transactions. This check will be fairly quick // Check for duplicate transactions. This check will be fairly quick
// since the transaction hashes are already cached due to building the // since the transaction hashes are already cached due to building the
// merkle tree above. // merkle tree above.
existingTxHashes := make(map[btcwire.ShaHash]bool) existingTxHashes := make(map[btcwire.ShaHash]struct{})
for _, tx := range transactions { for _, tx := range transactions {
hash := tx.Sha() hash := tx.Sha()
if _, exists := existingTxHashes[*hash]; exists { if _, exists := existingTxHashes[*hash]; exists {
@ -526,7 +526,7 @@ func checkBlockSanity(block *btcutil.Block, powLimit *big.Int, flags BehaviorFla
"transaction %v", hash) "transaction %v", hash)
return ruleError(ErrDuplicateTx, str) return ruleError(ErrDuplicateTx, str)
} }
existingTxHashes[*hash] = true existingTxHashes[*hash] = struct{}{}
} }
// The number of signature operations must be less than the maximum // The number of signature operations must be less than the maximum
@ -611,9 +611,9 @@ func isTransactionSpent(txD *TxData) bool {
func (b *BlockChain) checkBIP0030(node *blockNode, block *btcutil.Block) error { func (b *BlockChain) checkBIP0030(node *blockNode, block *btcutil.Block) error {
// Attempt to fetch duplicate transactions for all of the transactions // Attempt to fetch duplicate transactions for all of the transactions
// in this block from the point of view of the parent node. // in this block from the point of view of the parent node.
fetchSet := make(map[btcwire.ShaHash]bool) fetchSet := make(map[btcwire.ShaHash]struct{})
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
fetchSet[*tx.Sha()] = true fetchSet[*tx.Sha()] = struct{}{}
} }
txResults, err := b.fetchTxStore(node, fetchSet) txResults, err := b.fetchTxStore(node, fetchSet)
if err != nil { if err != nil {