-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update transactionCommitmentPoseidon function to handle differe…
…nt transaction types (#2153) * feat: Update transactionCommitmentPoseidon function to handle different transaction types The code changes in `transactionCommitmentPoseidon` function update the logic to handle different transaction types. With the changes, the function now checks the type of the transaction and updates the digest accordingly. For `DeployTransaction` and `L1HandlerTransaction`, the digest is updated with a zero value, while for other transaction types, the digest is updated with the transaction signature. * Added tests * Replace dynamic hashes with hardcoded value in test --------- Co-authored-by: Kirill <[email protected]>
- Loading branch information
1 parent
72535cc
commit 67430de
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTransactionCommitmentPoseidon(t *testing.T) { | ||
t.Run("nil", func(t *testing.T) { | ||
c, err := transactionCommitmentPoseidon(nil) | ||
require.NoError(t, err) | ||
assert.Equal(t, &felt.Zero, c) | ||
}) | ||
t.Run("txs with signature", func(t *testing.T) { | ||
var txs []Transaction | ||
|
||
type signature = []*felt.Felt | ||
// actual tx hash is irrelevant so it's ok to have different transactions with the same hash | ||
txHash := utils.HexToFelt(t, "0xCAFEBABE") | ||
// nil signature, empty signature and signature with some non-empty value | ||
for _, sign := range []signature{nil, make(signature, 0), {new(felt.Felt).SetUint64(uint64(3))}} { | ||
invoke := &InvokeTransaction{ | ||
TransactionHash: txHash, | ||
TransactionSignature: sign, | ||
} | ||
deployAccount := &DeployAccountTransaction{ | ||
DeployTransaction: DeployTransaction{ | ||
TransactionHash: txHash, | ||
}, | ||
TransactionSignature: sign, | ||
} | ||
declare := &DeclareTransaction{ | ||
TransactionHash: txHash, | ||
TransactionSignature: sign, | ||
} | ||
txs = append(txs, invoke, deployAccount, declare) | ||
} | ||
|
||
c, err := transactionCommitmentPoseidon(txs) | ||
require.NoError(t, err) | ||
expected := utils.HexToFelt(t, "0x4ca6d4ceb367bf070d896a1479190d3c7b751f525e69a46ee2c83f0afe7cb8") | ||
assert.Equal(t, expected, c, "expected: %s, got: %s", expected, c) | ||
}) | ||
t.Run("txs without signature", func(t *testing.T) { | ||
txs := []Transaction{ | ||
&L1HandlerTransaction{ | ||
TransactionHash: utils.HexToFelt(t, "0x1"), | ||
}, | ||
&DeployTransaction{ | ||
TransactionHash: utils.HexToFelt(t, "0x2"), | ||
}, | ||
} | ||
|
||
c, err := transactionCommitmentPoseidon(txs) | ||
require.NoError(t, err) | ||
expected := utils.HexToFelt(t, "0x6e067f82eefc8efa75b4ad389253757f4992eee0f81f0b43815fa56135ca801") | ||
assert.Equal(t, expected, c, "expected: %s, got: %s", expected, c) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters