Skip to content

Commit

Permalink
Merge pull request #30 from Gravity-Tech/add-validators-query
Browse files Browse the repository at this point in the history
Add 'All Validators' query path
  • Loading branch information
sham789 authored Oct 12, 2020
2 parents 3db4423 + 483c4c1 commit 5fc46a6
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 9 deletions.
Empty file modified cmd/gravity/commands/common.go
100644 → 100755
Empty file.
37 changes: 34 additions & 3 deletions cmd/gravity/commands/ledger.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -71,6 +73,7 @@ var (
RPC: cfg.DefaultRPCConfig(),
IsFastSync: true,
Mempool: cfg.DefaultMempoolConfig(),
Details: (&config.ValidatorDetails{}).DefaultNew(),
Adapters: map[string]config.AdaptorsConfig{
account.Ethereum.String(): {
NodeUrl: "https://ropsten.infura.io/v3/598efca7168947c6a186e2f85b600be1",
Expand Down Expand Up @@ -182,6 +185,31 @@ var (
},
}
)
func getPublicIP() (string, error) {
ifaces, _ := net.Interfaces()

for _, i := range ifaces {
addrs, _ := i.Addrs()
for _, addr := range addrs {

var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}

if strings.Contains(fmt.Sprintf("%v", addr), "/24") {
return fmt.Sprintf("%v", ip), nil
}

}
}

return "", fmt.Errorf("not found valid ip")
}


