forked from Onyx-Protocol/Onyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
61 lines (52 loc) · 1.39 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package protocol
import (
"sync"
"github.com/golang/groupcache/lru"
"chain/errors"
"chain/protocol/bc"
"chain/protocol/validation"
)
// ValidateTxCached checks a cache of prevalidated transactions
// before attempting to perform a context-free validation of the tx.
func (c *Chain) ValidateTxCached(tx *bc.Tx) error {
// Consult a cache of prevalidated transactions.
err, ok := c.prevalidated.lookup(tx.Hash)
if ok {
return err
}
err = validation.CheckTxWellFormed(tx)
c.prevalidated.cache(tx.Hash, err)
return err
}
type prevalidatedTxsCache struct {
mu sync.Mutex
lru *lru.Cache
}
func (c *prevalidatedTxsCache) lookup(txID bc.Hash) (err error, ok bool) {
c.mu.Lock()
v, ok := c.lru.Get(txID)
c.mu.Unlock()
if !ok {
return err, ok
}
if v == nil {
return nil, ok
}
return v.(error), ok
}
func (c *prevalidatedTxsCache) cache(txID bc.Hash, err error) {
c.mu.Lock()
c.lru.Add(txID, err)
c.mu.Unlock()
}
func (c *Chain) checkIssuanceWindow(tx *bc.Tx) error {
for _, txi := range tx.Inputs {
if _, ok := txi.TypedInput.(*bc.IssuanceInput); ok {
// TODO(tessr): consider removing 0 check once we can configure this
if c.MaxIssuanceWindow != 0 && tx.MinTime+bc.DurationMillis(c.MaxIssuanceWindow) < tx.MaxTime {
return errors.WithDetailf(validation.ErrBadTx, "issuance input's time window is larger than the network maximum (%s)", c.MaxIssuanceWindow)
}
}
}
return nil
}