Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAR2 committed Aug 19, 2020
1 parent 29d9df5 commit bbfa65d
Show file tree
Hide file tree
Showing 24 changed files with 1,421 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Inspired by https://container-solutions.com/faster-builds-in-docker-with-go-1-11/
# Base build image
FROM golang:1.14.6-alpine3.12 AS build_base
RUN apk add bash make git curl unzip rsync libc6-compat gcc musl-dev
WORKDIR /go/src/github.com/spacemeshos/explorer-backend

# Force the go compiler to use modules
ENV GO111MODULE=on
ENV GOPROXY=https://proxy.golang.org

# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .

# Download dependencies
RUN go mod download

RUN go get github.com/golang/[email protected]

# This image builds the explorer-backend
FROM build_base AS server_builder
# Here we copy the rest of the source code
COPY . .

# And compile the project
RUN make build

#In this last stage, we start from a fresh Alpine image, to reduce the image size and not ship the Go compiler in our production artifacts.
FROM alpine AS dash-backend

# Finally we copy the statically compiled Go binary.
COPY --from=server_builder /go/src/github.com/spacemeshos/explorer-backend/build/explorer-backend /bin/explorer-backend

ENTRYPOINT ["/bin/explorer-backend"]
EXPOSE 8080

# profiling port
EXPOSE 6060

# gRPC port
EXPOSE 9990
82 changes: 82 additions & 0 deletions cmd/collector/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"fmt"
"os"

"github.com/urfave/cli"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/dash-backend/collector"
"github.com/spacemeshos/explorer-backend/storage"
)

var (
version string
commit string
branch string
)

var (
nodeAddressStringFlag string
mongoDbUrlStringFlag string
mongoDbNameStringFlag string
)

var flags = []cli.Flag{
cli.StringFlag{
Name: "node",
Usage: "Spacemesh node API address string in format <host>:<port>",
Required: false,
Destination: &nodeAddressStringFlag,
Value: "localhost:9092",
},
cli.StringFlag{
Name: "mongodb",
Usage: "Explorer MongoDB Uri string in format mongodb://<host>:<port>",
Required: false,
Destination: &mongoDbUrlStringFlag,
Value: "mongodb://localhost:27017",
},
cli.StringFlag{
Name: "db",
Usage: "MongoDB Explorer database name string",
Required: false,
Destination: &mongoDbNameStringFlag,
Value: "explorer",
},
}

func main() {
app := cli.NewApp()
app.Name = "Spacemesh Explorer Collector"
app.Version = fmt.Sprintf("%s, commit '%s', branch '%s'", version, commit, branch)
app.Flags = flags
app.Writer = os.Stderr

app.Action = func(ctx *cli.Context) (error) {

log.InitSpacemeshLoggingSystem("", "spacemesh-explorer-collector.log")

mongoStorage := storage.New()

err := mongoStorage.Open(mongoDbUrlStringFlag, mongoDbNameStringFlag)
if err != nil {
log.Info("MongoDB storage open error %v", err)
return err
}

collector := collector.NewCollector(nodeAddressStringFlag, storage)

collector.Run()

log.Info("Collector is shutdown")
return nil
}

if err := app.Run(os.Args); err != nil {
log.Info("%+v", err)
os.Exit(1)
}

os.Exit(0)
}
2 changes: 2 additions & 0 deletions explorer-backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package explorer_backend

4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module explorer-backend

go 1.14

26 changes: 26 additions & 0 deletions model/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"
)

type Account struct {
Address string // account public address
Balance uint64 // known account balance
}

type AccountService interface {
GetAccount(ctx context.Context, query *bson.D) (*Account, error)
GetAccounts(ctx context.Context, query *bson.D) ([]*Account, error)
SaveAccount(ctx context.Context, in *Account) error
}

func NewAccount(in *pb.Account) *Account {
return &Account{
Address: utils.ToAddressString(account.GetAccountId().GetAddress()),
Balance: Amount(account.GetStateCurrent().GetBalance().GetValue()),
}
}

15 changes: 15 additions & 0 deletions model/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
)

type App struct {
Address string
}

type AppService interface {
GetAccount(ctx context.Context, query *bson.D) (*App, error)
GetApps(ctx context.Context, query *bson.D) ([]*App, error)
SaveApp(ctx context.Context, in *App) error
}
34 changes: 34 additions & 0 deletions model/atx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"
)

type Activation struct {
Id string
Layer uint64 // the layer that this activation is part of
SmesherId string // id of smesher who created the ATX
Coinbase string // coinbase account id
PrevAtx string // previous ATX pointed to
CommitmentSize uint64 // commitment size in bytes
}

type ActivationService interface {
GetActivation(ctx context.Context, query *bson.D) (*Activation, error)
GetActivations(ctx context.Context, query *bson.D) ([]*Activation, error)
SaveActivation(ctx context.Context, in *Activation) error
}

