-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
135 lines (123 loc) · 3.81 KB
/
handler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package txs
import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/sql"
)
var (
errDuplicateTX = errors.New("tx already exists")
errParse = errors.New("failed to parse tx")
errVerify = errors.New("failed to verify tx")
)
// TxHandler handles the transactions received via gossip or sync.
type TxHandler struct {
logger log.Log
state conservativeState
}
// NewTxHandler returns a new TxHandler.
func NewTxHandler(s conservativeState, l log.Log) *TxHandler {
return &TxHandler{
logger: l,
state: s,
}
}
func updateMetrics(err error, counter *prometheus.CounterVec) {
switch {
case err == nil:
counter.WithLabelValues(saved).Inc()
case errors.Is(err, errDuplicateTX):
counter.WithLabelValues(duplicate).Inc()
case errors.Is(err, errBadNonce):
counter.WithLabelValues(rejectedBadNonce).Inc()
case errors.Is(err, errParse):
counter.WithLabelValues(cantParse).Inc()
case errors.Is(err, errVerify):
counter.WithLabelValues(cantVerify).Inc()
default:
counter.WithLabelValues(rejectedInternalErr).Inc()
}
}
// HandleGossipTransaction handles data received on the transactions gossip channel.
func (th *TxHandler) HandleGossipTransaction(ctx context.Context, _ p2p.Peer, msg []byte) pubsub.ValidationResult {
err := th.handleTransaction(ctx, msg)
defer updateMetrics(err, gossipTxCount)
if err != nil {
th.logger.WithContext(ctx).With().Warning("failed to handle tx", log.Err(err))
return pubsub.ValidationIgnore
}
return pubsub.ValidationAccept
}
// HandleProposalTransaction handles data received on the transactions synced as a part of proposal.
func (th *TxHandler) HandleProposalTransaction(ctx context.Context, msg []byte) error {
err := th.handleTransaction(ctx, msg)
defer updateMetrics(err, proposalTxCount)
if err == nil || errors.Is(err, errDuplicateTX) {
return nil
}
return err
}
func (th *TxHandler) handleTransaction(ctx context.Context, msg []byte) error {
raw := types.NewRawTx(msg)
tx, err := th.state.GetMeshTransaction(raw.ID)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return fmt.Errorf("get tx %w", err)
}
if tx != nil && tx.TxHeader != nil {
return errDuplicateTX
}
req := th.state.Validation(raw)
header, err := req.Parse()
if err != nil {
return fmt.Errorf("%w: %s (err: %s)", errParse, raw.ID, err)
}
if !req.Verify() {
return fmt.Errorf("%w: %s", errVerify, raw.ID)
}
if err := th.state.AddToCache(ctx, &types.Transaction{RawTx: raw, TxHeader: header}); err != nil {
th.logger.WithContext(ctx).With().Warning("failed to add tx to conservative cache",
raw.ID,
log.Err(err))
return err
}
return nil
}
// HandleBlockTransaction handles transactions received as a reference to a block.
func (th *TxHandler) HandleBlockTransaction(ctx context.Context, data []byte) error {
raw := types.NewRawTx(data)
exists, err := th.state.HasTx(raw.ID)
if err != nil {
blockTxCount.WithLabelValues(rejectedInternalErr).Inc()
return fmt.Errorf("has block tx: %w", err)
} else if exists {
blockTxCount.WithLabelValues(duplicate).Inc()
return nil
}
tx := &types.Transaction{RawTx: raw}
req := th.state.Validation(raw)
header, err := req.Parse()
if err == nil {
if req.Verify() {
tx.TxHeader = header
} else {
blockTxCount.WithLabelValues(cantVerify).Inc()
}
} else {
blockTxCount.WithLabelValues(cantParse).Inc()
}
if err = th.state.AddToDB(tx); err != nil {
blockTxCount.WithLabelValues(rejectedInternalErr).Inc()
return fmt.Errorf("add block tx %w", err)
}
if header != nil {
blockTxCount.WithLabelValues(saved).Inc()
} else {
blockTxCount.WithLabelValues(savedNoHdr).Inc()
}
return nil
}