Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Chain type mapper for evm chains #70

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4c1396c
mapper
akriventsev Mar 9, 2021
9f716b0
fix
akriventsev Mar 10, 2021
1d5f08d
fix mapper
akriventsev Mar 12, 2021
910e891
calculate interval to 100
akriventsev Mar 12, 2021
a350b43
fix init
akriventsev Mar 12, 2021
92604c4
fix waves helper
akriventsev Mar 14, 2021
38b7152
rollback CalculateScoreInterval to 100
akriventsev Mar 14, 2021
b1eabf3
node chaintype
akriventsev Mar 14, 2021
8e19216
log
akriventsev Mar 14, 2021
b6f62af
log
akriventsev Mar 14, 2021
b0db198
log
akriventsev Mar 14, 2021
99f0ee3
chainType fix
akriventsev Mar 14, 2021
fdc52dc
fix nebula id translate
akriventsev Mar 14, 2021
135de50
fix
akriventsev Mar 14, 2021
0330c1c
add chain name to oracle
akriventsev Mar 15, 2021
b0c437d
add log
akriventsev Mar 15, 2021
afe9dc9
add log
akriventsev Mar 15, 2021
63cdc67
log
akriventsev Mar 15, 2021
6b5b3eb
fix chain type
akriventsev Mar 15, 2021
9ff6688
add error handling
akriventsev Mar 15, 2021
c59a7d8
log
akriventsev Mar 15, 2021
46b9636
log
akriventsev Mar 15, 2021
0fc0715
log
akriventsev Mar 15, 2021
176e92f
fix chain
akriventsev Mar 15, 2021
7ba225a
fix chaint type
akriventsev Mar 15, 2021
cd6dae6
fix chainType
akriventsev Mar 15, 2021
c576b92
log
akriventsev Mar 15, 2021
8560f2c
log
akriventsev Mar 15, 2021
4cfcfc4
log
akriventsev Mar 15, 2021
85ceba3
nebula log
akriventsev Mar 15, 2021
7663dbb
fix nebula address
akriventsev Mar 15, 2021
d6114de
fix imports
akriventsev Mar 15, 2021
0e5ac18
fix toBytes
akriventsev Mar 15, 2021
382c51e
log
akriventsev Mar 15, 2021
bc25b3c
fix nebula
akriventsev Mar 15, 2021
29eb709
sync logger
akriventsev Mar 15, 2021
fde090c
log
akriventsev Mar 15, 2021
44ad79f
log
akriventsev Mar 15, 2021
af1754f
fix nebula id
akriventsev Mar 15, 2021
b2fda2e
log
akriventsev Mar 15, 2021
62b5bf6
log
akriventsev Mar 15, 2021
20da42f
log
akriventsev Mar 15, 2021
e5cd478
log
akriventsev Mar 15, 2021
f90b350
log
akriventsev Mar 15, 2021
2fde2cd
fix adaptors
akriventsev Mar 15, 2021
55b8677
log
akriventsev Mar 15, 2021
cbed7d5
log
akriventsev Mar 15, 2021
6db502a
log
akriventsev Mar 15, 2021
d80eb6d
log
akriventsev Mar 15, 2021
17c0a9f
log
akriventsev Mar 15, 2021
f1432b7
log
akriventsev Mar 15, 2021
ab03727
log
akriventsev Mar 15, 2021
0529808
log
akriventsev Mar 16, 2021
5f94f6f
log
akriventsev Mar 16, 2021
1500c33
log
akriventsev Mar 16, 2021
2c22cb1
log
akriventsev Mar 16, 2021
5a07f19
chainType fix
akriventsev Mar 16, 2021
927a411
fix
akriventsev Mar 16, 2021
efa5358
log
akriventsev Mar 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ ledger/.data/*
oracle/config.*.json
# Deploy vendor
nebula-address.txt
migration.txt
migration.txt
cmd/gravity/gravity
.vscode/launch.json
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/gravity",
"args": [
"ledger",
"--home=/home/anaximen/ledger",
"start",
"--bootstrap="
]
},
]
}
58 changes: 30 additions & 28 deletions cmd/gravity/commands/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const (
NetworkFlag = "network"
BootstrapUrlFlag = "bootstrap"

Custom Network = "custom"
DevNet Network = "devnet"
Custom Network = "custom"
DevNet Network = "devnet"
Mainnet Network = "mainnet"

DevNetId ChainId = "gravity-devnet"
Expand All @@ -76,14 +76,16 @@ var (
Mempool: cfg.DefaultMempoolConfig(),
Details: (&config.ValidatorDetails{}).DefaultNew(),
Adapters: map[string]config.AdaptorsConfig{
account.Ethereum.String(): {
"ethereum": {
NodeUrl: "https://ropsten.infura.io/v3/598efca7168947c6a186e2f85b600be1",
GravityContractAddress: "0x80C52beF8622cDF368Bf8AaD5ee4A78cB68E2a79",
ChainType: "ethereum",
},
account.Waves.String(): {
"waves": {
NodeUrl: "https://nodes-stagenet.wavesnodes.com",
GravityContractAddress: "3MfrQBknYJSnifUxD86yMPTSHEhgcPe3NBq",
ChainId: "S",
ChainType: "waves",
},
},
}
Expand Down Expand Up @@ -186,6 +188,7 @@ var (
},
}
)

func getPublicIP() (string, error) {
ifaces, _ := net.Interfaces()

Expand All @@ -211,7 +214,6 @@ func getPublicIP() (string, error) {
return "", fmt.Errorf("not found valid ip")
}


func initLedgerConfig(ctx *cli.Context) error {
var err error

Expand Down Expand Up @@ -333,7 +335,7 @@ func startLedger(ctx *cli.Context) error {
if err != nil {
return err
}

account.ChainMapper.Assign(privKeysCfg.ChainIds)
var genesis config.Genesis
err = config.ParseConfig(path.Join(home, GenesisFileName), &genesis)
if err != nil {
Expand Down Expand Up @@ -384,7 +386,7 @@ func startLedger(ctx *cli.Context) error {
PubKey: ledgerPubKey,
}

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

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) {
func createApp(db *badger.DB, ledgerValidator *account.LedgerValidator, privKeys config.Keys, 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 {
cid, err := account.ChainMapper.ToByte(k)
if err != nil {
fmt.Printf("Chaintype '%s' error: %s", k, err)
continue
}
account.ChainMapper.ApendAdaptor(cid, v.ChainType)

chainType, err := account.ParseChainType(k)
if err != nil {
return nil, err
}

privKey, err := account.StringToPrivKey(privKeys[k].PrivKey, chainType)
privKey, err := account.StringToPrivKey(privKeys.TargetChains[k].PrivKey, chainType)
if err != nil {
return nil, err
}

var adaptor adaptors.IBlockchainAdaptor

switch chainType {
case account.Binance:
adaptor, err = adaptors.NewBinanceAdaptor(privKey, v.NodeUrl, ctx, adaptors.WithBinanceGravityContract(v.GravityContractAddress))
if err != nil {
return nil, err
}
case account.Ethereum:
adaptor, err = adaptors.NewEthereumAdaptor(privKey, v.NodeUrl, ctx, adaptors.WithEthereumGravityContract(v.GravityContractAddress))
if err != nil {
return nil, err
}
case account.Waves:
adaptor, err = adaptors.NewWavesAdapter(privKey, v.NodeUrl, v.ChainId[0], adaptors.WithWavesGravityContract(v.GravityContractAddress))
if err != nil {
return nil, err
}
chain := byte(0)
if len(v.ChainId) > 0 {
chain = byte(v.ChainId[0])
}
opts := adaptors.AdapterOptions{
"gravityContract": v.GravityContractAddress,
"chainID": chain,
}
adaptor, err := adaptors.NewFactory().CreateAdaptor(v.ChainType, privKey, v.NodeUrl, ctx, opts)
if err != nil {
return nil, err
}

bAdaptors[chainType] = adaptor
Expand Down
29 changes: 25 additions & 4 deletions cmd/gravity/commands/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func startOracle(ctx *cli.Context) error {

home := ctx.String(HomeFlag)
nebulaIdStr := ctx.Args().First()
zap.L().Sugar().Debugf("Nebule id from args is: [%s]", nebulaIdStr)
var cfg config.OracleConfig
err = config.ParseConfig(path.Join(home, DefaultNebulaeDir, fmt.Sprintf("%s.json", nebulaIdStr)), &cfg)
if err != nil {
Expand All @@ -121,31 +122,49 @@ func startOracle(ctx *cli.Context) error {
if err != nil {
return err
}

zap.L().Sugar().Debugf("Chain Ids: %s", privKeysCfg.ChainIds)
account.ChainMapper.Assign(privKeysCfg.ChainIds)
account.ChainMapper.ApendAdaptor(byte(0), "ethereum") // for backward compatibility
validatorPrivKey, err := hexutil.Decode(privKeysCfg.Validator.PrivKey)
if err != nil {
return err
}

chainType, err := account.ParseChainType(cfg.ChainType)
chain, err := account.ChainMapper.ToByte(cfg.ChainName)
if err != nil {
return err
}
chainType := account.ChainType(chain)
account.ChainMapper.ApendAdaptor(byte(chainType), cfg.ChainType)

nebulaId, err := account.StringToNebulaId(nebulaIdStr, chainType)
zap.L().Sugar().Debugf("parsing chain type: %s", cfg.ChainType)
cType, err := account.ParseChainType(cfg.ChainType)
if err != nil {
return err
}

oracleSecretKey, err := account.StringToPrivKey(privKeysCfg.TargetChains[chainType.String()].PrivKey, chainType)
zap.L().Sugar().Debugf("Chain type is: %d for nebulae id: %s", chainType, nebulaIdStr)
nebulaId, err := account.StringToNebulaId(nebulaIdStr, cType)
if err != nil {
return err
}

zap.L().Sugar().Debugf("PrivKey is: %s", privKeysCfg.TargetChains[cfg.ChainName].PrivKey)
oracleSecretKey, err := account.StringToPrivKey(privKeysCfg.TargetChains[cfg.ChainName].PrivKey, chainType)
if err != nil {
zap.L().Error(err.Error())
return err
}
zap.L().Sugar().Debug("Oracle secret key is ", oracleSecretKey)
var chainId byte
if len(cfg.ChainId) > 0 {
chainId = cfg.ChainId[0]
}
// chain, err := account.ChainMapper.ToByte(cfg.ChainName)
// if err != nil {
// zap.L().Error(err.Error())
// return err
// }
sysCtx := context.Background()
oracleNode, err := node.New(
nebulaId,
Expand All @@ -160,11 +179,13 @@ func startOracle(ctx *cli.Context) error {
sysCtx)

if err != nil {
zap.L().Error(err.Error())
return err
}

err = oracleNode.Init()
if err != nil {
zap.L().Error(err.Error())
return err
}

Expand Down
Binary file modified cmd/gravity/gravity
Binary file not shown.
45 changes: 24 additions & 21 deletions common/account/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package account

import (
"errors"
"strings"
)

type ChainType byte
Expand All @@ -19,26 +18,30 @@ var (
)

func ParseChainType(chainType string) (ChainType, error) {
switch strings.ToLower(chainType) {
case "bsc":
return Binance, nil
case "ethereum":
return Ethereum, nil
case "waves":
return Waves, nil
default:
return 0, ErrParseChainType
}
val, err := ChainMapper.ToByte(chainType)
return ChainType(val), err
// switch strings.ToLower(chainType) {
// case "bsc":
// return Binance, nil
// case "ethereum":
// return Ethereum, nil
// case "waves":
// return Waves, nil
// default:
// return 0, ErrParseChainType
// }
}
func (ch ChainType) String() string {
switch ch {
case Ethereum:
return "ethereum"
case Waves:
return "waves"
case Binance:
return "bsc"
default:
return "ethereum"
}
val, _ := ChainMapper.ToStr(byte(ch))
return val
// switch ch {
// case Ethereum:
// return "ethereum"
// case Waves:
// return "waves"
// case Binance:
// return "bsc"
// default:
// return "ethereum"
// }
}
53 changes: 53 additions & 0 deletions common/account/mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package account

import (
"fmt"
)

type Mapper struct {
strToByte map[string]byte
byteToStr map[byte]string
types map[byte]string
}

var ChainMapper Mapper

func (m *Mapper) Assign(arg map[string]ChainType) {
m.strToByte = make(map[string]byte)
m.byteToStr = make(map[byte]string)
m.types = make(map[byte]string)
for k, v := range arg {
m.byteToStr[byte(v)] = k
m.strToByte[k] = byte(v)
}
}
func (m *Mapper) ApendAdaptor(id byte, chaintype string) {
m.types[id] = chaintype
}
func (m *Mapper) ToType(id byte) (byte, error) {
v, ok := m.types[id]
if ok {
v2, ok2 := m.strToByte[v]
if ok2 {
return v2, nil
}
return 0, fmt.Errorf("Chain not found")

}
return 0, fmt.Errorf("Chain not found")
}
func (m *Mapper) ToStr(k byte) (string, error) {
v, ok := m.byteToStr[k]
if ok {
return v, nil
}
return "", fmt.Errorf("Chain not found")
}

func (m *Mapper) ToByte(k string) (byte, error) {
v, ok := m.strToByte[k]
if ok {
return v, nil
}
return 0, fmt.Errorf("Chain not found")
}
25 changes: 23 additions & 2 deletions common/account/nebula.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/btcsuite/btcutil/base58"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/wavesplatform/gowaves/pkg/crypto"
"go.uber.org/zap"
)

const (
Expand All @@ -16,6 +17,11 @@ const (
type NebulaId [NebulaIdLength]byte

func StringToNebulaId(address string, chainType ChainType) (NebulaId, error) {
// cid, err := ChainMapper.ToType(byte(chainType))
// if err != nil {
// return NebulaId{}, err
// }
// ctype := ChainType(cid)
var nebula NebulaId

switch chainType {
Expand Down Expand Up @@ -45,8 +51,16 @@ func BytesToNebulaId(value []byte) NebulaId {
}

func (id NebulaId) ToString(chainType ChainType) string {
nebula := id.ToBytes(chainType)
switch chainType {
zap.L().Sugar().Debugf("chainType: %d", chainType)
cid, err := ChainMapper.ToType(byte(chainType))
if err != nil {
zap.L().Error(err.Error())
return ""
}
ctype := ChainType(cid)

nebula := id.ToBytes(ctype)
switch ctype {
case Ethereum, Binance:
return hexutil.Encode(nebula[:])
case Waves:
Expand All @@ -56,6 +70,13 @@ func (id NebulaId) ToString(chainType ChainType) string {
return ""
}
func (id NebulaId) ToBytes(chainType ChainType) []byte {
// cid, err := ChainMapper.ToType(byte(chainType))
// if err != nil {
// zap.L().Error(err.Error())
// return nil
// }
// ctype := ChainType(cid)

switch chainType {
case Binance:
return id[NebulaIdLength-BSCAddressLength:]
Expand Down
Loading