Skip to content

Commit 8b10b45

Browse files
committed
Initial commit
0 parents  commit 8b10b45

File tree

11 files changed

+2412
-0
lines changed

11 files changed

+2412
-0
lines changed

Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM alpine:edge AS mongodb-dumper
2+
3+
RUN apk update
4+
RUN apk upgrade
5+
RUN apk add --update go=1.16.4-r0 gcc g++ vips-dev
6+
7+
WORKDIR /bitclout/src
8+
9+
COPY mongodb-dumper/go.mod mongodb-dumper/
10+
COPY mongodb-dumper/go.sum mongodb-dumper/
11+
COPY core/go.mod core/
12+
COPY core/go.sum core/
13+
COPY core/third_party/ core/third_party/
14+
15+
WORKDIR /bitclout/src/mongodb-dumper
16+
17+
RUN go mod download
18+
19+
# include mongodb-dumper src
20+
COPY mongodb-dumper/cmd cmd
21+
COPY mongodb-dumper/mongodb mongodb
22+
COPY mongodb-dumper/main.go .
23+
24+
# include core src
25+
COPY core/clouthash ../core/clouthash
26+
COPY core/cmd ../core/cmd
27+
COPY core/lib ../core/lib
28+
29+
# build mongodb-dumper
30+
RUN GOOS=linux go build -mod=mod -a -installsuffix cgo -o bin/mongodb-dumper main.go
31+
32+
# create tiny image
33+
FROM alpine:edge
34+
35+
COPY --from=mongodb-dumper /bitclout/src/mongodb-dumper/bin/mongodb-dumper /bitclout/bin/mongodb-dumper

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 BitClout Developer Community
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# mongodb-dumper
2+
3+
`mongodb-dumper` runs a full BitClout node and dumps the chain data into a MongoDB database
4+
5+
## Build
6+
7+
Running the following commands will create a Docker image called `mongodb-dumper:latest`.
8+
9+
1. Checkout `mongodb-dumper` and `core` in the same directory
10+
11+
2. In the `mongodb-dumper` repo, run the following (you may need sudo):
12+
13+
```
14+
docker build -t mongodb-dumper -f Dockerfile ..
15+
```
16+
17+
### Run
18+
19+
You may need sudo:
20+
21+
```
22+
docker run -it mongodb-dumper /bitclout/bin/mongodb-dumper run
23+
```
24+
25+
Configure the connection to mongodb:
26+
27+
```
28+
--mongodb-collection string MongoDB collection name (default "data")
29+
--mongodb-database string MongoDB database name (default "bitclout")
30+
--mongodb-uri string MongoDB connection URI (default "mongodb://localhost:27017")
31+
```

cmd/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import "github.com/spf13/viper"
4+
5+
type Mode string
6+
type Network string
7+
8+
type Config struct {
9+
MongoURI string
10+
MongoDatabase string
11+
MongoCollection string
12+
}
13+
14+
func LoadConfig() *Config {
15+
config := Config{}
16+
17+
config.MongoURI = viper.GetString("monog-uri")
18+
config.MongoDatabase = viper.GetString("monog-database")
19+
config.MongoCollection = viper.GetString("monog-collection")
20+
21+
return &config
22+
}

cmd/node.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
coreCmd "github.com/bitclout/core/cmd"
5+
"github.com/bitclout/mongodb-dumper/mongodb"
6+
)
7+
8+
type Node struct {
9+
SyncingService *mongodb.SyncingService
10+
Config *Config
11+
12+
CoreNode *coreCmd.Node
13+
}
14+
15+
func NewNode(config *Config, coreNode *coreCmd.Node) *Node {
16+
result := Node{}
17+
result.Config = config
18+
result.CoreNode = coreNode
19+
20+
return &result
21+
}
22+
23+
func (node *Node) Start() {
24+
node.SyncingService = mongodb.NewSyncingService(
25+
node.CoreNode.Server.GetBlockchain().DB(),
26+
node.Config.MongoURI,
27+
node.Config.MongoDatabase,
28+
node.Config.MongoCollection)
29+
30+
go func() {
31+
node.SyncingService.ConnectToMongo()
32+
node.SyncingService.Start()
33+
}()
34+
}
35+
36+
func (node *Node) Stop() {
37+
node.SyncingService.DisconnectFromMongo()
38+
}

