erigon-pulse/txpool/packets.go

90 lines
3.0 KiB
Go
Raw Normal View History

2021-07-15 08:52:28 +00:00
/*
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
2021-07-15 19:10:17 +00:00
import (
"fmt"
2021-07-27 03:13:50 +00:00
"github.com/ledgerwatch/erigon-lib/rlp"
2021-07-15 19:10:17 +00:00
)
2021-07-15 08:52:28 +00:00
2021-07-26 00:57:49 +00:00
type NewPooledTransactionHashesPacket [][32]byte
2021-07-27 08:47:33 +00:00
// ParseHashesCount looks at the RLP length Prefix for list of 32-byte hashes
2021-07-15 11:23:17 +00:00
// and returns number of hashes in the list to expect
2021-07-27 05:03:59 +00:00
func ParseHashesCount(payload Hashes, pos int) (int, int, error) {
2021-07-27 08:47:33 +00:00
dataPos, dataLen, list, err := rlp.Prefix(payload, pos)
2021-07-15 11:23:17 +00:00
if err != nil {
2021-07-27 05:03:59 +00:00
return 0, 0, fmt.Errorf("%s: hashes len: %w", rlp.ParseHashErrorPrefix, err)
2021-07-15 11:23:17 +00:00
}
if !list {
2021-07-27 05:03:59 +00:00
return 0, 0, fmt.Errorf("%s: hashes must be a list, not string", rlp.ParseHashErrorPrefix)
2021-07-15 11:23:17 +00:00
}
if dataLen%33 != 0 {
2021-07-27 05:03:59 +00:00
return 0, 0, fmt.Errorf("%s: hashes len must be multiple of 33", rlp.ParseHashErrorPrefix)
2021-07-15 11:23:17 +00:00
}
2021-07-15 19:10:17 +00:00
return dataLen / 33, dataPos, nil
2021-07-15 11:23:17 +00:00
}
2021-07-15 14:48:55 +00:00
// EncodeHashes produces RLP encoding of given number of hashes, as RLP list
// It appends encoding to the given given slice (encodeBuf), reusing the space
// there is there is enough capacity.
2021-07-15 19:10:17 +00:00
// The first returned value is the slice where encodinfg
2021-07-26 12:13:07 +00:00
func EncodeHashes(hashes Hashes, encodeBuf []byte) ([]byte, error) {
2021-07-26 13:39:30 +00:00
hashesLen := len(hashes) / 32 * 33
dataLen := hashesLen
2021-07-27 03:13:50 +00:00
prefixLen := rlp.ListPrefixLen(hashesLen)
2021-07-27 05:03:59 +00:00
encodeBuf = ensureEnoughSize(encodeBuf, prefixLen+dataLen)
2021-07-27 05:18:40 +00:00
rlp.EncodeListPrefix(hashesLen, encodeBuf)
2021-07-26 13:39:30 +00:00
pos := prefixLen
2021-07-26 12:13:07 +00:00
for i := 0; i < len(hashes); i += 32 {
2021-07-27 05:03:59 +00:00
rlp.EncodeHash(hashes[i:i+32], encodeBuf[pos:])
2021-07-26 13:39:30 +00:00
pos += 33
2021-07-15 19:10:17 +00:00
}
2021-07-27 05:03:59 +00:00
return encodeBuf, nil
}
func ensureEnoughSize(in []byte, size int) []byte {
if cap(in) < size {
return make([]byte, size)
}
return in[:size] // Reuse the space in pkbuf, is it has enough capacity
2021-07-15 14:48:55 +00:00
}
2021-07-16 16:00:51 +00:00
// EncodeGetPooledTransactions66 produces encoding of GetPooledTransactions66 packet
2021-07-26 12:13:07 +00:00
func EncodeGetPooledTransactions66(hashes []byte, requestId uint64, encodeBuf []byte) ([]byte, error) {
hashesLen := len(hashes) / 32 * 33
2021-07-27 03:13:50 +00:00
dataLen := rlp.ListPrefixLen(hashesLen) + hashesLen + rlp.U64Len(requestId)
prefixLen := rlp.ListPrefixLen(dataLen)
2021-07-27 05:03:59 +00:00
encodeBuf = ensureEnoughSize(encodeBuf, prefixLen+dataLen)
2021-07-27 08:47:33 +00:00
// Length Prefix for the entire structure
2021-07-27 05:18:40 +00:00
rlp.EncodeListPrefix(dataLen, encodeBuf)
2021-07-26 12:13:07 +00:00
pos := prefixLen
2021-07-16 18:42:03 +00:00
// encode requestId
2021-07-27 05:18:40 +00:00
rlp.EncodeU64(requestId, encodeBuf[pos:])
2021-07-27 03:13:50 +00:00
pos += rlp.U64Len(requestId)
2021-07-27 08:47:33 +00:00
// Encode length Prefix for hashes
2021-07-27 05:18:40 +00:00
rlp.EncodeListPrefix(hashesLen, encodeBuf[pos:])
2021-07-27 03:13:50 +00:00
pos += rlp.ListPrefixLen(hashesLen)
2021-07-26 12:13:07 +00:00
for i := 0; i < len(hashes); i += 32 {
2021-07-27 05:03:59 +00:00
rlp.EncodeHash(hashes[i:i+32], encodeBuf[pos:pos+33])
2021-07-26 12:13:07 +00:00
pos += 33
2021-07-16 16:00:51 +00:00
}
2021-07-27 05:03:59 +00:00
return encodeBuf, nil
2021-07-16 16:00:51 +00:00
}