From 9535312daf0bea0faa110656427656360c58867e Mon Sep 17 00:00:00 2001 From: rianhughes Date: Mon, 21 Aug 2023 12:49:40 +0300 Subject: [PATCH] Implement DeclareTxn rpcv04 --- rpc/block_test.go | 79 ++++++++++++++++++++++++++++++++++++++-- rpc/transaction.go | 21 +++++++++-- rpc/types_contract.go | 19 ++++++++++ rpc/types_transaction.go | 57 ++++++++++++++++++++++++++--- 4 files changed, 165 insertions(+), 11 deletions(-) diff --git a/rpc/block_test.go b/rpc/block_test.go index 0f634e0f..e542c833 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -351,7 +351,7 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) { }, Status: "ACCEPTED_ON_L1", Transactions: []Transaction{ - DeclareTxn{ + DeclareTxnV0{ CommonTransaction: CommonTransaction{ TransactionHash: utils.TestHexToFelt(t, "0x46a9f52a96b2d226407929e04cb02507e531f7c78b9196fc8c910351d8c33f3"), BroadcastedTxnCommonProperties: BroadcastedTxnCommonProperties{ @@ -368,6 +368,63 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) { }, } + var fullBlockGoerli848622 = Block{ + BlockHeader: BlockHeader{ + BlockHash: utils.TestHexToFelt(t, "0x32964e2e407bb9e71b2de8d9d9829b0537df7c4624e1816e6cece80781ab9cc"), + ParentHash: utils.TestHexToFelt(t, "0xecbed6cfe85c77f2f8acefe2effbda817f71ca7457f7ece8262d65cc87a9f7"), + SequencerAddress: utils.TestHexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"), + BlockNumber: 848622, + NewRoot: utils.TestHexToFelt(t, "07c4302f09f6a72129679378e9b8d6c67774c5c4e80b1fc186da114f71637b2e"), + Timestamp: 1692416283, + }, + Status: "ACCEPTED_ON_L1", + Transactions: []Transaction{ + DeclareTxnV1{ + CommonTransaction: CommonTransaction{ + TransactionHash: utils.TestHexToFelt(t, "0x5ad2f85499ea92d33d4a44c8cd4640d1ee4e25c3ee6df0bdf0a76c12c052f0a"), + BroadcastedTxnCommonProperties: BroadcastedTxnCommonProperties{ + Type: TransactionType_Declare, + MaxFee: utils.TestHexToFelt(t, "0x27a64c6e425"), + Version: TransactionV1, + Signature: []*felt.Felt{utils.TestHexToFelt(t, "0x1454ab28f0bf18f0fd8002bc92169e6443feba6c605728c86850c0dcc9f6f9a"), utils.TestHexToFelt(t, "0xf545949c899ff1d16c61629996e898db2697a2e3e7fa9071b016500ca5c1d1")}, + Nonce: utils.TestHexToFelt(t, "0x333"), + }, + }, + ClassHash: utils.TestHexToFelt(t, "0x681076f783aa2b3faec6ce80bb5485a260ed1672007925e1d502b003aff2232"), + SenderAddress: utils.TestHexToFelt(t, "0x45dba6ce6a4dc3d2f31aa6da5f51007f1e43e84a1e62c4481bac5454dea4e6d"), + }, + }, + } + + var fullBlockGoerli849399 = Block{ + BlockHeader: BlockHeader{ + BlockHash: utils.TestHexToFelt(t, "0x6e5b26127400bac0cd1f3c2ab6e76850ec457c71b1f2fc7cda755bee8a1102a"), + ParentHash: utils.TestHexToFelt(t, "0x7cd085d4ab95b3307303cb836ab49c0fbc8d1f9befdcfdc65292d99c9466d05"), + SequencerAddress: utils.TestHexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"), + BlockNumber: 849399, + NewRoot: utils.TestHexToFelt(t, "0x239a44410e78665f41f7a65ef3b5ed244ce411965498a83f80f904e22df1045"), + Timestamp: 1692560305, + }, + Status: "ACCEPTED_ON_L1", + Transactions: []Transaction{ + DeclareTxnV2{ + CommonTransaction: CommonTransaction{ + TransactionHash: utils.TestHexToFelt(t, "0x45d04652ba51685b7b82fc17b3d5741a7c43992369c0b0aebd60916fa23b9b2"), + BroadcastedTxnCommonProperties: BroadcastedTxnCommonProperties{ + Type: TransactionType_Declare, + MaxFee: utils.TestHexToFelt(t, "0x50c8f30287c"), + Version: TransactionV2, + Signature: []*felt.Felt{utils.TestHexToFelt(t, "0x6be01a56087382337a29fd4577dd20fd82cc9f38f69b8d19e07fc101c3c5ad9"), utils.TestHexToFelt(t, "0x4c633a5582d3932fbfcea8abd45c7453e88a562f1a38877b9575d6a6b926ea2")}, + Nonce: utils.TestHexToFelt(t, "0xd"), + }, + }, + ClassHash: utils.TestHexToFelt(t, "0x6fda8f6630f44571cd6b398795351b37daf27adacbf6fe9357bd23ad19b22f3"), + CompiledClassHash: utils.TestHexToFelt(t, "0x4380d7c6511f81668530570a8b07bd2148808f90e681bb769549ec4faafef65"), + SenderAddress: utils.TestHexToFelt(t, "0x6ef69146f56205e27624a9933f31d6009198c1ea480070a790f16a5d928be92"), + }, + }, + } + testSet := map[string][]testSetType{ "mock": {}, "testnet": { @@ -396,6 +453,20 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) { LookupTxnPositionInExpected: 0, ExpectedBlockWithTxs: &fullBlockGoerli300114, }, + { + BlockID: WithBlockNumber(849399), + ExpectedError: nil, + LookupTxnPositionInOriginal: 71, + LookupTxnPositionInExpected: 0, + ExpectedBlockWithTxs: &fullBlockGoerli849399, + }, + { + BlockID: WithBlockNumber(848622), + ExpectedError: nil, + LookupTxnPositionInOriginal: 6, + LookupTxnPositionInExpected: 0, + ExpectedBlockWithTxs: &fullBlockGoerli848622, + }, }, "mainnet": {}, }[testEnv] @@ -515,10 +586,12 @@ func TestCaptureUnsupportedBlockTxn(t *testing.T) { _, okv1 := v.(InvokeTxnV1) _, okv0 := v.(InvokeTxnV0) _, okl1 := v.(L1HandlerTxn) - _, okdec := v.(DeclareTxn) + _, okdec0 := v.(DeclareTxnV0) + _, okdec1 := v.(DeclareTxnV1) + _, okdec2 := v.(DeclareTxnV2) _, okdep := v.(DeployTxn) _, okdepac := v.(DeployAccountTxn) - if !okv0 && !okv1 && !okl1 && !okdec && !okdep && !okdepac { + if !okv0 && !okv1 && !okl1 && !okdec0 && !okdec1 && !okdec2 && !okdep && !okdepac { t.Fatalf("New Type Detected %T at Block(%d)/Txn(%d)", v, i, k) } } diff --git a/rpc/transaction.go b/rpc/transaction.go index 6c9bf6c3..c8062399 100644 --- a/rpc/transaction.go +++ b/rpc/transaction.go @@ -21,9 +21,24 @@ func adaptTransaction(t TXN) (Transaction, error) { json.Unmarshal(txMarshalled, &tx) return tx, nil case TransactionType_Declare: - var tx DeclareTxn - json.Unmarshal(txMarshalled, &tx) - return tx, nil + + switch t.Version { + case &felt.Zero: + var tx DeclareTxnV0 + json.Unmarshal(txMarshalled, &tx) + return tx, nil + case felt.Zero.SetUint64(1): + var tx DeclareTxnV1 + json.Unmarshal(txMarshalled, &tx) + return tx, nil + case felt.Zero.SetUint64(2): + var tx DeclareTxnV2 + json.Unmarshal(txMarshalled, &tx) + return tx, nil + default: + return nil, errors.New("Internal error with adaptTransaction()") + } + case TransactionType_DeployAccount: var tx DeployAccountTxn json.Unmarshal(txMarshalled, &tx) diff --git a/rpc/types_contract.go b/rpc/types_contract.go index a929acf1..16550cc5 100644 --- a/rpc/types_contract.go +++ b/rpc/types_contract.go @@ -27,6 +27,25 @@ type EntryPointsByType struct { L1Handler []EntryPoint `json:"L1_HANDLER"` } +type DeprecatedContractClass struct { + // Program A base64 representation of the compressed program code + Program string `json:"program"` + + EntryPointsByType EntryPointsByType `json:"entry_points_by_type"` + + ABI *ABI `json:"abi,omitempty"` +} + +type ContractClassTemp struct { + SierraProgram []*felt.Felt `json:"sierra_program"` + + ContractClassVersion string `json:"contract_class_version"` + + EntryPointsByType EntryPointsByType `json:"entry_points_by_type"` + + ABI *ABI `json:"abi,omitempty"` +} + type ContractClass struct { // Program A base64 representation of the compressed program code Program string `json:"program"` diff --git a/rpc/types_transaction.go b/rpc/types_transaction.go index 6c8c6e66..cc6040b6 100644 --- a/rpc/types_transaction.go +++ b/rpc/types_transaction.go @@ -93,17 +93,48 @@ func (tx L1HandlerTxn) Hash() *felt.Felt { return tx.TransactionHash } -type DeclareTxn struct { +type DeclareTxnV0 struct { + CommonTransaction + + // SenderAddress the address of the account contract sending the declaration transaction + SenderAddress *felt.Felt `json:"sender_address"` + + DeprecatedContractClass `json:"contract_class,omitempty"` + ClassHash *felt.Felt `json:"class_hash,omitempty"` +} + +type DeclareTxnV1 struct { CommonTransaction // ClassHash the hash of the declared class ClassHash *felt.Felt `json:"class_hash"` + DeprecatedContractClass `json:"contract_class"` + // SenderAddress the address of the account contract sending the declaration transaction SenderAddress *felt.Felt `json:"sender_address"` } -func (tx DeclareTxn) Hash() *felt.Felt { +type DeclareTxnV2 struct { + CommonTransaction + + // SenderAddress the address of the account contract sending the declaration transaction + SenderAddress *felt.Felt `json:"sender_address"` + + CompiledClassHash *felt.Felt `json:"compiled_class_hash"` + + ContractClassTemp `json:"contract_class,omitempty"` + ClassHash *felt.Felt `json:"class_hash,omitempty"` +} + +func (tx DeclareTxnV0) Hash() *felt.Felt { + return tx.TransactionHash +} + +func (tx DeclareTxnV1) Hash() *felt.Felt { + return tx.TransactionHash +} +func (tx DeclareTxnV2) Hash() *felt.Felt { return tx.TransactionHash } @@ -195,9 +226,24 @@ func unmarshalTxn(t interface{}) (Transaction, error) { case map[string]interface{}: switch TransactionType(casted["type"].(string)) { case TransactionType_Declare: - var txn DeclareTxn - remarshal(casted, &txn) - return txn, nil + + switch TransactionType(casted["version"].(string)) { + case "0x0": + var txn DeclareTxnV0 + remarshal(casted, &txn) + return txn, nil + case "0x1": + var txn DeclareTxnV1 + remarshal(casted, &txn) + return txn, nil + case "0x2": + var txn DeclareTxnV2 + remarshal(casted, &txn) + return txn, nil + default: + return nil, errors.New("Internal error with Declare transaction version and unmarshalTxn()") + } + case TransactionType_Deploy: var txn DeployTxn remarshal(casted, &txn) @@ -245,6 +291,7 @@ type TransactionVersion string const ( TransactionV0 TransactionVersion = "0x0" TransactionV1 TransactionVersion = "0x1" + TransactionV2 TransactionVersion = "0x2" ) func (v *TransactionVersion) BigInt() (*big.Int, error) {