Skip to content

Commit

Permalink
P2P sync update for 0.13.2 hashing scheme (#2033)
Browse files Browse the repository at this point in the history
feat: add hash map for verifying pre-0.13.2 blocks

- Introduced a map of post-0.13.2 hashes for pre-0.13.2 blocks to enable their verification.
- Compute post-0.13.2 commitments for pre-0.13.2 blocks when syncing with clients using commitments.
- Temporary measure to support pre-0.13.2 blocks; will be removed once no longer needed.
  • Loading branch information
kirugan authored Jan 18, 2025
1 parent 0c9ee5a commit 05b20ac
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 102 deletions.
4 changes: 2 additions & 2 deletions adapters/core2p2p/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func AdaptBlockID(header *core.Header) *gen.BlockID {
}
}

func AdaptSignature(sig []*felt.Felt) *gen.ConsensusSignature {
func adaptSignature(sig []*felt.Felt) *gen.ConsensusSignature {
return &gen.ConsensusSignature{
R: AdaptFelt(sig[0]),
S: AdaptFelt(sig[1]),
Expand Down Expand Up @@ -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: &gen.StateDiffCommitment{
StateDiffLength: stateDiffLength,
Root: AdaptHash(stateDiffCommitment),
Expand Down
1 change: 1 addition & 0 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func receiptCommon(r *core.TransactionReceipt) *gen.Receipt_Common {
if r.RevertReason != "" {
revertReason = &r.RevertReason
} else if r.Reverted {
// in some cases receipt marked as reverted may contain empty string in revert_reason
revertReason = utils.Ptr("")
}

Expand Down
36 changes: 36 additions & 0 deletions adapters/core2p2p/receipt_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core2p2p

import (
"testing"

"github.com/NethermindEth/juno/core"
"github.com/stretchr/testify/assert"
)

func TestReceiptCommon(t *testing.T) {
t.Run("successful", func(t *testing.T) {
receipt := &core.TransactionReceipt{
Reverted: false,
}
r := receiptCommon(receipt)
// if RevertReason is nil then receipt considered as non reverted
assert.Nil(t, r.RevertReason)
})
t.Run("reverted", func(t *testing.T) {
receipts := []*core.TransactionReceipt{
{
Reverted: true,
RevertReason: "Reason",
},
{
Reverted: true,
RevertReason: "",
},
}

for _, receipt := range receipts {
r := receiptCommon(receipt)
assert.Equal(t, &receipt.RevertReason, r.RevertReason)
}
})
}
2 changes: 1 addition & 1 deletion adapters/p2p2core/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func AdaptReceipt(r *gen.Receipt, txHash *felt.Felt) *core.TransactionReceipt {
L1ToL2Message: nil,
L2ToL1Message: utils.Map(common.MessagesSent, adaptMessageToL1),
TransactionHash: txHash,
Reverted: common.RevertReason != nil, // todo is it correct?
Reverted: common.RevertReason != nil, // in case it's empty string we should treat it as reverted
RevertReason: common.GetRevertReason(),
}
}
Expand Down
47 changes: 47 additions & 0 deletions adapters/p2p2core/receipt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package p2p2core_test

import (
"testing"

"github.com/NethermindEth/juno/adapters/p2p2core"
"github.com/NethermindEth/juno/p2p/gen"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
)

func TestAdaptReceipt(t *testing.T) {
t.Run("successful", func(t *testing.T) {
hash := utils.HexToFelt(t, "0xCAFEBABE")
receipt := &gen.Receipt{
Type: &gen.Receipt_L1Handler_{
L1Handler: &gen.Receipt_L1Handler{
Common: &gen.Receipt_Common{
RevertReason: nil,
},
},
},
}
r := p2p2core.AdaptReceipt(receipt, hash)
assert.False(t, r.Reverted)
assert.Equal(t, "", r.RevertReason)
})
t.Run("reverted", func(t *testing.T) {
reasons := []string{"reason", ""}

for _, reason := range reasons {
hash := utils.HexToFelt(t, "0xCAFEDEAD")
receipt := &gen.Receipt{
Type: &gen.Receipt_L1Handler_{
L1Handler: &gen.Receipt_L1Handler{
Common: &gen.Receipt_Common{
RevertReason: utils.Ptr(reason),
},
},
},
}
r := p2p2core.AdaptReceipt(receipt, hash)
assert.True(t, r.Reverted)
assert.Equal(t, receipt.GetL1Handler().GetCommon().GetRevertReason(), r.RevertReason)
}
})
}
1 change: 0 additions & 1 deletion adapters/p2p2core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func AdaptStateDiff(reader core.StateReader, contractDiffs []*gen.ContractDiff,
storageDiffs[*address] = utils.ToMap(diff.Values, adaptStoredValue)
}

// todo recheck this logic
if diff.ClassHash != nil {
addrToClsHash := addrToClassHash{
addr: diff.Address,
Expand Down
53 changes: 36 additions & 17 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -146,15 +145,13 @@ func blockHash(b *Block, stateDiff *StateDiff, network *utils.Network, overrideS
}

// if block.version >= 0.13.4
v0_13_4 := semver.MustParse("0.13.4")
if blockVer.GreaterThanEqual(v0_13_4) {
if blockVer.GreaterThanEqual(Ver0_13_4) {
return post0134Hash(b, stateDiff)
}

// 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)
if blockVer.GreaterThanEqual(Ver0_13_2) {
return Post0132Hash(b, stateDiff)
}

// following statements applied only if block.version < 0.13.2
Expand Down Expand Up @@ -188,11 +185,11 @@ func pre07Hash(b *Block, chain *felt.Felt) (*felt.Felt, *BlockCommitments, error
}

func post0134Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {
wg := conc.NewWaitGroup()
var txCommitment, eCommitment, rCommitment, sdCommitment *felt.Felt
var sdLength uint64
var tErr, eErr, rErr error

wg := conc.NewWaitGroup()
wg.Go(func() {
txCommitment, tErr = transactionCommitmentPoseidon0134(b.Transactions)
})
Expand Down Expand Up @@ -254,12 +251,12 @@ func post0134Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments
}, nil
}

func post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {
wg := conc.NewWaitGroup()
func Post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {
var txCommitment, eCommitment, rCommitment, sdCommitment *felt.Felt
var sdLength uint64
var tErr, eErr, rErr error

wg := conc.NewWaitGroup()
wg.Go(func() {
txCommitment, tErr = transactionCommitmentPoseidon0132(b.Transactions)
})
Expand Down Expand Up @@ -289,21 +286,43 @@ func post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments

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

// These values are nil for some pre 0.13.2 blocks
// `crypto.PoseidonArray` panics if any of the values are nil
seqAddr := &felt.Zero
gasPriceStrk := &felt.Zero
l1DataGasPricrInWei := &felt.Zero
l1DataGasPriceInFri := &felt.Zero

if b.SequencerAddress != nil {
seqAddr = b.SequencerAddress
}
if b.GasPriceSTRK != nil {
gasPriceStrk = b.GasPriceSTRK
}
if b.L1DataGasPrice != nil {
if b.L1DataGasPrice.PriceInWei != nil {
l1DataGasPricrInWei = b.L1DataGasPrice.PriceInWei
}
if b.L1DataGasPrice.PriceInFri != nil {
l1DataGasPriceInFri = b.L1DataGasPrice.PriceInFri
}
}

return crypto.PoseidonArray(
starknetBlockHash0,
new(felt.Felt).SetUint64(b.Number), // block number
b.GlobalStateRoot, // global state root
b.SequencerAddress, // sequencer address
seqAddr, // sequencer address
new(felt.Felt).SetUint64(b.Timestamp), // block timestamp
concatCounts,
sdCommitment,
txCommitment, // transaction commitment
eCommitment, // event commitment
rCommitment, // receipt commitment
b.GasPrice, // gas price in wei
b.GasPriceSTRK, // gas price in fri
b.L1DataGasPrice.PriceInWei,
b.L1DataGasPrice.PriceInFri,
txCommitment, // transaction commitment
eCommitment, // event commitment
rCommitment, // receipt commitment
b.GasPrice, // gas price in wei
gasPriceStrk, // gas price in fri
l1DataGasPricrInWei,
l1DataGasPriceInFri,
new(felt.Felt).SetBytes([]byte(b.ProtocolVersion)),
&felt.Zero, // reserved: extra data
b.ParentHash, // parent block hash
Expand Down
File renamed without changes.
16 changes: 0 additions & 16 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/big"
"slices"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -699,21 +698,6 @@ func transactionCommitmentPoseidon0132(transactions []Transaction) (*felt.Felt,
})
}

// ParseBlockVersion computes the block version, defaulting to "0.0.0" for empty strings
func ParseBlockVersion(protocolVersion string) (*semver.Version, error) {
if protocolVersion == "" {
return semver.NewVersion("0.0.0")
}

sep := "."
digits := strings.Split(protocolVersion, sep)
// pad with 3 zeros in case version has less than 3 digits
digits = append(digits, []string{"0", "0", "0"}...)

// get first 3 digits only
return semver.NewVersion(strings.Join(digits[:3], sep))
}

type eventWithTxHash struct {
Event *Event
TxHash *felt.Felt
Expand Down
27 changes: 27 additions & 0 deletions core/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core

import (
"strings"

"github.com/Masterminds/semver/v3"
)

var (
Ver0_13_2 = semver.MustParse("0.13.2")
Ver0_13_4 = semver.MustParse("0.13.4")
)

// ParseBlockVersion computes the block version, defaulting to "0.0.0" for empty strings
func ParseBlockVersion(protocolVersion string) (*semver.Version, error) {
if protocolVersion == "" {
return semver.NewVersion("0.0.0")
}

sep := "."
digits := strings.Split(protocolVersion, sep)
// pad with 3 zeros in case version has less than 3 digits
digits = append(digits, []string{"0", "0", "0"}...)

// get first 3 digits only
return semver.NewVersion(strings.Join(digits[:3], sep))
}
Loading

0 comments on commit 05b20ac

Please sign in to comment.