|
| 1 | +package p2p2consensus |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/NethermindEth/juno/core" |
| 7 | + "github.com/NethermindEth/juno/core/felt" |
| 8 | + p2pconsensus "github.com/NethermindEth/juno/p2p/proto/consensus/consensus" |
| 9 | + "github.com/NethermindEth/juno/utils" |
| 10 | + "github.com/starknet-io/starknet-p2pspecs/p2p/proto/common" |
| 11 | + "github.com/starknet-io/starknet-p2pspecs/p2p/proto/transaction" |
| 12 | +) |
| 13 | + |
| 14 | +//nolint:funlen |
| 15 | +func AdaptTransaction(t *p2pconsensus.ConsensusTransaction, network *utils.Network) (core.Transaction, core.Class) { |
| 16 | + if t == nil { |
| 17 | + return nil, nil |
| 18 | + } |
| 19 | + |
| 20 | + switch t.Txn.(type) { |
| 21 | + case *p2pconsensus.ConsensusTransaction_DeclareV3: |
| 22 | + tx := t.GetDeclareV3() |
| 23 | + |
| 24 | + nDAMode, err := adaptVolitionDomain(tx.Common.NonceDataAvailabilityMode) |
| 25 | + if err != nil { |
| 26 | + panic(fmt.Sprintf("Failed to convert Nonce DA mode: %v to uint32", tx.Common.NonceDataAvailabilityMode)) |
| 27 | + } |
| 28 | + |
| 29 | + fDAMode, err := adaptVolitionDomain(tx.Common.FeeDataAvailabilityMode) |
| 30 | + if err != nil { |
| 31 | + panic(fmt.Sprintf("Failed to convert Fee DA mode: %v to uint32", tx.Common.FeeDataAvailabilityMode)) |
| 32 | + } |
| 33 | + |
| 34 | + class := AdaptClass(tx.Class) |
| 35 | + classHash, err := class.Hash() |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + declareTx := &core.DeclareTransaction{ |
| 40 | + TransactionHash: AdaptHash(t.TransactionHash), |
| 41 | + ClassHash: classHash, |
| 42 | + SenderAddress: AdaptAddress(tx.Common.Sender), |
| 43 | + MaxFee: nil, // in 3 version this field was removed |
| 44 | + TransactionSignature: adaptAccountSignature(tx.Common.Signature), |
| 45 | + Nonce: AdaptFelt(tx.Common.Nonce), |
| 46 | + Version: txVersion(3), |
| 47 | + CompiledClassHash: AdaptHash(tx.Common.CompiledClassHash), |
| 48 | + Tip: tx.Common.Tip, |
| 49 | + ResourceBounds: map[core.Resource]core.ResourceBounds{ |
| 50 | + core.ResourceL1Gas: adaptResourceLimits(tx.Common.ResourceBounds.L1Gas), |
| 51 | + core.ResourceL2Gas: adaptResourceLimits(tx.Common.ResourceBounds.L2Gas), |
| 52 | + core.ResourceL1DataGas: adaptResourceLimits(tx.Common.ResourceBounds.L1DataGas), |
| 53 | + }, |
| 54 | + PaymasterData: utils.Map(tx.Common.PaymasterData, AdaptFelt), |
| 55 | + AccountDeploymentData: utils.Map(tx.Common.AccountDeploymentData, AdaptFelt), |
| 56 | + NonceDAMode: nDAMode, |
| 57 | + FeeDAMode: fDAMode, |
| 58 | + } |
| 59 | + |
| 60 | + return declareTx, class |
| 61 | + case *p2pconsensus.ConsensusTransaction_DeployAccountV3: |
| 62 | + tx := t.GetDeployAccountV3() |
| 63 | + |
| 64 | + nDAMode, err := adaptVolitionDomain(tx.NonceDataAvailabilityMode) |
| 65 | + if err != nil { |
| 66 | + panic(fmt.Sprintf("Failed to convert Nonce DA mode: %v to uint32", tx.NonceDataAvailabilityMode)) |
| 67 | + } |
| 68 | + |
| 69 | + fDAMode, err := adaptVolitionDomain(tx.FeeDataAvailabilityMode) |
| 70 | + if err != nil { |
| 71 | + panic(fmt.Sprintf("Failed to convert Fee DA mode: %v to uint32", tx.FeeDataAvailabilityMode)) |
| 72 | + } |
| 73 | + |
| 74 | + addressSalt := AdaptFelt(tx.AddressSalt) |
| 75 | + classHash := AdaptHash(tx.ClassHash) |
| 76 | + callData := utils.Map(tx.Calldata, AdaptFelt) |
| 77 | + deployAccTx := &core.DeployAccountTransaction{ |
| 78 | + DeployTransaction: core.DeployTransaction{ |
| 79 | + TransactionHash: AdaptHash(t.TransactionHash), |
| 80 | + ContractAddressSalt: addressSalt, |
| 81 | + ContractAddress: core.ContractAddress(&felt.Zero, classHash, addressSalt, callData), |
| 82 | + ClassHash: classHash, |
| 83 | + ConstructorCallData: callData, |
| 84 | + Version: txVersion(3), |
| 85 | + }, |
| 86 | + MaxFee: nil, // todo(kirill) update spec? missing field |
| 87 | + TransactionSignature: adaptAccountSignature(tx.Signature), |
| 88 | + Nonce: AdaptFelt(tx.Nonce), |
| 89 | + Tip: tx.Tip, |
| 90 | + ResourceBounds: map[core.Resource]core.ResourceBounds{ |
| 91 | + core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas), |
| 92 | + core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas), |
| 93 | + core.ResourceL1DataGas: adaptResourceLimits(tx.ResourceBounds.L1DataGas), |
| 94 | + }, |
| 95 | + PaymasterData: utils.Map(tx.PaymasterData, AdaptFelt), |
| 96 | + NonceDAMode: nDAMode, |
| 97 | + FeeDAMode: fDAMode, |
| 98 | + } |
| 99 | + |
| 100 | + return deployAccTx, nil |
| 101 | + case *p2pconsensus.ConsensusTransaction_InvokeV3: |
| 102 | + tx := t.GetInvokeV3() |
| 103 | + |
| 104 | + nDAMode, err := adaptVolitionDomain(tx.NonceDataAvailabilityMode) |
| 105 | + if err != nil { |
| 106 | + panic(fmt.Sprintf("Failed to convert Nonce DA mode: %v to uint32", tx.NonceDataAvailabilityMode)) |
| 107 | + } |
| 108 | + |
| 109 | + fDAMode, err := adaptVolitionDomain(tx.FeeDataAvailabilityMode) |
| 110 | + if err != nil { |
| 111 | + panic(fmt.Sprintf("Failed to convert Fee DA mode: %v to uint32", tx.FeeDataAvailabilityMode)) |
| 112 | + } |
| 113 | + |
| 114 | + invTx := &core.InvokeTransaction{ |
| 115 | + TransactionHash: AdaptHash(t.TransactionHash), |
| 116 | + ContractAddress: nil, // todo call core.ContractAddress() ? |
| 117 | + CallData: utils.Map(tx.Calldata, AdaptFelt), |
| 118 | + TransactionSignature: adaptAccountSignature(tx.Signature), |
| 119 | + MaxFee: nil, // in 3 version this field was removed |
| 120 | + Version: txVersion(3), |
| 121 | + Nonce: AdaptFelt(tx.Nonce), |
| 122 | + SenderAddress: AdaptAddress(tx.Sender), |
| 123 | + EntryPointSelector: nil, |
| 124 | + Tip: tx.Tip, |
| 125 | + ResourceBounds: map[core.Resource]core.ResourceBounds{ |
| 126 | + core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas), |
| 127 | + core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas), |
| 128 | + core.ResourceL1DataGas: adaptResourceLimits(tx.ResourceBounds.L1DataGas), |
| 129 | + }, |
| 130 | + PaymasterData: utils.Map(tx.PaymasterData, AdaptFelt), |
| 131 | + NonceDAMode: nDAMode, |
| 132 | + FeeDAMode: fDAMode, |
| 133 | + AccountDeploymentData: nil, |
| 134 | + } |
| 135 | + |
| 136 | + return invTx, nil |
| 137 | + case *p2pconsensus.ConsensusTransaction_L1Handler: |
| 138 | + tx := t.GetL1Handler() |
| 139 | + l1Tx := &core.L1HandlerTransaction{ |
| 140 | + TransactionHash: AdaptHash(t.TransactionHash), |
| 141 | + ContractAddress: AdaptAddress(tx.Address), |
| 142 | + EntryPointSelector: AdaptFelt(tx.EntryPointSelector), |
| 143 | + Nonce: AdaptFelt(tx.Nonce), |
| 144 | + CallData: utils.Map(tx.Calldata, AdaptFelt), |
| 145 | + Version: txVersion(0), |
| 146 | + } |
| 147 | + |
| 148 | + return l1Tx, nil |
| 149 | + default: |
| 150 | + panic(fmt.Errorf("unsupported tx type %T", t.Txn)) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func adaptResourceLimits(limits *transaction.ResourceLimits) core.ResourceBounds { |
| 155 | + return core.ResourceBounds{ |
| 156 | + MaxAmount: AdaptFelt(limits.MaxAmount).Uint64(), |
| 157 | + MaxPricePerUnit: AdaptFelt(limits.MaxPricePerUnit), |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func adaptAccountSignature(s *transaction.AccountSignature) []*felt.Felt { |
| 162 | + return utils.Map(s.Parts, AdaptFelt) |
| 163 | +} |
| 164 | + |
| 165 | +func txVersion(v uint64) *core.TransactionVersion { |
| 166 | + return new(core.TransactionVersion).SetUint64(v) |
| 167 | +} |
| 168 | + |
| 169 | +func adaptVolitionDomain(v common.VolitionDomain) (core.DataAvailabilityMode, error) { |
| 170 | + switch v { |
| 171 | + case common.VolitionDomain_L1: |
| 172 | + return core.DAModeL1, nil |
| 173 | + case common.VolitionDomain_L2: |
| 174 | + return core.DAModeL2, nil |
| 175 | + default: |
| 176 | + return 0, fmt.Errorf("unknown volition domain %d", v) |
| 177 | + } |
| 178 | +} |
0 commit comments