Skip to content

Commit

Permalink
revert unneeded changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Dec 17, 2024
1 parent 142ba87 commit 44e8796
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 126 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ build/
coverage/*
config/
.envrc
seq-db/

# pyenv
.python-version

# Default path for Juno DB files. It will get created if you follow the
# README and/or run `./build/juno` command.
juno/
p2p-dbs

myplugin.so
/juno/
p2p-dbs
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ sequencer-with-accounts:
--rpc-call-max-steps=4123000


sequencer-shadow-sepolia:
sequencer-shadow-sepolia: # Only works for 0.12.3 Sepolia blocks
./build/juno \
--http \
--http-port=6066 \
Expand All @@ -213,7 +213,7 @@ sequencer-shadow-sepolia:
--seq-enable \
--seq-shadow-mode \
--seq-block-time=5 \
--seq-shadow-mode-sync-to=0 \
--seq-shadow-mode-sync-to-block=0 \
--seq-rpc-endpoint="" \
--network sepolia \
--disable-l1-verification \
Expand Down
45 changes: 6 additions & 39 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,9 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
return err
}

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

if !stateRoot.Equal(stateUpdate.NewRoot) {
return fmt.Errorf("new state root does not match expected root")
}
if err := StoreBlockHeader(txn, block.Header); err != nil {
return err
}
Expand Down Expand Up @@ -503,7 +494,7 @@ func TransactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transa
return nil, err
}

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

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

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

prefix := db.ReceiptsByBlockNumberAndIndex.Key(numBytes)
Expand Down Expand Up @@ -871,10 +862,6 @@ func (b *Blockchain) revertHead(txn db.Transaction) error {
return err
}

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

header, err := blockHeaderByNumber(txn, blockNumber)
if err != nil {
return err
Expand Down Expand Up @@ -943,34 +930,14 @@ func removeTxsAndReceipts(txn db.Transaction, blockNumber, numTxs uint64) error
return nil
}

func MakeStateDiffForEmptyBlock(bc Reader, blockNumber uint64) (*core.StateDiff, error) {
stateDiff := core.EmptyStateDiff()

const blockHashLag = 10
if blockNumber < blockHashLag {
return stateDiff, nil
}

header, err := bc.BlockHeaderByNumber(blockNumber - blockHashLag)
if err != nil {
return nil, err
}

blockHashStorageContract := new(felt.Felt).SetUint64(1)
stateDiff.StorageDiffs[*blockHashStorageContract] = map[felt.Felt]*felt.Felt{
*new(felt.Felt).SetUint64(header.Number): header.Hash,
}
return stateDiff, nil
}

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

if !root.Equal(currentRoot) {
return fmt.Errorf("state's current root: %s does not match the expected root: %s", currentRoot, root)
return fmt.Errorf("state's current state root: %s does not match the expected state root: %s", currentRoot, root)
}
return nil
}
Expand All @@ -996,7 +963,7 @@ func (b *Blockchain) Finalise( //nolint:gocyclo
return err
}
stateUpdate.OldRoot = oldStateRoot
if err = state.Update(block.Number, stateUpdate.StateDiff, newClasses); err != nil {
if err = state.Update(block.Number, stateUpdate, newClasses); err != nil {
return err
}
newStateRoot, err := state.Root()
Expand Down Expand Up @@ -1032,7 +999,7 @@ func (b *Blockchain) Finalise( //nolint:gocyclo
}

if !newStateRoot.Equal(block.GlobalStateRoot) {
return fmt.Errorf("new pe root does not match expected root")
return fmt.Errorf("new state root does not match expected state root")
}

if err := StoreBlockHeader(txn, block.Header); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestStore(t *testing.T) {
wrongRootStateUpdate.NewRoot = new(felt.Felt).SetUint64(1337)
chain := blockchain.New(pebble.NewMemTest(t), &utils.Mainnet, nil)
require.ErrorContains(t, chain.Store(block0, &emptyCommitments, wrongRootStateUpdate, nil),
"new state root does not match expected root")
"does not match the expected root")
})
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const (
seqBlockTimeF = "seq-block-time"
seqGenesisFileF = "seq-genesis-file"
seqShadowModeF = "seq-shadow-mode"
seqShadowModeSyncToF = "seq-shadow-mode-sync-to"
seqShadowModeSyncToF = "seq-shadow-mode-sync-to-block"
seqDisableFeesF = "seq-disable-fees"

defaultConfig = ""
Expand Down Expand Up @@ -195,8 +195,8 @@ const (
seqBlockTimeUsage = "Time to build a block, in seconds"
seqGenesisFileUsage = "Path to the genesis file"
seqShadowModeUsage = "Launches the sequencer in shadow mode (note: network must be set to Sepolia)"
seqShadowModeSyncToUsage = "The Sepolia block that the Sequencer should sync to before Sequencing"
seqDisableFeesUsage = "Skip charge fee for sequencer execution (note: not for shadowing)"
seqShadowModeSyncToUsage = "The block number that the Sequencer should sync to before Sequencing"
seqDisableFeesUsage = "Skip charge fee for sequencer execution"
)

var Version string
Expand Down Expand Up @@ -385,13 +385,13 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.Flags().String(versionedConstantsFileF, defaultVersionedConstantsFile, versionedConstantsFileUsage)
junoCmd.MarkFlagsMutuallyExclusive(p2pFeederNodeF, p2pPeersF)
junoCmd.Flags().String(pluginPathF, defaultPluginPath, pluginPathUsage)
junoCmd.AddCommand(GenP2PKeyPair(), DBCmd(defaultDBPath))
junoCmd.Flags().Bool(seqEnF, defaultSeqEn, seqEnUsage)
junoCmd.Flags().String(seqRPCEndpointF, defaultSeqRPCEndpoint, seqRPCEndpointUsage)
junoCmd.Flags().Uint(seqBlockTimeF, defaultSeqBlockTime, seqBlockTimeUsage)
junoCmd.Flags().String(seqGenesisFileF, defaultSeqGenesisFile, seqGenesisFileUsage)
junoCmd.Flags().Bool(seqShadowModeF, defaultSeqShadowMode, seqShadowModeUsage)
junoCmd.Flags().Uint(seqShadowModeSyncToF, defaultSeqShadowModeSyncTo, seqShadowModeSyncToUsage)
junoCmd.AddCommand(GenP2PKeyPair(), DBCmd(defaultDBPath))
junoCmd.Flags().Bool(seqDisableFeesF, defaultSeqDisableFees, seqDisableFeesUsage)
return junoCmd
}
1 change: 1 addition & 0 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func VerifyBlockHash(b *Block, network *utils.Network, stateDiff *StateDiff) (*B

metaInfo := network.BlockHashMetaInfo
unverifiableRange := metaInfo.UnverifiableRange

skipVerification := unverifiableRange != nil && b.Number >= unverifiableRange[0] && b.Number <= unverifiableRange[1] //nolint:gocritic
// todo should we still keep it after p2p ?
if !skipVerification {
Expand Down
4 changes: 2 additions & 2 deletions core/felt/felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func (z *Felt) Impl() *fp.Element {
return &z.val
}

// UnmarshalText accepts numbers and strings as input.
// UnmarshalJSON accepts numbers and strings as input.
// See Element.SetString for valid prefixes (0x, 0b, ...).
// If there is an error, we try to explicitly unmarshal from hex before
// returning an error. This implementation is based on [gnark-crypto]'s UnmarshalText.
// returning an error. This implementation is based on [gnark-crypto]'s UnmarshalJSON.
//
// [gnark-crypto]: https://github.com/ConsenSys/gnark-crypto/blob/master/ecc/stark-curve/fp/element.go
func (z *Felt) UnmarshalJSON(data []byte) error {
Expand Down
30 changes: 19 additions & 11 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (s *State) Root() (*felt.Felt, error) {
if classesRoot.IsZero() {
return storageRoot, nil
}

return crypto.PoseidonArray(stateVersion, storageRoot, classesRoot), nil
}

Expand Down Expand Up @@ -187,23 +188,29 @@ func (s *State) verifyStateUpdateRoot(root *felt.Felt) error {
return err
}

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

// Update applies a StateUpdate to the State object. State is not
// updated if an error is encountered during the operation.
func (s *State) Update(blockNumber uint64, diff *StateDiff, declaredClasses map[felt.Felt]Class) error {
// updated if an error is encountered during the operation. If update's
// old or new root does not match the state's old or new roots,
// [ErrMismatchedRoot] is returned.
func (s *State) Update(blockNumber uint64, update *StateUpdate, declaredClasses map[felt.Felt]Class) error {
fmt.Println("--=")
err := s.verifyStateUpdateRoot(update.OldRoot)
if err != nil {
return err
}
// register declared classes mentioned in stateDiff.deployedContracts and stateDiff.declaredClasses
for cHash, class := range declaredClasses {
if err := s.putClass(&cHash, class, blockNumber); err != nil {
if err = s.putClass(&cHash, class, blockNumber); err != nil {
return err
}
}

if err := s.updateDeclaredClassesTrie(diff.DeclaredV1Classes, declaredClasses); err != nil {
if err = s.updateDeclaredClassesTrie(update.StateDiff.DeclaredV1Classes, declaredClasses); err != nil {
return err
}

Expand All @@ -213,17 +220,18 @@ func (s *State) Update(blockNumber uint64, diff *StateDiff, declaredClasses map[
}

// register deployed contracts
for addr, classHash := range diff.DeployedContracts {
for addr, classHash := range update.StateDiff.DeployedContracts {
if err = s.putNewContract(stateTrie, &addr, classHash, blockNumber); err != nil {
return err
}
}

if err = s.updateContracts(stateTrie, blockNumber, diff, true); err != nil {
if err = s.updateContracts(stateTrie, blockNumber, update.StateDiff, true); err != nil {
return err
}

return storageCloser()
if err = storageCloser(); err != nil {
return err
}
return s.verifyStateUpdateRoot(update.NewRoot)
}

var (
Expand Down
Loading

0 comments on commit 44e8796

Please sign in to comment.