-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
198 lines (166 loc) 路 6.25 KB
/
api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package preconshare
import (
"context"
"errors"
"time"
"github.com/cairoeth/preconfirmations/preconf-share/jsonrpcserver"
"github.com/cairoeth/preconfirmations/preconf-share/metrics"
"github.com/cairoeth/preconfirmations/preconf-share/spike"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/types"
"go.uber.org/zap"
)
var (
ErrInvalidInclusion = errors.New("invalid inclusion")
ErrInvalidBundleBodySize = errors.New("invalid bundle body size")
ErrInvalidBundleBody = errors.New("invalid bundle body")
ErrBackrunNotFound = errors.New("backrun not found")
ErrBackrunInvalidBundle = errors.New("backrun invalid bundle")
ErrBackrunInclusion = errors.New("backrun invalid inclusion")
ErrInternalServiceError = errors.New("mev-share service error")
bundleCacheSize = 1000
)
type SimScheduler interface {
ScheduleRequest(ctx context.Context, bundle *SendRequestArgs, highPriority bool) error
}
type BundleStorage interface {
GetBundleByMatchingHash(ctx context.Context, hash common.Hash) (*SendRequestArgs, error)
InsertPreconf(ctx context.Context, preconf *ConfirmRequestArgs) error
GetPreconfByMatchingHash(ctx context.Context, hash common.Hash) (*int64, *hexutil.Bytes, *int64, error)
UpdatePreconfTimeBySignature(ctx context.Context, time int64, hash common.Hash) error
}
type EthClient interface {
BlockNumber(ctx context.Context) (uint64, error)
}
type API struct {
log *zap.Logger
scheduler SimScheduler
bundleStorage BundleStorage
eth EthClient
signer types.Signer
simBackends []SimulationBackend
spikeManager *spike.Manager[*SendRequestArgs]
knownBundleCache *lru.Cache[common.Hash, SendRequestArgs]
cancellationCache *RedisCancellationCache
}
func NewAPI(
log *zap.Logger,
scheduler SimScheduler, bundleStorage BundleStorage, eth EthClient, signer types.Signer,
simBackends []SimulationBackend, cancellationCache *RedisCancellationCache,
sbundleValidDuration time.Duration,
) *API {
sm := spike.NewManager(func(ctx context.Context, k string) (*SendRequestArgs, error) {
return bundleStorage.GetBundleByMatchingHash(ctx, common.HexToHash(k))
}, sbundleValidDuration)
return &API{
log: log,
scheduler: scheduler,
bundleStorage: bundleStorage,
eth: eth,
signer: signer,
simBackends: simBackends,
spikeManager: sm,
knownBundleCache: lru.NewCache[common.Hash, SendRequestArgs](bundleCacheSize),
cancellationCache: cancellationCache,
}
}
func (m *API) SendRequest(ctx context.Context, request SendRequestArgs) (_ SendRequestResponse, err error) {
logger := m.log
startAt := time.Now()
defer func() {
metrics.RecordRPCCallDuration(SendRequestEndpointName, time.Since(startAt).Milliseconds())
}()
metrics.IncSbundlesReceived()
validateBundleTime := time.Now()
currentBlock, err := m.eth.BlockNumber(ctx)
if err != nil {
metrics.IncRPCCallFailure(SendRequestEndpointName)
logger.Error("failed to get current block", zap.Error(err))
return SendRequestResponse{}, ErrInternalServiceError
}
matchingHash, hasUnmatchedHash, err := ValidateRequest(&request, currentBlock, m.signer)
if err != nil {
logger.Warn("failed to validate request", zap.Error(err))
return SendRequestResponse{}, err
}
m.knownBundleCache.Add(matchingHash, request)
signerAddress := jsonrpcserver.GetSigner(ctx)
origin := jsonrpcserver.GetOrigin(ctx)
if request.Metadata == nil {
request.Metadata = &RequestMetadata{}
}
request.Metadata.Signer = signerAddress
request.Metadata.ReceivedAt = hexutil.Uint64(uint64(time.Now().UnixMicro()))
request.Metadata.OriginID = origin
request.Metadata.Prematched = !hasUnmatchedHash
metrics.RecordBundleValidationDuration(time.Since(validateBundleTime).Milliseconds())
metrics.IncSbundlesReceivedValid()
highPriority := jsonrpcserver.GetPriority(ctx)
err = m.scheduler.ScheduleRequest(ctx, &request, highPriority)
if err != nil {
metrics.IncRPCCallFailure(SendRequestEndpointName)
logger.Error("Failed to schedule request simulation", zap.Error(err))
return SendRequestResponse{}, ErrInternalServiceError
}
// sleep a bit to check if request was preconfirmed
time.Sleep(1000 * time.Millisecond)
// get preconf
block, signature, _, err := m.bundleStorage.GetPreconfByMatchingHash(ctx, matchingHash)
if err != nil {
logger.Error("Failed to get preconfirmations", zap.Error(err))
return SendRequestResponse{
RequestHash: matchingHash,
Signature: nil,
Block: 0,
}, nil
}
_ = m.bundleStorage.UpdatePreconfTimeBySignature(ctx, time.Since(startAt).Milliseconds(), matchingHash)
return SendRequestResponse{
RequestHash: matchingHash,
Signature: signature,
Block: hexutil.Uint64(uint64(*block)),
}, nil
}
func (m *API) ConfirmRequest(ctx context.Context, confirmation ConfirmRequestArgs) (_ ConfirmRequestResponse, err error) {
logger := m.log
startAt := time.Now()
defer func() {
metrics.RecordRPCCallDuration(ConfirmRequestEndpointName, time.Since(startAt).Milliseconds())
}()
logger.Info("Received request connfirmation")
// TODO: validate confirmation (check signature, that is targeted for the current request, etc.)
// Store preconfirmation in database.
err = m.bundleStorage.InsertPreconf(ctx, &confirmation)
if err != nil {
metrics.IncRPCCallFailure(ConfirmRequestEndpointName)
logger.Error("Failed to insert confirmation in db", zap.Error(err))
return ConfirmRequestResponse{}, ErrInternalServiceError
}
return ConfirmRequestResponse{
Valid: true,
}, nil
}
func (m *API) GetRequest(ctx context.Context, request GetRequestArgs) (_ GetRequestResponse, err error) {
logger := m.log
startAt := time.Now()
defer func() {
metrics.RecordRPCCallDuration(GetRequestEndpointName, time.Since(startAt).Milliseconds())
}()
// Get preconfirmation in database.
block, signature, time, err := m.bundleStorage.GetPreconfByMatchingHash(ctx, request.Hash)
if err != nil {
logger.Error("Failed to get preconfirmations", zap.Error(err))
return GetRequestResponse{
Signature: nil,
Block: 0,
Time: 0,
}, nil
}
return GetRequestResponse{
Signature: signature,
Block: hexutil.Uint64(uint64(*block)),
Time: hexutil.Uint64(uint64(*time)),
}, nil
}