Skip to content

Commit eea8a4b

Browse files
author
rian
committed
bootstrap wip - pendidng state doens't seem to be written to
1 parent 4a0dc1b commit eea8a4b

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

builder/bootstrap.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/NethermindEth/juno/core"
9+
"github.com/NethermindEth/juno/db"
910
"github.com/NethermindEth/juno/mempool"
1011
)
1112

@@ -22,12 +23,21 @@ func (b *Builder) BootstrapChain() error {
2223
}
2324

2425
for _, txn := range txns {
26+
fmt.Println("--push", txn.Transaction.Hash())
2527
b.pool.Push(txn)
2628
}
29+
if err := b.depletePool(context.Background()); err != nil {
30+
if !errors.Is(err, db.ErrKeyNotFound) {
31+
return err
32+
}
33+
}
34+
fmt.Println("depletedPool")
2735
if err := b.Finalise(); err != nil {
2836
return err
2937
}
30-
b.log.Debugw(fmt.Sprintf("Bootstrapped block %d ", i))
38+
fmt.Println("Finalised")
39+
// fmt.Println(fmt.Sprintf("Bootstrapped block %d, %d", i, len(txns)))
40+
// b.log.Debugw(fmt.Sprintf("Bootstrapped block %d ", i))
3141
}
3242
return nil
3343
}

builder/bootstrap_test.go

