forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_type.go
More file actions
160 lines (136 loc) · 4.28 KB
/
Copy pathrequest_type.go
File metadata and controls
160 lines (136 loc) · 4.28 KB
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
/*
* Copyright 2018 The CovenantSQL Authors.
*
* 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 types
import (
"fmt"
"time"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/crypto/verifier"
"github.com/CovenantSQL/CovenantSQL/proto"
)
//go:generate hsp
// QueryType enumerates available query type, currently read/write.
type QueryType int32
const (
// ReadQuery defines a read query type.
ReadQuery QueryType = iota
// WriteQuery defines a write query type.
WriteQuery
// NumberOfQueryType defines the number of query type.
NumberOfQueryType
)
// NamedArg defines the named argument structure for database.
type NamedArg struct {
Name string
Value interface{}
}
// Query defines single query.
type Query struct {
Pattern string
Args []NamedArg
}
// RequestPayload defines a queries payload.
type RequestPayload struct {
Queries []Query `json:"qs"`
}
// RequestHeader defines a query request header.
type RequestHeader struct {
QueryType QueryType `json:"qt"`
NodeID proto.NodeID `json:"id"` // request node id
DatabaseID proto.DatabaseID `json:"dbid"` // request database id
ConnectionID uint64 `json:"cid"`
SeqNo uint64 `json:"seq"`
Timestamp time.Time `json:"t"` // time in UTC zone
BatchCount uint64 `json:"bc"` // query count in this request
QueriesHash hash.Hash `json:"qh"` // hash of query payload
}
// GetQueryKey returns a unique query key of this request.
func (h *RequestHeader) GetQueryKey() QueryKey {
return QueryKey{
NodeID: h.NodeID,
ConnectionID: h.ConnectionID,
SeqNo: h.SeqNo,
}
}
// QueryKey defines an unique query key of a request.
type QueryKey struct {
NodeID proto.NodeID `json:"id"`
ConnectionID uint64 `json:"cid"`
SeqNo uint64 `json:"seq"`
}
// String implements fmt.Stringer for logging purpose.
func (k QueryKey) String() string {
return fmt.Sprintf("%s#%016x#%016x", string(k.NodeID[len(k.NodeID)-8:]), k.ConnectionID, k.SeqNo)
}
// SignedRequestHeader defines a signed query request header.
type SignedRequestHeader struct {
RequestHeader
verifier.DefaultHashSignVerifierImpl
}
// Request defines a complete query request.
type Request struct {
proto.Envelope
Header SignedRequestHeader `json:"h"`
Payload RequestPayload `json:"p"`
_marshalCache []byte `json:"-"`
}
// String implements fmt.Stringer for logging purpose.
func (t QueryType) String() string {
switch t {
case ReadQuery:
return "read"
case WriteQuery:
return "write"
default:
return "unknown"
}
}
// Verify checks hash and signature in request header.
func (sh *SignedRequestHeader) Verify() (err error) {
return sh.DefaultHashSignVerifierImpl.Verify(&sh.RequestHeader)
}
// Sign the request.
func (sh *SignedRequestHeader) Sign(signer *asymmetric.PrivateKey) (err error) {
return sh.DefaultHashSignVerifierImpl.Sign(&sh.RequestHeader, signer)
}
// Verify checks hash and signature in whole request.
func (r *Request) Verify() (err error) {
// verify payload hash in signed header
if err = verifyHash(&r.Payload, &r.Header.QueriesHash); err != nil {
return
}
// verify header sign
return r.Header.Verify()
}
// Sign the request.
func (r *Request) Sign(signer *asymmetric.PrivateKey) (err error) {
// set query count
r.Header.BatchCount = uint64(len(r.Payload.Queries))
// compute payload hash
if err = buildHash(&r.Payload, &r.Header.QueriesHash); err != nil {
return
}
return r.Header.Sign(signer)
}
// SetMarshalCache sets _marshalCache.
func (r *Request) SetMarshalCache(buf []byte) {
r._marshalCache = buf
}
// GetMarshalCache gets _marshalCache.
func (r *Request) GetMarshalCache() (buf []byte) {
return r._marshalCache
}