func NewActivation(atx *pb.Activation) *Activation {
return &Activation{
Id: utils.BytesToHex(atx.GetId().GetId()),
Layer: LayerID(atx.GetLayer().GetNumber()),
SmesherId: utils.BytesToHex(atx.GetSmesherId().GetId()),
Coinbase: utils.BytesToAddressString(atx.GetCoinbase().GetAddress()),
PrevAtx: utils.BytesToHex(atx.GetPrevAtx().GetId()),
CommitmentSize: atx.GetCommitmentSize(),
}
}

16 changes: 16 additions & 0 deletions model/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
)

type Block struct {
Id string
Layer uint64
}

type BlockService interface {
GetBlock(ctx context.Context, query *bson.D) (*Block, error)
GetBlocks(ctx context.Context, query *bson.D) ([]*Block, error)
SaveBlock(ctx context.Context, in *Block) error
}
32 changes: 32 additions & 0 deletions model/epoch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
)

type Statistics struct {
Capacity uint64 // Average tx/s rate over capacity considering all layers in the current epoch.
Decentral uint64 // Distribution of storage between all active smeshers.
Smeshers uint64 // Number of active smeshers in the current epoch.
Transactions uint64 // Total number of transactions processed by the state transition function.
Accounts uint64 // Total number of on-mesh accounts with a non-zero coin balance as of the current epoch.
Circulation uint64 // Total number of Smesh coins in circulation. This is the total balances of all on-mesh accounts.
Rewards uint64 // Total amount of Smesh minted as mining rewards as of the last known reward distribution event.
Security uint64 // Total amount of storage committed to the network based on the ATXs in the previous epoch.
}

type Stats struct {
Current Statistics
Cumulative Statistics
}

type Epoch struct {
Number uint64
Stats Stats
}

type EpochService interface {
GetEpoch(ctx context.Context, query *bson.D) (*Epoch, error)
GetEpochs(ctx context.Context, query *bson.D) ([]*Epoch, error)
SaveEpoch(ctx context.Context, in *Epoch) error
}
56 changes: 56 additions & 0 deletions model/layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"
)

type Layer struct {
Number uint64
Status int
}

type LayerService interface {
GetLayer(ctx context.Context, query *bson.D) (*Layer, error)
GetLayers(ctx context.Context, query *bson.D) ([]*Layer, error)
SaveLayer(ctx context.Context, in *Layer) error
}

func NewLayer(l *pb.Layer) (*Layer, []*Block, []*Activation, make(map[string]*Transaction) {
pbBlocks := l.GetBlocks()
pbAtxs := l.GetActivations()
layer := &Layer{
Number: LayerID(l.GetNumber().GetNumber()),
Status: l.GetStatus(),
}

blocks := make([]*Block, len(blocks)),
atxs := make([]*Activation, len(atxs)),
txs := make(map[string]Transaction),

for i, b := range pbBlocks {
blocks[i] := &Block{
Id: utils.ToHex(b.GetId()),
Layer: layer.Number,
}
for _, t := range b.GetTransactions() {
tx := NewTransaction(t, layer.Number, blocks[i].Id)
txs[tx.Id] = tx
}
}

for i, a := range pbAtxs {
atxs[i] = NewActivation(a)
}

return layer, blocks, atxs, txs
}

func IsApprovedLayer(l *pb.Layer) bool {
return l.GetStatus() >= pb.Layer_LAYER_STATUS_APPROVED
}

func IsConfirmedLayer(l *pb.Layer) bool {
return l.GetStatus() == pb.Layer_LAYER_STATUS_CONFIRMED
}
36 changes: 36 additions & 0 deletions model/reward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"
)

type Reward struct {
Layer uint64
Total uint64
LayerReward uint64
LayerComputed uint64 // layer number of the layer when reward was computed
// tx_fee = total - layer_reward
Coinbase string // account awarded this reward
Smesher string // it will be nice to always have this in reward events
}

type RewardService interface {
GetReward(ctx context.Context, query *bson.D) (*Reward, error)
GetRewards(ctx context.Context, query *bson.D) ([]*Reward, error)
SaveReward(ctx context.Context, in *Reward) error
}

func NewReward(reward *pb.Reward) *Reward {
var smesherId SmesherID
copy(smesherId[:], reward.GetSmesher().GetId())
return &Reward{
Layer: LayerID(reward.GetLayer().GetNumber()),
Total: Amount(reward.GetTotal().GetValue()),
Layer_reward: Amount(reward.GetLayerReward().GetValue()),
Layer_computed: LayerID(reward.GetLayerComputed().GetNumber()),
Coinbase: BytesToAddress(reward.GetCoinbase().GetAddress()),
Smesher: smesherId,
}
}
24 changes: 24 additions & 0 deletions model/smesher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package model

import (
"go.mongodb.org/mongo-driver/bson"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"
)

type Geo struct {
Name string `json:"name"`
Coordinates [2]float64 `json:"coordinates"`
}

type Smesher struct {
Id string
Geo Geo
CommitmentSize uint64 // commitment size in bytes
}

type SmesherService interface {
GetSmesher(ctx context.Context, query *bson.D) (*Smesher, error)
GetSmeshers(ctx context.Context, query *bson.D) ([]*Smesher, error)
SaveSmesher(ctx context.Context, in *Smesher) error
}
Loading

0 comments on commit bbfa65d

Please sign in to comment.