-
Notifications
You must be signed in to change notification settings - Fork 94
/
check_construction.go
153 lines (130 loc) · 4.05 KB
/
check_construction.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
// 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 cmd
import (
"context"
"fmt"
"log"
"time"
"github.com/coinbase/rosetta-cli/pkg/results"
"github.com/coinbase/rosetta-cli/pkg/tester"
"github.com/coinbase/rosetta-sdk-go/fetcher"
"github.com/coinbase/rosetta-sdk-go/utils"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
var (
checkConstructionCmd = &cobra.Command{
Use: "check:construction",
Short: "Check the correctness of a Rosetta Construction API Implementation",
Long: `The check:construction command runs an automated test of a
Construction API implementation by creating and broadcasting transactions
on a blockchain. In short, this tool generates new addresses, requests
funds, constructs transactions, signs transactions, broadcasts transactions,
and confirms transactions land on-chain. At each phase, a series of tests
are run to ensure that intermediate representations are correct (i.e. does
an unsigned transaction return a superset of operations provided during
construction?).
Check out the https://github.com/coinbase/rosetta-cli/tree/master/examples
directory for examples of how to configure this test for Bitcoin and
Ethereum.
Right now, this tool only supports transfer testing (for both account-based
and UTXO-based blockchains). However, we plan to add support for testing
arbitrary scenarios (i.e. staking, governance).`,
Run: runCheckConstructionCmd,
}
)
func runCheckConstructionCmd(cmd *cobra.Command, args []string) {
if Config.Construction == nil {
log.Fatal("construction configuration is missing!")
}
ensureDataDirectoryExists()
ctx, cancel := context.WithCancel(context.Background())
fetcher := fetcher.New(
Config.OnlineURL,
fetcher.WithMaxConnections(Config.MaxOnlineConnections),
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime)*time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout)*time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
)
_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network)
if fetchErr != nil {
results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("%w: unable to initialize asserter", fetchErr.Err),
1,
)
}
_, err := utils.CheckNetworkSupported(ctx, Config.Network, fetcher)
if err != nil {
results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("%w: unable to confirm network is supported", err),
1,
)
}
constructionTester, err := tester.InitializeConstruction(
ctx,
Config,
Config.Network,
fetcher,
cancel,
&SignalReceived,
)
if err != nil {
results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("%w: unable to initialize construction tester", err),
1,
)
}
defer constructionTester.CloseDatabase(ctx)
if err := constructionTester.PerformBroadcasts(ctx); err != nil {
log.Fatalf("%s: unable to perform broadcasts", err.Error())
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return constructionTester.StartPeriodicLogger(ctx)
})
g.Go(func() error {
return constructionTester.StartSyncer(ctx, cancel)
})
g.Go(func() error {
return constructionTester.StartConstructor(ctx)
})
g.Go(func() error {
return constructionTester.WatchEndConditions(ctx)
})
g.Go(func() error {
return tester.LogMemoryLoop(ctx)
})
g.Go(func() error {
return tester.StartServer(
ctx,
"check:construction status",
constructionTester,
Config.Construction.StatusPort,
)
})
sigListeners := []context.CancelFunc{cancel}
go handleSignals(&sigListeners)
err = g.Wait()
constructionTester.HandleErr(err, &sigListeners)
}