Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 25, 2024
1 parent 3d320d8 commit ed1c1d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
9 changes: 5 additions & 4 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
return err
}

err = b.seedSnapshot()
if err != nil {
return err
}
// TODO: the following is only used for snap sync, uncomment when we need it again
// err = b.seedSnapshot()
// if err != nil {
// return err
// }

return nil
}
Expand Down
114 changes: 57 additions & 57 deletions blockchain/snap_server_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package blockchain
import (
"context"
"errors"
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
Expand All @@ -14,6 +13,7 @@ import (

const MaxSnapshots = 128

//nolint:unused
type snapshotRecord struct {
stateRoot *felt.Felt
contractsRoot *felt.Felt
Expand Down Expand Up @@ -102,62 +102,62 @@ func (b *Blockchain) GetDClasses(felts []*felt.Felt) ([]*core.DeclaredClass, err
return classes, nil
}

func (b *Blockchain) seedSnapshot() error {
headheader, err := b.HeadsHeader()
if err != nil {
return err
}

stateR, srCloser, err := b.HeadState()
if err != nil {
return err
}

defer func() { _ = srCloser() }()

state := stateR.(*core.State)
contractsRoot, classRoot, err := state.StateAndClassRoot()
if err != nil {
return err
}

stateRoot, err := state.Root()
if err != nil {
return err
}

txn, closer, err := b.database.PersistedView()
if err != nil {
return err
}

dbsnap := snapshotRecord{
stateRoot: stateRoot,
contractsRoot: contractsRoot,
classRoot: classRoot,
blockHash: headheader.Hash,
txn: txn,
closer: closer,
}

fmt.Printf("Snapshot %d %s %s\n", headheader.Number, headheader.GlobalStateRoot, stateRoot)

// TODO: Reorgs
b.snapshots = append(b.snapshots, &dbsnap)
if len(b.snapshots) > MaxSnapshots {
toremove := b.snapshots[0]
err = toremove.closer()
if err != nil {
return err
}

// TODO: I think internally, it keep the old array.
// maybe the append copy it to a new array, who knows...
b.snapshots = b.snapshots[1:]
}

return nil
}
// func (b *Blockchain) seedSnapshot() error {
// headheader, err := b.HeadsHeader()
// if err != nil {
// return err
// }

// stateR, srCloser, err := b.HeadState()
// if err != nil {
// return err
// }

// defer func() { _ = srCloser() }()

// state := stateR.(*core.State)
// contractsRoot, classRoot, err := state.StateAndClassRoot()
// if err != nil {
// return err
// }

// stateRoot, err := state.Root()
// if err != nil {
// return err
// }

// txn, closer, err := b.database.PersistedView()
// if err != nil {
// return err
// }

// dbsnap := snapshotRecord{
// stateRoot: stateRoot,
// contractsRoot: contractsRoot,
// classRoot: classRoot,
// blockHash: headheader.Hash,
// txn: txn,
// closer: closer,
// }

// fmt.Printf("Snapshot %d %s %s\n", headheader.Number, headheader.GlobalStateRoot, stateRoot)

// // TODO: Reorgs
// b.snapshots = append(b.snapshots, &dbsnap)
// if len(b.snapshots) > MaxSnapshots {
// toremove := b.snapshots[0]
// err = toremove.closer()
// if err != nil {
// return err
// }

// // TODO: I think internally, it keep the old array.
// // maybe the append copy it to a new array, who knows...
// b.snapshots = b.snapshots[1:]
// }

// return nil
// }

func (b *Blockchain) Close() {
for _, snapshot := range b.snapshots {
Expand Down

0 comments on commit ed1c1d0

Please sign in to comment.