github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/transactionpool.go (about)

     1  package modules
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/NebulousLabs/Sia/encoding"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  const (
    11  	// TransactionSizeLimit defines the size of the largest transaction that
    12  	// will be accepted by the transaction pool according to the IsStandard
    13  	// rules.
    14  	TransactionSizeLimit = 16e3
    15  
    16  	// TransactionSetSizeLimit defines the largest set of dependent unconfirmed
    17  	// transactions that will be accepted by the transaction pool.
    18  	TransactionSetSizeLimit = 250e3
    19  )
    20  
    21  var (
    22  	// ErrDuplicateTransactionSet is the error that gets returned if a
    23  	// duplicate transaction set is given to the transaction pool.
    24  	ErrDuplicateTransactionSet = errors.New("transaction set contains only duplicate transaction")
    25  
    26  	// ErrLargeTransaction is the error that gets returned if a transaction
    27  	// provided to the transaction pool is larger than what is allowed by the
    28  	// IsStandard rules.
    29  	ErrLargeTransaction = errors.New("transaction is too large for this transaction pool")
    30  
    31  	// ErrLargeTransactionSet is the error that gets returned if a transaction
    32  	// set is given to the transaction pool if the transaction is larger than
    33  	// limit placed by the IsStandard rules of the transaction pool.
    34  	ErrLargeTransactionSet = errors.New("transaction set is too large for this transaction pool")
    35  
    36  	// ErrInvalidArbPrefix is the error that gets returned if a transaction is
    37  	// submitted to the transaction pool which contains a prefix that is not
    38  	// recognized. This helps prevent miners on old versions from mining
    39  	// potentially illegal transactions in the event of a soft-fork.
    40  	ErrInvalidArbPrefix = errors.New("transaction contains non-standard arbitrary data")
    41  
    42  	// PrefixNonSia defines the prefix that should be appended to any
    43  	// transactions that use the arbitrary data for reasons outside of the
    44  	// standard Sia protocol. This will prevent these transactions from being
    45  	// rejected by the IsStandard set of rules, but also means that the data
    46  	// will never be used within the formal Sia protocol.
    47  	PrefixNonSia = types.Specifier{'N', 'o', 'n', 'S', 'i', 'a'}
    48  
    49  	// TransactionPoolDir is the name of the directory that is used to store
    50  	// the transaction pool's persistent data.
    51  	TransactionPoolDir = "transactionpool"
    52  )
    53  
    54  // A TransactionPoolSubscriber receives updates about the confirmed and
    55  // unconfirmed set from the transaction pool. Generally, there is no need to
    56  // subscribe to both the consensus set and the transaction pool.
    57  type TransactionPoolSubscriber interface {
    58  	// ReceiveTransactionPoolUpdate notifies subscribers of a change to the
    59  	// consensus set and/or unconfirmed set, and includes the consensus change
    60  	// that would result if all of the transactions made it into a block.
    61  	ReceiveUpdatedUnconfirmedTransactions([]types.Transaction, ConsensusChange)
    62  }
    63  
    64  // A TransactionPool manages unconfirmed transactions.
    65  type TransactionPool interface {
    66  	// AcceptTransactionSet accepts a set of potentially interdependent
    67  	// transactions.
    68  	AcceptTransactionSet([]types.Transaction) error
    69  
    70  	// FeeEstimation returns an estimation for how high the transaction fee
    71  	// needs to be per byte. The minimum recommended targets getting accepted
    72  	// in ~3 blocks, and the maximum recommended targets getting accepted
    73  	// immediately. Taking the average has a moderate chance of being accepted
    74  	// within one block. The minimum has a strong chance of getting accepted
    75  	// within 10 blocks.
    76  	FeeEstimation() (minimumRecommended, maximumRecommended types.Currency)
    77  
    78  	// IsStandardTransaction returns `err = nil` if the transaction is
    79  	// standard, otherwise it returns an error explaining what is not standard.
    80  	IsStandardTransaction(types.Transaction) error
    81  
    82  	// PurgeTransactionPool is a temporary function available to the miner. In
    83  	// the event that a miner mines an unacceptable block, the transaction pool
    84  	// will be purged to clear out the transaction pool and get rid of the
    85  	// illegal transaction. This should never happen, however there are bugs
    86  	// that make this condition necessary.
    87  	PurgeTransactionPool()
    88  
    89  	// TransactionList returns a list of all transactions in the transaction
    90  	// pool. The transactions are provided in an order that can acceptably be
    91  	// put into a block.
    92  	TransactionList() []types.Transaction
    93  
    94  	// TransactionPoolSubscribe adds a subscriber to the transaction pool.
    95  	// Subscribers will receive all consensus set changes as well as
    96  	// transaction pool changes, and should not subscribe to both.
    97  	TransactionPoolSubscribe(TransactionPoolSubscriber)
    98  }
    99  
   100  // ConsensusConflict implements the error interface, and indicates that a
   101  // transaction was rejected due to being incompatible with the current
   102  // consensus set, meaning either a double spend or a consensus rule violation -
   103  // it is unlikely that the transaction will ever be valid.
   104  type ConsensusConflict string
   105  
   106  // NewConsensusConflict returns a consensus conflict, which implements the
   107  // error interface.
   108  func NewConsensusConflict(s string) ConsensusConflict {
   109  	return ConsensusConflict("consensus conflict: " + s)
   110  }
   111  
   112  // Error implements the error interface, turning the consensus conflict into an
   113  // acceptable error type.
   114  func (cc ConsensusConflict) Error() string {
   115  	return string(cc)
   116  }
   117  
   118  // CalculateFee returns the fee-per-byte of a transaction set.
   119  func CalculateFee(ts []types.Transaction) types.Currency {
   120  	var sum types.Currency
   121  	for _, t := range ts {
   122  		for _, fee := range t.MinerFees {
   123  			sum = sum.Add(fee)
   124  		}
   125  	}
   126  	size := len(encoding.Marshal(ts))
   127  	return sum.Div64(uint64(size))
   128  }