Skip to content

Commit

Permalink
Add iteration and shouldContinu from yield & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Aug 6, 2024
1 parent 26786c0 commit a0c2826
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 253 deletions.
1 change: 0 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
heightBin := core.MarshalBlockNumber(block.Number)
return txn.Set(db.ChainHeight.Key(), heightBin)
})

if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions blockchain/snap_server_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockchain
import (
"errors"
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
Expand All @@ -23,7 +24,6 @@ func (b *Blockchain) GetStateForStateRoot(stateRoot *felt.Felt) (*core.State, er
snapshot, err := b.findSnapshotMatching(func(record *snapshotRecord) bool {
return record.stateRoot.Equal(stateRoot)
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +66,6 @@ func (b *Blockchain) GetClasses(felts []*felt.Felt) ([]core.Class, error) {

return nil
})

if err != nil {
return nil, err
}
Expand All @@ -91,7 +90,6 @@ func (b *Blockchain) GetDClasses(felts []*felt.Felt) ([]*core.DeclaredClass, err

return nil
})

if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion core/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package core

import (
"errors"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/core/trie"
"github.com/NethermindEth/juno/db"
)

// contract storage has fixed height at 251
const ContractStorageTrieHeight = 251
const (
GlobalTrieHeight = 251
ContractStorageTrieHeight = 251
)

var (
ErrContractNotDeployed = errors.New("contract not deployed")
Expand Down
25 changes: 1 addition & 24 deletions core/crypto/pedersen_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"github.com/NethermindEth/juno/core/felt"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
pedersenhash "github.com/consensys/gnark-crypto/ecc/stark-curve/pedersen-hash"
lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

// PedersenArray implements [Pedersen array hashing].
Expand All @@ -17,32 +14,12 @@ func PedersenArray(elems ...*felt.Felt) *felt.Felt {
return digest.Update(elems...).Finish()
}

var lruPederson, _ = lru.New(100000000)

var pedersonCache = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "juno_pederson",
Help: "pederson",
}, []string{"hit"})

// Pedersen implements the [Pedersen hash].
//
// [Pedersen hash]: https://docs.starknet.io/documentation/develop/Hashing/hash-functions/#pedersen_hash
func Pedersen(a, b *felt.Felt) *felt.Felt {
key := lruKey{
x: *a, y: *b,
}

res, ok := lruPederson.Get(key)
if ok {
pedersonCache.WithLabelValues("true").Inc()
return res.(*felt.Felt).Clone()
}

hash := pedersenhash.Pedersen(a.Impl(), b.Impl())
result := felt.NewFelt(&hash)
lruPederson.Add(key, result.Clone())
pedersonCache.WithLabelValues("false").Inc()
return result
return felt.NewFelt(&hash)
}

var _ Digest = (*PedersenDigest)(nil)
Expand Down
29 changes: 1 addition & 28 deletions core/crypto/poseidon_hash.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package crypto

import (
lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"sync"

"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -56,37 +53,13 @@ func HadesPermutation(state []felt.Felt) {

var two = new(felt.Felt).SetUint64(2)

var lruPoseidon, _ = lru.New(10000000)
var poseidonCache = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "juno_poseidon",
Help: "pederson",
}, []string{"hit"})

type lruKey struct {
x felt.Felt
y felt.Felt
}

// Poseidon implements the [Poseidon hash].
//
// [Poseidon hash]: https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#poseidon_hash
func Poseidon(x, y *felt.Felt) *felt.Felt {
key := lruKey{
x: *x, y: *y,
}

res, ok := lruPoseidon.Get(key)
if ok {
poseidonCache.WithLabelValues("true").Inc()
return res.(*felt.Felt).Clone()
}

state := []felt.Felt{*x, *y, *two}
HadesPermutation(state)
result := new(felt.Felt).Set(&state[0])
lruPoseidon.Add(key, result.Clone())
poseidonCache.WithLabelValues("false").Inc()
return result
return new(felt.Felt).Set(&state[0])
}

var one = new(felt.Felt).SetUint64(1)
Expand Down
5 changes: 3 additions & 2 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/binary"
"errors"
"fmt"
"runtime"
"sort"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/core/trie"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/encoder"
"github.com/sourcegraph/conc/pool"
"runtime"
"sort"
)

const globalTrieHeight = 251
Expand Down
5 changes: 3 additions & 2 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package trie
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"math/big"
"strings"
"sync"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type hashFunc func(*felt.Felt, *felt.Felt) *felt.Felt
Expand Down Expand Up @@ -750,6 +750,7 @@ func (t *Trie) Iterate(startValue *felt.Felt, consumer func(key, value *felt.Fel
return t.doIterate(&startKey, t.rootKey, consumer)
}

// doIterate returns false if the end of the trie is reached, true otherwise
func (t *Trie) doIterate(startKey, key *Key, consumer func(key, value *felt.Felt) (bool, error)) (bool, error) {
if key == nil {
return false, nil
Expand Down
Loading

0 comments on commit a0c2826

Please sign in to comment.