Exemple #1
0
// Adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (self *UnconfirmedTxnPool) RecordTxn(bc *coin.Blockchain,
	t coin.Transaction, addrs map[cipher.Address]byte, maxSize int,
	burnFactor uint64) (error, bool) {
	if err := VerifyTransaction(bc, &t, maxSize, burnFactor); err != nil {
		return err, false
	}
	if err := bc.VerifyTransaction(t); err != nil {
		return err, false
	}

	// Update if we already have this txn
	h := t.Hash()
	ut, ok := self.Txns[h]
	if ok {
		now := util.Now()
		ut.Received = now
		ut.Checked = now
		self.Txns[h] = ut
		return nil, true
	}

	// Add txn to index
	self.Txns[h] = self.createUnconfirmedTxn(&bc.Unspent, t, addrs)
	// Add predicted unspents
	self.Unspent[h] = coin.CreateUnspents(bc.Head().Head, t)

	return nil, false
}
Exemple #2
0
// Adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (self *UnconfirmedTxnPool) RecordTxn(bc *coin.Blockchain,
	t coin.Transaction, addrs map[coin.Address]byte, maxSize int,
	burnFactor uint64) (error, bool) {
	if err := VerifyTransaction(bc, &t, maxSize, burnFactor); err != nil {
		return err, false
	}
	if err := bc.VerifyTransaction(t); err != nil {
		return err, false
	}

	// Update if we already have this txn
	ut, ok := self.Txns[t.Hash()]
	if ok {
		now := util.Now()
		ut.Received = now
		ut.Checked = now
		self.Txns[ut.Txn.Hash()] = ut
		return nil, true
	}

	// Add txn to index
	self.Txns[t.Hash()] = self.createUnconfirmedTxn(&bc.Unspent, t, addrs)
	// Add predicted unspents
	uxs := coin.CreateExpectedUnspents(t)
	for i, _ := range uxs {
		self.Unspent.Add(uxs[i])
	}

	return nil, false
}
Exemple #3
0
// Adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (self *UnconfirmedTxnPool) InjectTxn(bc *coin.Blockchain,
	t coin.Transaction) (error, bool) {

	if err := t.Verify(); err != nil {
		return err, false
	}

	if err := VerifyTransactionFee(bc, &t); err != nil {
		return err, false
	}
	if err := bc.VerifyTransaction(t); err != nil {
		return err, false
	}

	// Update if we already have this txn
	h := t.Hash()
	ut, ok := self.Txns[h]
	if ok {
		now := util.Now()
		ut.Received = now
		ut.Checked = now
		self.Txns[h] = ut
		return nil, true
	}

	// Add txn to index
	self.Txns[h] = self.createUnconfirmedTxn(&bc.Unspent, t)
	// Add predicted unspents
	self.Unspent[h] = coin.CreateUnspents(bc.Head().Head, t)

	return nil, false
}
Exemple #4
0
// Checks all unconfirmed txns against the blockchain. maxAge is how long
// we'll hold a txn regardless of whether it has been invalidated.
// checkPeriod is how often we check the txn against the blockchain.
func (self *UnconfirmedTxnPool) Refresh(bc *coin.Blockchain, checkPeriod int) {
	now := time.Now().Unix()
	toRemove := make([]cipher.SHA256, 0)
	for k, t := range self.Txns {
		if now-t.Checked >= int64(checkPeriod) {
			if bc.VerifyTransaction(t.Txn) == nil {
				t.Checked = now
				self.Txns[k] = t
			} else {
				toRemove = append(toRemove, k)
			}
		}
	}
	self.removeTxns(toRemove)
}
Exemple #5
0
// Checks all unconfirmed txns against the blockchain. maxAge is how long
// we'll hold a txn regardless of whether it has been invalidated.
// checkPeriod is how often we check the txn against the blockchain.
func (self *UnconfirmedTxnPool) Refresh(bc *coin.Blockchain,
	checkPeriod, maxAge time.Duration) {
	now := util.Now()
	toRemove := make([]coin.SHA256, 0)
	for k, t := range self.Txns {
		if now.Sub(t.Received) >= maxAge {
			toRemove = append(toRemove, k)
		} else if now.Sub(t.Checked) >= checkPeriod {
			if bc.VerifyTransaction(t.Txn) == nil {
				t.Checked = now
				self.Txns[k] = t
			} else {
				toRemove = append(toRemove, k)
			}
		}
	}
	self.removeTxns(bc, toRemove)
}