Skip to content

Commit 4c699c8

Browse files
committed
feat: minimal set package
1 parent 3996f8a commit 4c699c8

File tree

5 files changed

+122
-11
lines changed

5 files changed

+122
-11
lines changed

core/vm/contracts.libevm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import (
2020
"fmt"
2121
"math/big"
2222

23-
set "github.com/hashicorp/go-set/v3"
2423
"github.com/holiman/uint256"
2524
"golang.org/x/exp/slog"
2625

2726
"github.com/ava-labs/libevm/common"
2827
"github.com/ava-labs/libevm/core/types"
2928
"github.com/ava-labs/libevm/libevm"
29+
"github.com/ava-labs/libevm/libevm/set"
3030
"github.com/ava-labs/libevm/log"
3131
"github.com/ava-labs/libevm/params"
3232
)
@@ -41,15 +41,15 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
4141
log.Debug(
4242
"Overriding active precompiles",
4343
"Added", log.Lazy(func() slog.Value {
44-
diff := set.From(active).Difference(set.From(orig))
44+
diff := set.From(active...).Sub(set.From(orig...))
4545
return slog.AnyValue(diff.Slice())
4646
}),
4747
"Removed", log.Lazy(func() slog.Value {
48-
diff := set.From(orig).Difference(set.From(active))
48+
diff := set.From(orig...).Sub(set.From(active...))
4949
return slog.AnyValue(diff.Slice())
5050
}),
5151
"Unchanged", log.Lazy(func() slog.Value {
52-
both := set.From(active).Intersect(set.From(orig))
52+
both := set.From(active...).Intersect(set.From(orig...))
5353
return slog.AnyValue(both.Slice())
5454
}),
5555
)

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ require (
3232
github.com/golang-jwt/jwt/v4 v4.5.0
3333
github.com/golang/protobuf v1.5.3
3434
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
35-
github.com/google/go-cmp v0.6.0
35+
github.com/google/go-cmp v0.5.9
3636
github.com/google/gofuzz v1.2.0
3737
github.com/google/uuid v1.3.0
3838
github.com/gorilla/websocket v1.4.2
3939
github.com/graph-gophers/graphql-go v1.3.0
4040
github.com/hashicorp/go-bexpr v0.1.10
41-
github.com/hashicorp/go-set/v3 v3.0.0
4241
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4
4342
github.com/holiman/bloomfilter/v2 v2.0.3
4443
github.com/holiman/uint256 v1.2.4

go.sum

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
294294
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
295295
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
296296
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
297-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
298-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
297+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
298+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
299299
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
300300
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
301301
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
@@ -334,8 +334,6 @@ github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrj
334334
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
335335
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
336336
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
337-
github.com/hashicorp/go-set/v3 v3.0.0 h1:CaJBQvQCOWoftrBcDt7Nwgo0kdpmrKxar/x2o6pV9JA=
338-
github.com/hashicorp/go-set/v3 v3.0.0/go.mod h1:IEghM2MpE5IaNvL+D7X480dfNtxjRXZ6VMpK3C8s2ok=
339337
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
340338
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
341339
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -549,7 +547,6 @@ github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZ
549547
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
550548
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
551549
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
552-
github.com/shoenig/test v1.11.0 h1:NoPa5GIoBwuqzIviCrnUJa+t5Xb4xi5Z+zODJnIDsEQ=
553550
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
554551
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
555552
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=

libevm/set/set.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
// Package set provides a generic implementation of a set.
18+
package set
19+
20+
// A Set is a generic set implementation.
21+
type Set[T comparable] map[T]struct{}
22+
23+
// From returns a Set containing the elements.
24+
func From[T comparable](elements ...T) Set[T] {
25+
s := make(Set[T], len(elements))
26+
for _, e := range elements {
27+
s[e] = struct{}{}
28+
}
29+
return s
30+
}
31+
32+
// Sub returns the elements in `s` that aren't in `t`.
33+
func (s Set[T]) Sub(t Set[T]) Set[T] {
34+
return s.alsoIn(t, false)
35+
}
36+
37+
// Intersect returns the intersection of `s` and `t`.
38+
func (s Set[T]) Intersect(t Set[T]) Set[T] {
39+
return s.alsoIn(t, true)
40+
}
41+
42+
func (s Set[T]) alsoIn(t Set[T], inBoth bool) Set[T] {
43+
res := make(Set[T])
44+
for el := range s {
45+
if _, ok := t[el]; ok == inBoth {
46+
res[el] = struct{}{}
47+
}
48+
}
49+
return res
50+
}
51+
52+
// Slice returns the elements of `s` as a slice.
53+
func (s Set[T]) Slice() []T {
54+
sl := make([]T, 0, len(s))
55+
for el := range s {
56+
sl = append(sl, el)
57+
}
58+
return sl
59+
}

libevm/set/set_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package set
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestSub(t *testing.T) {
26+
for _, tt := range [][3][]int{ // start, sub, want
27+
{{}, {}, {}},
28+
{{0}, {}, {0}},
29+
{{}, {0}, {}},
30+
{{0, 1}, {0}, {1}},
31+
{{0, 1}, {1}, {0}},
32+
} {
33+
in, sub := tt[0], tt[1]
34+
want := tt[2]
35+
got := From(in...).Sub(From(sub...)).Slice()
36+
assert.Equalf(t, want, got, "Set(%v).Sub(%v)", in, sub)
37+
}
38+
}
39+
40+
func TestIntersect(t *testing.T) {
41+
for _, tt := range [][3][]int{ // L, R, intersection
42+
{{}, {}, {}},
43+
{{0}, {}, {}},
44+
{{0}, {0}, {0}},
45+
{{0, 1}, {0}, {0}},
46+
{{0, 1}, {1}, {1}},
47+
} {
48+
want := tt[2]
49+
50+
for i := 0; i <= 1; i++ { // commutativity
51+
lhs, rhs := tt[i], tt[1-i]
52+
got := From(lhs...).Intersect(From(rhs...)).Slice()
53+
assert.Equalf(t, want, got, "Set(%v).Intersect(%v)", lhs, rhs)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)