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

Add integration test for extra signers using tx preconditions #4320

Merged
merged 5 commits into from
Apr 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package integration

import (
"strconv"
"testing"
"time"

sdk "github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/services/horizon/internal/test/integration"
"github.com/stellar/go/txnbuild"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)

func TestTransactionPreconditionsMinSeq(t *testing.T) {
Expand All @@ -33,7 +32,11 @@ func TestTransactionPreconditionsMinSeq(t *testing.T) {

// Now the transaction should be submitted without problems
txParams.Preconditions.MinSequenceNumber = &currentAccountSeq
itest.MustSubmitTransaction(master, txParams)
tx := itest.MustSubmitTransaction(master, txParams)

txHistory, err := itest.Client().TransactionDetail(tx.Hash)
assert.NoError(t, err)
assert.Equal(t, txHistory.Preconditions.MinAccountSequence, strconv.FormatInt(*txParams.Preconditions.MinSequenceNumber, 10))
}

func TestTransactionPreconditionsTimeBounds(t *testing.T) {
Expand Down Expand Up @@ -63,7 +66,52 @@ func TestTransactionPreconditionsTimeBounds(t *testing.T) {
// Now the transaction should be submitted without problems, min < current tx submit time < max
txParams.Preconditions.TimeBounds.MinTime = time.Now().Unix() - 3600
txParams.Preconditions.TimeBounds.MaxTime = time.Now().Unix() + 3600
itest.MustSubmitTransaction(master, txParams)
tx := itest.MustSubmitTransaction(master, txParams)

txHistory, err := itest.Client().TransactionDetail(tx.Hash)
assert.NoError(t, err)
historyMaxTime, err := time.Parse(time.RFC3339, txHistory.Preconditions.Timebounds.MaxTime)
assert.NoError(t, err)
historyMinTime, err := time.Parse(time.RFC3339, txHistory.Preconditions.Timebounds.MinTime)
assert.NoError(t, err)

assert.Equal(t, historyMaxTime.UTC().Unix(), txParams.Preconditions.TimeBounds.MaxTime)
assert.Equal(t, historyMinTime.UTC().Unix(), txParams.Preconditions.TimeBounds.MinTime)
}

func TestTransactionPreconditionsExtraSigners(t *testing.T) {
tt := assert.New(t)
itest := integration.NewTest(t, integration.Config{})
if itest.GetEffectiveProtocolVersion() < 19 {
t.Skip("Can't run with protocol < 19")
}
master := itest.Master()
masterAccount := itest.MasterAccount()

// create a new signed payload signer
addtlSigners, addtlAccounts := itest.CreateAccounts(1, "1000")

// build a tx with seqnum based on master.seqNum+1 as source account
latestMasterAccount := itest.MustGetAccount(master)
currentAccountSeq, err := latestMasterAccount.GetSequenceNumber()
tt.NoError(err)
txParams := buildTXParams(master, masterAccount, currentAccountSeq, currentAccountSeq+1)

// this errors because the tx preconditions require extra signer that
// didn't sign this tx
txParams.Preconditions.ExtraSigners = []string{addtlAccounts[0].GetAccountID()}
_, err = itest.SubmitMultiSigTransaction([]*keypair.Full{master}, txParams)
tt.Error(err)

// Now the transaction should be submitted without problems, the extra signer specified
// has also signed this transaction.
txParams.Preconditions.ExtraSigners = []string{addtlAccounts[0].GetAccountID()}
tx, err := itest.SubmitMultiSigTransaction([]*keypair.Full{master, addtlSigners[0]}, txParams)
tt.NoError(err)
Copy link
Contributor

@Shaptic Shaptic Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make sure that retrieving the tx by ID shows the extra signers in the response, otherwise we aren't really testing Horizon at all besides txsub

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great suggestion, I added round trip asserts on the precond values in each of the test cases, thanks!


txHistory, err := itest.Client().TransactionDetail(tx.Hash)
assert.NoError(t, err)
assert.ElementsMatch(t, txHistory.Preconditions.ExtraSigners, txParams.Preconditions.ExtraSigners)
}

func buildTXParams(master *keypair.Full, masterAccount txnbuild.Account, sourceAccountSeq int64, txSequence int64) txnbuild.TransactionParams {
Expand Down