Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block.hash 0.13.4 calculation #2361

Merged
merged 8 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adapters/p2p2core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func AdaptBlockHeader(h *spec.SignedBlockHeader, eventsBloom *bloom.BloomFilter)
},
GasPrice: AdaptUint128(h.GasPriceWei),
GasPriceSTRK: AdaptUint128(h.GasPriceFri),
L2GasPrice: nil, // todo pass correct value once it's in the p2p spec
kirugan marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
1 change: 1 addition & 0 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func AdaptBlock(response *starknet.Block, sig *starknet.Signature) (*core.Block,
GasPriceSTRK: response.GasPriceSTRK(),
L1DAMode: core.L1DAMode(response.L1DAMode),
L1DataGasPrice: (*core.GasPrice)(response.L1DataGasPrice),
L2GasPrice: (*core.GasPrice)(response.L2GasPrice),
Signatures: sigs,
},
Transactions: txns,
Expand Down
105 changes: 98 additions & 7 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
L1DAMode L1DAMode
// The gas price for L1 data availability
L1DataGasPrice *GasPrice
L2GasPrice *GasPrice
}

type L1DAMode uint
Expand Down Expand Up @@ -137,16 +138,24 @@
if err != nil {
return nil, nil, err
}
v0_13_2 := semver.MustParse("0.13.2")

if blockVer.LessThan(v0_13_2) {
if b.Number < metaInfo.First07Block {
return pre07Hash(b, network.L2ChainIDFelt())
}
return post07Hash(b, overrideSeqAddr)
// if block.version >= 0.13.4
v0_13_4 := semver.MustParse("0.13.4")
if blockVer.GreaterThanEqual(v0_13_4) {
return Post0134Hash(b, stateDiff)
}

Check warning on line 146 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L145-L146

Added lines #L145 - L146 were not covered by tests

// if 0.13.2 <= block.version < 0.13.4
v0_13_2 := semver.MustParse("0.13.2")
if blockVer.GreaterThanEqual(v0_13_2) {
return Post0132Hash(b, stateDiff)
}

return Post0132Hash(b, stateDiff)
// following statements applied only if block.version < 0.13.2
if b.Number < metaInfo.First07Block {
return pre07Hash(b, network.L2ChainIDFelt())
}
return post07Hash(b, overrideSeqAddr)
}

// pre07Hash computes the block hash for blocks generated before Cairo 0.7.0
Expand All @@ -172,6 +181,73 @@
), &BlockCommitments{TransactionCommitment: txCommitment}, nil
}

func Post0134Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {
kirugan marked this conversation as resolved.
Show resolved Hide resolved
wg := conc.NewWaitGroup()
var txCommitment, eCommitment, rCommitment, sdCommitment *felt.Felt
var sdLength uint64
var tErr, eErr, rErr error

wg.Go(func() {
txCommitment, tErr = transactionCommitmentPoseidon0134(b.Transactions)
})
wg.Go(func() {
eCommitment, eErr = eventCommitmentPoseidon(b.Receipts)
})
wg.Go(func() {
rCommitment, rErr = receiptCommitment(b.Receipts)
})

Check warning on line 198 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L184-L198

Added lines #L184 - L198 were not covered by tests

wg.Go(func() {
sdLength = stateDiff.Length()
sdCommitment = stateDiff.Hash()
})

Check warning on line 203 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L200-L203

Added lines #L200 - L203 were not covered by tests

wg.Wait()

if tErr != nil {
return nil, nil, tErr
}
if eErr != nil {
return nil, nil, eErr
}
if rErr != nil {
return nil, nil, rErr
}

Check warning on line 215 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L205-L215

Added lines #L205 - L215 were not covered by tests

concatCounts := concatCounts(b.TransactionCount, b.EventCount, sdLength, b.L1DAMode)

pricesHash := gasPricesHash(
GasPrice{
PriceInFri: b.GasPriceSTRK,
PriceInWei: b.GasPrice,
},
*b.L1DataGasPrice,
*b.L2GasPrice,
)

return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH1")),
new(felt.Felt).SetUint64(b.Number), // block number
b.GlobalStateRoot, // global state root
b.SequencerAddress, // sequencer address
new(felt.Felt).SetUint64(b.Timestamp), // block timestamp
concatCounts,
sdCommitment,
txCommitment, // transaction commitment
eCommitment, // event commitment
rCommitment, // receipt commitment
pricesHash, // gas prices hash
new(felt.Felt).SetBytes([]byte(b.ProtocolVersion)),
&felt.Zero, // reserved: extra data
b.ParentHash, // parent block hash
), &BlockCommitments{
TransactionCommitment: txCommitment,
EventCommitment: eCommitment,
ReceiptCommitment: rCommitment,
StateDiffCommitment: sdCommitment,
}, nil

Check warning on line 248 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L217-L248

Added lines #L217 - L248 were not covered by tests
}

func Post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {
wg := conc.NewWaitGroup()
var txCommitment, eCommitment, rCommitment, sdCommitment *felt.Felt
Expand Down Expand Up @@ -319,3 +395,18 @@
)
return new(felt.Felt).SetBytes(concatBytes)
}

