Skip to content

Commit

Permalink
Merge pull request #429 from vegaprotocol/feat/secondary-bridge
Browse files Browse the repository at this point in the history
Prepare configuration for secondary bridge
  • Loading branch information
ValentinTrinque committed Mar 11, 2024
2 parents 5284a89 + 300bf98 commit a9809ab
Show file tree
Hide file tree
Showing 45 changed files with 1,041 additions and 255 deletions.
56 changes: 46 additions & 10 deletions cmd/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,67 @@ var ethereumMultisigSetupCmd = &cobra.Command{
return networkNotRunningErr("ethereum multisig init")
}

smartcontracts, err := netState.Config.SmartContractsInfo()
ctx := context.Background()

validatorsKeyPairs := getSigners(netState.GeneratedServices.ListValidators())

// Primary bridge

primarySmartContracts, err := netState.Config.PrimarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting smart contract informations: %w", err)
return fmt.Errorf("failed getting primary smart contract informations: %w", err)
}

chainID, err := strconv.Atoi(netState.Config.Network.Ethereum.ChainID)
primaryChainID, err := strconv.Atoi(netState.Config.Network.Ethereum.ChainID)
if err != nil {
return err
}

ctx := context.Background()
client, err := ethereum.NewEthereumMultisigClient(ctx, ethereum.EthereumMultisigClientParameters{
primaryClient, err := ethereum.NewEthereumMultisigClient(ctx, ethereum.EthereumMultisigClientParameters{
VegaBinary: *netState.Config.VegaBinary,
VegaHome: utils.VegaNodeHomePath(homePath, 0),

ChainID: chainID,
ChainID: primaryChainID,
EthereumAddress: netState.Config.Network.Ethereum.Endpoint,
SmartcontractsInfo: *smartcontracts,
SmartContractsInfo: *primarySmartContracts,
})
if err != nil {
return fmt.Errorf("failed to create ethereum client: %w", err)
return fmt.Errorf("failed to create primary ethereum client: %w", err)
}

validatorsKeyPairs := getSigners(netState.GeneratedServices.ListValidators())
return client.InitMultisig(ctx, *smartcontracts, validatorsKeyPairs)
if err := primaryClient.InitMultisig(ctx, *primarySmartContracts, validatorsKeyPairs); err != nil {
return fmt.Errorf("failed to init primary multisig smart contract: %w", err)
}

// Secondary bridge

secondarySmartContracts, err := netState.Config.SecondarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting primary smart contract informations: %w", err)
}

secondaryChainID, err := strconv.Atoi(netState.Config.Network.SecondaryEthereum.ChainID)
if err != nil {
return err
}

secondaryClient, err := ethereum.NewEthereumMultisigClient(ctx, ethereum.EthereumMultisigClientParameters{
VegaBinary: *netState.Config.VegaBinary,
VegaHome: utils.VegaNodeHomePath(homePath, 0),

ChainID: secondaryChainID,
EthereumAddress: netState.Config.Network.SecondaryEthereum.Endpoint,
SmartContractsInfo: *secondarySmartContracts,
})
if err != nil {
return fmt.Errorf("failed to create secondary ethereum client: %w", err)
}

if err := secondaryClient.InitMultisig(ctx, *secondarySmartContracts, validatorsKeyPairs); err != nil {
return fmt.Errorf("failed to init secondary multisig smart contract: %w", err)
}

return nil
},
}

Expand Down
29 changes: 23 additions & 6 deletions cmd/ethereum_asset_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

vgethereum "code.vegaprotocol.io/vegacapsule/libs/ethereum"
"code.vegaprotocol.io/vegacapsule/state"
"code.vegaprotocol.io/vegacapsule/types"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
Expand All @@ -25,12 +26,14 @@ var ethereumAssetDepositFlags = struct {
vegaPubKey string
assetSymbol string
amount int64
bridge string
}{}

