Skip to content

Commit

Permalink
bootstrap- wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed May 15, 2024
1 parent 0f24541 commit d3bda45
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
60 changes: 60 additions & 0 deletions builder/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package builder

import (
"context"
"errors"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/mempool"
)

// bootstrap submits transactions inside a block directly into the mempool
// this allows us to bootstrap the network with unsupported transactions
func (b *Builder) bootstrapChain() error {
if b.starknetData == nil {
return errors.New("can't bootstrap if the network isn't specified")
}
for i := 0; i < int(b.bootstrapToBlock); i++ {
txns, err := getTxns(b, uint64(i))
if err != nil {
return err
}
for _, txn := range txns {
b.pool.Push(txn)

Check failure on line 23 in builder/bootstrap.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `b.pool.Push` is not checked (errcheck)
}
if err := b.Finalise(); err != nil {
return err
}
}
return nil
}

func getTxns(builder *Builder, blockNumber uint64) ([]*mempool.BroadcastedTransaction, error) {
var mempoolTxns []*mempool.BroadcastedTransaction
block, err := builder.starknetData.BlockByNumber(context.Background(), blockNumber)
if err != nil {
return nil, err
}
txns := block.Transactions
for _, txn := range txns {
switch t := txn.(type) {
case *core.DeployTransaction, *core.DeployAccountTransaction, *core.InvokeTransaction, *core.L1HandlerTransaction:
mempoolTxns = append(mempoolTxns,
&mempool.BroadcastedTransaction{
Transaction: t,
})
case *core.DeclareTransaction:
class, err := builder.starknetData.Class(context.Background(), t.ClassHash)
if err != nil {
return nil, err
}
mempoolTxns = append(mempoolTxns, &mempool.BroadcastedTransaction{
Transaction: t,
DeclaredClass: class,
})
default:
return nil, errors.New("unknown transaction")
}
}
return mempoolTxns, nil
}
21 changes: 21 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/NethermindEth/juno/feed"
"github.com/NethermindEth/juno/mempool"
"github.com/NethermindEth/juno/service"
"github.com/NethermindEth/juno/starknetdata"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
Expand Down Expand Up @@ -42,6 +43,10 @@ type Builder struct {
pendingBlock blockchain.Pending
headState core.StateReader
headCloser blockchain.StateCloser

bootstrap bool
bootstrapToBlock uint64
starknetData starknetdata.StarknetData // To bootstrap sequencer
}

func New(privKey *ecdsa.PrivateKey, ownAddr *felt.Felt, bc *blockchain.Blockchain, builderVM vm.VM,
Expand All @@ -66,6 +71,16 @@ func (b *Builder) WithEventListener(l EventListener) *Builder {
return b
}

func (b *Builder) WithBootstrap(bootstrap bool) *Builder {
b.bootstrap = bootstrap
return b
}

func (b *Builder) WithStarknetData(starknetData starknetdata.StarknetData) *Builder {
b.starknetData = starknetData
return b
}

func (b *Builder) Run(ctx context.Context) error {
if err := b.initPendingBlock(); err != nil {
return err
Expand All @@ -84,6 +99,12 @@ func (b *Builder) Run(ctx context.Context) error {
close(doneListen)
}()

if b.bootstrap {
if err := b.bootstrapChain(); err != nil {
return err
}
}

for {
select {
case <-ctx.Done():
Expand Down
12 changes: 7 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,24 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen

nodeVM := vm.New(log)
throttledVM := NewThrottledVM(nodeVM, cfg.MaxVMs, int32(cfg.MaxVMQueue))
client := feeder.NewClient(cfg.Network.FeederURL).WithUserAgent(ua).WithLogger(log).
WithTimeout(cfg.GatewayTimeout).WithAPIKey(cfg.GatewayAPIKey)
starknetData := adaptfeeder.New(client)
var rpcHandler *rpc.Handler
if cfg.Sequencer {
pKey, kErr := ecdsa.GenerateKey(rand.Reader)
if kErr != nil {
return nil, kErr
}

poolDB, _ := pebble.NewMem()
p := mempool.New(poolDB)
sequencer := builder.New(pKey, new(felt.Felt).SetUint64(1337), chain, nodeVM, time.Second*time.Duration(cfg.SeqBlockTime), p, log) //nolint: gomnd
sequencer := builder.New(pKey, new(felt.Felt).SetUint64(1337), chain, nodeVM, time.Second*time.Duration(cfg.SeqBlockTime), p, //nolint: gomnd

Check failure on line 165 in node/node.go

View workflow job for this annotation

GitHub Actions / lint

line is 143 characters (lll)
log).WithBootstrap(true).WithStarknetData(starknetData)
rpcHandler = rpc.New(chain, sequencer, throttledVM, version, &cfg.Network, log).WithMempool(p)
services = append(services, sequencer)
} else {

Check failure on line 169 in node/node.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)
client := feeder.NewClient(cfg.Network.FeederURL).WithUserAgent(ua).WithLogger(log).
WithTimeout(cfg.GatewayTimeout).WithAPIKey(cfg.GatewayAPIKey)
synchronizer := sync.New(chain, adaptfeeder.New(client), log, cfg.PendingPollInterval, dbIsRemote)

synchronizer := sync.New(chain, starknetData, log, cfg.PendingPollInterval, dbIsRemote)
gatewayClient := gateway.NewClient(cfg.Network.GatewayURL, log).WithUserAgent(ua).WithAPIKey(cfg.GatewayAPIKey)

var p2pService *p2p.Service
Expand Down

0 comments on commit d3bda45

Please sign in to comment.