6 "github.com/golang/groupcache/lru"
8 "github.com/bytom/errors"
9 "github.com/bytom/protocol/bc"
10 "github.com/bytom/protocol/validation"
13 // ErrBadTx is returned for transactions failing validation
14 var ErrBadTx = errors.New("invalid transaction")
16 // ValidateTx validates the given transaction. A cache holds
17 // per-transaction validation results and is consulted before
18 // performing full validation.
19 func (c *Chain) ValidateTx(tx *bc.Tx) error {
20 err := c.checkIssuanceWindow(tx)
25 err, ok = c.prevalidated.lookup(tx.ID)
27 err = validation.ValidateTx(tx, c.InitialBlockHash)
28 c.prevalidated.cache(tx.ID, err)
30 return errors.Sub(ErrBadTx, err)
33 type prevalidatedTxsCache struct {
38 func (c *prevalidatedTxsCache) lookup(txID bc.Hash) (err error, ok bool) {
40 v, ok := c.lru.Get(txID)
51 func (c *prevalidatedTxsCache) cache(txID bc.Hash, err error) {
57 func (c *Chain) checkIssuanceWindow(tx *bc.Tx) error {
58 if c.MaxIssuanceWindow == 0 {
61 for _, entryID := range tx.InputIDs {
62 if _, err := tx.Issuance(entryID); err == nil {
63 if tx.MinTimeMs+bc.DurationMillis(c.MaxIssuanceWindow) < tx.MaxTimeMs {
64 return errors.WithDetailf(ErrBadTx, "issuance input's time window is larger than the network maximum (%s)", c.MaxIssuanceWindow)