forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asserter.go
225 lines (193 loc) · 6.45 KB
/
asserter.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package asserter
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path"
"github.com/coinbase/rosetta-sdk-go/types"
)
var (
// ErrAsserterNotInitialized is returned when some call in the asserter
// package requires the asserter to be initialized first.
ErrAsserterNotInitialized = errors.New("asserter not initialized")
)
// Asserter contains all logic to perform static
// validation on Rosetta Server responses.
type Asserter struct {
// These variables are used for response assertion.
network *types.NetworkIdentifier
operationTypes []string
operationStatusMap map[string]bool
errorTypeMap map[int32]*types.Error
genesisBlock *types.BlockIdentifier
// These variables are used for request assertion.
historicalBalanceLookup bool
supportedNetworks []*types.NetworkIdentifier
}
// NewServer constructs a new Asserter for use in the
// server package.
func NewServer(
supportedOperationTypes []string,
historicalBalanceLookup bool,
supportedNetworks []*types.NetworkIdentifier,
) (*Asserter, error) {
if err := OperationTypes(supportedOperationTypes); err != nil {
return nil, err
}
if err := SupportedNetworks(supportedNetworks); err != nil {
return nil, err
}
return &Asserter{
operationTypes: supportedOperationTypes,
historicalBalanceLookup: historicalBalanceLookup,
supportedNetworks: supportedNetworks,
}, nil
}
// NewClientWithResponses constructs a new Asserter
// from a NetworkStatusResponse and
// NetworkOptionsResponse.
func NewClientWithResponses(
network *types.NetworkIdentifier,
networkStatus *types.NetworkStatusResponse,
networkOptions *types.NetworkOptionsResponse,
) (*Asserter, error) {
if err := NetworkIdentifier(network); err != nil {
return nil, err
}
if err := NetworkStatusResponse(networkStatus); err != nil {
return nil, err
}
if err := NetworkOptionsResponse(networkOptions); err != nil {
return nil, err
}
return NewClientWithOptions(
network,
networkStatus.GenesisBlockIdentifier,
networkOptions.Allow.OperationTypes,
networkOptions.Allow.OperationStatuses,
networkOptions.Allow.Errors,
)
}
// Configuration is the static configuration of an Asserter. This
// configuration can be exported by the Asserter and used to instantiate an
// Asserter.
type Configuration struct {
NetworkIdentifier *types.NetworkIdentifier `json:"network_identifier"`
GenesisBlockIdentifier *types.BlockIdentifier `json:"genesis_block_identifier"`
AllowedOperationTypes []string `json:"allowed_operation_types"`
AllowedOperationStatuses []*types.OperationStatus `json:"allowed_operation_statuses"`
AllowedErrors []*types.Error `json:"allowed_errors"`
}
// NewClientWithFile constructs a new Asserter using a specification
// file instead of responses. This can be useful for running reliable
// systems that error when updates to the server (more error types,
// more operations, etc.) significantly change how to parse the chain.
// The filePath provided is parsed relative to the current directory.
func NewClientWithFile(
filePath string,
) (*Asserter, error) {
content, err := ioutil.ReadFile(path.Clean(filePath))
if err != nil {
return nil, err
}
config := &Configuration{}
if err := json.Unmarshal(content, config); err != nil {
return nil, err
}
return NewClientWithOptions(
config.NetworkIdentifier,
config.GenesisBlockIdentifier,
config.AllowedOperationTypes,
config.AllowedOperationStatuses,
config.AllowedErrors,
)
}
// NewClientWithOptions constructs a new Asserter using the provided
// arguments instead of using a NetworkStatusResponse and a
// NetworkOptionsResponse.
func NewClientWithOptions(
network *types.NetworkIdentifier,
genesisBlockIdentifier *types.BlockIdentifier,
operationTypes []string,
operationStatuses []*types.OperationStatus,
errors []*types.Error,
) (*Asserter, error) {
if err := NetworkIdentifier(network); err != nil {
return nil, err
}
if err := BlockIdentifier(genesisBlockIdentifier); err != nil {
return nil, err
}
if err := OperationStatuses(operationStatuses); err != nil {
return nil, err
}
if err := OperationTypes(operationTypes); err != nil {
return nil, err
}
asserter := &Asserter{
network: network,
operationTypes: operationTypes,
genesisBlock: genesisBlockIdentifier,
}
asserter.operationStatusMap = map[string]bool{}
for _, status := range operationStatuses {
asserter.operationStatusMap[status.Status] = status.Successful
}
asserter.errorTypeMap = map[int32]*types.Error{}
for _, err := range errors {
asserter.errorTypeMap[err.Code] = err
}
return asserter, nil
}
// ClientConfiguration returns all variables currently set in an Asserter.
// This function will error if it is called on an uninitialized asserter.
func (a *Asserter) ClientConfiguration() (*Configuration, error) {
if a == nil {
return nil, ErrAsserterNotInitialized
}
operationStatuses := []*types.OperationStatus{}
for k, v := range a.operationStatusMap {
operationStatuses = append(operationStatuses, &types.OperationStatus{
Status: k,
Successful: v,
})
}
errors := []*types.Error{}
for _, v := range a.errorTypeMap {
errors = append(errors, v)
}
return &Configuration{
NetworkIdentifier: a.network,
GenesisBlockIdentifier: a.genesisBlock,
AllowedOperationTypes: a.operationTypes,
AllowedOperationStatuses: operationStatuses,
AllowedErrors: errors,
}, nil
}
// OperationSuccessful returns a boolean indicating if a types.Operation is
// successful and should be applied in a transaction. This should only be called
// AFTER an operation has been validated.
func (a *Asserter) OperationSuccessful(operation *types.Operation) (bool, error) {
if a == nil {
return false, ErrAsserterNotInitialized
}
val, ok := a.operationStatusMap[operation.Status]
if !ok {
return false, fmt.Errorf("%s not found", operation.Status)
}
return val, nil
}