From 22044027c9fd18233a0b2c77080db3697d97fbb9 Mon Sep 17 00:00:00 2001 From: IronGauntlets Date: Fri, 20 Dec 2024 19:39:56 +0000 Subject: [PATCH 1/4] Update feeder test Client Sever to accept Testing.TB This allows benchmarks to use a feeder test client such that blocks, transactions, events, etc. can be used in benchmarking code. --- clients/feeder/feeder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/feeder/feeder.go b/clients/feeder/feeder.go index 82e99390ad..010adee3bc 100644 --- a/clients/feeder/feeder.go +++ b/clients/feeder/feeder.go @@ -92,7 +92,7 @@ func NopBackoff(d time.Duration) time.Duration { } // NewTestClient returns a client and a function to close a test server. -func NewTestClient(t *testing.T, network *utils.Network) *Client { +func NewTestClient(t testing.TB, network *utils.Network) *Client { srv := newTestServer(t, network) t.Cleanup(srv.Close) ua := "Juno/v0.0.1-test Starknet Implementation" @@ -117,7 +117,7 @@ func NewTestClient(t *testing.T, network *utils.Network) *Client { return c } -func newTestServer(t *testing.T, network *utils.Network) *httptest.Server { +func newTestServer(t testing.TB, network *utils.Network) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { queryMap, err := url.ParseQuery(r.URL.RawQuery) if err != nil { From 7fb2ac4b117f90708f4525e6b7d5c0ade3972705 Mon Sep 17 00:00:00 2001 From: IronGauntlets Date: Fri, 20 Dec 2024 19:41:15 +0000 Subject: [PATCH 2/4] Fix typo --- core/crypto/poseidon_hash.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/crypto/poseidon_hash.go b/core/crypto/poseidon_hash.go index eaf9e1f5c2..96665bf502 100644 --- a/core/crypto/poseidon_hash.go +++ b/core/crypto/poseidon_hash.go @@ -113,7 +113,7 @@ func setRoundKeys() { } } -var _ Digest = (*PedersenDigest)(nil) +var _ Digest = (*PoseidonDigest)(nil) type PoseidonDigest struct { state [3]felt.Felt From 21bf8a771bd2d94598de92914fe307560bdf7756 Mon Sep 17 00:00:00 2001 From: IronGauntlets Date: Mon, 23 Dec 2024 17:45:43 +0000 Subject: [PATCH 3/4] Rename NewFelt()->New() and return a Felt --- core/crypto/pedersen_hash.go | 6 ++++-- core/felt/felt.go | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/crypto/pedersen_hash.go b/core/crypto/pedersen_hash.go index 583d557cb9..b36a2ca6af 100644 --- a/core/crypto/pedersen_hash.go +++ b/core/crypto/pedersen_hash.go @@ -19,7 +19,8 @@ func PedersenArray(elems ...*felt.Felt) *felt.Felt { // [Pedersen hash]: https://docs.starknet.io/architecture-and-concepts/cryptography/hash-functions/#pedersen_hash func Pedersen(a, b *felt.Felt) *felt.Felt { hash := pedersenhash.Pedersen(a.Impl(), b.Impl()) - return felt.NewFelt(&hash) + f := felt.New(hash) + return &f } var _ Digest = (*PedersenDigest)(nil) @@ -39,5 +40,6 @@ func (d *PedersenDigest) Update(elems ...*felt.Felt) Digest { func (d *PedersenDigest) Finish() *felt.Felt { d.digest = pedersenhash.Pedersen(&d.digest, new(fp.Element).SetUint64(d.count)) - return felt.NewFelt(&d.digest) + f := felt.New(d.digest) + return &f } diff --git a/core/felt/felt.go b/core/felt/felt.go index 284e7a064b..73859f84fc 100644 --- a/core/felt/felt.go +++ b/core/felt/felt.go @@ -14,9 +14,9 @@ type Felt struct { val fp.Element } -func NewFelt(element *fp.Element) *Felt { - return &Felt{ - val: *element, +func New(element fp.Element) Felt { + return Felt{ + val: element, } } From c860c2337d4def5b8c39878a6dcf4801b989eacd Mon Sep 17 00:00:00 2001 From: IronGauntlets Date: Mon, 23 Dec 2024 18:00:25 +0000 Subject: [PATCH 4/4] Add BenchmarkStateDiffHash() --- core/state_update_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/state_update_test.go b/core/state_update_test.go index 74fac50699..a6b1505fb3 100644 --- a/core/state_update_test.go +++ b/core/state_update_test.go @@ -79,6 +79,18 @@ func TestStateDiffHash(t *testing.T) { } } +func BenchmarkStateDiffHash(b *testing.B) { + client := feeder.NewTestClient(b, &utils.SepoliaIntegration) + gw := adaptfeeder.New(client) + su, err := gw.StateUpdate(context.Background(), 38748) + require.NoError(b, err) + + b.ResetTimer() + for n := 0; n < b.N; n++ { + su.StateDiff.Hash() + } +} + func TestStateDiffLength(t *testing.T) { client := feeder.NewTestClient(t, &utils.Sepolia) gw := adaptfeeder.New(client)