Skip to content

Commit

Permalink
Fix bug in weight >= threshold check
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Jan 22, 2020
1 parent 0a9dbc8 commit 99529e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions txnbuild/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,12 @@ func VerifyChallengeTxThreshold(challengeTx, serverAccountID, network string, th
return nil, err
}

weight := Threshold(0)
weight := int32(0)
for _, s := range signersFound {
weight += Threshold(signerSummary[s])
weight += signerSummary[s]
}

if weight < threshold {
if weight < int32(threshold) {
return nil, errors.Errorf("signers with weight %d do not meet threshold %d", weight, threshold)
}

Expand Down
38 changes: 38 additions & 0 deletions txnbuild/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,44 @@ func TestVerifyChallengeTxThreshold_invalidNoSigners(t *testing.T) {
assert.EqualError(t, err, "no signers provided")
}

func TestVerifyChallengeTxThreshold_weightsAddToMoreThan8Bits(t *testing.T) {
serverKP := newKeypair0()
clientKP1 := newKeypair1()
clientKP2 := newKeypair2()
txSource := NewSimpleAccount(serverKP.Address(), -1)
opSource := NewSimpleAccount(clientKP1.Address(), 0)
op := ManageData{
SourceAccount: &opSource,
Name: "testserver auth",
Value: []byte(base64.StdEncoding.EncodeToString(make([]byte, 48))),
}
tx := Transaction{
SourceAccount: &txSource,
Operations: []Operation{&op},
Timebounds: NewTimeout(1000),
Network: network.TestNetworkPassphrase,
}
threshold := Threshold(1)
signerSummary := SignerSummary{
clientKP1.Address(): 255,
clientKP2.Address(): 1,
}
wantSigners := []string{
clientKP1.Address(),
clientKP2.Address(),
}

err := tx.Build()
require.NoError(t, err)
err = tx.Sign(serverKP, clientKP1, clientKP2)
assert.NoError(t, err)
tx64, err := tx.Base64()
require.NoError(t, err)
signersFound, err := VerifyChallengeTxThreshold(tx64, serverKP.Address(), network.TestNetworkPassphrase, threshold, signerSummary)
assert.Equal(t, wantSigners, signersFound)
assert.NoError(t, err)
}

func TestVerifyChallengeTxSigners_validServerAndClientMasterKey(t *testing.T) {
serverKP := newKeypair0()
clientKP := newKeypair1()
Expand Down

0 comments on commit 99529e7

Please sign in to comment.