kopia lustrzana https://github.com/bugout-dev/moonstream
Nodebalancer starknet blockchain support
rodzic
6e35ab9927
commit
017c921a15
|
@ -15,6 +15,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Main variable of pool of blockchains which contains pool of nodes
|
// Main variable of pool of blockchains which contains pool of nodes
|
||||||
|
@ -50,7 +51,8 @@ type BlockchainPool struct {
|
||||||
|
|
||||||
// Node status response struct for HealthCheck
|
// Node status response struct for HealthCheck
|
||||||
type NodeStatusResultResponse struct {
|
type NodeStatusResultResponse struct {
|
||||||
Number string `json:"number"`
|
BlockNumber uint64 `json:"block_number"`
|
||||||
|
Number string `json:"number"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeStatusResponse struct {
|
type NodeStatusResponse struct {
|
||||||
|
@ -194,14 +196,21 @@ func (bpool *BlockchainPool) StatusLog() {
|
||||||
// HealthCheck fetch the node latest block
|
// HealthCheck fetch the node latest block
|
||||||
func (bpool *BlockchainPool) HealthCheck() {
|
func (bpool *BlockchainPool) HealthCheck() {
|
||||||
for _, b := range bpool.Blockchains {
|
for _, b := range bpool.Blockchains {
|
||||||
|
var timeout time.Duration
|
||||||
|
getLatestBlockReq := `{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`
|
||||||
|
if b.Blockchain == "starknet" {
|
||||||
|
getLatestBlockReq = `{"jsonrpc":"2.0","method":"starknet_getBlockWithTxHashes","params":["latest"],"id":"0"}`
|
||||||
|
timeout = NB_HEALTH_CHECK_CALL_TIMEOUT * 2
|
||||||
|
}
|
||||||
|
|
||||||
for _, n := range b.Nodes {
|
for _, n := range b.Nodes {
|
||||||
alive := false
|
alive := false
|
||||||
|
|
||||||
httpClient := http.Client{Timeout: NB_HEALTH_CHECK_CALL_TIMEOUT}
|
httpClient := http.Client{Timeout: timeout}
|
||||||
resp, err := httpClient.Post(
|
resp, err := httpClient.Post(
|
||||||
n.Endpoint.String(),
|
n.Endpoint.String(),
|
||||||
"application/json",
|
"application/json",
|
||||||
bytes.NewBuffer([]byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`)),
|
bytes.NewBuffer([]byte(getLatestBlockReq)),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n.UpdateNodeState(0, alive)
|
n.UpdateNodeState(0, alive)
|
||||||
|
@ -231,12 +240,17 @@ func (bpool *BlockchainPool) HealthCheck() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1)
|
var blockNumber uint64
|
||||||
blockNumber, err := strconv.ParseUint(blockNumberHex, 16, 64)
|
if b.Blockchain == "starknet" {
|
||||||
if err != nil {
|
blockNumber = statusResponse.Result.BlockNumber
|
||||||
n.UpdateNodeState(0, alive)
|
} else {
|
||||||
log.Printf("Unable to parse block number from hex to string, err: %v", err)
|
blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1)
|
||||||
continue
|
blockNumber, err = strconv.ParseUint(blockNumberHex, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
n.UpdateNodeState(0, alive)
|
||||||
|
log.Printf("Unable to parse block number from hex to string, err: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark node in list of pool as alive and update current block
|
// Mark node in list of pool as alive and update current block
|
||||||
|
|
|
@ -27,9 +27,9 @@ var (
|
||||||
"eth_getUncleCountByBlockNumber": true,
|
"eth_getUncleCountByBlockNumber": true,
|
||||||
"eth_getWork": true,
|
"eth_getWork": true,
|
||||||
"eth_mining": true,
|
"eth_mining": true,
|
||||||
// "eth_sendRawTransaction": true,
|
"eth_sendRawTransaction": true,
|
||||||
"eth_protocolVersion": true,
|
"eth_protocolVersion": true,
|
||||||
"eth_syncing": true,
|
"eth_syncing": true,
|
||||||
|
|
||||||
"net_listening": true,
|
"net_listening": true,
|
||||||
"net_peerCount": true,
|
"net_peerCount": true,
|
||||||
|
@ -56,14 +56,46 @@ var (
|
||||||
"zks_getTransactionDetails": true,
|
"zks_getTransactionDetails": true,
|
||||||
"zks_L1BatchNumber": true,
|
"zks_L1BatchNumber": true,
|
||||||
"zks_L1ChainId": true,
|
"zks_L1ChainId": true,
|
||||||
|
|
||||||
|
// starknet methods
|
||||||
|
"starknet_specVersion": true,
|
||||||
|
"starknet_getBlockWithTxHashes": true,
|
||||||
|
"starknet_getBlockWithTxs": true,
|
||||||
|
"starknet_getStateUpdate": true,
|
||||||
|
"starknet_getStorageAt": true,
|
||||||
|
"starknet_getTransactionStatus": true,
|
||||||
|
"starknet_getTransactionByHash": true,
|
||||||
|
"starknet_getTransactionByBlockIdAndIndex": true,
|
||||||
|
"starknet_getTransactionReceipt": true,
|
||||||
|
"starknet_getClass": true,
|
||||||
|
"starknet_getClassHashAt": true,
|
||||||
|
"starknet_getClassAt": true,
|
||||||
|
"starknet_getBlockTransactionCount": true,
|
||||||
|
"starknet_call": true,
|
||||||
|
"starknet_estimateFee": true,
|
||||||
|
"starknet_estimateMessageFee": true,
|
||||||
|
"starknet_blockNumber": true,
|
||||||
|
"starknet_blockHashAndNumber": true,
|
||||||
|
"starknet_chainId": true,
|
||||||
|
"starknet_syncing": true,
|
||||||
|
"starknet_getEvents": true,
|
||||||
|
"starknet_getNonce": true,
|
||||||
|
|
||||||
|
"starknet_traceTransaction": true,
|
||||||
|
"starknet_simulateTransactions": true,
|
||||||
|
"starknet_traceBlockTransactions": true,
|
||||||
|
|
||||||
|
"starknet_addInvokeTransaction": true,
|
||||||
|
"starknet_addDeclareTransaction": true,
|
||||||
|
"starknet_addDeployAccountTransaction": true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSONRPCRequest struct {
|
type JSONRPCRequest struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
Jsonrpc string `json:"jsonrpc"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params []interface{} `json:"params"`
|
Params interface{} `json:"params"`
|
||||||
ID interface{} `json:"id"` // According to the JSON-RPC specification, the id can be a string, number, or null
|
ID interface{} `json:"id"` // According to the JSON-RPC specification, the id can be a string, number, or null
|
||||||
}
|
}
|
||||||
|
|
||||||
type BlockchainConfig struct {
|
type BlockchainConfig struct {
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
var NB_VERSION = "0.2.4"
|
var NB_VERSION = "0.2.5"
|
||||||
|
|
Ładowanie…
Reference in New Issue