mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-05 18:42:19 +00:00
105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
/*
|
|
Copyright 2021 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package txpool
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func decodeHex(in string) []byte {
|
|
payload, err := hex.DecodeString(in)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
var hashParseTests = []struct {
|
|
payloadStr string
|
|
hashStr string
|
|
expectedErr bool
|
|
}{
|
|
{payloadStr: "a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328", hashStr: "595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328", expectedErr: false},
|
|
}
|
|
|
|
func TestParseHash(t *testing.T) {
|
|
require := require.New(t)
|
|
for _, tt := range hashParseTests {
|
|
var hashBuf [32]byte
|
|
payload := decodeHex(tt.payloadStr)
|
|
_, parseEnd, err := ParseHash(payload, 0, hashBuf[:0])
|
|
require.Equal(tt.expectedErr, err != nil)
|
|
require.Equal(len(payload), parseEnd)
|
|
require.Equal(decodeHex(tt.hashStr), hashBuf[:])
|
|
}
|
|
}
|
|
|
|
var hashEncodeTests = []struct {
|
|
payloadStr string
|
|
hashesStr string
|
|
hashCount int
|
|
expectedErr bool
|
|
}{
|
|
{payloadStr: "e1a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328",
|
|
hashesStr: "595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328", hashCount: 1, expectedErr: false},
|
|
{hashesStr: fmt.Sprintf("%x", toHashes([32]byte{1}, [32]byte{2}, [32]byte{3})),
|
|
payloadStr: "f863a00100000000000000000000000000000000000000000000000000000000000000a00200000000000000000000000000000000000000000000000000000000000000a00300000000000000000000000000000000000000000000000000000000000000", hashCount: 3, expectedErr: false},
|
|
}
|
|
|
|
func TestEncodeHash(t *testing.T) {
|
|
require := require.New(t)
|
|
for _, tt := range hashEncodeTests {
|
|
var encodeBuf []byte
|
|
encodeBuf = EncodeHashes(decodeHex(tt.hashesStr), encodeBuf)
|
|
require.Equal(decodeHex(tt.payloadStr), encodeBuf)
|
|
}
|
|
}
|
|
|
|
var gpt66EncodeTests = []struct {
|
|
payloadStr string
|
|
hashesStr string
|
|
hashCount int
|
|
requestId uint64
|
|
expectedErr bool
|
|
}{
|
|
{payloadStr: "e68306f854e1a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328",
|
|
hashesStr: "595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328", hashCount: 1, requestId: 456788, expectedErr: false},
|
|
}
|
|
|
|
// TestEncodeGPT66 tests the encoding of GetPoolTransactions66 packet
|
|
func TestEncodeGPT66(t *testing.T) {
|
|
require := require.New(t)
|
|
for _, tt := range gpt66EncodeTests {
|
|
var encodeBuf []byte
|
|
var err error
|
|
encodeBuf, err = EncodeGetPooledTransactions66(decodeHex(tt.hashesStr), tt.requestId, encodeBuf)
|
|
require.Equal(tt.expectedErr, err != nil)
|
|
require.Equal(decodeHex(tt.payloadStr), encodeBuf)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
requestID, hashes, _, err := ParseGetPooledTransactions66(encodeBuf, 0, nil)
|
|
require.Equal(tt.expectedErr, err != nil)
|
|
require.Equal(tt.requestId, requestID)
|
|
require.Equal(decodeHex(tt.hashesStr), hashes)
|
|
}
|
|
}
|