+23-31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package builder_test
33
import (
44
"context"
55
"crypto/rand"
6+
"encoding/json"
7+
"fmt"
68
"testing"
79
"time"
810

@@ -15,29 +17,30 @@ import (
1517
"github.com/NethermindEth/juno/mocks"
1618
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
1719
"github.com/NethermindEth/juno/utils"
20+
"github.com/NethermindEth/juno/vm"
1821
"github.com/consensys/gnark-crypto/ecc/stark-curve/ecdsa"
19-
"github.com/stretchr/testify/assert"
2022
"github.com/stretchr/testify/require"
2123
"go.uber.org/mock/gomock"
2224
)
2325

2426
func TestBootstrapChain(t *testing.T) {
2527
testDB := pebble.NewMemTest(t)
2628
mockCtrl := gomock.NewController(t)
27-
mockVM := mocks.NewMockVM(mockCtrl)
29+
// mockVM := mocks.NewMockVM(mockCtrl)
30+
vm := vm.New(utils.NewNopZapLogger())
2831
snData := mocks.NewMockStarknetData(mockCtrl)
2932
network := &utils.Sepolia
3033
client := feeder.NewTestClient(t, network)
3134
gw := adaptfeeder.New(client)
3235
bc := blockchain.New(testDB, network)
3336
bc.StoreGenesis(core.EmptyStateDiff(), nil)
3437

35-
seqAddr := utils.HexToFelt(t, "0xDEADBEEF")
38+
seqAddr := utils.HexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8")
3639
privKey, err := ecdsa.GenerateKey(rand.Reader)
3740
require.NoError(t, err)
3841
p := mempool.New(pebble.NewMemTest(t))
3942

40-
testBuilder := builder.New(privKey, seqAddr, bc, mockVM, time.Second*1, p,
43+
testBuilder := builder.New(privKey, seqAddr, bc, vm, time.Second*1, p,
4144
utils.NewNopZapLogger()).WithBootstrapToBlock(2).WithStarknetData(snData).WithBootstrap(true)
4245

4346
block0, err := gw.BlockByNumber(context.Background(), 0)
@@ -54,40 +57,29 @@ func TestBootstrapChain(t *testing.T) {
5457
class2, err := gw.Class(context.Background(), classHash2)
5558
require.NoError(t, err)
5659

57-
t.Run("successfully bootstrap Sepolias first two block", func(t *testing.T) {
60+
t.Run("successfully bootcstrap Sepolias first two block", func(t *testing.T) {
5861
require.NoError(t, err)
5962
snData.EXPECT().BlockByNumber(context.Background(), uint64(0)).Return(block0, nil)
6063
snData.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return(block1, nil)
6164
snData.EXPECT().Class(context.Background(), classHash0).Return(class0, nil)
6265
snData.EXPECT().Class(context.Background(), classHash1).Return(class1, nil)
6366
snData.EXPECT().Class(context.Background(), classHash2).Return(class2, nil)
64-
67+
// mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(),
68+
// gomock.Any(), gomock.Any(), false, false, false, false).Return(
69+
// []*felt.Felt{&felt.Zero}, []*felt.Felt{}, []vm.TransactionTrace{{}}, executionErr,
70+
// )
6571
// ctx, cancel := context.WithCancel(context.Background())
66-
// go func() {
67-
// time.Sleep(3 * time.Second)
68-
// fmt.Println("waking up")
69-
// cancel()
70-
// }()
71-
// err = testBuilder.Run(ctx)
72-
// require.NoError(t, err)
73-
74-
if err := testBuilder.InitPendingBlock(); err != nil {
75-
panic(err)
76-
}
77-
defer func() {
78-
if pErr := testBuilder.ClearPending(); pErr != nil {
79-
panic(pErr)
80-
}
81-
}()
82-
83-
// doneListen := make(chan struct{})
84-
// go func() {
85-
// if pErr := b.listenPool(ctx); pErr != nil {
86-
// b.log.Errorw("listening pool", "err", pErr)
87-
// }
88-
// close(doneListen)
89-
// }()
72+
// cancel()
73+
err = testBuilder.Run(context.Background())
74+
require.NoError(t, err)
9075

91-
assert.NoError(t, testBuilder.BootstrapChain(), "can't bootstrap if the network isn't specified", err.Error())
76+
head, err := bc.BlockByNumber(0)
77+
require.NoError(t, err)
78+
qwe, err := json.MarshalIndent(head, "", "")
79+
fmt.Println(string(qwe))
80+
require.NoError(t, err)
81+
require.Equal(t, uint64(1), head.Number)
82+
require.Equal(t, block0.TransactionCount, head.TransactionCount, "TransactionCount diff")
83+
require.Equal(t, block0.GlobalStateRoot.String(), head.GlobalStateRoot.String(), "GlobalStateRoot diff")
9284
})
9385
}

builder/builder.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package builder
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
stdsync "sync"
78
"time"
89

@@ -96,26 +97,31 @@ func (b *Builder) Run(ctx context.Context) error {
9697
}
9798
}()
9899

99-
doneListen := make(chan struct{})
100-
go func() {
101-
if pErr := b.listenPool(ctx); pErr != nil {
102-
b.log.Errorw("listening pool", "err", pErr)
103-
}
104-
close(doneListen)
105-
}()
100+
// doneListen := make(chan struct{})
101+
// go func() {
102+
// if pErr := b.listenPool(ctx); pErr != nil {
103+
// b.log.Errorw("listening pool", "err", pErr)
104+
// }
105+
// close(doneListen)
106+
// }()
106107

107108
if b.bootstrap {
109+
fmt.Println("bootstrap")
108110
b.log.Debugw("Starting process to bootstrap the Sequencer")
109111
if err := b.BootstrapChain(); err != nil {
110112
return err
111113
}
112114
b.log.Debugw("Finished process to bootstrap the Sequencer")
115+
var cancel context.CancelFunc
116+
ctx, cancel = context.WithCancel(ctx)
117+
cancel()
118+
// close(doneListen)
113119
}
114120

115121
for {
116122
select {
117123
case <-ctx.Done():
118-
<-doneListen
124+
// <-doneListen
119125
return nil
120126
case <-time.After(b.blockTime):
121127
b.log.Debugw("Finalising new block")
@@ -166,6 +172,8 @@ func (b *Builder) Finalise() error {
166172
if err := b.bc.Finalise(&b.pendingBlock, b.Sign); err != nil {
167173
return err
168174
}
175+
fmt.Sprintln("Finalised block", "number", b.pendingBlock.Block.Number, "hash",
176+
b.pendingBlock.Block.Hash.ShortString(), "state", b.pendingBlock.Block.GlobalStateRoot.ShortString())
169177
b.log.Infow("Finalised block", "number", b.pendingBlock.Block.Number, "hash",
170178
b.pendingBlock.Block.Hash.ShortString(), "state", b.pendingBlock.Block.GlobalStateRoot.ShortString())
171179
b.listener.OnBlockFinalised(b.pendingBlock.Block.Header)
@@ -262,11 +270,18 @@ func Receipt(fee *felt.Felt, feeUnit core.FeeUnit, txHash *felt.Felt, trace *vm.
262270

263271
func (b *Builder) listenPool(ctx context.Context) error {
264272
for {
273+
// select {
274+
// case <-ctx.Done():
275+
// fmt.Println("listenPool done")
276+
// return nil
277+
// default:
278+
fmt.Println("listenPool bdepletePool ")
265279
if err := b.depletePool(ctx); err != nil {
266280
if !errors.Is(err, db.ErrKeyNotFound) {
267281
return err
268282
}
269283
}
284+
// }
270285

271286
select {
272287
case <-ctx.Done():
@@ -280,6 +295,9 @@ func (b *Builder) listenPool(ctx context.Context) error {
280295
func (b *Builder) depletePool(ctx context.Context) error {
281296
for {
282297
userTxn, err := b.pool.Pop()
298+
fmt.Println("userTxn", userTxn.Transaction.Hash(), err)
299+
fmt.Println("b.pendingBlock.Block.Transactions", len(b.pendingBlock.Block.Transactions))
300+
fmt.Println("b.pendingBlock.Block.DeployedContracts", b.pendingBlock.StateUpdate.StateDiff.DeployedContracts)
283301
if err != nil {
284302
return err
285303
}
@@ -290,7 +308,7 @@ func (b *Builder) depletePool(ctx context.Context) error {
290308
if !errors.As(err, &txnExecutionError) {
291309
return err
292310
}
293-
311+
fmt.Println("\n\nrunTxn err", err, "\n\n.")
294312
b.log.Debugw("failed txn", "hash", userTxn.Transaction.Hash().String(), "err", err.Error())
295313
}
296314

@@ -323,6 +341,8 @@ func (b *Builder) runTxn(txn *mempool.BroadcastedTransaction) error {
323341
}
324342
fee, _, trace, err := b.vm.Execute([]core.Transaction{txn.Transaction}, classes, []*felt.Felt{}, blockInfo, state,
325343
b.bc.Network(), false, false, false, false)
344+
fmt.Println("execute", fee, trace, err)
345+
fmt.Println("execute", state)
326346
if err != nil {
327347
return err
328348
}

0 commit comments

Comments
 (0)