Skip to content

Commit

Permalink
Add P2P GetTransactions handler (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Oct 24, 2023
1 parent 0009e84 commit 325e62c
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 35 deletions.
11 changes: 11 additions & 0 deletions adapters/core2p2p/felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core2p2p
import (
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
)

func AdaptHash(f *felt.Felt) *spec.Hash {
Expand All @@ -15,6 +16,12 @@ func AdaptHash(f *felt.Felt) *spec.Hash {
}
}

func AdaptAccountSignature(signature []*felt.Felt) *spec.AccountSignature {
return &spec.AccountSignature{
Parts: utils.Map(signature, AdaptFelt),
}
}

func AdaptFelt(f *felt.Felt) *spec.Felt252 {
if f == nil {
return nil
Expand All @@ -25,6 +32,10 @@ func AdaptFelt(f *felt.Felt) *spec.Felt252 {
}
}

func AdaptFeltSlice(sl []*felt.Felt) []*spec.Felt252 {
return utils.Map(sl, AdaptFelt)
}

func AdaptAddress(f *felt.Felt) *spec.Address {
if f == nil {
return nil
Expand Down
125 changes: 125 additions & 0 deletions adapters/core2p2p/transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package core2p2p

import (
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/p2p/starknet/spec"
)

func AdaptTransaction(transaction core.Transaction) *spec.Transaction {
if transaction == nil {
return nil
}

var specTx spec.Transaction

switch tx := transaction.(type) {
case *core.DeployTransaction:
specTx.Txn = adaptDeployTransaction(tx)
case *core.DeployAccountTransaction:
switch {
case tx.Version.Is(1):
specTx.Txn = &spec.Transaction_DeployAccountV1_{
DeployAccountV1: &spec.Transaction_DeployAccountV1{
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
ClassHash: AdaptHash(tx.ClassHash),
Nonce: AdaptFelt(tx.Nonce),
AddressSalt: AdaptFelt(tx.ContractAddressSalt),
Calldata: AdaptFeltSlice(tx.ConstructorCallData),
},
}
default:
panic(fmt.Errorf("unsupported InvokeTransaction version %s", tx.Version))
}
case *core.DeclareTransaction:
switch {
case tx.Version.Is(0):
specTx.Txn = &spec.Transaction_DeclareV0_{
DeclareV0: &spec.Transaction_DeclareV0{
Sender: AdaptAddress(tx.SenderAddress),
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
ClassHash: AdaptHash(tx.ClassHash),
},
}
case tx.Version.Is(1):
specTx.Txn = &spec.Transaction_DeclareV1_{
DeclareV1: &spec.Transaction_DeclareV1{
Sender: AdaptAddress(tx.SenderAddress),
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
ClassHash: AdaptHash(tx.ClassHash),
Nonce: AdaptFelt(tx.Nonce),
},
}
case tx.Version.Is(2):
specTx.Txn = &spec.Transaction_DeclareV2_{
DeclareV2: &spec.Transaction_DeclareV2{
Sender: AdaptAddress(tx.SenderAddress),
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
ClassHash: AdaptHash(tx.ClassHash),
Nonce: AdaptFelt(tx.Nonce),
CompiledClassHash: AdaptFelt(tx.CompiledClassHash),
},
}
default:
panic(fmt.Errorf("unsupported Declare transaction version %s", tx.Version))
}
case *core.InvokeTransaction:
switch {
case tx.Version.Is(0):
specTx.Txn = &spec.Transaction_InvokeV0_{
InvokeV0: &spec.Transaction_InvokeV0{
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
Address: AdaptAddress(tx.ContractAddress),
EntryPointSelector: AdaptFelt(tx.EntryPointSelector),
Calldata: AdaptFeltSlice(tx.CallData),
},
}
case tx.Version.Is(1):
specTx.Txn = &spec.Transaction_InvokeV1_{
InvokeV1: &spec.Transaction_InvokeV1{
Sender: AdaptAddress(tx.SenderAddress),
MaxFee: AdaptFelt(tx.MaxFee),
Signature: AdaptAccountSignature(tx.Signature()),
Calldata: AdaptFeltSlice(tx.CallData),
},
}
default:
panic(fmt.Errorf("unsupported Invoke transaction version %s", tx.Version))
}
case *core.L1HandlerTransaction:
specTx.Txn = adaptL1HandlerTransaction(tx)
}

return &specTx
}

func adaptDeployTransaction(tx *core.DeployTransaction) *spec.Transaction_Deploy_ {
return &spec.Transaction_Deploy_{
Deploy: &spec.Transaction_Deploy{
ClassHash: AdaptHash(tx.ClassHash),
AddressSalt: AdaptFelt(tx.ContractAddressSalt),
Calldata: AdaptFeltSlice(tx.ConstructorCallData),
},
}
}

func adaptL1HandlerTransaction(tx *core.L1HandlerTransaction) *spec.Transaction_L1Handler {
if !tx.Version.Is(1) {
panic(fmt.Errorf("unsupported L1Handler tx version %s", tx.Version))
}

return &spec.Transaction_L1Handler{
L1Handler: &spec.Transaction_L1HandlerV1{
Nonce: AdaptFelt(tx.Nonce),
Address: AdaptAddress(tx.ContractAddress),
EntryPointSelector: AdaptFelt(tx.EntryPointSelector),
Calldata: AdaptFeltSlice(tx.CallData),
},
}
}
3 changes: 1 addition & 2 deletions l1/eth_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func (s *EthSubscriber) FinalisedHeight(ctx context.Context) (uint64, error) {
return 0, fmt.Errorf("get finalised Ethereum block: %w", err)
}

//nolint:gosec
number, ok := finalisedBlock["number"]
number, ok := finalisedBlock["number"] //nolint:gosec
if !ok {
return 0, fmt.Errorf("number field not present in Ethereum block")
}
Expand Down
31 changes: 24 additions & 7 deletions p2p/starknet/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,33 @@ func (h *Handler) onReceiptsRequest(req *spec.ReceiptsRequest) (Stream[proto.Mes
}

func (h *Handler) onTransactionsRequest(req *spec.TransactionsRequest) (Stream[proto.Message], error) {
// todo: read from bcReader and adapt to p2p type
count := uint64(0)
it, err := h.newIterator(req.Iteration)
if err != nil {
return nil, err
}

fin := h.newFin(&spec.TransactionsResponse{
Responses: &spec.TransactionsResponse_Fin{},
})

return func() (proto.Message, bool) {
if count > 3 {
return nil, false
if !it.Valid() {
return fin()
}
count++

block, err := it.Block()
if err != nil {
h.log.Errorw("Iterator failure", "err", err)
return fin()
}
it.Next()

return &spec.TransactionsResponse{
Id: &spec.BlockID{
Number: count - 1,
Id: core2p2p.AdaptBlockID(block.Header),
Responses: &spec.TransactionsResponse_Transactions{
Transactions: &spec.Transactions{
Items: utils.Map(block.Transactions, core2p2p.AdaptTransaction),
},
},
}, true
}, nil
Expand Down
2 changes: 1 addition & 1 deletion p2p/starknet/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type iterator struct {
blockNumber uint64
step uint64
limit uint64
forward bool

forward bool
reachedEnd bool
}

Expand Down
Loading

0 comments on commit 325e62c

Please sign in to comment.