Skip to content

Commit

Permalink
Gracefully handle tx with incomplete cairo0 class (#2286)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Nov 26, 2024
1 parent d2031ca commit bccf025
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion rpc/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ func adaptDeclaredClass(declaredClass json.RawMessage) (core.Class, error) {
}
return sn2core.AdaptCairo1Class(feederClass.V1, compiledClass)
case feederClass.V0 != nil:
program := feederClass.V0.Program

// strip the quotes
base64Program := string(feederClass.V0.Program[1 : len(feederClass.V0.Program)-1])
if len(program) < 2 {
return nil, errors.New("invalid program")
}
base64Program := string(program[1 : len(program)-1])

feederClass.V0.Program, err = utils.Gzip64Decode(base64Program)
if err != nil {
return nil, err
Expand Down
27 changes: 27 additions & 0 deletions rpc/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/jsonrpc"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/utils"
Expand Down Expand Up @@ -65,6 +66,32 @@ func TestEstimateFee(t *testing.T) {
}), err)
require.Equal(t, httpHeader.Get(rpc.ExecutionStepsHeader), "0")
})

t.Run("transaction with invalid contract class", func(t *testing.T) {
toFelt := func(hex string) *felt.Felt {
return utils.HexToFelt(t, hex)
}
invalidTx := rpc.BroadcastedTransaction{
Transaction: rpc.Transaction{
Type: rpc.TxnDeclare,
Version: toFelt("0x1"),
Nonce: toFelt("0x0"),
MaxFee: toFelt("0x1"),
SenderAddress: toFelt("0x2"),
Signature: &[]*felt.Felt{
toFelt("0x123"),
},
},
ContractClass: json.RawMessage(`{}`),
}
_, _, err := handler.EstimateFee([]rpc.BroadcastedTransaction{invalidTx}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
expectedErr := &jsonrpc.Error{
Code: jsonrpc.InvalidParams,
Message: "Invalid Params",
Data: "invalid program",
}
require.Equal(t, expectedErr, err)
})
}

func assertEqualCairo0Class(t *testing.T, cairo0Class *core.Cairo0Class, class *rpc.Class) {
Expand Down

0 comments on commit bccf025

Please sign in to comment.