func init() {
ethereumAssetDepositCmd.Flags().StringVar(&ethereumAssetDepositFlags.assetSymbol, "asset-symbol", "", "symbol of the asset to be deposited")
ethereumAssetDepositCmd.Flags().StringVar(&ethereumAssetDepositFlags.vegaPubKey, "pub-key", "", "Vega public key to where the asset will be deposited")
ethereumAssetDepositCmd.Flags().Int64Var(&ethereumAssetDepositFlags.amount, "amount", 0, "amount to be deposited")
ethereumAssetDepositCmd.Flags().StringVar(&ethereumAssetDepositFlags.bridge, "bridge", "primary", "bridge linked to the deposit")
ethereumAssetDepositCmd.MarkFlagRequired("asset-symbol")
ethereumAssetDepositCmd.MarkFlagRequired("pub-key")
ethereumAssetDepositCmd.MarkFlagRequired("amount")
Expand All @@ -55,23 +58,37 @@ var ethereumAssetDepositCmd = &cobra.Command{

conf := netState.Config

smartContracts, err := conf.SmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting smart contract informations: %w", err)
}

asset := conf.GetSmartContractToken(ethereumAssetDepositFlags.assetSymbol)
if asset == nil {
return fmt.Errorf("failed to get non existing asset: %q", ethereumAssetDepositFlags.assetSymbol)
}

var (
networkAddress string
smartContracts *types.SmartContractsInfo
)
switch ethereumAssetDepositFlags.bridge {
case "primary":
networkAddress = conf.Network.Ethereum.Endpoint
smartContracts, err = conf.PrimarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting primary smart contract informations: %w", err)
}
case "secondary":
networkAddress = conf.Network.SecondaryEthereum.Endpoint
smartContracts, err = conf.SecondarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting secondary smart contract informations: %w", err)
}
}

depositArgs := ethereumAssetDepositOrStakeArgs{
amount: ethereumAssetDepositFlags.amount,
vegaPubKey: ethereumAssetDepositFlags.vegaPubKey,
ownerPrivateKey: smartContracts.EthereumOwner.Private,
bridgeAddress: smartContracts.ERC20Bridge.EthereumAddress,
assetAddress: asset.EthereumAddress,
networkAddress: conf.Network.Ethereum.Endpoint,
networkAddress: networkAddress,
}

return ethereumAssetDeposit(cmd.Context(), depositArgs)
Expand Down
29 changes: 23 additions & 6 deletions cmd/ethereum_asset_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

vgethereum "code.vegaprotocol.io/vegacapsule/libs/ethereum"
"code.vegaprotocol.io/vegacapsule/state"
"code.vegaprotocol.io/vegacapsule/types"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
Expand All @@ -24,12 +25,14 @@ var ethereumAssetMintFlags = struct {
toAddress string
assetSymbol string
amount int64
bridge string
}{}

func init() {
ethereumAssetMintCmd.Flags().StringVar(&ethereumAssetMintFlags.assetSymbol, "asset-symbol", "", "symbol of the asset to be minted")
ethereumAssetMintCmd.Flags().StringVar(&ethereumAssetMintFlags.toAddress, "to-addr", "", "address of where the token will be minted to")
ethereumAssetMintCmd.Flags().Int64Var(&ethereumAssetMintFlags.amount, "amount", 0, "amount to be minted")
ethereumAssetMintCmd.Flags().StringVar(&ethereumAssetMintFlags.bridge, "bridge", "primary", "bridge linked to the deposit")
ethereumAssetMintCmd.MarkFlagRequired("asset-symbol")
ethereumAssetMintCmd.MarkFlagRequired("to-address")
ethereumAssetMintCmd.MarkFlagRequired("amount")
Expand All @@ -54,22 +57,36 @@ var ethereumAssetMintCmd = &cobra.Command{

conf := netState.Config

smartContracts, err := conf.SmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting smart contract informations: %w", err)
}

asset := conf.GetSmartContractToken(ethereumAssetMintFlags.assetSymbol)
if asset == nil {
return fmt.Errorf("failed to get non existing asset: %q", ethereumAssetMintFlags.assetSymbol)
}

var (
networkAddress string
smartContracts *types.SmartContractsInfo
)
switch ethereumAssetMintFlags.bridge {
case "primary":
networkAddress = conf.Network.Ethereum.Endpoint
smartContracts, err = conf.PrimarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting primary smart contract informations: %w", err)
}
case "secondary":
networkAddress = conf.Network.SecondaryEthereum.Endpoint
smartContracts, err = conf.SecondarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting secondary smart contract informations: %w", err)
}
}

mintArgs := ethereumAssetMintArgs{
amount: ethereumAssetMintFlags.amount,
ownerPrivateKey: smartContracts.EthereumOwner.Private,
toAddress: ethereumAssetMintFlags.toAddress,
assetAddress: asset.EthereumAddress,
networkAddress: conf.Network.Ethereum.Endpoint,
networkAddress: networkAddress,
}