cmd/root.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
10+
homedir "github.com/mitchellh/go-homedir"
11+
"github.com/spf13/viper"
12+
)
13+
14+
var cfgFile string
15+
16+
// rootCmd represents the base command when called without any subcommands
17+
var rootCmd = &cobra.Command{
18+
Use: "mongodb-dumper",
19+
Short: "BitClout node with mongodb dumper",
20+
Long: `...`,
21+
}
22+
23+
func Execute() {
24+
cobra.CheckErr(rootCmd.Execute())
25+
}
26+
27+
func init() {
28+
cobra.OnInitialize(initConfig)
29+
30+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bitclout/mongodb-dumper.yaml)")
31+
}
32+
33+
func initConfig() {
34+
if cfgFile != "" {
35+
viper.SetConfigFile(cfgFile)
36+
} else {
37+
home, err := homedir.Expand("~/.bitclout")
38+
cobra.CheckErr(err)
39+
40+
viper.AddConfigPath(home)
41+
viper.SetConfigName("mongodb-dumper")
42+
}
43+
44+
// Environment variable support
45+
viper.AutomaticEnv()
46+
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
47+
48+
if err := viper.ReadInConfig(); err == nil {
49+
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
50+
}
51+
}

cmd/run.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/pflag"
6+
"github.com/spf13/viper"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
coreCmd "github.com/bitclout/core/cmd"
12+
"github.com/golang/glog"
13+
)
14+
15+
// runCmd represents the run command
16+
var runCmd = &cobra.Command{
17+
Use: "run",
18+
Short: "Run the MongoDB dumper",
19+
Long: `...`,
20+
Run: Run,
21+
}
22+
23+
func Run(cmd *cobra.Command, args []string) {
24+
// Start the core node
25+
coreConfig := coreCmd.LoadConfig()
26+
coreNode := coreCmd.NewNode(coreConfig)
27+
coreNode.Start()
28+
29+
// Start the mongo dumper
30+
mongoConfig := LoadConfig()
31+
mongoNode := NewNode(mongoConfig, coreNode)
32+
mongoNode.Start()
33+
34+
shutdownListener := make(chan os.Signal)
35+
signal.Notify(shutdownListener, syscall.SIGINT, syscall.SIGTERM)
36+
defer func() {
37+
coreNode.Stop()
38+
mongoNode.Stop()
39+
glog.Info("Shutdown complete")
40+
}()
41+
42+
<-shutdownListener
43+
}
44+
45+
func init() {
46+
// Add all the core node flags
47+
coreCmd.SetupRunFlags(runCmd)
48+
49+
// Add the mongo dumper flags
50+
runCmd.PersistentFlags().String("mongo-uri", "mongodb://localhost:27017", "Mongo connection URI")
51+
runCmd.PersistentFlags().String("mongo-database", "bitclout", "Mongo database name")
52+
runCmd.PersistentFlags().String("mongo-collection", "data", "Mongo collection name")
53+
54+
runCmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
55+
viper.BindPFlag(flag.Name, flag)
56+
})
57+
58+
rootCmd.AddCommand(runCmd)
59+
}

go.mod

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module github.com/bitclout/mongodb-dumper
2+
3+
go 1.16
4+
5+
replace github.com/bitclout/core => ../core/
6+
7+
replace github.com/golang/glog => ../core/third_party/github.com/golang/glog
8+
9+
replace github.com/laser/go-merkle-tree => ../core/third_party/github.com/laser/go-merkle-tree
10+
11+
replace github.com/sasha-s/go-deadlock => ../core/third_party/github.com/sasha-s/go-deadlock
12+
13+
require (
14+
cloud.google.com/go/storage v1.15.0
15+
github.com/DataDog/datadog-go v4.5.0+incompatible
16+
github.com/bitclout/core v0.0.0-00010101000000-000000000000
17+
github.com/btcsuite/btcd v0.21.0-beta
18+
github.com/btcsuite/btcutil v1.0.2
19+
github.com/davecgh/go-spew v1.1.1
20+
github.com/dgraph-io/badger/v3 v3.2011.1
21+
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
22+
github.com/fatih/structs v1.1.0
23+
github.com/golang/glog v0.0.0-20210429001901-424d2337a529
24+
github.com/gorilla/mux v1.8.0
25+
github.com/h2non/bimg v1.1.5
26+
github.com/kevinburke/twilio-go v0.0.0-20210327194925-1623146bcf73
27+
github.com/laser/go-merkle-tree v0.0.0-20180821204614-16c2f6ea4444
28+
github.com/mitchellh/go-homedir v1.1.0
29+
github.com/nyaruka/phonenumbers v1.0.69
30+
github.com/pkg/errors v0.9.1
31+
github.com/rollbar/rollbar-go v1.4.0
32+
github.com/sasha-s/go-deadlock v0.2.0
33+
github.com/spf13/cobra v1.1.3
34+
github.com/spf13/pflag v1.0.5
35+
github.com/spf13/viper v1.7.1
36+
github.com/stretchr/testify v1.7.0
37+
github.com/tyler-smith/go-bip39 v1.1.0
38+
go.mongodb.org/mongo-driver v1.4.5
39+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
40+
google.golang.org/api v0.46.0
41+
gopkg.in/DataDog/dd-trace-go.v1 v1.29.0
42+
)

0 commit comments

Comments
 (0)