Skip to content

Commit

Permalink
Add validation rules to rpc.Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored and IronGauntlets committed Jul 7, 2023
1 parent 681e2af commit a4e7477
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/validator"
"github.com/ethereum/go-ethereum/common"
"github.com/go-playground/validator/v10"
"github.com/sourcegraph/conc"
)

Expand Down Expand Up @@ -248,7 +248,7 @@ func makeRPC(httpPort, wsPort uint16, rpcHandler *rpc.Handler, log utils.SimpleL
},
}

jsonrpcServer := jsonrpc.NewServer().WithValidator(validator.New())
jsonrpcServer := jsonrpc.NewServer().WithValidator(validator.Validator())
for _, method := range methods {
if err := jsonrpcServer.RegisterMethod(method); err != nil {
return nil, err
Expand Down
28 changes: 15 additions & 13 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@ func (t *TransactionType) UnmarshalJSON(data []byte) error {
}

// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L1252
//
//nolint:lll
type Transaction struct {
Hash *felt.Felt `json:"transaction_hash,omitempty"`
Type TransactionType `json:"type"`
Version *felt.Felt `json:"version,omitempty"`
Nonce *felt.Felt `json:"nonce,omitempty"`
MaxFee *felt.Felt `json:"max_fee,omitempty"`
Type TransactionType `json:"type" validate:"required"`
Version *felt.Felt `json:"version,omitempty" validate:"required"`
Nonce *felt.Felt `json:"nonce,omitempty" validate:"required_unless=Version 0x0"`
MaxFee *felt.Felt `json:"max_fee,omitempty" validate:"required"`
ContractAddress *felt.Felt `json:"contract_address,omitempty"`
ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty"`
ClassHash *felt.Felt `json:"class_hash,omitempty"`
ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty"`
SenderAddress *felt.Felt `json:"sender_address,omitempty"`
Signature *[]*felt.Felt `json:"signature,omitempty"`
CallData *[]*felt.Felt `json:"calldata,omitempty"`
EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty"`
CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty"`
ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty" validate:"required_if=Type DEPLOY,required_if=Type DEPLOY_ACCOUNT"`
ClassHash *felt.Felt `json:"class_hash,omitempty" validate:"required_if=Type DEPLOY,required_if=Type DEPLOY_ACCOUNT"`
ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty" validate:"required_if=Type DEPLOY,required_if=Type DEPLOY_ACCOUNT"`
SenderAddress *felt.Felt `json:"sender_address,omitempty" validate:"required_if=Type DECLARE,required_if=Type INVOKE Version 0x1"`
Signature *[]*felt.Felt `json:"signature,omitempty" validate:"required"`
CallData *[]*felt.Felt `json:"calldata,omitempty" validate:"required_if=Type INVOKE"`
EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty" validate:"required_if=Type INVOKE Version 0x0"`
CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty" validate:"required_if=Type DECLARE Version 0x2"`
}

type MsgToL1 struct {
Expand Down Expand Up @@ -124,7 +126,7 @@ type DeclareTxResponse struct {
// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L1273-L1287
type BroadcastedTransaction struct {
Transaction
ContractClass json.RawMessage `json:"contract_class,omitempty"`
ContractClass json.RawMessage `json:"contract_class,omitempty" validate:"required_if=Transaction.Type DECLARE"`
}

type FeeEstimate struct {
Expand Down
40 changes: 40 additions & 0 deletions validator/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package validator

import (
"reflect"
"sync"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/rpc"
"github.com/go-playground/validator/v10"
)

var (
once sync.Once
v *validator.Validate
)

// Validator returns a singleton that can be used to validate various objects
func Validator() *validator.Validate {
once.Do(func() {
v = validator.New()
// Register these types to use their string representation for validation
// purposes
v.RegisterCustomTypeFunc(func(field reflect.Value) any {
switch f := field.Interface().(type) {
case felt.Felt:
return f.String()
case *felt.Felt:
return f.String()
}
panic("not a felt")
}, felt.Felt{}, &felt.Felt{})
v.RegisterCustomTypeFunc(func(field reflect.Value) any {
if t, ok := field.Interface().(rpc.TransactionType); ok {
return t.String()
}
panic("not a TransactionType")
}, rpc.TransactionType(0))
})
return v
}

0 comments on commit a4e7477

Please sign in to comment.