-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
203 lines (168 loc) · 5.11 KB
/
main.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
// 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 main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"github.com/helium/rosetta-helium/helium"
"github.com/helium/rosetta-helium/services"
"github.com/helium/rosetta-helium/utils"
"go.uber.org/zap"
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
badger "github.com/dgraph-io/badger/v3"
)
const (
serverPort = 8080
)
// NewBlockchainRouter creates a Mux http.Handler from a collection
// of server controllers.
func NewBlockchainRouter(
network *types.NetworkIdentifier,
a *asserter.Asserter,
) http.Handler {
networkAPIService := services.NewNetworkAPIService(network)
networkAPIController := server.NewNetworkAPIController(
networkAPIService,
a,
)
blockAPIService := services.NewBlockAPIService(network)
blockAPIController := server.NewBlockAPIController(
blockAPIService,
a,
)
accountAPIService := services.NewAccountAPIService(network)
accountAPIController := server.NewAccountAPIController(
accountAPIService,
a,
)
constructionAPIService := services.NewConstructionAPIService(network)
constructionAPIController := server.NewConstructionAPIController(
constructionAPIService,
a,
)
return server.NewRouter(networkAPIController, blockAPIController, accountAPIController, constructionAPIController)
}
func LoadGhostTxns(network *types.NetworkIdentifier, db *badger.DB) error {
if werr := filepath.Walk("ghost-transactions", func(path string, info os.FileInfo, err error) error {
if err != nil {
zap.S().Error(err.Error())
}
if info.IsDir() {
return nil
}
jsonFile, jerr := os.Open("ghost-transactions/" + info.Name())
if jerr != nil {
return jerr
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result []map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
for _, txn := range result {
var txnMetadata helium.UnstakeTransaction
var txnMetadataMap map[string]interface{}
json.Unmarshal([]byte(fmt.Sprint(txn["Fields"])), &txnMetadata)
json.Unmarshal([]byte(fmt.Sprint(txn["Fields"])), &txnMetadataMap)
dbKey := &utils.GhostTxnKey{
Network: network,
Block: &types.BlockIdentifier{
Index: txnMetadata.StakeReleaseHeight,
},
Transaction: &types.TransactionIdentifier{
Hash: txnMetadata.Hash,
},
}
Unstake, uErr := helium.CreateCreditOp(helium.UnstakeValidatorOp, txnMetadata.Address, txnMetadata.StakeAmount, helium.HNT, helium.SuccessStatus, 0, txnMetadataMap)
if uErr != nil {
return errors.New(fmt.Sprint(uErr))
}
cerr := utils.CreateGhostTxn(dbKey, &utils.GhostTxnMetadata{
Operations: []*types.Operation{
Unstake,
},
Metadata: txnMetadataMap,
})
if cerr != nil && cerr != badger.ErrBannedKey {
return cerr
}
}
zap.S().Info("Loaded " + fmt.Sprint(len(result)) + " ghost txns from file " + info.Name())
return nil
}); werr != nil {
return werr
}
return nil
}
func main() {
logger, _ := zap.NewDevelopment()
defer logger.Sync() // flushes buffer, if any
globalLogger := zap.ReplaceGlobals(logger)
defer globalLogger()
bdb, err := badger.Open(badger.DefaultOptions("badger"))
if err != nil {
zap.S().Fatal(err)
}
defer bdb.Close()
utils.DB = bdb
var testnet bool
var network *types.NetworkIdentifier
flag.BoolVar(&testnet, "testnet", false, "run testnet version of rosetta-helium")
flag.Parse()
if !testnet {
zap.S().Info("Initilizing mainnet node...")
network = &types.NetworkIdentifier{
Blockchain: "Helium",
Network: helium.MainnetNetwork,
}
if lerr := LoadGhostTxns(network, bdb); lerr != nil {
zap.S().Error("Cannot load ghost transactions: " + lerr.Error())
os.Exit(1)
}
} else {
zap.S().Info("Initilizing testnet node...")
network = &types.NetworkIdentifier{
Blockchain: "Helium",
Network: helium.TestnetNetwork,
}
}
helium.CurrentNetwork = network
// The asserter automatically rejects incorrectly formatted
// requests.
a, err := asserter.NewServer(
helium.OperationTypes,
helium.HistoricalBalanceSupported,
[]*types.NetworkIdentifier{network},
nil,
false,
)
if err != nil {
log.Fatal(err)
}
// Create the main router handler then apply the logger and Cors
// middlewares in sequence.
router := NewBlockchainRouter(network, a)
loggedRouter := server.LoggerMiddleware(router)
corsRouter := server.CorsMiddleware(loggedRouter)
zap.S().Info("Listening on port ", serverPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", serverPort), corsRouter))
}