Skip to content

Instantly share code, notes, and snippets.

@0xalpharush
Last active December 10, 2024 04:11
Show Gist options
  • Select an option

  • Save 0xalpharush/519f349791cc0b5f47cd6e873e978440 to your computer and use it in GitHub Desktop.

Select an option

Save 0xalpharush/519f349791cc0b5f47cd6e873e978440 to your computer and use it in GitHub Desktop.
diff --git a/beacon-chain/p2p/encoder/ssz.go b/beacon-chain/p2p/encoder/ssz.go
index e852f519e..29da4f315 100644
--- a/beacon-chain/p2p/encoder/ssz.go
+++ b/beacon-chain/p2p/encoder/ssz.go
@@ -1,6 +1,7 @@
package encoder
import (
+ "encoding/binary"
"fmt"
"io"
"sync"
@@ -47,6 +48,26 @@ func (_ SszNetworkEncoder) EncodeGossip(w io.Writer, msg fastssz.Marshaler) (int
return 0, errors.Errorf("gossip message exceeds max gossip size: %d bytes > %d bytes", len(b), MaxGossipSize)
}
b = snappy.Encode(nil /*dst*/, b)
+ x := make([]byte, 10)
+ n := binary.PutUvarint(x, uint64(len(b)))
+ length := proto.EncodeVarint(uint64(len(b)))
+ b = snappy.Encode(nil /*dst*/, b)
+
+ if len(length) < 5 {
+ fmt.Printf("Before length: %v\n", b[:n])
+ for i := 0; i < len(length); i++ {
+ length[i] = length[i] | 0x80
+ }
+ for len(length) < 5 {
+ length = append(length, "\x80"...)
+ if len(length) == 5 {
+ length = append(length, "\x00"...)
+ }
+ }
+ fmt.Printf("After length: %v\n", length)
+ // replace snappy length
+ b = append(length, b[n:]...)
+ }
return w.Write(b)
}
diff --git a/beacon-chain/p2p/encoder/ssz.go b/beacon-chain/p2p/encoder/ssz.go
index 4e39854af..05fa7beb0 100644
--- a/beacon-chain/p2p/encoder/ssz.go
+++ b/beacon-chain/p2p/encoder/ssz.go
@@ -1,6 +1,7 @@
package encoder
import (
+ "encoding/binary"
"fmt"
"io"
"sync"
@@ -88,11 +89,27 @@ func (_ SszNetworkEncoder) EncodeWithMaxLength(w io.Writer, msg fastssz.Marshale
)
}
// write varint first
- _, err = w.Write(proto.EncodeVarint(uint64(len(b))))
+ // _, err = w.Write(proto.EncodeVarint(uint64(len(b))))
+ length := proto.EncodeVarint(uint64(len(b)))
+ if len(length) < 5 {
+ fmt.Printf("RPC: Before length: %v\n", length)
+ for i := 0; i < len(length); i++ {
+ length[i] = length[i] | 0x80
+ }
+ for len(length) < 5 {
+ length = append(length, "\x80"...)
+ if len(length) == 5 {
+ length = append(length, "\x00"...)
+ }
+ }
+ fmt.Printf("RPC: After length: %v\n", length)
+ }
+ _, err = w.Write(length)
if err != nil {
return 0, err
}
return writeSnappyBuffer(w, b)
+
}
{
"participants": [
{
"el_type": "geth",
"cl_type": "nimbus",
"count": 1
},
{
"el_type": "geth",
"cl_type": "prysm",
"cl_image": "ar/badprysm2/beacon-chain:latest",
"vc_image": "ar/badprysm2/validator:latest",
"count": 1
},
{
"el_type": "geth",
"cl_type": "prysm",
"count": 1
},
{
"el_type": "geth",
"cl_type": "lighthouse",
"count": 1
}
],
"launch_additional_services": false
}
package main
import (
"errors"
"fmt"
"io"
"github.com/gogo/protobuf/proto"
)
const maxVarintLength = 10
// mockReader wraps a byte slice to implement io.Reader
type mockReader struct {
data []byte
pos int
}
func newMockReader(data []byte) *mockReader {
return &mockReader{
data: data,
pos: 0,
}
}
func (m *mockReader) Read(p []byte) (n int, err error) {
if m.pos >= len(m.data) {
return 0, io.EOF
}
n = copy(p, m.data[m.pos:])
m.pos += n
return n, nil
}
// readVarint at the beginning of a byte slice. This varint may be used to indicate
// the length of the remaining bytes in the reader.
func readVarint(r io.Reader) (uint64, error) {
b := make([]byte, 0, maxVarintLength)
for i := 0; i < maxVarintLength; i++ {
b1 := make([]byte, 1)
n, err := r.Read(b1)
if err != nil {
return 0, err
}
if n != 1 {
return 0, errors.New("did not read a byte from stream")
}
b = append(b, b1[0])
// If most significant bit is not set, we have reached the end of the Varint.
if b1[0]&0x80 == 0 {
break
}
// If the varint is larger than 10 bytes, it is invalid as it would
// exceed the size of MaxUint64.
if i+1 >= maxVarintLength {
return 0, errors.New("varint too large")
}
}
vi, n := proto.DecodeVarint(b)
if n != len(b) {
return 0, errors.New("did not read the full varint")
}
return vi, nil
}
func main() {
reader := newMockReader([]byte{0xd7, 0xbb, 0x81, 0x00})
value, err := readVarint(reader)
if err != nil {
panic(err)
}
fmt.Println(value)
reader = newMockReader([]byte{0xd7, 0xbb, 0x81, 0x80, 0x00})
value, err = readVarint(reader)
if err != nil {
panic(err)
}
fmt.Println(value)
reader = newMockReader([]byte{0xd7, 0xbb, 0x81, 0x80, 0x80, 0x00})
value, err = readVarint(reader)
if err != nil {
panic(err)
}
fmt.Println(value)
}
diff --git a/tests/test_snappy.nim b/tests/test_snappy.nim
index f70cbc9..d59071a 100644
--- a/tests/test_snappy.nim
+++ b/tests/test_snappy.nim
@@ -6,7 +6,7 @@ import
unittest2,
../snappy,
../snappy/[faststreams, streams],
- ./cpp_snappy, ./randgen
+ ./randgen
include system/timers
@@ -45,7 +45,7 @@ proc roundTrip(msg: string, source: openArray[byte]) =
var encodedWithSnappy = snappy.encode(source)
var encodedWithFastStreams = faststreamsEncode(source)
var encodedWithNimStreams = streamsEncode(source)
- var encodedWithCpp = cpp_snappy.encode(source)
+ # var encodedWithCpp = cpp_snappy.encode(source)
# check encodedWithCpp.len == encodedWithOpenArrays.len
# check: encodedWithOpenArrays == encodedWithCpp
@@ -57,26 +57,26 @@ proc roundTrip(msg: string, source: openArray[byte]) =
encodedWithSnappy == encodedWithNimStreams
snappy.decode(encodedWithSnappy) == source
- cpp_snappy.decode(encodedWithSnappy) == source
+ # cpp_snappy.decode(encodedWithSnappy) == source
snappy.decode(encodedWithFastStreams) == source
- cpp_snappy.decode(encodedWithFastStreams) == source
+ # cpp_snappy.decode(encodedWithFastStreams) == source
snappy.decode(encodedWithNimStreams) == source
- cpp_snappy.decode(encodedWithNimStreams) == source
+ # cpp_snappy.decode(encodedWithNimStreams) == source
- snappy.decode(encodedWithCpp) == source
- cpp_snappy.decode(encodedWithCpp) == source
+ # snappy.decode(encodedWithCpp) == source
+ # cpp_snappy.decode(encodedWithCpp) == source
proc roundTripRev(msg: string, source: openArray[byte]) =
- var
- decoded = snappy.decode(source)
- outputSize: csize_t = 0
- ok = snappy_uncompressed_length(cast[cstring](source[0].unsafeAddr), source.len.csize_t, outputSize) == 0
- cpp_decoded = cpp_snappy.decode(source)
+ # var
+ # decoded = snappy.decode(source)
+ # outputSize: csize_t = 0
+ # ok = snappy_uncompressed_length(cast[cstring](source[0].unsafeAddr), source.len.csize_t, outputSize) == 0
+ # cpp_decoded = cpp_snappy.decode(source)
check:
- decoded == cpp_decoded
+ true
proc roundTripRev(msg: string, sourceName: string) =
var src = readSource(sourceName)
@@ -165,57 +165,63 @@ suite "snappy":
check encoded[0] == byte(0)
# Decompress fewer bytes than the header reports.
- badData "\x05\x00a"
+ # badData "\x05\x00a"
# A varint that overflows u64.
- badData "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00"
+ # badData "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00"
- # A varint that fits in u64 but overflows u32.
- badData "\x80\x80\x80\x80\x10"
+ # # A varint that fits in u64 but overflows u32.
+ let input = "\xd7\xbb\x81\x80\x80\x00"
+ let (lenU32, bytesRead) = fromBytes(uint32, input.toBytes, Leb128)
+ echo "lenU32 ", lenU32
+ echo "bytesRead ", bytesRead
- # A literal whose length is too small.
- # Since the literal length is 1, 'h' is read as a literal and 'i' is
- # interpreted as a copy 1 operation missing its offset byte.
- badData "\x02\x00hi"
+ echo uncompressedLen(input.toBytes)
+ # 215 187 129 128 128 0
- # A literal whose length is too big.
- badData "\x02\xechi"
+ # # A literal whose length is too small.
+ # # Since the literal length is 1, 'h' is read as a literal and 'i' is
+ # # interpreted as a copy 1 operation missing its offset byte.
+ # badData "\x02\x00hi"
- # A literal whose length is too big, requires 1 extra byte to be read, and
- # src is too short to read that byte.
- badData "\x02\xf0hi"
+ # # A literal whose length is too big.
+ # badData "\x02\xechi"
- # A literal whose length is too big, requires 1 extra byte to be read,
- # src is too short to read the full literal.
- badData "\x02\xf0hi\x00\x00\x00"
+ # # A literal whose length is too big, requires 1 extra byte to be read, and
+ # # src is too short to read that byte.
+ # badData "\x02\xf0hi"
- # A copy 1 operation that stops at the tag byte. This fails because there's
- # no byte to read for the copy offset.
- badData "\x02\x00a\x01"
+ # # A literal whose length is too big, requires 1 extra byte to be read,
+ # # src is too short to read the full literal.
+ # badData "\x02\xf0hi\x00\x00\x00"
- # A copy 2 operation that stops at the tag byte and another copy 2 operation
- # that stops after the first byte in the offset.
- badData "\x11\x00a\x3e"
- badData "\x11\x00a\x3e\x01"
+ # # A copy 1 operation that stops at the tag byte. This fails because there's
+ # # no byte to read for the copy offset.
+ # badData "\x02\x00a\x01"
- # Same as copy 2, but for copy 4.
- badData "\x11\x00a\x3f"
- badData "\x11\x00a\x3f\x00"
- badData "\x11\x00a\x3f\x00\x00"
- badData "\x11\x00a\x3f\x00\x00\x00"
+ # # A copy 2 operation that stops at the tag byte and another copy 2 operation
+ # # that stops after the first byte in the offset.
+ # badData "\x11\x00a\x3e"
+ # badData "\x11\x00a\x3e\x01"
- # A copy operation whose offset is zero.
- badData "\x11\x00a\x01\x00"
+ # # Same as copy 2, but for copy 4.
+ # badData "\x11\x00a\x3f"
+ # badData "\x11\x00a\x3f\x00"
+ # badData "\x11\x00a\x3f\x00\x00"
+ # badData "\x11\x00a\x3f\x00\x00\x00"
- # A copy operation whose offset is too big.
- badData "\x11\x00a\x01\xFF"
+ # # A copy operation whose offset is zero.
+ # badData "\x11\x00a\x01\x00"
- # A copy operation whose length is too big.
- badData "\x05\x00a\x1d\x01"
+ # # A copy operation whose offset is too big.
+ # badData "\x11\x00a\x01\xFF"
- badData "\x11\x00\x00\xfc\xfe\xff\xff\xff"
+ # # A copy operation whose length is too big.
+ # badData "\x05\x00a\x1d\x01"
- badData "\x11\x00\x00\xfc\xff\xff\xff\xff"
+ # badData "\x11\x00\x00\xfc\xfe\xff\xff\xff"
+
+ # badData "\x11\x00\x00\xfc\xff\xff\xff\xff"
test "random data":
# Selected random inputs pulled from quickcheck failure witnesses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment