Skip to content

Commit

Permalink
fix error code range
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 18, 2024
1 parent d009bf2 commit 3501813
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion common/geth/handle_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (f *FailoverController) handleHttpError(httpRespError rpc.HTTPError) bool {
if sc >= 200 && sc < 300 {
// 2xx error
return false
} else if sc >= 300 && sc < 400 {
} else if sc >= 400 && sc < 500 {
// 4xx error, Client Error. Alchemy documents 400,401,403,429
// https://docs.alchemy.com/reference/error-reference
return false
Expand Down
52 changes: 43 additions & 9 deletions common/geth/multihoming_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Layr-Labs/eigenda/common/geth"
damock "github.com/Layr-Labs/eigenda/common/mock"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,7 +18,7 @@ var (
rpcURLs = []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}
)

func makeTestMultihomingClient(numRetries int) (*geth.MultiHomingClient, error) {
func makeTestMultihomingClient(numRetries int, designatedError error) (*geth.MultiHomingClient, error) {
logger := logging.NewNoopLogger()

ethClientCfg := geth.EthClientConfig{
Expand All @@ -38,7 +38,7 @@ func makeTestMultihomingClient(numRetries int) (*geth.MultiHomingClient, error)

for i := 0; i < len(rpcURLs); i++ {
mockEthClient := &damock.MockEthClient{}
mockEthClient.On("ChainID", mock.Anything).Return(big.NewInt(0), ethereum.NotFound)
mockEthClient.On("ChainID", mock.Anything).Return(big.NewInt(0), designatedError)
mockClient.RPCs = append(mockClient.RPCs, mockEthClient)
}

Expand All @@ -53,8 +53,39 @@ func makeFailureCall(t *testing.T, client *geth.MultiHomingClient, numCall int)
}
}

func TestMultihomingClientZeroRetry(t *testing.T) {
client, _ := makeTestMultihomingClient(0)
func make500Error() rpc.HTTPError {
var httpRespError rpc.HTTPError
httpRespError.StatusCode = 500
httpRespError.Status = "INTERNAL_ERROR"
httpRespError.Body = []byte{}
return httpRespError
}

func TestMultihomingClientSenderFaultZeroRetry(t *testing.T) {
// 2xx and 4xx attributes to sender's fault, RPC should not rotate
statusCodes := []int{201, 202, 400, 401, 403, 429}
for _, sc := range statusCodes {
var httpRespError rpc.HTTPError
httpRespError.StatusCode = sc
httpRespError.Status = "INTERNAL_ERROR"
httpRespError.Body = []byte{}

client, _ := makeTestMultihomingClient(0, httpRespError)

index, _ := client.GetRPCInstance()
require.Equal(t, index, 0)

makeFailureCall(t, client, 1)

// given num retry is 0, when failure arises above, current rpc should becomes the next one
index, _ = client.GetRPCInstance()
require.Equal(t, index, 0)
}
}

func TestMultihomingClientRPCFaultZeroRetry(t *testing.T) {
httpRespError := make500Error()
client, _ := makeTestMultihomingClient(0, httpRespError)

index, _ := client.GetRPCInstance()
require.Equal(t, index, 0)
Expand All @@ -76,8 +107,10 @@ func TestMultihomingClientZeroRetry(t *testing.T) {
require.Equal(t, index, 0)
}

func TestMultihomingClientOneRetry(t *testing.T) {
client, _ := makeTestMultihomingClient(1)
func TestMultihomingClientRPCFaultOneRetry(t *testing.T) {
httpRespError := make500Error()

client, _ := makeTestMultihomingClient(1, httpRespError)

index, _ := client.GetRPCInstance()
require.Equal(t, index, 0)
Expand All @@ -99,8 +132,9 @@ func TestMultihomingClientOneRetry(t *testing.T) {
require.Equal(t, index, 0)
}

func TestMultihomingClientTwoRetry(t *testing.T) {
client, _ := makeTestMultihomingClient(2)
func TestMultihomingClientRPCFaultTwoRetry(t *testing.T) {
httpRespError := make500Error()
client, _ := makeTestMultihomingClient(2, httpRespError)

index, _ := client.GetRPCInstance()
require.Equal(t, index, 0)
Expand Down

0 comments on commit 3501813

Please sign in to comment.