Skip to content

Commit

Permalink
implement juno sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jul 17, 2024
1 parent 6e90f2e commit cb3bf13
Show file tree
Hide file tree
Showing 41 changed files with 22,377 additions and 332 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ courtney/
__pycache__/
config/
.envrc
seq-db/

# pyenv
.python-version
Expand Down
39 changes: 32 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,37 @@ node3:
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9093

sequencer:
./build/juno \
--http \
--http-port=6060 \
--http-host=0.0.0.0 \
--db-path=./seq-db \
--log-level=debug \
--seq-enable \
--seq-block-time=1 \
--network sequencer \
--rpc-call-max-steps=4123000

sequencer-with-accounts:
./build/juno \
--http \
--http-port=6060 \
--http-host=0.0.0.0 \
--db-path=./seq-db \
--log-level=debug \
--seq-enable \
--seq-block-time=1 \
--network sequencer \
--genesis-file "./genesis/genesis_prefund_accounts.json" \
--rpc-call-max-steps=4123000

pathfinder: juno-cached
./build/juno \
--network=sepolia \
--log-level=debug \
--db-path=./p2p-dbs/node-pathfinder \
--p2p \
--p2p-peers=/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWF1JrZWQoBiBSjsFSuLbDiDvqcmJQRLaFQLmpVkHA9duk \
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9094
--network=sepolia \
--log-level=debug \
--db-path=./p2p-dbs/node-pathfinder \
--p2p \
--p2p-peers=/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWF1JrZWQoBiBSjsFSuLbDiDvqcmJQRLaFQLmpVkHA9duk \
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9094
159 changes: 130 additions & 29 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,40 +337,51 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class,
) error {
return b.database.Update(func(txn db.Transaction) error {
if err := verifyBlock(txn, block); err != nil {
if err := b.storeBlock(txn, block, blockCommitments); err != nil {
return err
}
if err := core.NewState(txn).Update(block.Number, stateUpdate, newClasses); err != nil {

state := core.NewState(txn)
if err := state.Update(block.Number, stateUpdate.StateDiff, newClasses); err != nil {
return err
}
if err := StoreBlockHeader(txn, block.Header); err != nil {

if err := b.verifyStateUpdateRoot(state, stateUpdate.NewRoot); err != nil {
return err
}

for i, tx := range block.Transactions {
if err := storeTransactionAndReceipt(txn, block.Number, uint64(i), tx,
block.Receipts[i]); err != nil {
return err
}
}
return storeStateUpdate(txn, block.Number, stateUpdate)
})
}

if err := storeStateUpdate(txn, block.Number, stateUpdate); err != nil {
return err
}
func (b *Blockchain) storeBlock(txn db.Transaction, block *core.Block, blockCommitments *core.BlockCommitments) error {
if err := verifyBlock(txn, block); err != nil {
return err
}

if err := StoreBlockCommitments(txn, block.Number, blockCommitments); err != nil {
return err
}
if err := StoreBlockHeader(txn, block.Header); err != nil {
return err

Check warning on line 363 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L363

Added line #L363 was not covered by tests
}

if err := b.storeEmptyPending(txn, block.Header); err != nil {
for i, tx := range block.Transactions {
if err := storeTransactionAndReceipt(txn, block.Number, uint64(i), tx,
block.Receipts[i]); err != nil {
return err
}
}

// Head of the blockchain is maintained as follows:
// [db.ChainHeight]() -> (BlockNumber)
heightBin := core.MarshalBlockNumber(block.Number)
return txn.Set(db.ChainHeight.Key(), heightBin)
})
if err := StoreBlockCommitments(txn, block.Number, blockCommitments); err != nil {
return err

Check warning on line 374 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L374

Added line #L374 was not covered by tests
}

if err := b.storeEmptyPending(txn, block.Header); err != nil {
return err

Check warning on line 378 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L378

Added line #L378 was not covered by tests
}

// Head of the blockchain is maintained as follows:
// [db.ChainHeight]() -> (BlockNumber)
heightBin := core.MarshalBlockNumber(block.Number)
return txn.Set(db.ChainHeight.Key(), heightBin)
}

// VerifyBlock assumes the block has already been sanity-checked.
Expand Down Expand Up @@ -514,7 +525,7 @@ func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transa
return nil, err
}

var txs []core.Transaction
txs := []core.Transaction{}
numBytes := core.MarshalBlockNumber(number)

prefix := db.TransactionsByBlockNumberAndIndex.Key(numBytes)
Expand Down Expand Up @@ -549,7 +560,7 @@ func receiptsByBlockNumber(txn db.Transaction, number uint64) ([]*core.Transacti
return nil, err
}

var receipts []*core.TransactionReceipt
receipts := []*core.TransactionReceipt{}
numBytes := core.MarshalBlockNumber(number)

prefix := db.ReceiptsByBlockNumberAndIndex.Key(numBytes)
Expand Down Expand Up @@ -760,11 +771,6 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
return nil, nil, err
}

