-
Notifications
You must be signed in to change notification settings - Fork 12
/
service.go
170 lines (147 loc) · 6.55 KB
/
service.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
package publicapi
import (
"context"
"github.com/orbs-network/orbs-network-go/config"
"github.com/orbs-network/orbs-network-go/instrumentation/log"
"github.com/orbs-network/orbs-network-go/instrumentation/metric"
"github.com/orbs-network/orbs-network-go/instrumentation/trace"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/orbs-network/orbs-spec/types/go/services/handlers"
"time"
)
var LogTag = log.Service("public-api")
type txResponse struct {
transactionStatus protocol.TransactionStatus
transactionReceipt *protocol.TransactionReceipt
blockHeight primitives.BlockHeight
blockTimestamp primitives.TimestampNano
}
type service struct {
config config.PublicApiConfig
transactionPool services.TransactionPool
virtualMachine services.VirtualMachine
blockStorage services.BlockStorage
logger log.BasicLogger
waiter *waiter
metrics *metrics
}
type metrics struct {
sendTransactionTime *metric.Histogram
getTransactionStatusTime *metric.Histogram
callMethodTime *metric.Histogram
}
func newMetrics(factory metric.Factory, sendTransactionTimeout time.Duration, getTransactionStatusTimeout time.Duration, callMethodTimeout time.Duration) *metrics {
return &metrics{
sendTransactionTime: factory.NewLatency("PublicApi.SendTransactionProcessingTime", sendTransactionTimeout),
getTransactionStatusTime: factory.NewLatency("PublicApi.GetTransactionStatusProcessingTime", getTransactionStatusTimeout),
callMethodTime: factory.NewLatency("PublicApi.CallMethodProcessingTime", callMethodTimeout),
}
}
func NewPublicApi(
config config.PublicApiConfig,
transactionPool services.TransactionPool,
virtualMachine services.VirtualMachine,
blockStorage services.BlockStorage,
logger log.BasicLogger,
metricFactory metric.Factory,
) services.PublicApi {
s := &service{
config: config,
transactionPool: transactionPool,
virtualMachine: virtualMachine,
blockStorage: blockStorage,
logger: logger.WithTags(LogTag),
waiter: newWaiter(),
metrics: newMetrics(metricFactory, config.SendTransactionTimeout(), 2*time.Second, 1*time.Second),
}
transactionPool.RegisterTransactionResultsHandler(s)
return s
}
func (s *service) HandleTransactionResults(ctx context.Context, input *handlers.HandleTransactionResultsInput) (*handlers.HandleTransactionResultsOutput, error) {
logger := s.logger.WithTags(trace.LogFieldFrom(ctx), log.String("flow", "checkpoint"))
for _, txReceipt := range input.TransactionReceipts {
logger.Info("transaction reported as committed", log.Transaction(txReceipt.Txhash()))
s.waiter.complete(txReceipt.Txhash().KeyForMap(),
&txResponse{
transactionStatus: protocol.TRANSACTION_STATUS_COMMITTED,
transactionReceipt: txReceipt,
blockHeight: input.BlockHeight,
blockTimestamp: input.Timestamp,
})
}
return &handlers.HandleTransactionResultsOutput{}, nil
}
func (s *service) HandleTransactionError(ctx context.Context, input *handlers.HandleTransactionErrorInput) (*handlers.HandleTransactionErrorOutput, error) {
logger := s.logger.WithTags(trace.LogFieldFrom(ctx), log.String("flow", "checkpoint"))
logger.Info("transaction reported as errored", log.Transaction(input.Txhash), log.Stringable("tx-status", input.TransactionStatus))
s.waiter.complete(input.Txhash.KeyForMap(),
&txResponse{
transactionStatus: input.TransactionStatus,
transactionReceipt: nil,
blockHeight: input.BlockHeight,
blockTimestamp: input.BlockTimestamp,
})
return &handlers.HandleTransactionErrorOutput{}, nil
}
func isTransactionRequestValid(config config.PublicApiConfig, tx *protocol.Transaction) protocol.TransactionStatus {
return isTransactionValuesValid(config, tx.VirtualChainId(), tx.ProtocolVersion())
}
func isTransactionValuesValid(config config.PublicApiConfig, vcId primitives.VirtualChainId, protocolVersion primitives.ProtocolVersion) protocol.TransactionStatus {
if config.VirtualChainId() != vcId {
return protocol.TRANSACTION_STATUS_REJECTED_VIRTUAL_CHAIN_MISMATCH
}
if primitives.ProtocolVersion(1) != protocolVersion {
return protocol.TRANSACTION_STATUS_REJECTED_UNSUPPORTED_VERSION
}
return protocol.TRANSACTION_STATUS_RESERVED // used as an OK
}
func translateTxStatusToResponseCode(txStatus protocol.TransactionStatus) protocol.RequestStatus {
switch txStatus {
case protocol.TRANSACTION_STATUS_COMMITTED:
return protocol.REQUEST_STATUS_COMPLETED
case protocol.TRANSACTION_STATUS_DUPLICATE_TRANSACTION_ALREADY_COMMITTED:
return protocol.REQUEST_STATUS_COMPLETED
case protocol.TRANSACTION_STATUS_PENDING:
return protocol.REQUEST_STATUS_IN_PROCESS
case protocol.TRANSACTION_STATUS_DUPLICATE_TRANSACTION_ALREADY_PENDING:
return protocol.REQUEST_STATUS_IN_PROCESS
case protocol.TRANSACTION_STATUS_NO_RECORD_FOUND:
return protocol.REQUEST_STATUS_NOT_FOUND
case protocol.TRANSACTION_STATUS_REJECTED_UNSUPPORTED_VERSION:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_VIRTUAL_CHAIN_MISMATCH:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_TIMESTAMP_WINDOW_EXCEEDED:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_SIGNATURE_MISMATCH:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_UNKNOWN_SIGNER_SCHEME:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_GLOBAL_PRE_ORDER:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_VIRTUAL_CHAIN_PRE_ORDER:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_SMART_CONTRACT_PRE_ORDER:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_TIMESTAMP_AHEAD_OF_NODE_TIME:
return protocol.REQUEST_STATUS_REJECTED
case protocol.TRANSACTION_STATUS_REJECTED_CONGESTION:
return protocol.REQUEST_STATUS_CONGESTION
}
return protocol.REQUEST_STATUS_RESERVED
}
func translateExecutionStatusToResponseCode(executionResult protocol.ExecutionResult) protocol.RequestStatus {
switch executionResult {
case protocol.EXECUTION_RESULT_SUCCESS:
return protocol.REQUEST_STATUS_COMPLETED
case protocol.EXECUTION_RESULT_ERROR_SMART_CONTRACT:
return protocol.REQUEST_STATUS_COMPLETED
case protocol.EXECUTION_RESULT_ERROR_INPUT:
return protocol.REQUEST_STATUS_REJECTED
case protocol.EXECUTION_RESULT_ERROR_UNEXPECTED:
return protocol.REQUEST_STATUS_SYSTEM_ERROR
}
return protocol.REQUEST_STATUS_RESERVED
}