// Adds a transaction to the rejected list or not, it it has been mined already // Make sure to call it with locked TxMutex. // Returns the OneTxRejected or nil if it has not been added. func RejectTx(id *btc.Uint256, size int, why byte) *OneTxRejected { rec := new(OneTxRejected) rec.Id = id rec.Time = time.Now() rec.Size = uint32(size) rec.Reason = why TransactionsRejected[id.BIdx()] = rec return rec }
func txChecker(h *btc.Uint256) bool { TxMutex.Lock() rec, ok := TransactionsToSend[h.BIdx()] TxMutex.Unlock() if ok && rec.Own != 0 { return false // Assume own txs as non-trusted } if ok { common.CountSafe("TxScrBoosted") } else { common.CountSafe("TxScrMissed") } return ok }
func (db *BlockDB) BlockGet(hash *btc.Uint256) (bl []byte, trusted bool, e error) { db.mutex.Lock() rec, ok := db.blockIndex[hash.BIdx()] if !ok { db.mutex.Unlock() e = errors.New("btc.Block not in the index") return } trusted = rec.trusted if crec, hit := db.cache[hash.BIdx()]; hit { bl = crec.data crec.used = time.Now() db.mutex.Unlock() return } db.mutex.Unlock() bl = make([]byte, rec.blen) // we will re-open the data file, to not spoil the writting pointer f, e := os.Open(db.dirname + "blockchain.dat") if e != nil { return } _, e = f.Seek(int64(rec.fpos), os.SEEK_SET) if e == nil { _, e = f.Read(bl[:]) } f.Close() if rec.compressed { if rec.snappied { bl, _ = snappy.Decode(nil, bl) } else { gz, _ := gzip.NewReader(bytes.NewReader(bl)) bl, _ = ioutil.ReadAll(gz) gz.Close() } } db.addToCache(hash, bl) return }
func (db *BlockDB) addToCache(h *btc.Uint256, bl []byte) { if rec, ok := db.cache[h.BIdx()]; ok { rec.used = time.Now() return } if uint(len(db.cache)) >= MaxCachedBlocks { var oldest_t time.Time var oldest_k [btc.Uint256IdxLen]byte for k, v := range db.cache { if oldest_t.IsZero() || v.used.Before(oldest_t) { oldest_t = v.used oldest_k = k } } delete(db.cache, oldest_k) } db.cache[h.BIdx()] = &cacheRecord{used: time.Now(), data: bl} }
// Return false if we do not want to receive a data for this tx func NeedThisTx(id *btc.Uint256, cb func()) (res bool) { TxMutex.Lock() if _, present := TransactionsToSend[id.BIdx()]; present { //res = false } else if _, present := TransactionsRejected[id.BIdx()]; present { //res = false } else if _, present := TransactionsPending[id.BIdx()]; present { //res = false } else if txo, _ := common.BlockChain.Unspent.UnspentGet(&btc.TxPrevOut{Hash: id.Hash}); txo != nil { // This assumes that tx's out #0 has not been spent yet, which may not always be the case, but well... common.CountSafe("TxMinedRejected") } else { res = true if cb != nil { cb() } } TxMutex.Unlock() return }