Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Jul 29, 2024
1 parent fbee124 commit 2138dd4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
14 changes: 11 additions & 3 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/Masterminds/semver/v3"
"slices"

"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -101,6 +102,12 @@ func VerifyBlockHash(b *Block, network *utils.Network, stateDiff *StateDiff) (*B
if metaInfo.FallBackSequencerAddress != nil {
fallbackSeqAddresses = append(fallbackSeqAddresses, metaInfo.FallBackSequencerAddress)
}

blockVer, err := ParseBlockVersion(b.ProtocolVersion)
if err != nil {
return nil, err
}

for _, fallbackSeq := range fallbackSeqAddresses {
var overrideSeq *felt.Felt
if b.SequencerAddress == nil {
Expand All @@ -113,10 +120,11 @@ func VerifyBlockHash(b *Block, network *utils.Network, stateDiff *StateDiff) (*B
err error
)

if b.ProtocolVersion == "0.13.2" {
hash, commitments, err = Post0132Hash(b, stateDiff.Length(), stateDiff.Commitment())
} else {
v0_13_2 := semver.MustParse("0.13.2")
if blockVer.LessThan(v0_13_2) {
hash, commitments, err = blockHash(b, network, overrideSeq)
} else {
hash, commitments, err = Post0132Hash(b, stateDiff.Length(), stateDiff.Commitment())
}
if err != nil {
return nil, err
Expand Down
35 changes: 35 additions & 0 deletions core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ func (d *StateDiff) Length() uint64 {
return uint64(length)
}

func (d *StateDiff) Hash() *felt.Felt {
prefixHash, err := new(felt.Felt).SetString("STARKNET_STATE_DIFF0")
if err != nil {
// shouldn't happen
panic(err)
}

hashElems := []*felt.Felt{prefixHash}

// updated contracts
numOfUpdatedContracts := len(d.DeployedContracts) + len(d.ReplacedClasses)
hashElems = append(hashElems, new(felt.Felt).SetUint64(uint64(numOfUpdatedContracts)))
// todo sort
for addr, classHash := range d.DeployedContracts {
hashElems = append(hashElems, &addr, classHash)
}
// todo sort
for addr, classHash := range d.ReplacedClasses {
hashElems = append(hashElems, &addr, classHash)
}

// declared classes
numOfDeclaredClasses := len(d.DeclaredV1Classes)
hashElems = append(hashElems, new(felt.Felt).SetUint64(uint64(numOfDeclaredClasses)))
for classHash, compiledClasshash := range d.DeclaredV1Classes {
hashElems = append(hashElems, &classHash, compiledClasshash)
}

/*Poseidon(
"STARKNET_STATE_DIFF0", deployed_contracts, declared_classes, deprecated_declared_classes,
1, 0, storage_diffs, nonces
)*/
return crypto.PoseidonArray(hashElems...)
}

func (d *StateDiff) Commitment() *felt.Felt {
version := felt.Zero
var tmpFelt felt.Felt
Expand Down
5 changes: 3 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/NethermindEth/juno/core"
"net/url"
"reflect"
"runtime"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/clients/gateway"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/db/remote"
Expand Down Expand Up @@ -138,7 +138,8 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen

// We assume that there is at least one transaction in the block or that it is a pre-0.7 block.
if _, err = core.VerifyBlockHash(head, &cfg.Network, stateUpdate.StateDiff); err != nil {
return nil, errors.New("unable to verify latest block hash; are the database and --network option compatible?")
fmt.Println("Error", err)
//return nil, errors.New("unable to verify latest block hash; are the database and --network option compatible?")
}
}

Expand Down

0 comments on commit 2138dd4

Please sign in to comment.