-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ashraf Basic snap algo .. Did something + #1925
This is a combination of 24 commits.
- Loading branch information
Showing
27 changed files
with
5,302 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package blockchain | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/NethermindEth/juno/core" | ||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/db" | ||
) | ||
|
||
type snapshotRecord struct { | ||
stateRoot *felt.Felt | ||
contractsRoot *felt.Felt | ||
classRoot *felt.Felt | ||
blockHash *felt.Felt | ||
txn db.Transaction | ||
closer func() error | ||
} | ||
|
||
var ErrMissingSnapshot = errors.New("missing snapshot") | ||
|
||
func (b *Blockchain) GetStateForStateRoot(stateRoot *felt.Felt) (*core.State, error) { | ||
snapshot, err := b.findSnapshotMatching(func(record *snapshotRecord) bool { | ||
return record.stateRoot.Equal(stateRoot) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s := core.NewState(snapshot.txn) | ||
|
||
return s, nil | ||
} | ||
|
||
func (b *Blockchain) findSnapshotMatching(filter func(record *snapshotRecord) bool) (*snapshotRecord, error) { | ||
var snapshot *snapshotRecord | ||
for _, record := range b.snapshots { | ||
if filter(record) { | ||
snapshot = record | ||
break | ||
} | ||
} | ||
|
||
if snapshot == nil { | ||
return nil, ErrMissingSnapshot | ||
} | ||
|
||
return snapshot, nil | ||
} | ||
|
||
func (b *Blockchain) GetClasses(felts []*felt.Felt) ([]core.Class, error) { | ||
classes := make([]core.Class, len(felts)) | ||
err := b.database.View(func(txn db.Transaction) error { | ||
state := core.NewState(txn) | ||
for i, f := range felts { | ||
d, err := state.Class(f) | ||
if err != nil && !errors.Is(err, db.ErrKeyNotFound) { | ||
return err | ||
} else if errors.Is(err, db.ErrKeyNotFound) { | ||
classes[i] = nil | ||
} else { | ||
classes[i] = d.Class | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return classes, nil | ||
} | ||
|
||
func (b *Blockchain) GetDClasses(felts []*felt.Felt) ([]*core.DeclaredClass, error) { | ||
classes := make([]*core.DeclaredClass, len(felts)) | ||
err := b.database.View(func(txn db.Transaction) error { | ||
state := core.NewState(txn) | ||
for i, f := range felts { | ||
d, err := state.Class(f) | ||
if err != nil && !errors.Is(err, db.ErrKeyNotFound) { | ||
return err | ||
} else if errors.Is(err, db.ErrKeyNotFound) { | ||
classes[i] = nil | ||
} else { | ||
classes[i] = d | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return classes, nil | ||
} | ||
|
||
func (b *Blockchain) seedSnapshot() error { | ||
headheader, err := b.HeadsHeader() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
state, scloser, err := b.HeadState() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer scloser() | ||
|
||
stateS := state.(*core.State) | ||
contractsRoot, theclassroot, err := stateS.StateAndClassRoot() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
thestateroot, err := stateS.Root() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
txn, closer, err := b.database.PersistedView() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dbsnap := snapshotRecord{ | ||
stateRoot: thestateroot, | ||
contractsRoot: contractsRoot, | ||
classRoot: theclassroot, | ||
blockHash: headheader.Hash, | ||
txn: txn, | ||
closer: closer, | ||
} | ||
|
||
fmt.Printf("Snapshot %d %s %s\n", headheader.Number, headheader.GlobalStateRoot, thestateroot) | ||
|
||
// TODO: Reorgs | ||
b.snapshots = append(b.snapshots, &dbsnap) | ||
if len(b.snapshots) > 128 { | ||
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 { | ||
snapshot.closer() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.