2023-03-07 22:37:45 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-05-05 07:00:03 +00:00
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2023-10-31 23:37:40 +00:00
|
|
|
rpchttpclient "github.com/tendermint/tendermint/rpc/client/http"
|
2023-03-07 22:37:45 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-03-07 22:37:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrChainAlreadyExists = errors.New("chain already exists")
|
|
|
|
)
|
|
|
|
|
2023-06-20 16:29:25 +00:00
|
|
|
// ChainDetails wraps information about the properties & endpoints of a chain.
|
2023-03-07 22:37:45 +00:00
|
|
|
type ChainDetails struct {
|
2023-06-20 16:29:25 +00:00
|
|
|
RpcUrl string
|
|
|
|
GrpcUrl string
|
|
|
|
EvmRpcUrl string
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-03-17 18:30:23 +00:00
|
|
|
ChainId string
|
2023-03-07 22:37:45 +00:00
|
|
|
StakingDenom string
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// EvmClient dials the underlying EVM RPC url and returns an ethclient.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c ChainDetails) EvmClient() (*ethclient.Client, error) {
|
2023-06-20 16:29:25 +00:00
|
|
|
return ethclient.Dial(c.EvmRpcUrl)
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// GrpcConn creates a new connection to the underlying Grpc url.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c ChainDetails) GrpcConn() (*grpc.ClientConn, error) {
|
2023-06-20 16:29:25 +00:00
|
|
|
return util.NewGrpcConnection(c.GrpcUrl)
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
// RpcConn creates a new connection to the underlying Rpc url.
|
|
|
|
func (c ChainDetails) RpcConn() (*rpchttpclient.HTTP, error) {
|
|
|
|
return rpchttpclient.New(c.RpcUrl, "/websocket")
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// Chains wraps a map of name -> details about how to connect to a chain.
|
|
|
|
// It prevents registering multiple chains with the same name & encapsulates
|
|
|
|
// panicking if attempting to access a chain that does not exist.
|
2023-03-07 22:37:45 +00:00
|
|
|
type Chains struct {
|
|
|
|
byName map[string]*ChainDetails
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// NewChains creates an empty Chains map.
|
2023-03-07 22:37:45 +00:00
|
|
|
func NewChains() Chains {
|
|
|
|
return Chains{byName: make(map[string]*ChainDetails, 0)}
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// MustGetChain returns the chain of a given name,
|
|
|
|
// or panics if a chain with that name has not been registered.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c Chains) MustGetChain(name string) *ChainDetails {
|
|
|
|
chain, found := c.byName[name]
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("no chain with name %s found", name))
|
|
|
|
}
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// Register adds a chain to the map.
|
|
|
|
// It returns an error if a ChainDetails with that name has already been registered.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c *Chains) Register(name string, chain *ChainDetails) error {
|
|
|
|
if _, found := c.byName[name]; found {
|
|
|
|
return ErrChainAlreadyExists
|
|
|
|
}
|
|
|
|
c.byName[name] = chain
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the Chain details are all hardcoded because they are currently fixed by kvtool.
|
2023-06-20 16:29:25 +00:00
|
|
|
// someday they may be accepted as configurable parameters.
|
2023-03-07 22:37:45 +00:00
|
|
|
var (
|
2024-05-01 05:53:58 +00:00
|
|
|
kvtoolZgChainChain = ChainDetails{
|
2023-06-20 16:29:25 +00:00
|
|
|
RpcUrl: "http://localhost:26657",
|
|
|
|
GrpcUrl: "http://localhost:9090",
|
|
|
|
EvmRpcUrl: "http://localhost:8545",
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
ChainId: "0gchainlocalnet_8888-1",
|
2024-05-10 06:31:49 +00:00
|
|
|
StakingDenom: chaincfg.GasDenom,
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
2023-06-20 16:29:25 +00:00
|
|
|
kvtoolIbcChain = ChainDetails{
|
|
|
|
RpcUrl: "http://localhost:26658",
|
|
|
|
GrpcUrl: "http://localhost:9092",
|
|
|
|
EvmRpcUrl: "http://localhost:8547",
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
ChainId: "0gchainlocalnet_8889-2",
|
2023-03-07 22:37:45 +00:00
|
|
|
StakingDenom: "uatom",
|
|
|
|
}
|
|
|
|
)
|