/
configuration.go
170 lines (145 loc) · 5.02 KB
/
configuration.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
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 Rosetta Dogecoin Developers
//
// 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 dogecoin
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"time"
"github.com/rosetta-dogecoin/rosetta-dogecoin/configuration"
"github.com/coinbase/rosetta-sdk-go/storage/encoder"
"github.com/coinbase/rosetta-sdk-go/types"
)
const (
// Zstandard compression dictionaries
transactionNamespace = "transaction"
mainnetRPCPort = 22555
testnetRPCPort = 44555
// min prune depth is 288:
// https://github.com/bitcoin/bitcoin/blob/ad2952d17a2af419a04256b10b53c7377f826a27/src/validation.h#L84
pruneDepth = int64(10000) //nolint
// min prune height (on mainnet):
// https://github.com/bitcoin/bitcoin/blob/62d137ac3b701aae36c1aa3aa93a83fd6357fde6/src/chainparams.cpp#L102
minPruneHeight = int64(100000) //nolint
// attempt to prune once an hour
pruneFrequency = 60 * time.Minute
bitcoindPath = "dogecoind"
indexerPath = "indexer"
// allFilePermissions specifies anyone can do anything
// to the file.
allFilePermissions = 0777
// defaults
defaultConfigurationDirectory = "/app"
mainnetConfigFile = "bitcoin-mainnet.conf"
testnetConfigFile = "bitcoin-testnet.conf"
mainnetTxDict = "mainnet-transaction.zstd"
testnetTxDict = "testnet-transaction.zstd"
)
var (
// configurationDirectory is the configuration path prefix
configurationDirectory string
)
// LoadConfiguration attempts to create a new Configuration
// using the ENVs in the environment.
func LoadConfiguration(baseDirectory string) (*configuration.Configuration, error) {
configurationDirectory = os.Getenv("CONFIG_DIR")
if len(configurationDirectory) == 0 {
configurationDirectory = defaultConfigurationDirectory
}
config := &configuration.Configuration{}
config.Pruning = &configuration.PruningConfiguration{
Frequency: pruneFrequency,
Depth: pruneDepth,
MinHeight: minPruneHeight,
}
modeValue := configuration.Mode(os.Getenv(configuration.ModeEnv))
switch modeValue {
case configuration.Online:
config.Mode = configuration.Online
config.IndexerPath = path.Join(baseDirectory, indexerPath)
if err := ensurePathExists(config.IndexerPath); err != nil {
return nil, fmt.Errorf("%w: unable to create indexer path", err)
}
config.BitcoindPath = path.Join(baseDirectory, bitcoindPath)
if err := ensurePathExists(config.BitcoindPath); err != nil {
return nil, fmt.Errorf("%w: unable to create bitcoind path", err)
}
case configuration.Offline:
config.Mode = configuration.Offline
case "":
return nil, errors.New("MODE must be populated")
default:
return nil, fmt.Errorf("%s is not a valid mode", modeValue)
}
networkValue := os.Getenv(configuration.NetworkEnv)
switch networkValue {
case configuration.Mainnet:
config.Network = &types.NetworkIdentifier{
Blockchain: Blockchain,
Network: MainnetNetwork,
}
config.GenesisBlockIdentifier = MainnetGenesisBlockIdentifier
config.Params = MainnetParams
config.Currency = MainnetCurrency
config.ConfigPath = configurationDirectory + "/" + mainnetConfigFile
config.RPCPort = mainnetRPCPort
config.Compressors = []*encoder.CompressorEntry{
{
Namespace: transactionNamespace,
DictionaryPath: configurationDirectory + "/" + mainnetTxDict,
},
}
case configuration.Testnet:
config.Network = &types.NetworkIdentifier{
Blockchain: Blockchain,
Network: TestnetNetwork,
}
config.GenesisBlockIdentifier = TestnetGenesisBlockIdentifier
config.Params = TestnetParams
config.Currency = TestnetCurrency
config.ConfigPath = configurationDirectory + "/" + testnetConfigFile
config.RPCPort = testnetRPCPort
config.Compressors = []*encoder.CompressorEntry{
{
Namespace: transactionNamespace,
DictionaryPath: configurationDirectory + "/" + testnetTxDict,
},
}
case "":
return nil, errors.New("NETWORK must be populated")
default:
return nil, fmt.Errorf("%s is not a valid network", networkValue)
}
portValue := os.Getenv(configuration.PortEnv)
if len(portValue) == 0 {
return nil, errors.New("PORT must be populated")
}
port, err := strconv.Atoi(portValue)
if err != nil || len(portValue) == 0 || port <= 0 {
return nil, fmt.Errorf("%w: unable to parse port %s", err, portValue)
}
config.Port = port
return config, nil
}
// ensurePathsExist directories along
// a path if they do not exist.
func ensurePathExists(path string) error {
if err := os.MkdirAll(path, os.FileMode(allFilePermissions)); err != nil {
return fmt.Errorf("%w: unable to create %s directory", err, path)
}
return nil
}