Skip to content

Commit

Permalink
feat: Update transactionCommitmentPoseidon function to handle differe…
Browse files Browse the repository at this point in the history
…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
AnkushinDaniil and kirugan authored Sep 24, 2024
1 parent 72535cc commit 67430de
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
63 changes: 63 additions & 0 deletions core/block_pkg_test.go
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)
})
}
7 changes: 4 additions & 3 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,11 @@ func transactionCommitmentPoseidon(transactions []Transaction) (*felt.Felt, erro
var digest crypto.PoseidonDigest
digest.Update(transaction.Hash())

if txSignature := transaction.Signature(); len(txSignature) > 0 {
digest.Update(txSignature...)
} else {
switch transaction.(type) {
case *DeployTransaction, *L1HandlerTransaction:
digest.Update(&felt.Zero)
default:
digest.Update(transaction.Signature()...)
}

if _, err := trie.Put(new(felt.Felt).SetUint64(uint64(i)), digest.Finish()); err != nil {
Expand Down

0 comments on commit 67430de

Please sign in to comment.