Skip to content

Commit

Permalink
Ashraf Basic snap algo .. Did something + #1925
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored and weiihann committed Oct 25, 2024
1 parent 8ca9307 commit 35f6d09
Show file tree
Hide file tree
Showing 35 changed files with 5,337 additions and 39 deletions.
67 changes: 65 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,28 @@ type Blockchain struct {
network *utils.Network
database db.DB

snapshots []*snapshotRecord

listener EventListener

cachedPending atomic.Pointer[Pending]
}

func New(database db.DB, network *utils.Network) *Blockchain {
RegisterCoreTypesToEncoder()
return &Blockchain{
bc := &Blockchain{
database: database,
network: network,
listener: &SelectiveListener{},
}

// TODO: Used only for testing though...
err := bc.seedSnapshot()
if err != nil {
fmt.Printf("Error seeding snapshot %s", err)
}

return bc
}

func (b *Blockchain) WithListener(listener EventListener) *Blockchain {
Expand Down Expand Up @@ -336,7 +346,7 @@ func (b *Blockchain) SetL1Head(update *core.L1Head) error {
func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommitments,
stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class,
) error {
return b.database.Update(func(txn db.Transaction) error {
err := b.database.Update(func(txn db.Transaction) error {
if err := verifyBlock(txn, block); err != nil {
return err
}
Expand Down Expand Up @@ -372,6 +382,49 @@ 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
}

err = b.seedSnapshot()
if err != nil {
return err
}

return nil
}

func (b *Blockchain) StoreRaw(blockNumber uint64, stateDiff *core.StateDiff) error {
return b.database.Update(func(txn db.Transaction) error {
return core.NewState(txn).UpdateNoVerify(blockNumber, stateDiff, make(map[felt.Felt]core.Class))
})
}

func (b *Blockchain) PutClasses(blockNumber uint64, classHashes map[felt.Felt]*felt.Felt, newClasses map[felt.Felt]core.Class) error {
return b.database.Update(func(txn db.Transaction) error {
v1ClassHashes := map[felt.Felt]*felt.Felt{}
for ch, class := range newClasses {
if class.Version() == 1 {
v1ClassHashes[ch] = classHashes[ch]
}
}

return core.NewState(txn).UpdateNoVerify(blockNumber, &core.StateDiff{
DeclaredV1Classes: v1ClassHashes,
}, newClasses)
})
}

func (b *Blockchain) PutContracts(address, nonces, classHash []*felt.Felt) error {
return b.database.Update(func(txn db.Transaction) error {
return core.NewState(txn).UpdateContractNoLog(address, nonces, classHash)
})
}

func (b *Blockchain) PutStorage(storage map[felt.Felt]map[felt.Felt]*felt.Felt) error {
return b.database.Update(func(txn db.Transaction) error {
return core.NewState(txn).UpdateContractStorages(storage)
})
}

// VerifyBlock assumes the block has already been sanity-checked.
Expand Down Expand Up @@ -769,6 +822,16 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
return core.NewState(txn), txn.Discard, nil
}

func (b *Blockchain) HeadStateFreakingState() (*core.State, StateCloser, error) {
b.listener.OnRead("HeadState")
txn, err := b.database.NewTransaction(false)
if err != nil {
return nil, nil, err
}

return core.NewState(txn), txn.Discard, nil
}

// StateAtBlockNumber returns a StateReader that provides a stable view to the state at the given block number
func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error) {
b.listener.OnRead("StateAtBlockNumber")
Expand Down
19 changes: 19 additions & 0 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestNew(t *testing.T) {
gw := adaptfeeder.New(client)
t.Run("empty blockchain's head is nil", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()
assert.Equal(t, &utils.Mainnet, chain.Network())
b, err := chain.Head()
assert.Nil(t, b)
Expand All @@ -42,8 +43,10 @@ func TestNew(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, &utils.Mainnet)
assert.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))
chain.Close()

chain = blockchain.New(testDB, &utils.Mainnet)
defer chain.Close()
b, err := chain.Head()
require.NoError(t, err)
assert.Equal(t, block0, b)
Expand All @@ -55,6 +58,7 @@ func TestHeight(t *testing.T) {
gw := adaptfeeder.New(client)
t.Run("return nil if blockchain is empty", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Sepolia)
defer chain.Close()
_, err := chain.Height()
assert.Error(t, err)
})
Expand All @@ -68,16 +72,19 @@ func TestHeight(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, &utils.Mainnet)
assert.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))
chain.Close()

chain = blockchain.New(testDB, &utils.Mainnet)
height, err := chain.Height()
require.NoError(t, err)
assert.Equal(t, block0.Number, height)
chain.Close()
})
}

func TestBlockByNumberAndHash(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Sepolia)
defer chain.Close()
t.Run("same block is returned for both GetBlockByNumber and GetBlockByHash", func(t *testing.T) {
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -114,6 +121,7 @@ func TestVerifyBlock(t *testing.T) {
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()

t.Run("error if chain is empty and incoming block number is not 0", func(t *testing.T) {
block := &core.Block{Header: &core.Header{Number: 10}}
Expand Down Expand Up @@ -176,6 +184,7 @@ func TestSanityCheckNewHeight(t *testing.T) {
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()

client := feeder.NewTestClient(t, &utils.Mainnet)

Expand Down Expand Up @@ -221,6 +230,7 @@ func TestStore(t *testing.T) {

t.Run("add block to empty blockchain", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()
require.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))

headBlock, err := chain.Head()
Expand All @@ -247,6 +257,7 @@ func TestStore(t *testing.T) {
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()
require.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))
require.NoError(t, chain.Store(block1, &emptyCommitments, stateUpdate1, nil))

Expand All @@ -270,6 +281,7 @@ func TestStore(t *testing.T) {

func TestBlockCommitments(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)

Expand All @@ -295,6 +307,7 @@ func TestBlockCommitments(t *testing.T) {

func TestTransactionAndReceipt(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet)
defer chain.Close()

client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -383,6 +396,7 @@ func TestTransactionAndReceipt(t *testing.T) {
func TestState(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, &utils.Mainnet)
defer chain.Close()

client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -446,6 +460,7 @@ func TestState(t *testing.T) {
func TestEvents(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, &utils.Goerli2)
defer chain.Close()

client := feeder.NewTestClient(t, &utils.Goerli2)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -565,6 +580,7 @@ func TestEvents(t *testing.T) {
func TestRevert(t *testing.T) {
testdb := pebble.NewMemTest(t)
chain := blockchain.New(testdb, &utils.Mainnet)
defer chain.Close()

client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -653,13 +669,15 @@ func TestL1Update(t *testing.T) {
got, err := chain.L1Head()
require.NoError(t, err)
assert.Equal(t, head, got)
chain.Close()
})
}
}

func TestPending(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, &utils.Mainnet)
defer chain.Close()
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)

Expand Down Expand Up @@ -801,6 +819,7 @@ func TestPending(t *testing.T) {
func TestStorePendingIncludesNumber(t *testing.T) {
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), &network)
defer chain.Close()

// Store pending genesis.
require.NoError(t, chain.StorePending(&blockchain.Pending{
Expand Down
Loading

0 comments on commit 35f6d09

Please sign in to comment.