mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 19:51:20 +00:00
12403d249f
* HTTP proxy server for Eth2 APIs (#8904) * Implement API HTTP proxy server * cleanup + more comments * gateway will no longer be dependent on beaconv1 * handle error during ErrorJson type assertion * simplify handling of endpoint data * fix mux v1 route * use URL encoding for all requests * comment fieldProcessor * fix failing test * change proxy port to not interfere with e2e * gzl * simplify conditional expression * Move appending custom error header to grpcutils package * add api-middleware-port flag * fix documentation for processField * modify e2e port * change field processing error message * better error message for field processing * simplify base64ToHexProcessor * fix json structs * Run several new endpoints through API middleware (#8922) * Implement API HTTP proxy server * cleanup + more comments * gateway will no longer be dependent on beaconv1 * handle error during ErrorJson type assertion * simplify handling of endpoint data * fix mux v1 route * use URL encoding for all requests * comment fieldProcessor * fix failing test * change proxy port to not interfere with e2e * gzl * simplify conditional expression * Move appending custom error header to grpcutils package * add api-middleware-port flag * fix documentation for processField * modify e2e port * change field processing error message * better error message for field processing * simplify base64ToHexProcessor * fix json structs * /eth/v1/beacon/states/{state_id}/validators * /eth/v1/beacon/states/{state_id}/validators/{validator_id} * /eth/v1/beacon/states/{state_id}/validator_balances * /eth/v1/beacon/states/{state_id}/committees * allow skipping base64-encoding for query params * /eth/v1/beacon/pool/attestations * replace break with continue * Remove unused functions (#8924) Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Process SSZ-serialized beacon state through API middleware (#8925) * update field names * Process SSZ-serialized beacon state through API middleware * revert changes to go.mod and go.sum * Revert "Merge branch '__develop' into feature/api-middleware" This reverts commit 7c739a8fd71e2c1e3a14be85abd29a59b57ae9b5, reversing changes made to 2d0f8e012ecb006888ed8e826b45625a3edc2eeb. * update ethereumapis * update validator field name * update deps.bzl * update json tags (#8942) * Run `/node/syncing` through API Middleware (#8944) * add IsSyncing field to grpc response * run /node/syncing through the middleware Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Return HTTP status codes other than 200 and 500 from node and debug endpoints (#8937) * error codes for node endpoints * error codes for debug endpoints * better comment about headers * gzl * review comments * comment on return value * update fakeChecker used for fuzz tests * fix failing tests * Allow to pass URL params literally, without encoding to base64 (#8938) * Allow to pass URL params literally, without encoding to base64 * fix compile error Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Process SSZ-serialized beacon state through API middleware (#8925) * update field names * Process SSZ-serialized beacon state through API middleware * revert changes to go.mod and go.sum * Revert "Merge branch '__develop' into feature/api-middleware" This reverts commit 7c739a8fd71e2c1e3a14be85abd29a59b57ae9b5, reversing changes made to 2d0f8e012ecb006888ed8e826b45625a3edc2eeb. * update ethereumapis * update validator field name * update deps.bzl * update json tags (#8942) * Run `/node/syncing` through API Middleware (#8944) * add IsSyncing field to grpc response * run /node/syncing through the middleware Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Return HTTP status codes other than 200 and 500 from node and debug endpoints (#8937) * error codes for node endpoints * error codes for debug endpoints * better comment about headers * gzl * review comments * comment on return value * update fakeChecker used for fuzz tests * fix failing tests * Allow to pass URL params literally, without encoding to base64 (#8938) * Allow to pass URL params literally, without encoding to base64 * fix compile error Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * unused import * Return correct status codes from beacon endpoints (#8960) * Various API Middleware fixes (#8963) * Return correct status codes from `/states` endpoints * better error messages in debug and node * better error messages in state * returning correct error codes from validator endpoints * correct error codes for getting a block header * gzl * fix err variable name * fix nil block comparison * test fixes * make status enum test better * fix ineffectual assignment * make PR unstuck * return proper status codes * return uppercase keys from /config/spec * return lowercase validator status * convert requested enum values to uppercase * validator fixes * Implement `/beacon/headers` endpoint (#8966) * Refactor API Middleware into more manageable code (#8984) * move endpoint registration out of shared package * divide main function into smaller components * return early on error * implement hooks * implement custom handlers and add documentation * fix test compile error * restrict package visibility * remove redundant error checking * rename file * API Middleware unit tests (#8998) * move endpoint registration out of shared package * divide main function into smaller components * return early on error * implement hooks * implement custom handlers and add documentation * fix test compile error * restrict package visibility * remove redundant error checking * rename file * api_middleware_processing * endpoints * gzl * remove gazelle:ignore * merge * Implement SSZ version of `/blocks/{block_id}` (#8970) * Implement SSZ version of `/blocks/{block_id}` * add dependencies back * fix indentation in deps.bzl * parameterize ssz functions * get block ssz * update ethereumapis dependency * gzl * Do not reuse `Endpoint` structs between API calls (#9007) * code refactor * implement endpoint factory * fix test * fmt * include pbs * gaz * test naming fixes * remove unused code * radek comments * revert endpoint test * bring back bytes test case * move `signedBeaconBlock` to `migration` package * change `fmt.Errorf` to `errors.Wrap` * capitalize SSZ * capitalize URL * more review feedback * rename `handleGetBlockSSZ` to `handleGetBeaconBlockSSZ` * rename `IndexOutOfRangeError` to `ValidatorIndexOutOfRangeError` * simplify parameter names * test header * more corrections * properly allocate array capacity Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Nishant Das <nishdas93@gmail.com>
400 lines
9.5 KiB
Go
400 lines
9.5 KiB
Go
package bytesutil_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestToBytes(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt.a, len(tt.b))
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes1(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes1(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes2(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes2(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes3(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes3(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes4(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes4(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes8(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes8(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestFromBool(t *testing.T) {
|
|
tests := []byte{
|
|
0,
|
|
1,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBool(tt)
|
|
c := bytesutil.FromBool(b)
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes2(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
(1 << 16) - 1,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 2)
|
|
c := bytesutil.FromBytes2(b)
|
|
assert.Equal(t, uint16(tt), c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes4(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
4294967295, // 2^32 - 1
|
|
4294967200,
|
|
3894948296,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 4)
|
|
c := bytesutil.FromBytes4(b)
|
|
if c != tt {
|
|
t.Errorf("Wanted %d but got %d", tt, c)
|
|
}
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes8(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
922376854775806,
|
|
42893720984775807,
|
|
18446744073709551615,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 8)
|
|
c := bytesutil.FromBytes8(b)
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b []byte
|
|
}{
|
|
{[]byte{'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F', 'G'}},
|
|
{[]byte{'A', 'C', 'D', 'E', 'F'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F'}},
|
|
{[]byte{}, []byte{}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Trunc(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestReverse(t *testing.T) {
|
|
tests := []struct {
|
|
input [][32]byte
|
|
output [][32]byte
|
|
}{
|
|
{[][32]byte{{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}, {'G'}, {'H'}},
|
|
[][32]byte{{'H'}, {'G'}, {'F'}, {'E'}, {'D'}, {'C'}, {'B'}, {'A'}}},
|
|
{[][32]byte{{1}, {2}, {3}, {4}},
|
|
[][32]byte{{4}, {3}, {2}, {1}}},
|
|
{[][32]byte{}, [][32]byte{}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ReverseBytes32Slice(tt.input)
|
|
assert.DeepEqual(t, tt.output, b)
|
|
}
|
|
}
|
|
|
|
func TestSetBit(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c []byte
|
|
}{
|
|
{[]byte{0b00000000}, 1, []byte{0b00000010}},
|
|
{[]byte{0b00000010}, 7, []byte{0b10000010}},
|
|
{[]byte{0b10000010}, 9, []byte{0b10000010, 0b00000010}},
|
|
{[]byte{0b10000010}, 27, []byte{0b10000010, 0b00000000, 0b00000000, 0b00001000}},
|
|
{[]byte{0b10000010, 0b00000000}, 8, []byte{0b10000010, 0b00000001}},
|
|
{[]byte{0b10000010, 0b00000000}, 31, []byte{0b10000010, 0b00000000, 0b00000000, 0b10000000}},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.c, bytesutil.SetBit(tt.a, tt.b))
|
|
}
|
|
}
|
|
|
|
func TestClearBit(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c []byte
|
|
}{
|
|
{[]byte{0b00000000}, 1, []byte{0b00000000}},
|
|
{[]byte{0b00000010}, 1, []byte{0b00000000}},
|
|
{[]byte{0b10000010}, 1, []byte{0b10000000}},
|
|
{[]byte{0b10000010}, 8, []byte{0b10000010}},
|
|
{[]byte{0b10000010, 0b00001111}, 7, []byte{0b00000010, 0b00001111}},
|
|
{[]byte{0b10000010, 0b00001111}, 10, []byte{0b10000010, 0b00001011}},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.c, bytesutil.ClearBit(tt.a, tt.b))
|
|
}
|
|
}
|
|
|
|
func TestMakeEmptyBitfields(t *testing.T) {
|
|
tests := []struct {
|
|
a int
|
|
b int
|
|
}{
|
|
{0, 1},
|
|
{1, 1},
|
|
{2, 1},
|
|
{7, 1},
|
|
{8, 2},
|
|
{15, 2},
|
|
{16, 3},
|
|
{100, 13},
|
|
{104, 14},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.b, len(bytesutil.MakeEmptyBitlists(tt.a)))
|
|
}
|
|
}
|
|
|
|
func TestHighestBitIndex(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
error bool
|
|
}{
|
|
{nil, 0, true},
|
|
{[]byte{}, 0, true},
|
|
{[]byte{0b00000001}, 1, false},
|
|
{[]byte{0b10100101}, 8, false},
|
|
{[]byte{0x00, 0x00}, 0, false},
|
|
{[]byte{0xff, 0xa0}, 16, false},
|
|
{[]byte{12, 34, 56, 78}, 31, false},
|
|
{[]byte{255, 255, 255, 255}, 32, false},
|
|
}
|
|
for _, tt := range tests {
|
|
i, err := bytesutil.HighestBitIndex(tt.a)
|
|
if !tt.error {
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, tt.b, i)
|
|
} else {
|
|
assert.ErrorContains(t, "input list can't be empty or nil", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHighestBitIndexBelow(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c int
|
|
error bool
|
|
}{
|
|
{nil, 0, 0, true},
|
|
{[]byte{}, 0, 0, true},
|
|
{[]byte{0b00010001}, 0, 0, false},
|
|
{[]byte{0b00010001}, 1, 1, false},
|
|
{[]byte{0b00010001}, 2, 1, false},
|
|
{[]byte{0b00010001}, 4, 1, false},
|
|
{[]byte{0b00010001}, 5, 5, false},
|
|
{[]byte{0b00010001}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 0, 0, false},
|
|
{[]byte{0b00010001, 0b00000000}, 1, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 2, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 4, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 5, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 15, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 16, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 9, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 10, 10, false},
|
|
{[]byte{0b00010001, 0b00100010}, 11, 10, false},
|
|
{[]byte{0b00010001, 0b00100010}, 14, 14, false},
|
|
{[]byte{0b00010001, 0b00100010}, 15, 14, false},
|
|
{[]byte{0b00010001, 0b00100010}, 24, 14, false},
|
|
{[]byte{0b00010001, 0b00100010, 0b10000000}, 23, 14, false},
|
|
{[]byte{0b00010001, 0b00100010, 0b10000000}, 24, 24, false},
|
|
{[]byte{0b00000000, 0b00000001, 0b00000011}, 17, 17, false},
|
|
{[]byte{0b00000000, 0b00000001, 0b00000011}, 18, 18, false},
|
|
{[]byte{12, 34, 56, 78}, 1000, 31, false},
|
|
{[]byte{255, 255, 255, 255}, 1000, 32, false},
|
|
}
|
|
for _, tt := range tests {
|
|
i, err := bytesutil.HighestBitIndexAt(tt.a, tt.b)
|
|
if !tt.error {
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, tt.c, i)
|
|
} else {
|
|
assert.ErrorContains(t, "input list can't be empty or nil", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUint64ToBytes_RoundTrip(t *testing.T) {
|
|
for i := uint64(0); i < 10000; i++ {
|
|
b := bytesutil.Uint64ToBytesBigEndian(i)
|
|
if got := bytesutil.BytesToUint64BigEndian(b); got != i {
|
|
t.Error("Round trip did not match original value")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsHex(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b bool
|
|
}{
|
|
{nil, false},
|
|
{[]byte(""), false},
|
|
{[]byte("0x"), false},
|
|
{[]byte("0x0"), true},
|
|
{[]byte("foo"), false},
|
|
{[]byte("1234567890abcDEF"), false},
|
|
{[]byte("XYZ4567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
|
|
{[]byte("0x1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), true},
|
|
{[]byte("1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
|
|
}
|
|
for _, tt := range tests {
|
|
isHex, err := bytesutil.IsHex(tt.a)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.b, isHex)
|
|
}
|
|
}
|