Commit 0dcb1b2c authored by Sebastian Stammler's avatar Sebastian Stammler Committed by GitHub

op-node/p2p: add descriptive error code string (#10662)

parent 272d4471
...@@ -68,6 +68,13 @@ const ( ...@@ -68,6 +68,13 @@ const (
ResultCodeUnknownErr byte = 3 ResultCodeUnknownErr byte = 3
) )
var resultCodeString = []string{
"success",
"not found",
"invalid request",
"unknown error",
}
func PayloadByNumberProtocolID(l2ChainID *big.Int) protocol.ID { func PayloadByNumberProtocolID(l2ChainID *big.Int) protocol.ID {
return protocol.ID(fmt.Sprintf("/opstack/req/payload_by_number/%d/0", l2ChainID)) return protocol.ID(fmt.Sprintf("/opstack/req/payload_by_number/%d/0", l2ChainID))
} }
...@@ -614,7 +621,13 @@ func (s *SyncClient) peerLoop(ctx context.Context, id peer.ID) { ...@@ -614,7 +621,13 @@ func (s *SyncClient) peerLoop(ctx context.Context, id peer.ID) {
type requestResultErr byte type requestResultErr byte
func (r requestResultErr) Error() string { func (r requestResultErr) Error() string {
return fmt.Sprintf("peer failed to serve request with code %d", uint8(r)) var errStr string
if ri := int(r); ri < len(resultCodeString) {
errStr = resultCodeString[ri]
} else {
errStr = "invalid code"
}
return fmt.Sprintf("peer failed to serve request with code %d: %s", uint8(r), errStr)
} }
func (r requestResultErr) ResultCode() byte { func (r requestResultErr) ResultCode() byte {
......
...@@ -2,7 +2,9 @@ package p2p ...@@ -2,7 +2,9 @@ package p2p
import ( import (
"context" "context"
"fmt"
"math/big" "math/big"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
...@@ -378,7 +380,7 @@ func TestNetworkNotifyAddPeerAndRemovePeer(t *testing.T) { ...@@ -378,7 +380,7 @@ func TestNetworkNotifyAddPeerAndRemovePeer(t *testing.T) {
require.NoError(t, err, "failed to connect to peer B from peer A") require.NoError(t, err, "failed to connect to peer B from peer A")
require.Equal(t, hostA.Network().Connectedness(hostB.ID()), network.Connected) require.Equal(t, hostA.Network().Connectedness(hostB.ID()), network.Connected)
//wait for async add process done // wait for async add process done
<-waitChan <-waitChan
_, ok := syncCl.peers[hostB.ID()] _, ok := syncCl.peers[hostB.ID()]
require.True(t, ok, "peerB should exist in syncClient") require.True(t, ok, "peerB should exist in syncClient")
...@@ -386,7 +388,7 @@ func TestNetworkNotifyAddPeerAndRemovePeer(t *testing.T) { ...@@ -386,7 +388,7 @@ func TestNetworkNotifyAddPeerAndRemovePeer(t *testing.T) {
err = hostA.Network().ClosePeer(hostB.ID()) err = hostA.Network().ClosePeer(hostB.ID())
require.NoError(t, err, "close peer fail") require.NoError(t, err, "close peer fail")
//wait for async removing process done // wait for async removing process done
<-waitChan <-waitChan
_, peerBExist3 := syncCl.peers[hostB.ID()] _, peerBExist3 := syncCl.peers[hostB.ID()]
require.True(t, !peerBExist3, "peerB should not exist in syncClient") require.True(t, !peerBExist3, "peerB should not exist in syncClient")
...@@ -400,5 +402,44 @@ func TestPanicGuard(t *testing.T) { ...@@ -400,5 +402,44 @@ func TestPanicGuard(t *testing.T) {
err := panicGuard(mockPanickingFn)(context.Background(), peer.ID(""), 37) err := panicGuard(mockPanickingFn)(context.Background(), peer.ID(""), 37)
require.EqualError(t, err, "recovered from a panic: gotcha") require.EqualError(t, err, "recovered from a panic: gotcha")
}) })
}
func TestRequestResultErr_Error(t *testing.T) {
for _, test := range []struct {
code byte
expStr string
}{
{
code: 0,
expStr: "success",
},
{
code: 1,
expStr: "not found",
},
{
code: 2,
expStr: "invalid request",
},
{
code: 3,
expStr: "unknown error",
},
{
code: 4,
expStr: "invalid code",
},
{
code: 0xff,
expStr: "invalid code",
},
} {
t.Run(fmt.Sprintf("code %d", test.code), func(t *testing.T) {
err := requestResultErr(test.code)
errStr := err.Error()
if !strings.HasSuffix(errStr, test.expStr) {
t.Fatalf("unexpected error string %q, expted suffix %q", errStr, test.expStr)
}
})
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment