Skip to content

Commit

Permalink
soroban-rpc: Fix set_authorization_entries bug in preflight (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms authored Sep 8, 2023
1 parent dce1986 commit 91595de
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
58 changes: 58 additions & 0 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,64 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
assert.Equal(t, result, resultForRequestWithDifferentTxSource)
}

func TestSimulateTransactionWithAuth(t *testing.T) {
test := NewTest(t)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
account := txnbuild.NewSimpleAccount(address, 0)

helloWorldContract := getHelloWorldContract(t)

params := preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(account.AccountID, helloWorldContract),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})

tx, err := txnbuild.NewTransaction(params)
assert.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)

deployContractOp := createCreateContractOperation(t, address, helloWorldContract, StandaloneNetworkPassphrase)
deployContractParams := txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
deployContractOp,
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
}
response := simulateTransactionFromTxParams(t, client, deployContractParams)
require.NotEmpty(t, response.Results)
require.Len(t, response.Results[0].Auth, 1)
require.Empty(t, deployContractOp.Auth)

var auth xdr.SorobanAuthorizationEntry
assert.NoError(t, xdr.SafeUnmarshalBase64(response.Results[0].Auth[0], &auth))
require.Equal(t, auth.Credentials.Type, xdr.SorobanCredentialsTypeSorobanCredentialsSourceAccount)
deployContractOp.Auth = append(deployContractOp.Auth, auth)
deployContractParams.Operations = []txnbuild.Operation{deployContractOp}

// preflight deployContractOp with auth
deployContractParams = preflightTransactionParams(t, client, deployContractParams)
tx, err = txnbuild.NewTransaction(deployContractParams)
assert.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)
}

func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
test := NewTest(t)

Expand Down
13 changes: 6 additions & 7 deletions cmd/soroban-rpc/lib/preflight/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub(crate) fn preflight_invoke_hf_op(
.context("cannot create budget")?;
let storage = Storage::with_recording_footprint(ledger_storage_rc.clone());
let host = Host::with_storage_and_budget(storage, budget);
host.set_source_account(source_account.clone())
.context("cannot set source account")?;
host.set_diagnostic_level(DiagnosticLevel::Debug)
.context("cannot set debug diagnostic level")?;
host.set_ledger_info(ledger_info.clone())
.context("cannot set ledger info")?;

// We make an assumption here:
// - if a transaction doesn't include any soroban authorization entries the client either
Expand All @@ -63,13 +69,6 @@ pub(crate) fn preflight_invoke_hf_op(
.context("cannot set authorization entries")?;
}

host.set_diagnostic_level(DiagnosticLevel::Debug)
.context("cannot set debug diagnostic level")?;
host.set_source_account(source_account.clone())
.context("cannot set source account")?;
host.set_ledger_info(ledger_info.clone())
.context("cannot set ledger info")?;

// Run the preflight.
let maybe_result = host
.invoke_function(invoke_hf_op.host_function.clone())
Expand Down

0 comments on commit 91595de

Please sign in to comment.