func gasPricesHash(gasPrices, dataGasPrices, l2GasPrices GasPrice) *felt.Felt {
return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_GAS_PRICES0")),
// gas prices
gasPrices.PriceInWei,
gasPrices.PriceInFri,
// data gas prices
dataGasPrices.PriceInWei,
dataGasPrices.PriceInFri,
// l2 gas prices
l2GasPrices.PriceInWei,
l2GasPrices.PriceInFri,
)

Check warning on line 411 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L399-L411

Added lines #L399 - L411 were not covered by tests
}
32 changes: 31 additions & 1 deletion core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const (
ResourceL1Gas Resource = iota + 1
ResourceL2Gas
ResourceL1DataGas
)

func (r Resource) String() string {
Expand All @@ -32,6 +33,8 @@
return "L1_GAS"
case ResourceL2Gas:
return "L2_GAS"
case ResourceL1DataGas:
return "L1_DATA"

Check warning on line 37 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L36-L37

Added lines #L36 - L37 were not covered by tests
default:
return ""
}
Expand Down Expand Up @@ -471,7 +474,19 @@
func tipAndResourcesHash(tip uint64, resourceBounds map[Resource]ResourceBounds) *felt.Felt {
l1Bounds := new(felt.Felt).SetBytes(resourceBounds[ResourceL1Gas].Bytes(ResourceL1Gas))
l2Bounds := new(felt.Felt).SetBytes(resourceBounds[ResourceL2Gas].Bytes(ResourceL2Gas))
return crypto.PoseidonArray(new(felt.Felt).SetUint64(tip), l1Bounds, l2Bounds)
elems := []*felt.Felt{
new(felt.Felt).SetUint64(tip),
l1Bounds,
l2Bounds,
}

// l1_data_gas resource bounds were added in 0.13.4
if bounds, ok := resourceBounds[ResourceL1DataGas]; ok {
l1DataBounds := new(felt.Felt).SetBytes(bounds.Bytes(ResourceL1DataGas))
elems = append(elems, l1DataBounds)
}

Check warning on line 487 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L485-L487

Added lines #L485 - L487 were not covered by tests

return crypto.PoseidonArray(elems...)
}

func dataAvailabilityMode(feeDAMode, nonceDAMode DataAvailabilityMode) uint64 {
Expand Down Expand Up @@ -653,6 +668,21 @@
return calculateCommitment(transactions, trie.RunOnTempTriePedersen, hashFunc)
}

// transactionCommitmentPoseidon0134 handles empty signatures compared to transactionCommitmentPoseidon:
// empty signatures interpreted as [] instead of [0]
kirugan marked this conversation as resolved.
Show resolved Hide resolved
func transactionCommitmentPoseidon0134(transactions []Transaction) (*felt.Felt, error) {
return calculateCommitment(transactions, trie.RunOnTempTriePoseidon, func(transaction Transaction) *felt.Felt {
var digest crypto.PoseidonDigest
digest.Update(transaction.Hash())

if txSignature := transaction.Signature(); len(txSignature) > 0 {
digest.Update(txSignature...)
}

Check warning on line 680 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L673-L680

Added lines #L673 - L680 were not covered by tests

return digest.Finish()

Check warning on line 682 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L682

Added line #L682 was not covered by tests
})
}

func transactionCommitmentPoseidon(transactions []Transaction) (*felt.Felt, error) {
kirugan marked this conversation as resolved.
Show resolved Hide resolved
return calculateCommitment(transactions, trie.RunOnTempTriePoseidon, func(transaction Transaction) *felt.Felt {
var digest crypto.PoseidonDigest
Expand Down
2 changes: 1 addition & 1 deletion rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
case `"l2_gas"`:
*r = ResourceL2Gas
default:
return fmt.Errorf("unknown Resource: %q", string(data))
return fmt.Errorf("unknown Resource3: %q", string(data))

Check warning on line 187 in rpc/transaction.go

View check run for this annotation

Codecov / codecov/patch

rpc/transaction.go#L187

Added line #L187 was not covered by tests
kirugan marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions starknet/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Block struct {
L1GasPrice *GasPrice `json:"l1_gas_price"`
L1DAMode L1DAMode `json:"l1_da_mode"`
L1DataGasPrice *GasPrice `json:"l1_data_gas_price"`
L2GasPrice *GasPrice `json:"l2_gas_price"`

// TODO we can remove the GasPrice method and the GasPriceLegacy field
// once v0.13 lands on mainnet. In the meantime, we include both to support
Expand Down
5 changes: 4 additions & 1 deletion starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
const (
ResourceL1Gas Resource = iota + 1
ResourceL2Gas
ResourceL1DataGas
)

func (r *Resource) UnmarshalJSON(data []byte) error {
Expand All @@ -120,10 +121,12 @@
switch string(text) {
case "L1_GAS":
*r = ResourceL1Gas
case "L1_DATA_GAS":
*r = ResourceL1DataGas

Check warning on line 125 in starknet/transaction.go

View check run for this annotation

Codecov / codecov/patch

starknet/transaction.go#L124-L125

Added lines #L124 - L125 were not covered by tests
case "L2_GAS":
*r = ResourceL2Gas
default:
return fmt.Errorf("unknown resource: %q", string(text))
return fmt.Errorf("unknown resource3: %q", string(text))

Check warning on line 129 in starknet/transaction.go

View check run for this annotation

Codecov / codecov/patch

starknet/transaction.go#L129

Added line #L129 was not covered by tests
kirugan marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
Expand Down
Loading