Skip to content

Commit

Permalink
Fix p2p sync for 0.13.2 blocks (#2146)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Sep 19, 2024
1 parent 4fb5d0f commit 37ed095
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 48 deletions.
2 changes: 2 additions & 0 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func receiptCommon(r *core.TransactionReceipt) *spec.Receipt_Common {
var revertReason *string
if r.RevertReason != "" {
revertReason = &r.RevertReason
} else if r.Reverted {
revertReason = utils.Ptr("")
}

return &spec.Receipt_Common{
Expand Down
13 changes: 11 additions & 2 deletions adapters/p2p2core/felt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package p2p2core

import (
"encoding/binary"
"reflect"

"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -35,6 +36,14 @@ func adapt(v interface{ GetElements() []byte }) *felt.Felt {
}

func AdaptUint128(u *spec.Uint128) *felt.Felt {
// todo handle u128
return &felt.Zero
if u == nil {
return nil
}

bytes := make([]byte, 16) //nolint:mnd

binary.BigEndian.PutUint64(bytes[:8], u.High)
binary.BigEndian.PutUint64(bytes[8:], u.Low)

return new(felt.Felt).SetBytes(bytes)
}
26 changes: 26 additions & 0 deletions adapters/p2p2core/felt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package p2p2core
import (
"testing"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
)

Expand All @@ -11,3 +14,26 @@ func TestAdaptNilReturnsNil(t *testing.T) {
assert.Nil(t, AdaptAddress(nil))
assert.Nil(t, AdaptFelt(nil))
}

func TestAdaptUint128(t *testing.T) {
t.Run("nil", func(t *testing.T) {
u128 := AdaptUint128(nil)
assert.Nil(t, u128)
})
t.Run("non-nil", func(t *testing.T) {
cases := []struct {
Low, High uint64
Expect *felt.Felt
}{
{32, 64, utils.HexToFelt(t, "0x400000000000000020")},
}

for _, c := range cases {
result := AdaptUint128(&spec.Uint128{
Low: c.Low,
High: c.High,
})
assert.Equal(t, c.Expect, result, "expected %v actual %v", c.Expect, result)
}
})
}
30 changes: 22 additions & 8 deletions adapters/p2p2core/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ func adaptExecutionResources(er *spec.Receipt_ExecutionResources) *core.Executio
Keccak: uint64(er.GetBuiltins().GetKeccak()),
Poseidon: uint64(er.GetBuiltins().GetPoseidon()),
SegmentArena: 0, // todo(kirill) recheck
// todo(kirill) set fields after spec update
AddMod: 0,
MulMod: 0,
RangeCheck96: 0,
AddMod: uint64(er.GetBuiltins().GetAddMod()),
MulMod: uint64(er.GetBuiltins().GetMulMod()),
RangeCheck96: uint64(er.GetBuiltins().GetRangeCheck96()),
},
DataAvailability: &core.DataAvailability{
L1Gas: feltToUint64(er.L1Gas),
L1DataGas: feltToUint64(er.L1DataGas),
},
MemoryHoles: uint64(er.MemoryHoles),
Steps: uint64(er.Steps), // todo SPEC 32 -> 64 bytes
TotalGasConsumed: &core.GasConsumed{
L1Gas: feltToUint64(er.TotalL1Gas),
// total_l1_data_gas = l1_data_gas, because there's only one place that can generate l1_data_gas costs
L1DataGas: feltToUint64(er.L1DataGas),
},
DataAvailability: nil, // todo(kirill) recheck
MemoryHoles: uint64(er.MemoryHoles),
Steps: uint64(er.Steps), // todo SPEC 32 -> 64 bytes
TotalGasConsumed: nil, // todo(kirill) fill after spec update
}
}

Expand All @@ -71,3 +77,11 @@ func adaptMessageToL1(m *spec.MessageToL1) *core.L2ToL1Message {
Payload: utils.Map(m.Payload, AdaptFelt),
}
}

func feltToUint64(f *spec.Felt252) uint64 {
var result uint64
if adapted := AdaptFelt(f); adapted != nil {
result = adapted.Uint64()
}
return result
}
4 changes: 3 additions & 1 deletion adapters/p2p2core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func AdaptStateDiff(reader core.StateReader, contractDiffs []*spec.ContractDiff,
if diff.Nonce != nil {
nonces[*address] = AdaptFelt(diff.Nonce)
}
storageDiffs[*address] = utils.ToMap(diff.Values, adaptStoredValue)
if diff.Values != nil {
storageDiffs[*address] = utils.ToMap(diff.Values, adaptStoredValue)
}

// todo recheck this logic
if diff.ClassHash != nil {
Expand Down
13 changes: 0 additions & 13 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,6 @@ 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,
Expand Down
14 changes: 0 additions & 14 deletions core/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,3 @@ func Test0132BlockHash(t *testing.T) {
})
}
}

func TestBlockHashP2P(t *testing.T) {
mainnetGW := adaptfeeder.New(feeder.NewTestClient(t, &utils.Mainnet))

t.Run("error if block.SequencerAddress is nil", func(t *testing.T) {
mainnetBlock1, err := mainnetGW.BlockByNumber(context.Background(), 1)
require.NoError(t, err)

mainnetBlock1.SequencerAddress = nil

_, err = core.BlockHash(mainnetBlock1)
assert.EqualError(t, err, "block.SequencerAddress is nil")
})
}
2 changes: 1 addition & 1 deletion p2p/starknet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
unmarshalMaxSize = 15 * utils.Megabyte
readTimeout = 5 * time.Second
readTimeout = 10 * time.Second
)

type NewStreamFunc func(ctx context.Context, pids ...protocol.ID) (network.Stream, error)
Expand Down
11 changes: 2 additions & 9 deletions p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (s *syncService) processSpecBlockParts(
return orderedBlockBodiesCh
}

//nolint:gocyclo,funlen
//nolint:gocyclo
func (s *syncService) adaptAndSanityCheckBlock(ctx context.Context, header *spec.SignedBlockHeader, contractDiffs []*spec.ContractDiff,
classes []*spec.Class, txs []*spec.Transaction, receipts []*spec.Receipt, events []*spec.Event, prevBlockRoot *felt.Felt,
) <-chan blockBody {
Expand Down Expand Up @@ -326,17 +326,10 @@ func (s *syncService) adaptAndSanityCheckBlock(ctx context.Context, header *spec
return
}

h, err := core.BlockHash(coreBlock)
if err != nil {
bodyCh <- blockBody{err: fmt.Errorf("block hash calculation error: %v", err)}
return
}
coreBlock.Hash = h

newClasses := make(map[felt.Felt]core.Class)
for _, cls := range classes {
coreC := p2p2core.AdaptClass(cls)
h, err = coreC.Hash()
h, err := coreC.Hash()
if err != nil {
bodyCh <- blockBody{err: fmt.Errorf("class hash calculation error: %v", err)}
return
Expand Down

0 comments on commit 37ed095

Please sign in to comment.