Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC v0.8.0 #629

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/test_account.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: cd account && go test -timeout 600s -v -env devnet .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "http://0.0.0.0:5050"
INTEGRATION_BASE: "http://localhost:5050"

# Test Account on mock
- name: Test Account with mocks
Expand Down
2 changes: 1 addition & 1 deletion account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func (account *Account) WaitForTransactionReceipt(ctx context.Context, transacti
for {
select {
case <-ctx.Done():
return nil, rpc.Err(rpc.InternalError, ctx.Err())
return nil, rpc.Err(rpc.InternalError, &rpc.RPCData{Message: ctx.Err().Error()})
case <-t.C:
receiptWithBlockInfo, err := account.TransactionReceipt(ctx, transactionHash)
if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) {
ShouldCallTransactionReceipt: true,
Hash: new(felt.Felt).SetUint64(1),
ExpectedReceipt: nil,
ExpectedErr: rpc.Err(rpc.InternalError, "UnExpectedErr"),
ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "UnExpectedErr"}),
},
{
Timeout: time.Duration(1000),
Expand All @@ -1010,7 +1010,7 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) {
Hash: new(felt.Felt).SetUint64(3),
ShouldCallTransactionReceipt: false,
ExpectedReceipt: nil,
ExpectedErr: rpc.Err(rpc.InternalError, context.DeadlineExceeded),
ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: context.DeadlineExceeded.Error()}),
},
},
}[testEnv]
Expand Down Expand Up @@ -1067,7 +1067,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
type testSetType struct {
Timeout int
Hash *felt.Felt
ExpectedErr error
ExpectedErr *rpc.RPCError
ExpectedReceipt rpc.TransactionReceipt
}
testSet := map[string][]testSetType{
Expand All @@ -1076,7 +1076,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
Timeout: 3, // Should poll 3 times
Hash: new(felt.Felt).SetUint64(100),
ExpectedReceipt: rpc.TransactionReceipt{},
ExpectedErr: rpc.Err(rpc.InternalError, "Post \"http://0.0.0.0:5050/\": context deadline exceeded"),
ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "Post \"http://localhost:5050\": context deadline exceeded"}),
},
},
}[testEnv]
Expand All @@ -1087,11 +1087,13 @@ func TestWaitForTransactionReceipt(t *testing.T) {

resp, err := acnt.WaitForTransactionReceipt(ctx, test.Hash, 1*time.Second)
if test.ExpectedErr != nil {
require.Equal(t, test.ExpectedErr.Error(), err.Error())
rpcErr, ok := err.(*rpc.RPCError)
require.True(t, ok)
require.Equal(t, test.ExpectedErr.Code, rpcErr.Code)
require.Equal(t, test.ExpectedErr.Data.Message, rpcErr.Data.Message)
} else {
require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (*resp).ExecutionStatus)
}

}
}

