Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve evm event log sorting #2118

Closed
corverroos opened this issue Oct 10, 2024 · 2 comments
Closed

Improve evm event log sorting #2118

corverroos opened this issue Oct 10, 2024 · 2 comments
Labels
network-upgrade Resolving this issue breaks consensus

Comments

@corverroos
Copy link
Collaborator

Problem to Solve

We current do the following sorting of evm event logs in octane:

		topicI := slices.Concat(events[i].Topics...)
		topicJ := slices.Concat(events[j].Topics...)
		if cmp := bytes.Compare(topicI, topicJ); cmp != 0 {
			return cmp < 0
		}

The spearbit audit raised that this could be replaced with:

if cmp := slices.CompareFunc(events[i].Topics, events[j].Topics, bytes.Compare); cmp != 0 {
	return cmp < 0
}

The suggestion is simpler and more performant, but introduces slightly different logic in some edge cases.

Proposed Solution

Replace this logic during the next network upgrade. Bumping evmengine consensus version by 1.

@corverroos corverroos added the network-upgrade Resolving this issue breaks consensus label Oct 10, 2024
@chmllr
Copy link
Contributor

chmllr commented Oct 10, 2024

Here's one example where the functions produce different results:

package main

import (
	"bytes"
	"fmt"
	"slices"
)

func compareConcat(a, b [][]byte) bool {
	concatA := slices.Concat(a...)
	concatB := slices.Concat(b...)
	if cmp := bytes.Compare(concatA, concatB); cmp != 0 {
		return cmp < 0
	}
	return false
}

func compareFunc(a, b [][]byte) bool {
	if cmp := slices.CompareFunc(a, b, bytes.Compare); cmp != 0 {
		return cmp < 0
	}
	return false
}

func main() {
	slicesOfBytes := []struct {
		a, b [][]byte
	}{
		// same result
		{[][]byte{[]byte("a")}, [][]byte{[]byte("b"), []byte("c")}},
		// different results
		{[][]byte{[]byte("a")}, [][]byte{[]byte(""), []byte("bc")}},
	}

	fmt.Println("Comparing slices with Concat method:")
	for _, pair := range slicesOfBytes {
		fmt.Printf("compareConcat(%v, %v) = %v\n", pair.a, pair.b, compareConcat(pair.a, pair.b))
	}

	for _, pair := range slicesOfBytes {
		fmt.Printf("compareFunc(%v, %v) = %v\n", pair.a, pair.b, compareFunc(pair.a, pair.b))
	}
}

@corverroos
Copy link
Collaborator Author

Actually, sorting events in a different order than original isn't valid, see piplabs/story#301. Closing in favour of #2106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
network-upgrade Resolving this issue breaks consensus
Projects
None yet
Development

No branches or pull requests

2 participants