Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed May 16, 2024
1 parent 16cb86b commit 4a0dc1b
Show file tree
Hide file tree
Showing 3 changed files with 9,199 additions and 10 deletions.
41 changes: 34 additions & 7 deletions builder/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/builder"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/mempool"
"github.com/NethermindEth/juno/mocks"
Expand All @@ -29,13 +30,14 @@ func TestBootstrapChain(t *testing.T) {
client := feeder.NewTestClient(t, network)
gw := adaptfeeder.New(client)
bc := blockchain.New(testDB, network)
bc.StoreGenesis(core.EmptyStateDiff(), nil)

Check failure on line 33 in builder/bootstrap_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `bc.StoreGenesis` is not checked (errcheck)

seqAddr := utils.HexToFelt(t, "0xDEADBEEF")
privKey, err := ecdsa.GenerateKey(rand.Reader)
require.NoError(t, err)
p := mempool.New(pebble.NewMemTest(t))

testBuilder := builder.New(privKey, seqAddr, bc, mockVM, time.Second*5, p,
testBuilder := builder.New(privKey, seqAddr, bc, mockVM, time.Second*1, p,
utils.NewNopZapLogger()).WithBootstrapToBlock(2).WithStarknetData(snData).WithBootstrap(true)

block0, err := gw.BlockByNumber(context.Background(), 0)
Expand All @@ -48,19 +50,44 @@ func TestBootstrapChain(t *testing.T) {
classHash1 := utils.HexToFelt(t, "0xd0e183745e9dae3e4e78a8ffedcce0903fc4900beace4e0abf192d4c202da3")
class1, err := gw.Class(context.Background(), classHash1)
require.NoError(t, err)

err = bc.StorePending(&blockchain.Pending{
Block: nil,
StateUpdate: nil,
})
classHash2 := utils.HexToFelt(t, "0x1b661756bf7d16210fc611626e1af4569baa1781ffc964bd018f4585ae241c1")
class2, err := gw.Class(context.Background(), classHash2)
require.NoError(t, err)
t.Run("successfully bootstrap Sepolias first two block", func(t *testing.T) {

t.Run("successfully bootstrap Sepolias first two block", func(t *testing.T) {
require.NoError(t, err)
snData.EXPECT().BlockByNumber(context.Background(), uint64(0)).Return(block0, nil)
snData.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return(block1, nil)
snData.EXPECT().Class(context.Background(), classHash0).Return(class0, nil)
snData.EXPECT().Class(context.Background(), classHash1).Return(class1, nil)
snData.EXPECT().Class(context.Background(), classHash2).Return(class2, nil)

// ctx, cancel := context.WithCancel(context.Background())

Check failure on line 65 in builder/bootstrap_test.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// go func() {
// time.Sleep(3 * time.Second)
// fmt.Println("waking up")
// cancel()
// }()
// err = testBuilder.Run(ctx)
// require.NoError(t, err)

if err := testBuilder.InitPendingBlock(); err != nil {

Check failure on line 74 in builder/bootstrap_test.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 36 (govet)
panic(err)
}
defer func() {
if pErr := testBuilder.ClearPending(); pErr != nil {
panic(pErr)
}
}()

// doneListen := make(chan struct{})

Check failure on line 83 in builder/bootstrap_test.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// go func() {
// if pErr := b.listenPool(ctx); pErr != nil {
// b.log.Errorw("listening pool", "err", pErr)
// }
// close(doneListen)
// }()

assert.NoError(t, testBuilder.BootstrapChain(), "can't bootstrap if the network isn't specified", err.Error())
})
}
6 changes: 3 additions & 3 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (b *Builder) Run(ctx context.Context) error {
return err
}
defer func() {
if pErr := b.clearPending(); pErr != nil {
if pErr := b.ClearPending(); pErr != nil {
b.log.Errorw("clearing pending", "err", pErr)
}
}()
Expand Down Expand Up @@ -146,7 +146,7 @@ func (b *Builder) InitPendingBlock() error {
return err
}

func (b *Builder) clearPending() error {
func (b *Builder) ClearPending() error {
b.pendingBlock = blockchain.Pending{}
if b.headState != nil {
if err := b.headCloser(); err != nil {
Expand All @@ -170,7 +170,7 @@ func (b *Builder) Finalise() error {
b.pendingBlock.Block.Hash.ShortString(), "state", b.pendingBlock.Block.GlobalStateRoot.ShortString())
b.listener.OnBlockFinalised(b.pendingBlock.Block.Header)

if err := b.clearPending(); err != nil {
if err := b.ClearPending(); err != nil {
return err
}
return b.InitPendingBlock()
Expand Down
Loading

0 comments on commit 4a0dc1b

Please sign in to comment.