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 55dbca7
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 235 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
41 changes: 7 additions & 34 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 @@ -45,7 +42,7 @@ func round(state []felt.Felt, full bool, index int) {
mixLayer(state)
}

func HadesPermutation(state []felt.Felt) {
func hadesPermutation(state []felt.Felt) {
initialiseRoundKeys.Do(setRoundKeys)
totalRounds := fullRounds + partialRounds
for i := 0; i < totalRounds; i++ {
Expand All @@ -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
hadesPermutation(state)
return new(felt.Felt).Set(&state[0])
}

var one = new(felt.Felt).SetUint64(1)
Expand All @@ -104,15 +77,15 @@ func PoseidonArray(elems ...*felt.Felt) *felt.Felt {
for i := 0; i < len(elems)/2; i++ {
state[0].Add(&state[0], elems[2*i])
state[1].Add(&state[1], elems[2*i+1])
HadesPermutation(state)
hadesPermutation(state)
}

rem := len(elems) % 2
if rem == 1 {
state[0].Add(&state[0], elems[len(elems)-1])
}
state[rem].Add(&state[rem], one)
HadesPermutation(state)
hadesPermutation(state)

return new(felt.Felt).Set(&state[0])
}
Expand Down Expand Up @@ -154,7 +127,7 @@ func (d *PoseidonDigest) Update(elems ...*felt.Felt) Digest {
} else {
d.state[0].Add(&d.state[0], d.lastElem)
d.state[1].Add(&d.state[1], elems[idx])
HadesPermutation(d.state[:])
hadesPermutation(d.state[:])
d.lastElem = nil
}
}
Expand All @@ -168,7 +141,7 @@ func (d *PoseidonDigest) Finish() *felt.Felt {
d.state[0].Add(&d.state[0], d.lastElem)
d.state[1].Add(&d.state[1], one)
}
HadesPermutation(d.state[:])
hadesPermutation(d.state[:])
return &d.state[0]
}

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
6 changes: 4 additions & 2 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package trie
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"math/big"
"strings"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
Expand Down Expand Up @@ -752,6 +753,7 @@ func (t *Trie) Iterate(startValue *felt.Felt, consumer func(key, value *felt.Fel

func (t *Trie) doIterate(startKey, key *Key, consumer func(key, value *felt.Felt) (bool, error)) (bool, error) {
if key == nil {
// shouldn't it be finished == true here?
return false, nil
}

Expand Down
Loading

0 comments on commit 55dbca7

Please sign in to comment.