func initLedgerConfig(ctx *cli.Context) error {
var err error
Expand Down Expand Up @@ -246,6 +274,9 @@ func initLedgerConfig(ctx *cli.Context) error {
} else {
ledgerConf = config.DefaultLedgerConfig()
}

ledgerConf.PublicIP, _ = getPublicIP()

b, err = json.MarshalIndent(&ledgerConf, "", " ")
err = ioutil.WriteFile(path.Join(home, LedgerConfigFileName), b, 0644)
if err != nil {
Expand Down Expand Up @@ -345,7 +376,7 @@ func startLedger(ctx *cli.Context) error {
PubKey: ledgerPubKey,
}

gravityApp, err := crateApp(db, ledgerValidator, privKeysCfg.TargetChains, ledgerConf, genesis, bootstrap, tConfig.RPC.ListenAddress, sysCtx)
gravityApp, err := createApp(db, ledgerValidator, privKeysCfg.TargetChains, ledgerConf, genesis, bootstrap, tConfig.RPC.ListenAddress, sysCtx)
if err != nil {
return fmt.Errorf("failed to parse gravity config: %w", err)
}
Expand Down Expand Up @@ -424,7 +455,7 @@ func startLedger(ctx *cli.Context) error {
return nil
}

func crateApp(db *badger.DB, ledgerValidator *account.LedgerValidator, privKeys map[string]config.Key, cfg config.LedgerConfig, genesisCfg config.Genesis, bootstrap string, localHost string, ctx context.Context) (*app.GHApplication, error) {
func createApp(db *badger.DB, ledgerValidator *account.LedgerValidator, privKeys map[string]config.Key, cfg config.LedgerConfig, genesisCfg config.Genesis, bootstrap string, localHost string, ctx context.Context) (*app.GHApplication, error) {
bAdaptors := make(map[account.ChainType]adaptors.IBlockchainAdaptor)
for k, v := range cfg.Adapters {
chainType, err := account.ParseChainType(k)
Expand Down Expand Up @@ -493,7 +524,7 @@ func crateApp(db *badger.DB, ledgerValidator *account.LedgerValidator, privKeys
}
}

application, err := app.NewGHApplication(bAdaptors, blockScheduler, db, &genesis, ctx)
application, err := app.NewGHApplication(bAdaptors, blockScheduler, db, &genesis, ctx, &cfg)
if err != nil {
return nil, err
}
Expand Down
Empty file modified cmd/gravity/commands/oracle.go
100644 → 100755
Empty file.
Binary file modified cmd/gravity/gravity
Binary file not shown.
Empty file modified cmd/gravity/main.go
100644 → 100755
Empty file.
28 changes: 28 additions & 0 deletions config/ledger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"encoding/json"
"github.com/Gravity-Tech/gravity-core/common/account"
cfg "github.com/tendermint/tendermint/config"
)
Expand All @@ -15,13 +16,39 @@ type AdaptorsConfig struct {
GravityContractAddress string
}

type ValidatorDetails struct {
Name, Description, JoinedAt string
// Misc
AvatarURL, Website string
}

func (validatorDetails *ValidatorDetails) Bytes() ([]byte, error) {
res, err := json.Marshal(validatorDetails)

if err != nil {
return make([]byte, 0), err
}

return res, nil
}

func (validatorDetails *ValidatorDetails) DefaultNew() *ValidatorDetails {
return &ValidatorDetails{
Name: "Gravity Node", Description: "", JoinedAt: "",
AvatarURL: "", Website: "",
}
}

type LedgerConfig struct {
Moniker string
IsFastSync bool
Mempool *cfg.MempoolConfig
RPC *cfg.RPCConfig
P2P *cfg.P2PConfig

Details *ValidatorDetails
PublicIP string

Adapters map[string]AdaptorsConfig
}

Expand All @@ -32,6 +59,7 @@ func DefaultLedgerConfig() LedgerConfig {
Mempool: cfg.DefaultMempoolConfig(),
RPC: cfg.DefaultRPCConfig(),
P2P: cfg.DefaultP2PConfig(),
Details: (&ValidatorDetails{}).DefaultNew(),
Adapters: map[string]AdaptorsConfig{
account.Ethereum.String(): {
NodeUrl: "",
Expand Down
11 changes: 8 additions & 3 deletions ledger/app/app.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/Gravity-Tech/gravity-core/config"
"github.com/tendermint/tendermint/version"
"sort"

Expand Down Expand Up @@ -48,18 +49,20 @@ type GHApplication struct {
scheduler *scheduler.Scheduler
ctx context.Context
genesis *Genesis
ledgerConfig *config.LedgerConfig
}

var _ abcitypes.Application = (*GHApplication)(nil)

func NewGHApplication(adaptors map[account.ChainType]adaptors.IBlockchainAdaptor, scheduler *scheduler.Scheduler, db *badger.DB, genesis *Genesis, ctx context.Context) (*GHApplication, error) {
func NewGHApplication(adaptors map[account.ChainType]adaptors.IBlockchainAdaptor, scheduler *scheduler.Scheduler, db *badger.DB, genesis *Genesis, ctx context.Context, config *config.LedgerConfig) (*GHApplication, error) {
return &GHApplication{
db: db,
adaptors: adaptors,
scheduler: scheduler,
ctx: ctx,
genesis: genesis,
storage: storage.New(),
ledgerConfig: config,
}, nil
}

Expand Down Expand Up @@ -120,7 +123,9 @@ func (app *GHApplication) Query(reqQuery abcitypes.RequestQuery) (resQuery abcit

store := storage.New()
store.NewTransaction(app.db)
b, err := query.Query(store, reqQuery.Path, reqQuery.Data)

b, err := query.Query(store, reqQuery.Path, reqQuery.Data, app.ledgerConfig.Details)

if err == query.ErrValueNotFound {
resQuery.Code = NotFoundCode
} else if err != nil {
Expand Down Expand Up @@ -246,4 +251,4 @@ func (app *GHApplication) EndBlock(req abcitypes.RequestEndBlock) abcitypes.Resp
} else {
return abcitypes.ResponseEndBlock{}
}
}
}
23 changes: 22 additions & 1 deletion ledger/query/oracles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package query

import (
"encoding/json"

"github.com/Gravity-Tech/gravity-core/common/account"
"github.com/Gravity-Tech/gravity-core/common/storage"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type ByValidatorRq struct {
Expand All @@ -22,6 +22,27 @@ type ResultsRq struct {
NebulaAddress string
}

func allValidators(store *storage.Storage, _ []byte) ([]byte, error) {
scores, err := store.Scores()
result := make([]string, len(scores))

if err != nil {
return make([]byte, 0), err
}

for consulPubKey, _ := range scores {
result = append(result, hexutil.Encode(consulPubKey[:]))
}

encoded, err := json.Marshal(result)

if err != nil {
return make([]byte, 0), err
}

return encoded, nil
}

func oraclesByValidator(store *storage.Storage, value []byte) (storage.OraclesByTypeMap, error) {
var rq ByValidatorRq
err := json.Unmarshal(value, &rq)
Expand Down
10 changes: 8 additions & 2 deletions ledger/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"
"encoding/json"
"errors"

"github.com/Gravity-Tech/gravity-core/config"
"github.com/Gravity-Tech/gravity-core/common/storage"
)

Expand All @@ -28,14 +28,16 @@ const (
SignNewConsulsByConsulPath Path = "signNewConsulsByConsul"
SignNewOraclesByConsulPath Path = "signNewOraclesByConsul"
NebulaOraclesIndexPath Path = "nebulaOraclesIndex"
AllValidatorsPath Path = "allValidators"
ValidatorDetailsPath Path = "validatorDetails"
)

var (
ErrInvalidPath = errors.New("invalid path")
ErrValueNotFound = errors.New("value not found")
)

func Query(store *storage.Storage, path string, rq []byte) ([]byte, error) {
func Query(store *storage.Storage, path string, rq []byte, validatorDetails *config.ValidatorDetails) ([]byte, error) {
var value interface{}
var err error
switch Path(path) {
Expand Down Expand Up @@ -73,6 +75,10 @@ func Query(store *storage.Storage, path string, rq []byte) ([]byte, error) {
value, err = nebulaOraclesIndex(store, rq)
case LastRoundApprovedPath:
value, err = store.LastRoundApproved()
case AllValidatorsPath:
value, err = allValidators(store, rq)
case ValidatorDetailsPath:
value, err = validatorDetails.Bytes()
default:
return nil, ErrInvalidPath
}
Expand Down

0 comments on commit 5fc46a6

Please sign in to comment.