Skip to content

Commit

Permalink
Create general variables to gain performance (#2365)
Browse files Browse the repository at this point in the history
Create general variables to gain performance
  • Loading branch information
AnkushinDaniil authored Jan 10, 2025
1 parent f743e37 commit 0fb6380
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ const (
Blob
)

var (
starknetBlockHash0 = new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH0"))
starknetBlockHash1 = new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH1"))
starknetGasPrices0 = new(felt.Felt).SetBytes([]byte("STARKNET_GAS_PRICES0"))
)

type GasPrice struct {
PriceInWei *felt.Felt
PriceInFri *felt.Felt
Expand Down Expand Up @@ -226,7 +232,7 @@ func post0134Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments
)

return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH1")),
starknetBlockHash1,
new(felt.Felt).SetUint64(b.Number), // block number
b.GlobalStateRoot, // global state root
b.SequencerAddress, // sequencer address
Expand Down Expand Up @@ -284,7 +290,7 @@ func post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments
concatCounts := concatCounts(b.TransactionCount, b.EventCount, sdLength, b.L1DAMode)

return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH0")),
starknetBlockHash0,
new(felt.Felt).SetUint64(b.Number), // block number
b.GlobalStateRoot, // global state root
b.SequencerAddress, // sequencer address
Expand Down Expand Up @@ -398,7 +404,7 @@ func concatCounts(txCount, eventCount, stateDiffLen uint64, l1Mode L1DAMode) *fe

func gasPricesHash(gasPrices, dataGasPrices, l2GasPrices GasPrice) *felt.Felt {
return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_GAS_PRICES0")),
starknetGasPrices0,
// gas prices
gasPrices.PriceInWei,
gasPrices.PriceInFri,
Expand Down
4 changes: 3 additions & 1 deletion core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (d *StateDiff) Length() uint64 {
return uint64(length)
}

var starknetStateDiff0 = new(felt.Felt).SetBytes([]byte("STARKNET_STATE_DIFF0"))

func (d *StateDiff) Hash() *felt.Felt {
digest := new(crypto.PoseidonDigest)

digest.Update(new(felt.Felt).SetBytes([]byte("STARKNET_STATE_DIFF0")))
digest.Update(starknetStateDiff0)

// updated_contracts = deployedContracts + replacedClasses
// Digest: [number_of_updated_contracts, address_0, class_hash_0, address_1, class_hash_1, ...].
Expand Down

0 comments on commit 0fb6380

Please sign in to comment.