Expand Down
45 changes: 45 additions & 0 deletions mocks/mock_rpc_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,20 @@ func (provider *Provider) BlockWithReceipts(ctx context.Context, blockID BlockID

var m map[string]interface{}
if err := json.Unmarshal(result, &m); err != nil {
return nil, Err(InternalError, err.Error())
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}

// PendingBlockWithReceipts doesn't contain a "status" field
if _, ok := m["status"]; ok {
var block BlockWithReceipts
if err := json.Unmarshal(result, &block); err != nil {
return nil, Err(InternalError, err.Error())
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}
return &block, nil
} else {
var pendingBlock PendingBlockWithReceipts
if err := json.Unmarshal(result, &pendingBlock); err != nil {
return nil, Err(InternalError, err.Error())
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}
return &pendingBlock, nil
}
Expand Down
1 change: 0 additions & 1 deletion rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,4 @@ func validatePendingBlockHeader(t *testing.T, pBlock *PendingBlockHeader) {
require.NotZero(t, pBlock.L1GasPrice)
require.NotZero(t, pBlock.StarknetVersion)
require.NotZero(t, pBlock.L1DataGasPrice)
require.NotNil(t, pBlock.L1DAMode)
}
7 changes: 5 additions & 2 deletions rpc/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCall(t *testing.T) {
FunctionCall FunctionCall
BlockID BlockID
ExpectedPatternResult *felt.Felt
ExpectedError error
ExpectedError *RPCError
}
testSet := map[string][]testSetType{
"devnet": {
Expand Down Expand Up @@ -111,7 +111,10 @@ func TestCall(t *testing.T) {
require := require.New(t)
output, err := testConfig.provider.Call(context.Background(), FunctionCall(test.FunctionCall), test.BlockID)
if test.ExpectedError != nil {
require.EqualError(test.ExpectedError, err.Error())
rpcErr, ok := err.(*RPCError)
require.True(ok)
require.Equal(test.ExpectedError.Code, rpcErr.Code)
require.Equal(test.ExpectedError.Message, rpcErr.Message)
} else {
require.NoError(err)
require.NotEmpty(output, "should return an output")
Expand Down
4 changes: 2 additions & 2 deletions rpc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func (provider *Provider) Syncing(ctx context.Context) (*SyncStatus, error) {
var result interface{}
// Note: []interface{}{}...force an empty `params[]` in the jsonrpc request
if err := provider.c.CallContext(ctx, &result, "starknet_syncing", []interface{}{}...); err != nil {
return nil, Err(InternalError, err)
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}
switch res := result.(type) {
case bool:
return &SyncStatus{SyncStatus: &res}, nil
case SyncStatus:
return &res, nil
default:
return nil, Err(InternalError, "internal error with starknet_syncing")
return nil, Err(InternalError, &RPCData{Message: "internal error with starknet_syncing"})
}

}
34 changes: 31 additions & 3 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contract
func typecastClassOutput(rawClass map[string]any) (ClassOutput, error) {
rawClassByte, err := json.Marshal(rawClass)
if err != nil {
return nil, Err(InternalError, err)
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}

// if contract_class_version exists, then it's a ContractClass type
if _, exists := (rawClass)["contract_class_version"]; exists {
var contractClass ContractClass
err = json.Unmarshal(rawClassByte, &contractClass)
if err != nil {
return nil, Err(InternalError, err)
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}
return &contractClass, nil
}
var depContractClass DeprecatedContractClass
err = json.Unmarshal(rawClassByte, &depContractClass)
if err != nil {
return nil, Err(InternalError, err)
return nil, Err(InternalError, &RPCData{Message: err.Error()})
}
return &depContractClass, nil
}
Expand Down Expand Up @@ -135,6 +135,15 @@ func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAd
// Estimates the resources required by a given sequence of transactions when applied on a given state.
// If one of the transactions reverts or fails due to any reason (e.g. validation failure or an internal error),
// a TRANSACTION_EXECUTION_ERROR is returned. For v0-2 transactions the estimate is given in wei, and for v3 transactions it is given in fri.
//
// Parameters:
// - ctx: The context of the function call
// - requests: A sequence of transactions to estimate, running each transaction on the state resulting from applying all the previous ones
// - simulationFlags: Describes what parts of the transaction should be executed
// - blockID: The hash of the requested block, or number (height) of the requested block, or a block tag, for the block referencing the state or call the transaction on
// Returns:
// - []FeeEstimation: A sequence of fee estimation where the i'th estimate corresponds to the i'th transaction
// - error: An error if any occurred during the execution
func (provider *Provider) EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlags []SimulationFlag, blockID BlockID) ([]FeeEstimation, error) {
var raw []FeeEstimation
if err := do(ctx, provider.c, "starknet_estimateFee", &raw, requests, simulationFlags, blockID); err != nil {
Expand All @@ -160,3 +169,22 @@ func (provider *Provider) EstimateMessageFee(ctx context.Context, msg MsgFromL1,
}
return &raw, nil
}

// Get merkle paths in one of the state tries: global state, classes, individual contract.
// A single request can query for any mix of the three types of storage proofs (classes, contracts, and storage)
//
// Parameters:
// - ctx: The context of the function call
// - storageProofInput: an input containing at least one of the fields filled
// Returns:
// - *StorageProofResult: The requested storage proofs. Note that if a requested leaf has the default value,
// the path to it may end in an edge node whose path is not a prefix of the requested leaf, thus effectively proving non-membership
// - error: an error if any occurred during the execution
func (provider *Provider) GetStorageProof(ctx context.Context, storageProofInput StorageProofInput) (*StorageProofResult, error) {
var raw StorageProofResult
if err := do(ctx, provider.c, "starknet_getStorageProof", &raw, storageProofInput); err != nil {

return nil, tryUnwrapToRPCErr(err, ErrBlockNotFound, ErrStorageProofNotSupported)
}
return &raw, nil
}
Loading
Loading