This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use context with timeout for external RPC API calls (#27)
* Reorder import statements * Introduce HashLength const * Use context with timeout in rpc functions * Provide timeout from RPC configuration * ConvertProof unit tests * Reuse decode hex function from Geth * Address comments
- Loading branch information
1 parent
dc84ba1
commit 498b0fb
Showing
9 changed files
with
161 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package etherman | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func generateProof(t *testing.T) string { | ||
t.Helper() | ||
|
||
var buf bytes.Buffer | ||
buf.WriteString("0x") | ||
|
||
for i := 0; i < 2*ProofLength; i++ { | ||
hash := generateRandomHexString(t, HashLength) | ||
_, err := buf.WriteString(hash) | ||
require.NoError(t, err) | ||
} | ||
|
||
return buf.String() | ||
} | ||
|
||
func generateRandomHexString(t *testing.T, length int) string { | ||
t.Helper() | ||
|
||
numBytes := length / 2 | ||
randomBytes := make([]byte, numBytes) | ||
|
||
_, err := rand.Read(randomBytes) | ||
require.NoError(t, err) | ||
|
||
return hex.EncodeToString(randomBytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package etherman | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertProof(t *testing.T) { | ||
validInputProof := generateProof(t) | ||
|
||
validRawProof, err := hexutil.Decode(validInputProof) | ||
require.NoError(t, err) | ||
|
||
validProof, err := BytesToProof(validRawProof) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
input string | ||
expected Proof | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "valid proof", | ||
input: validInputProof, | ||
expected: validProof, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "invalid proof length", | ||
input: "0x1234", | ||
expectedErr: fmt.Sprintf("invalid proof length. Expected length: %d, Actual length %d", ProofLength*HashLength*2+2, 6), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := ConvertProof(tt.input) | ||
|
||
if tt.expectedErr != "" { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
} else { | ||
require.NoError(t, err) | ||
require.True(t, result.Equals(tt.expected), "expected: %v, actual: %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters