From 017c921a1575a6c5bc98ec211ae5382f34cd02c1 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Wed, 25 Oct 2023 11:30:27 +0000 Subject: [PATCH] Nodebalancer starknet blockchain support --- nodebalancer/cmd/nodebalancer/balancer.go | 32 ++++++++++---- nodebalancer/cmd/nodebalancer/blockchain.go | 46 +++++++++++++++++---- nodebalancer/cmd/nodebalancer/version.go | 2 +- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/nodebalancer/cmd/nodebalancer/balancer.go b/nodebalancer/cmd/nodebalancer/balancer.go index ff43a45a..d8dea3c4 100644 --- a/nodebalancer/cmd/nodebalancer/balancer.go +++ b/nodebalancer/cmd/nodebalancer/balancer.go @@ -15,6 +15,7 @@ import ( "strings" "sync" "sync/atomic" + "time" ) // Main variable of pool of blockchains which contains pool of nodes @@ -50,7 +51,8 @@ type BlockchainPool struct { // Node status response struct for HealthCheck type NodeStatusResultResponse struct { - Number string `json:"number"` + BlockNumber uint64 `json:"block_number"` + Number string `json:"number"` } type NodeStatusResponse struct { @@ -194,14 +196,21 @@ func (bpool *BlockchainPool) StatusLog() { // HealthCheck fetch the node latest block func (bpool *BlockchainPool) HealthCheck() { 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 { alive := false - httpClient := http.Client{Timeout: NB_HEALTH_CHECK_CALL_TIMEOUT} + httpClient := http.Client{Timeout: timeout} resp, err := httpClient.Post( n.Endpoint.String(), "application/json", - bytes.NewBuffer([]byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`)), + bytes.NewBuffer([]byte(getLatestBlockReq)), ) if err != nil { n.UpdateNodeState(0, alive) @@ -231,12 +240,17 @@ func (bpool *BlockchainPool) HealthCheck() { continue } - blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1) - 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 + var blockNumber uint64 + if b.Blockchain == "starknet" { + blockNumber = statusResponse.Result.BlockNumber + } else { + blockNumberHex := strings.Replace(statusResponse.Result.Number, "0x", "", -1) + 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 diff --git a/nodebalancer/cmd/nodebalancer/blockchain.go b/nodebalancer/cmd/nodebalancer/blockchain.go index 82598995..9f073fdf 100644 --- a/nodebalancer/cmd/nodebalancer/blockchain.go +++ b/nodebalancer/cmd/nodebalancer/blockchain.go @@ -27,9 +27,9 @@ var ( "eth_getUncleCountByBlockNumber": true, "eth_getWork": true, "eth_mining": true, - // "eth_sendRawTransaction": true, - "eth_protocolVersion": true, - "eth_syncing": true, + "eth_sendRawTransaction": true, + "eth_protocolVersion": true, + "eth_syncing": true, "net_listening": true, "net_peerCount": true, @@ -56,14 +56,46 @@ var ( "zks_getTransactionDetails": true, "zks_L1BatchNumber": 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 { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params []interface{} `json:"params"` - ID interface{} `json:"id"` // According to the JSON-RPC specification, the id can be a string, number, or null + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params interface{} `json:"params"` + ID interface{} `json:"id"` // According to the JSON-RPC specification, the id can be a string, number, or null } type BlockchainConfig struct { diff --git a/nodebalancer/cmd/nodebalancer/version.go b/nodebalancer/cmd/nodebalancer/version.go index 013fae97..dd47a51c 100644 --- a/nodebalancer/cmd/nodebalancer/version.go +++ b/nodebalancer/cmd/nodebalancer/version.go @@ -1,3 +1,3 @@ package main -var NB_VERSION = "0.2.4" +var NB_VERSION = "0.2.5"