-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
169 lines (155 loc) · 4.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package txs
import (
"context"
"errors"
"fmt"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"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 (
errWrongHash = fmt.Errorf("%w: incorrect hash", pubsub.ErrValidationReject)
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 {
self peer.ID
logger log.Log
state conservativeState
}
// NewTxHandler returns a new TxHandler.
func NewTxHandler(s conservativeState, id peer.ID, l log.Log) *TxHandler {
return &TxHandler{
self: id,
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, peer p2p.Peer, msg []byte) error {
if peer == th.self {
return nil
}
err := th.VerifyAndCacheTx(ctx, msg)
updateMetrics(err, gossipTxCount)
if err != nil {
if !errors.Is(err, errDuplicateTX) {
th.logger.WithContext(ctx).With().Warning("failed to handle tx", log.Err(err))
}
return err
}
return nil
}
// HandleProposalTransaction handles data received on the transactions synced as a part of proposal.
func (th *TxHandler) HandleProposalTransaction(
ctx context.Context,
expHash types.Hash32,
_ p2p.Peer,
msg []byte,
) error {
err := th.verifyAndCache(ctx, expHash, msg)
updateMetrics(err, proposalTxCount)
if errors.Is(err, errDuplicateTX) {
return nil
}
return err
}
func (th *TxHandler) VerifyAndCacheTx(ctx context.Context, msg []byte) error {
return th.verifyAndCache(ctx, types.Hash32{}, msg)
}
func (th *TxHandler) verifyAndCache(ctx context.Context, expHash types.Hash32, msg []byte) error {
raw := types.NewRawTx(msg)
mtx, err := th.state.GetMeshTransaction(raw.ID)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return fmt.Errorf("get tx %w", err)
}
if mtx != nil && mtx.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)
}
tx := &types.Transaction{RawTx: raw, TxHeader: header}
if expHash != (types.Hash32{}) && tx.ID.Hash32() != expHash {
return fmt.Errorf("%w: proposal tx want %s, got %s", errWrongHash, expHash.ShortString(), tx.ID.ShortString())
}
if header.LayerLimits.Min != 0 || header.LayerLimits.Max != 0 {
return fmt.Errorf("%w: layers limits are not enabled %s", errParse, raw.ID)
}
if header.GasPrice == 0 || header.Fee() == 0 {
return fmt.Errorf("%w: zero gas price %s", errParse, raw.ID)
}
if !req.Verify() {
return fmt.Errorf("%w: %s", errVerify, raw.ID)
}
if err := th.state.AddToCache(ctx, tx, time.Now()); 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(_ context.Context, expHash types.Hash32, _ p2p.Peer, 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}
if tx.ID.Hash32() != expHash {
return fmt.Errorf("%w: block tx want %s, got %s", errWrongHash, expHash.ShortString(), tx.ID.ShortString())
}
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
}