Repository navigation
Expand file tree
/
Copy pathhelper.go
More file actions
109 lines (98 loc) · 2.89 KB
/
Copy pathhelper.go
File metadata and controls
109 lines (98 loc) · 2.89 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
/*
* 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 test
import (
"context"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/route"
rpc "github.com/CovenantSQL/CovenantSQL/rpc/mux"
"github.com/CovenantSQL/CovenantSQL/types"
)
// WaitBPChainService waits until BP chain service is ready.
func WaitBPChainService(ctx context.Context, period time.Duration) (err error) {
var (
ticker = time.NewTicker(period)
req = &types.FetchBlockReq{
Height: 0, // Genesis block
}
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err = rpc.RequestBP(
route.MCCFetchBlock.String(), req, nil,
); err == nil || !strings.Contains(err.Error(), "can't find service") {
return
}
case <-ctx.Done():
err = ctx.Err()
return
}
}
}
// Create allocates new database.
func Create(
meta types.ResourceMeta,
gasPrice uint64,
advancePayment uint64,
privateKey *asymmetric.PrivateKey,
) (
dbID proto.DatabaseID, dsn string, err error,
) {
var (
nonceReq = new(types.NextAccountNonceReq)
nonceResp = new(types.NextAccountNonceResp)
req = new(types.AddTxReq)
resp = new(types.AddTxResp)
clientAddr proto.AccountAddress
)
if clientAddr, err = crypto.PubKeyHash(privateKey.PubKey()); err != nil {
err = errors.Wrap(err, "get local account address failed")
return
}
// allocate nonce
nonceReq.Addr = clientAddr
if err = rpc.RequestBP(route.MCCNextAccountNonce.String(), nonceReq, nonceResp); err != nil {
err = errors.Wrap(err, "allocate create database transaction nonce failed")
return
}
req.Tx = types.NewCreateDatabase(&types.CreateDatabaseHeader{
Owner: clientAddr,
ResourceMeta: meta,
GasPrice: gasPrice,
AdvancePayment: advancePayment,
TokenType: types.Particle,
Nonce: nonceResp.Nonce,
})
if err = req.Tx.Sign(privateKey); err != nil {
err = errors.Wrap(err, "sign request failed")
return
}
if err = rpc.RequestBP(route.MCCAddTx.String(), req, resp); err != nil {
err = errors.Wrap(err, "call create database transaction failed")
return
}
dbID = proto.FromAccountAndNonce(clientAddr, uint32(nonceResp.Nonce))
dsn = fmt.Sprintf("cql://%s", string(dbID))
return
}