return ethereumAssetMint(cmd.Context(), mintArgs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethereum_asset_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var ethereumAssetStakeCmd = &cobra.Command{

conf := netState.Config

smartContracts, err := conf.SmartContractsInfo()
smartContracts, err := conf.PrimarySmartContractsInfo()
if err != nil {
return fmt.Errorf("failed getting smart contract informations: %w", err)
}
Expand Down
26 changes: 23 additions & 3 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ var stateCmd = &cobra.Command{
}

func init() {
stateCmd.AddCommand(stateGetSmartcontractsAddressesCmd)
stateCmd.AddCommand(stateGetSmartContractsAddressesCmd)
stateCmd.AddCommand(stateGetSecondarySmartContractsAddressesCmd)
}

var stateGetSmartcontractsAddressesCmd = &cobra.Command{
var stateGetSmartContractsAddressesCmd = &cobra.Command{
Use: "get-smartcontracts-addresses",
Short: "Print smartcontracts addresses and keys passed to vegacapsule as a config parameter",
Short: "Print primary smartcontracts addresses and keys passed to vegacapsule as a config parameter",
RunE: func(cmd *cobra.Command, args []string) error {
netState, err := state.LoadNetworkState(homePath)
if err != nil {
Expand All @@ -35,3 +36,22 @@ var stateGetSmartcontractsAddressesCmd = &cobra.Command{
return nil
},
}

var stateGetSecondarySmartContractsAddressesCmd = &cobra.Command{
Use: "get-secondary-smartcontracts-addresses",
Short: "Print secondary smartcontracts addresses and keys passed to vegacapsule as a config parameter",
RunE: func(cmd *cobra.Command, args []string) error {
netState, err := state.LoadNetworkState(homePath)
if err != nil {
return err
}

if netState.Empty() {
return networkNotBootstrappedErr("state get-secondary-smartcontracts-addresses")
}

fmt.Println(*netState.Config.Network.SecondarySmartContractsAddresses)

return nil
},
}
83 changes: 80 additions & 3 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ pre_start {
docker_service "ganache-1" {
...
}
docker_service "ganache-2" {
...
}
docker_service "postgres-1" {
...
}
Expand Down Expand Up @@ -411,6 +414,9 @@ network "testnet" {
ethereum {
...
}
secondary_ethereum {
...
}
pre_start {
...
Expand All @@ -436,7 +442,7 @@ network "testnet" {

## *EthereumConfig*

Allows the user to define the specific Ethereum network to be used.
Allows the user to define the specific primary Ethereum network to be used.
It can either be one of the [public networks](https://ethereum.org/en/developers/docs/networks/#public-networks) or
a local instance of Ganache.

Expand Down Expand Up @@ -479,7 +485,61 @@ a local instance of Ganache.
ethereum {
chain_id = "1440"
network_id = "1441"
endpoint = "http://127.0.0.1:8545/"
endpoint = "ws://127.0.0.1:8545/"
}
```

</dl>

---

## *SecondaryEthereumConfig*

Allows the user to define the specific secondary Ethereum network to be used.
It can either be one of the [public networks](https://ethereum.org/en/developers/docs/networks/#public-networks) or
a local instance of Ganache.

### Fields

<dl>
<dt>
<code>chain_id</code> <strong>string</strong> - required
</dt>

<dd>



</dd>

<dt>
<code>network_id</code> <strong>string</strong> - required
</dt>

<dd>



</dd>

<dt>
<code>endpoint</code> <strong>string</strong> - required
</dt>

<dd>



</dd>

### Complete example

```hcl
secondary_ethereum {
chain_id = "1450"
network_id = "1451"
endpoint = "ws://127.0.0.1:8546/"
}
```
Expand Down Expand Up @@ -1339,7 +1399,24 @@ docker_service "ganache-1" {
}
auth_soft_fail = true
}
docker_service "ganache-2" {
image = "vegaprotocol/ganache:latest"
cmd = "ganache-cli"
args = [
"--blockTime", "1",
"--chainId", "1450",
"--networkId", "1451",
"-h", "0.0.0.0",
"-p", "8546",
"-m", "ozone access unlock valid olympic save include omit supply green clown session",
"--db", "/app/ganache-db",
]
static_port {
value = 8546
to = 8546
}
auth_soft_fail = true
}
```

</dl>
Expand Down
Loading

0 comments on commit a9809ab

Please sign in to comment.