Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJeremyHe committed Dec 17, 2024
1 parent bfeb7f6 commit dd3f7ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
25 changes: 14 additions & 11 deletions arbnode/espresso_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,46 +78,49 @@ func validateIfPayloadIsInBlock(p []byte, payloads []espressoTypes.Bytes) bool {
return validated
}

func ParsePayload(rawPayload []byte) (signature []byte, indices []uint64, messages [][]byte, err error) {
if len(rawPayload) < LEN_SIZE {
func ParseHotShotPayload(payload []byte) (signature []byte, indices []uint64, messages [][]byte, err error) {
if len(payload) < LEN_SIZE {
return nil, nil, nil, errors.New("payload too short to parse signature size")
}

// Extract the signature size
signatureSize := binary.BigEndian.Uint64(rawPayload[:LEN_SIZE])
signatureSize := binary.BigEndian.Uint64(payload[:LEN_SIZE])
currentPos := LEN_SIZE

if len(rawPayload[currentPos:]) < int(signatureSize) {
if len(payload[currentPos:]) < int(signatureSize) {
return nil, nil, nil, errors.New("payload too short for signature")
}

// Extract the signature
signature = rawPayload[currentPos : currentPos+int(signatureSize)]
signature = payload[currentPos : currentPos+int(signatureSize)]
currentPos += int(signatureSize)

indices = []uint64{}
messages = [][]byte{}

// Parse messages
for {
if len(rawPayload[currentPos:]) < LEN_SIZE+INDEX_SIZE {
break // No more messages to parse
if currentPos == len(payload)-1 {
break
}
if len(payload[currentPos:]) < LEN_SIZE+INDEX_SIZE {
return nil, nil, nil, errors.New("message size mismatch")
}

// Extract the index
index := binary.BigEndian.Uint64(rawPayload[currentPos : currentPos+INDEX_SIZE])
index := binary.BigEndian.Uint64(payload[currentPos : currentPos+INDEX_SIZE])
currentPos += INDEX_SIZE

// Extract the message size
messageSize := binary.BigEndian.Uint64(rawPayload[currentPos : currentPos+LEN_SIZE])
messageSize := binary.BigEndian.Uint64(payload[currentPos : currentPos+LEN_SIZE])
currentPos += LEN_SIZE

if len(rawPayload[currentPos:]) < int(messageSize) {
if len(payload[currentPos:]) < int(messageSize) {
return nil, nil, nil, errors.New("message size mismatch")
}

// Extract the message
message := rawPayload[currentPos : currentPos+int(messageSize)]
message := payload[currentPos : currentPos+int(messageSize)]
currentPos += int(messageSize)

indices = append(indices, index)
Expand Down
8 changes: 2 additions & 6 deletions arbnode/espresso_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestParsePayload(t *testing.T) {
}

// Parse the signed payload
signature, indices, messages, err := ParsePayload(signedPayload)
signature, indices, messages, err := ParseHotShotPayload(signedPayload)
if err != nil {
t.Fatalf("failed to parse payload: %v", err)
}
Expand Down Expand Up @@ -98,10 +98,6 @@ func TestParsePayloadInvalidCases(t *testing.T) {
description: "Empty payload",
payload: []byte{},
},
{
description: "Signature size exceeds payload",
payload: append(make([]byte, 8), []byte("short")...),
},
{
description: "Message size exceeds remaining payload",
payload: func() []byte {
Expand All @@ -119,7 +115,7 @@ func TestParsePayloadInvalidCases(t *testing.T) {

for _, tc := range invalidPayloads {
t.Run(tc.description, func(t *testing.T) {
_, _, _, err := ParsePayload(tc.payload)
_, _, _, err := ParseHotShotPayload(tc.payload)
if err == nil {
t.Errorf("expected error for case '%s', but got none", tc.description)
}
Expand Down

0 comments on commit dd3f7ef

Please sign in to comment.