2023-06-26 22:03:51 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2023-12-13 17:17:37 +00:00
|
|
|
|
2024-04-24 07:10:22 +00:00
|
|
|
"github.com/0glabs/0g-chain/client/grpc"
|
2023-06-26 22:03:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LiveNodeRunnerConfig implements NodeRunner.
|
|
|
|
// It connects to a running network via the RPC, GRPC, and EVM urls.
|
|
|
|
type LiveNodeRunnerConfig struct {
|
2024-04-24 11:41:25 +00:00
|
|
|
ZgChainRpcUrl string
|
|
|
|
ZgChainGrpcUrl string
|
|
|
|
ZgChainEvmRpcUrl string
|
2023-10-31 23:37:40 +00:00
|
|
|
|
|
|
|
UpgradeHeight int64
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LiveNodeRunner implements NodeRunner for an already-running chain.
|
|
|
|
// If a LiveNodeRunner is used, end-to-end tests are run against a live chain.
|
|
|
|
type LiveNodeRunner struct {
|
|
|
|
config LiveNodeRunnerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NodeRunner = LiveNodeRunner{}
|
|
|
|
|
|
|
|
// NewLiveNodeRunner creates a new LiveNodeRunner.
|
|
|
|
func NewLiveNodeRunner(config LiveNodeRunnerConfig) *LiveNodeRunner {
|
|
|
|
return &LiveNodeRunner{config}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartChains implements NodeRunner.
|
|
|
|
// It initializes connections to the chain based on parameters.
|
|
|
|
// It attempts to ping the necessary endpoints and panics if they cannot be reached.
|
|
|
|
func (r LiveNodeRunner) StartChains() Chains {
|
2024-04-24 11:41:25 +00:00
|
|
|
fmt.Println("establishing connection to live 0g-chain network")
|
2023-06-26 22:03:51 +00:00
|
|
|
chains := NewChains()
|
|
|
|
|
2024-04-24 11:41:25 +00:00
|
|
|
zgChain := ChainDetails{
|
|
|
|
RpcUrl: r.config.ZgChainRpcUrl,
|
|
|
|
GrpcUrl: r.config.ZgChainGrpcUrl,
|
|
|
|
EvmRpcUrl: r.config.ZgChainEvmRpcUrl,
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 11:41:25 +00:00
|
|
|
if err := waitForChainStart(zgChain); err != nil {
|
2023-06-26 22:03:51 +00:00
|
|
|
panic(fmt.Sprintf("failed to ping chain: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine chain id
|
2024-04-24 11:41:25 +00:00
|
|
|
client, err := grpc.NewClient(zgChain.GrpcUrl)
|
2023-06-26 22:03:51 +00:00
|
|
|
if err != nil {
|
2024-04-24 11:41:25 +00:00
|
|
|
panic(fmt.Sprintf("failed to create 0gchain grpc client: %s", err))
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
2023-12-13 17:17:37 +00:00
|
|
|
|
|
|
|
nodeInfo, err := client.Query.Tm.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
|
2023-06-26 22:03:51 +00:00
|
|
|
if err != nil {
|
2024-04-24 11:41:25 +00:00
|
|
|
panic(fmt.Sprintf("failed to fetch 0gchain node info: %s", err))
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
2024-04-24 11:41:25 +00:00
|
|
|
zgChain.ChainId = nodeInfo.DefaultNodeInfo.Network
|
2023-06-26 22:03:51 +00:00
|
|
|
|
|
|
|
// determine staking denom
|
2023-12-13 17:17:37 +00:00
|
|
|
stakingParams, err := client.Query.Staking.Params(context.Background(), &stakingtypes.QueryParamsRequest{})
|
2023-06-26 22:03:51 +00:00
|
|
|
if err != nil {
|
2024-04-24 11:41:25 +00:00
|
|
|
panic(fmt.Sprintf("failed to fetch 0gchain staking params: %s", err))
|
2023-06-26 22:03:51 +00:00
|
|
|
}
|
2024-04-24 11:41:25 +00:00
|
|
|
zgChain.StakingDenom = stakingParams.Params.BondDenom
|
2023-06-26 22:03:51 +00:00
|
|
|
|
2024-04-24 11:41:25 +00:00
|
|
|
chains.Register("0gchain", &zgChain)
|
2023-06-26 22:03:51 +00:00
|
|
|
|
2024-04-24 11:41:25 +00:00
|
|
|
fmt.Printf("successfully connected to live network %+v\n", zgChain)
|
2023-06-26 22:03:51 +00:00
|
|
|
|
|
|
|
return chains
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown implements NodeRunner.
|
|
|
|
// As the chains are externally operated, this is a no-op.
|
|
|
|
func (LiveNodeRunner) Shutdown() {
|
|
|
|
fmt.Println("shutting down e2e test connections.")
|
|
|
|
}
|