Skip to content

Commit

Permalink
feat: support choose one of the fastest multi rpc (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn-Huang-Tron authored Mar 14, 2024
1 parent 8938347 commit bccdb77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
24 changes: 18 additions & 6 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,26 @@ func InitChain(
peerid string,
chainconfig *config.ChainConfig,
) (*ChainInfo, error) {

StateStore = stateStore

backend, err := ethclient.Dial(chainconfig.Endpoint)
if err != nil {
return nil, fmt.Errorf("dial eth client: %w", err)
var backend *ethclient.Client
var err error
if len(chainconfig.MultiEndpoint) > 0 {
var backendChan chan *ethclient.Client = make(chan *ethclient.Client, len(chainconfig.MultiEndpoint))
for _, endpoint := range chainconfig.MultiEndpoint {
go func(e string) {
b, err := ethclient.Dial(e)
if err == nil {
backendChan <- b
}
}(endpoint)
}
backend = <-backendChan
} else {
backend, err = ethclient.Dial(chainconfig.Endpoint)
if err != nil {
return nil, fmt.Errorf("dial eth client: %w", err)
}
}

_, err = backend.BlockNumber(context.Background())
if err != nil {
errMsg := "Could not connect to blockchain rpc, please check your network connection"
Expand Down
19 changes: 14 additions & 5 deletions chain/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ var (
testDeploymentGas = ""

//endpoint
ethEndpoint = ""
tronEndpoint = ""
bttcEndpoint = "https://rpc.bittorrentchain.io/"
bttcTestEndpoint = "https://pre-rpc.bt.io/"
testEndpoint = "http://18.144.29.246:8110"
ethEndpoint = ""
tronEndpoint = ""
bttcEndpoint = "https://rpc.bittorrentchain.io/"
bttcTestEndpoint = "https://pre-rpc.bt.io/"
testEndpoint = "http://18.144.29.246:8110"
bttcMultiEndpoint = []string{
"https://rpc.bittorrentchain.io/",
"https://rpc.bt.io/",
"https://bttc.trongrid.io/",
}
bttcTestMultiEndpoint = []string{"https://pre-rpc.bt.io/"}

DefaultChain = bttcChainID
)
Expand All @@ -88,6 +94,7 @@ type ChainConfig struct {
Endpoint string
StatusAddress common.Address
FileMetaAddress common.Address
MultiEndpoint []string
}

func GetChainConfig(chainID int64) (*ChainConfig, bool) {
Expand Down Expand Up @@ -119,6 +126,7 @@ func GetChainConfig(chainID int64) (*ChainConfig, bool) {
cfg.BatchAddress = bttcBatchAddress
cfg.StatusAddress = bttcStatusAddress
cfg.FileMetaAddress = bttcFileMetaAddress
cfg.MultiEndpoint = bttcMultiEndpoint
return &cfg, true
case bttcTestChainID:
cfg.StartBlock = bttcStartBlock
Expand All @@ -130,6 +138,7 @@ func GetChainConfig(chainID int64) (*ChainConfig, bool) {
cfg.VaultLogicAddress = bttcTestMutiVaultLogicAddress
cfg.StatusAddress = bttcTestStatusAddress
cfg.FileMetaAddress = bttcTestFileMetaAddress
cfg.MultiEndpoint = bttcTestMultiEndpoint
return &cfg, true
case testChainID:
cfg.StartBlock = ethStartBlock
Expand Down

0 comments on commit bccdb77

Please sign in to comment.