Skip to content

Commit

Permalink
fix: return value from inc and try to figure out why bump doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Sep 13, 2023
1 parent 300d5c9 commit b9f132c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Contract {
addr
}

pub fn inc(env: Env) {
pub fn inc(env: Env) -> u32 {
let mut count: u32 = env.storage().persistent().get(&COUNTER).unwrap_or(0); // Panic if the value of COUNTER is not u32.
log!(&env, "count: {}", count);

Expand All @@ -39,6 +39,7 @@ impl Contract {

// Save the count.
env.storage().persistent().set(&COUNTER, &count);
count
}

#[allow(unused_variables)]
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Cmd {
.await?;

tracing::trace!(?result);
tracing::trace!(?meta);
tracing::trace!("Meta:\n {meta:#?}");
if !events.is_empty() {
tracing::info!("Events:\n {events:#?}");
}
Expand Down
34 changes: 20 additions & 14 deletions cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,26 @@ func TestCLIContractDeploy(t *testing.T) {

func TestCLIContractDeployAndInvoke(t *testing.T) {
NewCLITest(t)
output := runSuccessfulCLICmd(t, "contract deploy --salt=0 --wasm "+helloWorldContractPath)
contractID := strings.TrimSpace(output)
output = runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
contractID := runSuccessfulCLICmd(t, "contract deploy --salt=0 --wasm "+helloWorldContractPath)
output := runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
require.Contains(t, output, `["Hello","world"]`)
}

func TestCLISimulateTransactionBumpAndRestoreFootprint(t *testing.T) {
test := NewCLITest(t)
output := runSuccessfulCLICmd(t, "contract deploy --salt=0 --wasm "+helloWorldContractPath)
contractID := strings.TrimSpace(output)
contractID := runSuccessfulCLICmd(t, "contract deploy --salt=0 --wasm "+helloWorldContractPath)
count := runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- inc", contractID))
initialExpiration := bumpSym(t, contractID, "COUNTER", "--durability persistent --ledgers-to-expire 0")
newExpiration := bumpSym(t, contractID, "COUNTER", "--durability persistent --ledgers-to-expire 20")
require.Equal(t, initialExpiration+20, newExpiration)
require.Equal(t, "1", count)
waitForKeyToExpire(t, test, getTestContractIDFromAccountAndSalt(t, 0), symToScVal(xdr.ScSymbol("COUNTER")))
count = runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- inc", contractID))
require.Equal(t, "2", count)
key, err := xdr.MarshalBase64(symToScVal(xdr.ScSymbol("COUNTER")))
require.NoError(t, err)
initialExpiration := bumpBase64(t, contractID, key, "--durability persistent --ledgers-to-expire 1")
newExpiration := bumpSym(t, contractID, "COUNTER", "--durability persistent --ledgers-to-expire 20")
require.Greater(t, newExpiration, initialExpiration)
waitForKeyToExpire(t, test, getTestContractIDFromAccountAndSalt(t, 0), symToScVal(xdr.ScSymbol("COUNTER")))
count = runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- inc", contractID))
require.Equal(t, "3", count)
}

func symToScVal(s xdr.ScSymbol) xdr.ScVal {
Expand Down Expand Up @@ -158,12 +160,15 @@ func deploy(t *testing.T, wasmPath string, id uint32) string {
}

func bumpBase64(t *testing.T, contractID string, keyBase64 string, args string) uint64 {
return parseInt(t, runSuccessfulCLICmd(t, fmt.Sprintf("contract bump --id=%s --ledger_expiration_only --key-xdr=%s %s", contractID, keyBase64, args)))
return bump(t, contractID, fmt.Sprintf("--key-xdr=%s %s", keyBase64, args))
}

func bumpSym(t *testing.T, contractID string, sym string, args string) uint64 {
return parseInt(t, runSuccessfulCLICmd(t, fmt.Sprintf("contract bump --id=%s --ledger_expiration_only --key=%s %s", contractID, sym, args)))
return bump(t, contractID, fmt.Sprintf("--key=%s %s", sym, args))
}

func bump(t *testing.T, contractID string, args string) uint64 {
return parseInt(t, runSuccessfulCLICmd(t, fmt.Sprintf("contract bump --id=%s --ledger-expiration-only %s", contractID, args)))
}

func parseInt(t *testing.T, s string) uint64 {
Expand All @@ -178,9 +183,10 @@ func parseInt(t *testing.T, s string) uint64 {

func runSuccessfulCLICmd(t *testing.T, cmd string) string {
res := runCLICommand(t, cmd)
require.NoError(t, res.Error, fmt.Sprintf("stderr:\n%s\nstdout:\n%s\n", res.Stderr(), res.Stdout()))
println(fmt.Sprintf("stderr:\n%s\nstdout:\n-------\n%s\n-----\n", res.Stderr(), res.Stdout()))
return res.Stdout()
stdout, stderr := res.Stdout(), res.Stderr()
require.NoError(t, res.Error, fmt.Sprintf("stderr:\n%s\nstdout:\n%s\n", stderr, stdout))
println(fmt.Sprintf("stderr:\n%s\nstdout:\n-------\n%s\n-----\n", stderr, stdout))
return strings.TrimSpace(stdout)
}

func cliCmd(t *testing.T, cmd string) icmd.Cmd {
Expand Down

0 comments on commit b9f132c

Please sign in to comment.