_, err = chainHeight(txn)
if err != nil {
return nil, nil, utils.RunAndWrapOnError(txn.Discard, err)
}

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

Expand Down Expand Up @@ -841,7 +847,11 @@ func (b *Blockchain) revertHead(txn db.Transaction) error {

state := core.NewState(txn)
// revert state
if err = state.Revert(blockNumber, stateUpdate); err != nil {
if err = state.Revert(blockNumber, stateUpdate.StateDiff); err != nil {
return err

Check warning on line 851 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L851

Added line #L851 was not covered by tests
}

if err = b.verifyStateUpdateRoot(state, stateUpdate.OldRoot); err != nil {
return err
}

Expand Down Expand Up @@ -1072,3 +1082,94 @@ func MakeStateDiffForEmptyBlock(bc Reader, blockNumber uint64) (*core.StateDiff,
}
return stateDiff, nil
}

func (b *Blockchain) verifyStateUpdateRoot(s *core.State, root *felt.Felt) error {
currentRoot, err := s.Root()
if err != nil {
return err

Check warning on line 1089 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1089

Added line #L1089 was not covered by tests
}

if !root.Equal(currentRoot) {
return fmt.Errorf("state's current root: %s does not match the expected root: %s", currentRoot, root)
}
return nil
}

type BlockSignFunc func(blockHash, stateDiffCommitment *felt.Felt) ([]*felt.Felt, error)

// Finalise will calculate the state commitment and block hash for the given pending block and append it to the
// blockchain
func (b *Blockchain) Finalise(pending *Pending, sign BlockSignFunc) error {
return b.database.Update(func(txn db.Transaction) error {
var err error

state := core.NewState(txn)
pending.StateUpdate.OldRoot, err = state.Root()
if err != nil {
return err

Check warning on line 1109 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1109

Added line #L1109 was not covered by tests
}

if h, hErr := chainHeight(txn); hErr == nil {
pending.Block.Number = h + 1
}

if err = state.Update(pending.Block.Number, pending.StateUpdate.StateDiff, pending.NewClasses); err != nil {
return err

Check warning on line 1117 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1117

Added line #L1117 was not covered by tests
}

pending.Block.GlobalStateRoot, err = state.Root()
if err != nil {
return err

Check warning on line 1122 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1122

Added line #L1122 was not covered by tests
}
pending.StateUpdate.NewRoot = pending.Block.GlobalStateRoot

var commitments *core.BlockCommitments
pending.Block.Hash, commitments, err = core.BlockHash(pending.Block)
if err != nil {
return err

Check warning on line 1129 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1129

Added line #L1129 was not covered by tests
}
pending.StateUpdate.BlockHash = pending.Block.Hash

pending.Block.Signatures = [][]*felt.Felt{}
var sig []*felt.Felt
sig, err = sign(pending.Block.Hash, pending.StateUpdate.StateDiff.Commitment())
if err != nil {
return err

Check warning on line 1137 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1137

Added line #L1137 was not covered by tests
}
if sig != nil {
pending.Block.Signatures = append(pending.Block.Signatures, sig)
}

if err = b.storeBlock(txn, pending.Block, commitments); err != nil {
return err

Check warning on line 1144 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L1144

Added line #L1144 was not covered by tests
}

return storeStateUpdate(txn, pending.Block.Number, pending.StateUpdate)
})
}

func (b *Blockchain) StoreGenesis(diff *core.StateDiff, classes map[felt.Felt]core.Class) error {
receipts := make([]*core.TransactionReceipt, 0)
pendingGenesis := Pending{
Block: &core.Block{
Header: &core.Header{
ParentHash: &felt.Zero,
Number: 0,
SequencerAddress: &felt.Zero,
EventsBloom: core.EventsBloom(receipts),
GasPrice: &felt.Zero,
GasPriceSTRK: &felt.Zero,
},
Transactions: make([]core.Transaction, 0),
Receipts: receipts,
},
StateUpdate: &core.StateUpdate{
OldRoot: &felt.Zero,
StateDiff: diff,
},
NewClasses: classes,
}
return b.Finalise(&pendingGenesis, func(_, _ *felt.Felt) ([]*felt.Felt, error) {
return nil, nil
})
}
Loading

0 comments on commit cb3bf13

Please sign in to comment.