Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodebalancer starknet blockchain support #941

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions nodebalancer/cmd/nodebalancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"
)

// Main variable of pool of blockchains which contains pool of nodes
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
46 changes: 39 additions & 7 deletions nodebalancer/cmd/nodebalancer/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion nodebalancer/cmd/nodebalancer/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var NB_VERSION = "0.2.4"
var NB_VERSION = "0.2.5"
Loading