Skip to content

Commit

Permalink
feat: Fix fetching classes errors and add main package to run it
Browse files Browse the repository at this point in the history
  • Loading branch information
hudem1 committed Feb 11, 2025
1 parent 947405a commit b9c431e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 44 deletions.
13 changes: 13 additions & 0 deletions customsync/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"

sync "github.com/NethermindEth/juno/customsync"
)

func main() {
cs := sync.NewCustomSynchronizer()
cs.Run()

Check failure on line 11 in customsync/main/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `cs.Run` is not checked (errcheck)
fmt.Println(cs)

Check warning on line 12 in customsync/main/main.go

View check run for this annotation

Codecov / codecov/patch

customsync/main/main.go#L9-L12

Added lines #L9 - L12 were not covered by tests
}
118 changes: 74 additions & 44 deletions customsync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,113 @@ package customsync
import (
"context"
"errors"
"fmt"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/starknetdata"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
)

type CustomSynchronizer struct {
blockchain *blockchain.Blockchain
// db db.DB
readOnlyBlockchain bool
blockchain *blockchain.Blockchain
starknetData starknetdata.StarknetData
startingBlockNumber uint64
highestBlockHeader *core.Header
}

// Main entry point into the synchronizer
// Starts the Synchronizer, or returns an error if service is already running
func (cs *CustomSynchronizer) Run(ctx context.Context) error {
cs.syncBlocks(ctx)
func (cs *CustomSynchronizer) Run() error {
cs.syncBlocks()
return nil

Check warning on line 29 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L27-L29

Added lines #L27 - L29 were not covered by tests
}

func (cs *CustomSynchronizer) syncBlocks(ctx context.Context) {
tipHeader, headerErr := cs.blockchain.HeadsHeader()
if headerErr != nil {
func NewCustomSynchronizer() *CustomSynchronizer {
starknetdata := adaptfeeder.New(feeder.NewClient(utils.Sepolia.FeederURL))

db, err := pebble.New("my_custom_db")
if err != nil {
return nil
}
blockchain := blockchain.New(db, &utils.Sepolia, nil)

return &CustomSynchronizer{
blockchain: blockchain,
starknetData: starknetdata,
}

Check warning on line 44 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L32-L44

Added lines #L32 - L44 were not covered by tests
}

func (cs *CustomSynchronizer) syncBlocks() {
height, heightErr := cs.blockchain.Height()
if heightErr != nil && !errors.Is(heightErr, db.ErrKeyNotFound) {
return
}

Check warning on line 51 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L47-L51

Added lines #L47 - L51 were not covered by tests
cs.highestBlockHeader = tipHeader
cs.startingBlockNumber = tipHeader.Number + 1

cs.startingBlockNumber = height

nextBlockToSync := cs.startingBlockNumber
for {
// Get block + state update from Sequencer
stateUpdate, block, err := cs.starknetData.StateUpdateWithBlock(ctx, nextBlockToSync)
if err != nil {
stateUpdate, block, stateUpdateErr := cs.starknetData.StateUpdateWithBlock(context.Background(), nextBlockToSync)
if stateUpdateErr != nil {
continue

Check warning on line 60 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L53-L60

Added lines #L53 - L60 were not covered by tests
}

// Get new classes from Sequencer
newClasses, newClassesErr := cs.fetchNewClasses(ctx, stateUpdate)
newClasses, newClassesErr := cs.fetchNewClasses(stateUpdate)
if newClassesErr != nil {
continue

Check warning on line 66 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}

cs.verifyAndStoreBlockWithStateUpdate(ctx, block, stateUpdate, newClasses)
verifAndStoreErr := cs.verifyAndStoreBlockWithStateUpdate(block, stateUpdate, newClasses)
if verifAndStoreErr != nil {
continue

Check warning on line 71 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L69-L71

Added lines #L69 - L71 were not covered by tests
}

nextBlockToSync++

Check warning on line 74 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L74

Added line #L74 was not covered by tests
}
}

func (cs *CustomSynchronizer) verifyAndStoreBlockWithStateUpdate(ctx context.Context, block *core.Block, stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class) {
func (cs *CustomSynchronizer) verifyAndStoreBlockWithStateUpdate(block *core.Block, stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class) error {

Check failure on line 78 in customsync/sync.go

View workflow job for this annotation

GitHub Actions / lint

The line is 159 characters long, which exceeds the maximum of 140 characters. (lll)
// Verify block integrity
blockCommitments, checkErr := cs.blockchain.SanityCheckNewHeight(block, stateUpdate, newClasses)
if checkErr != nil {
// do smth
blockCommitments, sanityCheckErr := cs.blockchain.SanityCheckNewHeight(block, stateUpdate, newClasses)
if sanityCheckErr != nil {
return sanityCheckErr
}

Check warning on line 83 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L78-L83

Added lines #L78 - L83 were not covered by tests

// Update blockchain database
// Store:
// - newly declared classes, classes trie, new contract in storage, (maybe contractStorageTrie also) & stateTrie,
// - newly declared classes, classes trie, new contract in storage, contractStorageTrie & stateTrie,
// update contracts storages (bc of stateUpdates) + global state trie root,
// - block header (stringified kinda)
// - txs & receipts (stringified kinda)
// - state updates (stringified kinda)
// - block commitments (stringified kinda)
// - l1 handler msg hashes: `handler msg hash` -> `tx hash`
// - block header
// - txs & receipts
// - state updates
// - block commitments
// - l1 handler msg hashes
// - ChainHeight
storeErr := cs.blockchain.Store(block, blockCommitments, stateUpdate, newClasses)
if storeErr != nil {
// do smth
if errors.Is(storeErr, blockchain.ErrParentDoesNotMatchHead) {
revertHeadErr := cs.blockchain.RevertHead()
if revertHeadErr != nil {
// cs.lo
}
}
fmt.Println("Error when trying to store the block")
// revert head here
return storeErr
} else {
fmt.Println("Store block: {'number': ", block.Number, ", 'hash': ", block.Hash.ShortString(), ", 'root': ", block.GlobalStateRoot.ShortString(), "}")

Check failure on line 101 in customsync/sync.go

View workflow job for this annotation

GitHub Actions / lint

The line is 151 characters long, which exceeds the maximum of 140 characters. (lll)
}

Check warning on line 102 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L95-L102

Added lines #L95 - L102 were not covered by tests

return nil

Check warning on line 104 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L104

Added line #L104 was not covered by tests
}

func (cs *CustomSynchronizer) fetchNewClasses(ctx context.Context, stateUpdate *core.StateUpdate) (map[felt.Felt]core.Class, error) {
func (cs *CustomSynchronizer) fetchNewClasses(stateUpdate *core.StateUpdate) (map[felt.Felt]core.Class, error) {
newClasses := make(map[felt.Felt]core.Class)

// Register newly declared v0 classes
for _, v0ClassHash := range stateUpdate.StateDiff.DeclaredV0Classes {
class, classErr := cs.fetchClassIfNotFoundLocally(ctx, v0ClassHash, newClasses)
class, classErr := cs.fetchClassIfNotFoundLocally(v0ClassHash, newClasses)
if classErr != nil {
return nil, classErr
}

Check warning on line 115 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L107-L115

Added lines #L107 - L115 were not covered by tests
Expand All @@ -102,7 +121,7 @@ func (cs *CustomSynchronizer) fetchNewClasses(ctx context.Context, stateUpdate *

// Register newly declared v1 classes
for _, v1ClassHash := range stateUpdate.StateDiff.DeclaredV1Classes {
class, classErr := cs.fetchClassIfNotFoundLocally(ctx, v1ClassHash, newClasses)
class, classErr := cs.fetchClassIfNotFoundLocally(v1ClassHash, newClasses)
if classErr != nil {
return nil, classErr
}

Check warning on line 127 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L123-L127

Added lines #L123 - L127 were not covered by tests
Expand All @@ -115,29 +134,40 @@ func (cs *CustomSynchronizer) fetchNewClasses(ctx context.Context, stateUpdate *
return newClasses, nil

Check warning on line 134 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L134

Added line #L134 was not covered by tests
}

func (cs *CustomSynchronizer) fetchClassIfNotFoundLocally(ctx context.Context, classHash *felt.Felt, newClasses map[felt.Felt]core.Class) (*core.Class, error) {
func (cs *CustomSynchronizer) fetchClassIfNotFoundLocally(classHash *felt.Felt, newClasses map[felt.Felt]core.Class) (*core.Class, error) {

Check failure on line 137 in customsync/sync.go

View workflow job for this annotation

GitHub Actions / lint

ptrToRefParam: consider to make non-pointer type for `*core.Class` (gocritic)
// Make sure class has not already been added
if _, classHashPresent := newClasses[*classHash]; classHashPresent {
return nil, nil
}

Check warning on line 141 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L137-L141

Added lines #L137 - L141 were not covered by tests

// Get latest local blockchain state reader
stateReader, stateCloser, stateErr := cs.blockchain.HeadState()
defer stateCloser()
if stateErr != nil {
return nil, stateErr
if !errors.Is(stateErr, db.ErrKeyNotFound) {
return nil, stateErr
}
stateCloser = func() error {
return nil
}

Check warning on line 151 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L144-L151

Added lines #L144 - L151 were not covered by tests
}
defer func() { _ = stateCloser() }()

Check warning on line 153 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L153

Added line #L153 was not covered by tests

// Get class if already in our local database
declaredClass, classErr := stateReader.Class(classHash)
classErr := db.ErrKeyNotFound
// if DB not empty
if stateReader != nil {
_, classErr = stateReader.Class(classHash)
}

Check warning on line 160 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L156-L160

Added lines #L156 - L160 were not covered by tests

if !errors.Is(classErr, db.ErrKeyNotFound) {
return nil, classErr
}

Check warning on line 164 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L162-L164

Added lines #L162 - L164 were not covered by tests
if classErr == nil {
return &declaredClass.Class, nil

// class is not found locally, fetch it from sequencer
class, fetchedClassErr := cs.starknetData.Class(context.Background(), classHash)
if fetchedClassErr != nil {
return nil, fetchedClassErr
}

Check warning on line 170 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L167-L170

Added lines #L167 - L170 were not covered by tests

// Fetch class from Sequencer
classDef, fetchedClassErr := cs.starknetData.Class(ctx, classHash)
return &classDef, fetchedClassErr
return &class, nil

Check warning on line 172 in customsync/sync.go

View check run for this annotation

Codecov / codecov/patch

customsync/sync.go#L172

Added line #L172 was not covered by tests
}

0 comments on commit b9c431e

Please sign in to comment.