Skip to content

Commit

Permalink
add state root check
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Aug 3, 2024
1 parent fe14d54 commit f7e606f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ type BlockSignFunc func(blockHash, stateDiffCommitment *felt.Felt) ([]*felt.Felt

// 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 {
func (b *Blockchain) Finalise(pending *Pending, sign BlockSignFunc, shadowGlobalStateRoot *felt.Felt) error {
return b.database.Update(func(txn db.Transaction) error {
var err error

Expand All @@ -1121,6 +1121,10 @@ func (b *Blockchain) Finalise(pending *Pending, sign BlockSignFunc) error {
if err != nil {
return err
}
if shadowGlobalStateRoot != nil && !shadowGlobalStateRoot.Equal(pending.Block.GlobalStateRoot) {
return fmt.Errorf("sequencers state root %s != shadow block state root %s",
pending.Block.GlobalStateRoot.String(), shadowGlobalStateRoot.String())
}
pending.StateUpdate.NewRoot = pending.Block.GlobalStateRoot

var commitments *core.BlockCommitments
Expand Down Expand Up @@ -1171,5 +1175,5 @@ func (b *Blockchain) StoreGenesis(diff *core.StateDiff, classes map[felt.Felt]co
}
return b.Finalise(&pendingGenesis, func(_, _ *felt.Felt) ([]*felt.Felt, error) {
return nil, nil
})
}, nil)
}
4 changes: 2 additions & 2 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,15 +914,15 @@ func TestFinalize(t *testing.T) {
return sigs[0], nil
}

require.NoError(t, chain.Finalise(pendingGenesis, sign))
require.NoError(t, chain.Finalise(pendingGenesis, sign, nil))
require.Equal(t, pendingGenesis.Block.Signatures, sigs)
h, err := chain.Head()
require.NoError(t, err)
require.Equal(t, pendingGenesis.Block, h)

pending, err := chain.Pending()
require.NoError(t, err)
require.NoError(t, chain.Finalise(&pending, sign))
require.NoError(t, chain.Finalise(&pending, sign, nil))
require.Equal(t, pendingGenesis.Block.Signatures, sigs)
h, err = chain.Head()
require.NoError(t, err)
Expand Down
16 changes: 8 additions & 8 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ type Builder struct {
headState core.StateReader
headCloser blockchain.StateCloser

shadowMode bool
starknetData starknetdata.StarknetData
chanNumTxnsToShadow chan int
chanFinaliseShadow chan struct{}
shadowMode bool
shadowGlobalStateRoot *felt.Felt
starknetData starknetdata.StarknetData
chanNumTxnsToShadow chan int
chanFinaliseShadow chan struct{}

chanFinalise chan struct{}
chanFinalised chan struct{}
Expand Down Expand Up @@ -206,7 +207,7 @@ func (b *Builder) Finalise() error {
b.pendingLock.Lock()
defer b.pendingLock.Unlock()

if err := b.bc.Finalise(&b.pendingBlock, b.Sign); err != nil {
if err := b.bc.Finalise(&b.pendingBlock, b.Sign, b.shadowGlobalStateRoot); err != nil {
return err
}
b.log.Infow("Finalised block", "number", b.pendingBlock.Block.Number, "hash",
Expand Down Expand Up @@ -517,6 +518,7 @@ func (b *Builder) shadowTxns(ctx context.Context) error {
if err != nil {
return err
}
b.shadowGlobalStateRoot = snHeadBlock.GlobalStateRoot
b.log.Debugw(fmt.Sprintf("Juno head at block %d, Sepolia at block %d, attempting to sequence next block", builderHeadBlock.Number, snHeadBlock.Number))

Check failure on line 522 in builder/builder.go

View workflow job for this annotation

GitHub Actions / lint

line is 153 characters (lll)
if builderHeadBlock.Number < snHeadBlock.Number {
block, _, classes, err := b.getSyncData(builderHeadBlock.Number + 1) // todo: don't need state updates here
Expand All @@ -526,7 +528,7 @@ func (b *Builder) shadowTxns(ctx context.Context) error {
fmt.Println("chanNumTxnsToShadow <- ")
b.chanNumTxnsToShadow <- int(block.TransactionCount)
fmt.Println(" not blocking chanNumTxnsToShadow <- ")
for i, txn := range block.Transactions {
for _, txn := range block.Transactions {
var declaredClass core.Class
declareTxn, ok := txn.(*core.DeclareTransaction)
if ok {
Expand All @@ -540,8 +542,6 @@ func (b *Builder) shadowTxns(ctx context.Context) error {
if err != nil {
return err
}
qwe := declaredClass == nil
b.log.Debugw(fmt.Sprintf("Pushed txn number %d, %v", i, qwe)) // Todo : remove
}

} else {

Check failure on line 547 in builder/builder.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
Expand Down

0 comments on commit f7e606f

Please sign in to comment.