diff --git a/adapters/core2p2p/block.go b/adapters/core2p2p/block.go index fa3c5b446d..e35acccbe9 100644 --- a/adapters/core2p2p/block.go +++ b/adapters/core2p2p/block.go @@ -20,7 +20,7 @@ func AdaptBlockID(header *core.Header) *spec.BlockID { } } -func AdaptSignature(sig []*felt.Felt) *spec.ConsensusSignature { +func adaptSignature(sig []*felt.Felt) *spec.ConsensusSignature { return &spec.ConsensusSignature{ R: AdaptFelt(sig[0]), S: AdaptFelt(sig[1]), @@ -48,7 +48,7 @@ func AdaptHeader(header *core.Header, commitments *core.BlockCommitments, Receipts: AdaptHash(commitments.ReceiptCommitment), ProtocolVersion: header.ProtocolVersion, GasPriceFri: AdaptUint128(header.GasPriceSTRK), - Signatures: utils.Map(header.Signatures, AdaptSignature), + Signatures: utils.Map(header.Signatures, adaptSignature), StateDiffCommitment: &spec.StateDiffCommitment{ StateDiffLength: stateDiffLength, Root: AdaptHash(stateDiffCommitment), diff --git a/adapters/core2p2p/receipt.go b/adapters/core2p2p/receipt.go index a62cd20752..ce783c2939 100644 --- a/adapters/core2p2p/receipt.go +++ b/adapters/core2p2p/receipt.go @@ -108,7 +108,6 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio if da := er.DataAvailability; da != nil { // todo(kirill) check that it might be null l1Gas = new(felt.Felt).SetUint64(da.L1Gas) l1DataGas = new(felt.Felt).SetUint64(da.L1DataGas) - } if tgs := er.TotalGasConsumed; tgs != nil { totalL1Gas = new(felt.Felt).SetUint64(tgs.L1Gas) diff --git a/adapters/p2p2core/block.go b/adapters/p2p2core/block.go index e593a3d6c8..62acad43e9 100644 --- a/adapters/p2p2core/block.go +++ b/adapters/p2p2core/block.go @@ -40,8 +40,8 @@ func AdaptBlockHeader(h *spec.SignedBlockHeader, eventsBloom *bloom.BloomFilter) PriceInWei: AdaptUint128(h.DataGasPriceWei), PriceInFri: AdaptUint128(h.DataGasPriceFri), }, - GasPrice: AdaptUint128(h.GasPriceWei), - GasPriceSTRK: AdaptUint128(h.GasPriceFri), + GasPrice: AdaptUint128(h.GasPriceFri), + GasPriceSTRK: AdaptUint128(h.GasPriceWei), } } diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 01f3ef3292..c3e00ec70d 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -25,6 +25,8 @@ type Reader interface { BlockByNumber(number uint64) (block *core.Block, err error) BlockByHash(hash *felt.Felt) (block *core.Block, err error) + BlockP2PHashByNumber(number uint64) (hash *felt.Felt, err error) + HeadsHeader() (header *core.Header, err error) BlockHeaderByNumber(number uint64) (header *core.Header, err error) BlockHeaderByHash(hash *felt.Felt) (header *core.Header, err error) @@ -369,21 +371,29 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit return err } + fmt.Println("block.ProtocolVersion", block.ProtocolVersion) + blockVer, err := core.ParseBlockVersion(block.ProtocolVersion) if err != nil { return err } + fmt.Println("blockVer", blockVer) + if blockVer.LessThan(core.Ver0_13_2) { // temporary hack + fmt.Println("block.SequencerAddress", block.SequencerAddress) if block.SequencerAddress == nil { block.SequencerAddress = &felt.Zero } + fmt.Println("ComputeAndStoreP2PHash") if err := ComputeAndStoreP2PHash(txn, block, stateUpdate.StateDiff); err != nil { return err } } + fmt.Println("blockCommitments", blockCommitments) + if err := StoreBlockCommitments(txn, block.Number, blockCommitments); err != nil { return err } @@ -884,7 +894,7 @@ func (b *Blockchain) GetReverseStateDiff() (*core.StateDiff, error) { if err != nil { return err } - stateUpdate, err := stateUpdateByNumber(txn, blockNumber) + stateUpdate, err := StateUpdateByNumber(txn, blockNumber) if err != nil { return err } diff --git a/core/block.go b/core/block.go index 09f9caa1b0..ab0cf01555 100644 --- a/core/block.go +++ b/core/block.go @@ -6,7 +6,6 @@ import ( "fmt" "slices" - "github.com/Masterminds/semver/v3" "github.com/NethermindEth/juno/core/crypto" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/utils" @@ -127,6 +126,19 @@ func VerifyBlockHash(b *Block, network *utils.Network, stateDiff *StateDiff) (*B return nil, errors.New("can not verify hash in block header") } +// BlockHash assumes block.SequencerAddress is not nil as this is called with post v0.12.0 +// and by then issues with unverifiable block hash were resolved. +// In future, this may no longer be required. +// Todo: Pass stateDiff so that p2p layer can calculate post 0.13.2 Block Hash +func BlockHash(b *Block) (*felt.Felt, error) { + if b.SequencerAddress == nil { + return nil, errors.New("block.SequencerAddress is nil") + } + + h, _, err := post07Hash(b, nil) + return h, err +} + // blockHash computes the block hash, with option to override sequence address func blockHash(b *Block, stateDiff *StateDiff, network *utils.Network, overrideSeqAddr *felt.Felt) (*felt.Felt, *BlockCommitments, error, @@ -137,7 +149,6 @@ func blockHash(b *Block, stateDiff *StateDiff, network *utils.Network, overrideS if err != nil { return nil, nil, err } - v0_13_2 := semver.MustParse("0.13.2") if blockVer.LessThan(Ver0_13_2) { if b.Number < metaInfo.First07Block { diff --git a/db/buckets.go b/db/buckets.go index 3918eb5f29..29fe043d66 100644 --- a/db/buckets.go +++ b/db/buckets.go @@ -33,6 +33,7 @@ const ( BlockCommitments Temporary // used temporarily for migrations SchemaIntermediateState + P2PHash ) // Key flattens a prefix and series of byte arrays into a single []byte. diff --git a/p2p/sync.go b/p2p/sync.go index d88badb365..322033fdc0 100644 --- a/p2p/sync.go +++ b/p2p/sync.go @@ -76,62 +76,6 @@ func (s *syncService) start(ctx context.Context) { continue } - txsCh, err := s.genTransactions(iterCtx, blockNumber) - if err != nil { - s.logError("Failed to get transactions", err) - cancelIteration() - continue - } - - eventsCh, err := s.genEvents(iterCtx, blockNumber) - if err != nil { - s.logError("Failed to get classes", err) - cancelIteration() - continue - } - - classesCh, err := s.genClasses(iterCtx, blockNumber) - if err != nil { - s.logError("Failed to get classes", err) - cancelIteration() - continue - } - - stateDiffsCh, err := s.genStateDiffs(iterCtx, blockNumber) - if err != nil { - s.logError("Failed to get state diffs", err) - cancelIteration() - continue - } - - blocksCh := pipeline.Bridge(iterCtx, s.processSpecBlockParts(iterCtx, uint64(nextHeight), pipeline.FanIn(iterCtx, - pipeline.Stage(iterCtx, headersAndSigsCh, specBlockPartsFunc[specBlockHeaderAndSigs]), - pipeline.Stage(iterCtx, classesCh, specBlockPartsFunc[specClasses]), - pipeline.Stage(iterCtx, stateDiffsCh, specBlockPartsFunc[specContractDiffs]), - pipeline.Stage(iterCtx, txsCh, specBlockPartsFunc[specTxWithReceipts]), - pipeline.Stage(iterCtx, eventsCh, specBlockPartsFunc[specEvents]), - ))) - - for b := range blocksCh { - if b.err != nil { - // cannot process any more blocks - s.log.Errorw("Failed to process block", "err", b.err) - cancelIteration() - break - } - - storeTimer := time.Now() - err = s.blockchain.Store(b.block, b.commitments, b.stateUpdate, b.newClasses) - if err != nil { - s.log.Errorw("Failed to Store Block", "number", b.block.Number, "err", err) - cancelIteration() - break - } - - s.log.Infow("Stored Block", "number", b.block.Number, "hash", b.block.Hash.ShortString(), - "root", b.block.GlobalStateRoot.ShortString()) - s.listener.OnSyncStepDone(junoSync.OpStore, b.block.Number, time.Since(storeTimer)) - } cancelIteration() } }