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

txnbuild: more unit tests for FromXDR methods #1558

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion txnbuild/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func memoFromXDR(memo xdr.Memo) (Memo, error) {
newMemo = MemoHash(value)
memoCreated = ok
case xdr.MemoTypeMemoReturn:
value, ok := memo.GetHash()
value, ok := memo.GetRetHash()
Copy link
Member

Choose a reason for hiding this comment

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

It's awesome that these tests found this bug!

newMemo = MemoReturn(value)
memoCreated = ok
case xdr.MemoTypeMemoNone:
Expand Down
51 changes: 51 additions & 0 deletions txnbuild/memo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package txnbuild

import (
"testing"

"github.com/stellar/go/xdr"

"github.com/stretchr/testify/assert"
)

func TestMemoFromXDR(t *testing.T) {
// memo text
xdrMemo, err := xdr.NewMemo(xdr.MemoTypeMemoText, "abc123")
assert.NoError(t, err)
memo, err := memoFromXDR(xdrMemo)
if assert.NoError(t, err) {
assert.Equal(t, MemoText("abc123"), memo, "memo text should match")
}

// memo id
xdrMemo, err = xdr.NewMemo(xdr.MemoTypeMemoId, xdr.Uint64(1234))
assert.NoError(t, err)
memo, err = memoFromXDR(xdrMemo)
if assert.NoError(t, err) {
assert.Equal(t, MemoID(1234), memo, "memo id should match")
}

// memo hash
xdrMemo, err = xdr.NewMemo(xdr.MemoTypeMemoHash, xdr.Hash([32]byte{0x10}))
assert.NoError(t, err)
memo, err = memoFromXDR(xdrMemo)
if assert.NoError(t, err) {
assert.Equal(t, MemoHash([32]byte{0x10}), memo, "memo hash should match")
}

// memo return
xdrMemo, err = xdr.NewMemo(xdr.MemoTypeMemoReturn, xdr.Hash([32]byte{0x01}))
assert.NoError(t, err)
memo, err = memoFromXDR(xdrMemo)
if assert.NoError(t, err) {
assert.Equal(t, MemoReturn([32]byte{0x01}), memo, "memo return should match")
}

// memo none
xdrMemo, err = xdr.NewMemo(xdr.MemoTypeMemoNone, "")
assert.NoError(t, err)
memo, err = memoFromXDR(xdrMemo)
if assert.NoError(t, err) {
assert.Equal(t, nil, memo, "memo should be nil")
}
}
236 changes: 236 additions & 0 deletions txnbuild/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package txnbuild
import (
"testing"

"github.com/stellar/go/amount"
"github.com/stellar/go/xdr"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -179,3 +182,236 @@ func TestCreatePassiveSellOfferFromXDR(t *testing.T) {
}
}
}

func TestSetOptionsFromXDR(t *testing.T) {

var opSource xdr.AccountId
err := opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)
cFlags := xdr.Uint32(5)
sFlags := xdr.Uint32(7)
mw := xdr.Uint32(7)
lt := xdr.Uint32(2)
mt := xdr.Uint32(4)
ht := xdr.Uint32(6)
hDomain := xdr.String32("stellar.org")
var skey xdr.SignerKey
err = skey.SetAddress("GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3")
assert.NoError(t, err)
signer := xdr.Signer{
Key: skey,
Weight: xdr.Uint32(4),
}

xdrSetOptions := xdr.SetOptionsOp{
InflationDest: &opSource,
ClearFlags: &cFlags,
SetFlags: &sFlags,
MasterWeight: &mw,
LowThreshold: &lt,
MedThreshold: &mt,
HighThreshold: &ht,
HomeDomain: &hDomain,
Signer: &signer,
}

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeSetOptions,
SetOptionsOp: &xdrSetOptions,
},
}

var so SetOptions
err = so.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", so.SourceAccount.GetAccountID(), "source accounts should match")
assert.Equal(t, Threshold(7), *so.MasterWeight, "master weight should match")
assert.Equal(t, Threshold(2), *so.LowThreshold, "low threshold should match")
assert.Equal(t, Threshold(4), *so.MediumThreshold, "medium threshold should match")
assert.Equal(t, Threshold(6), *so.HighThreshold, "high threshold should match")
assert.Equal(t, "stellar.org", *so.HomeDomain, "Home domain should match")
assert.Equal(t, "GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3", so.Signer.Address, "Signer address should match")
assert.Equal(t, Threshold(4), so.Signer.Weight, "Signer weight should match")
assert.Equal(t, int(AuthRequired), int(so.SetFlags[0]), "Set AuthRequired flags should match")
assert.Equal(t, int(AuthRevocable), int(so.SetFlags[1]), "Set AuthRevocable flags should match")
assert.Equal(t, int(AuthImmutable), int(so.SetFlags[2]), "Set AuthImmutable flags should match")
assert.Equal(t, int(AuthRequired), int(so.ClearFlags[0]), "Clear AuthRequired flags should match")
assert.Equal(t, int(AuthImmutable), int(so.ClearFlags[1]), "Clear AuthImmutable flags should match")
}

}

func TestChangeTrustFromXDR(t *testing.T) {
asset := CreditAsset{Code: "ABC", Issuer: "GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3"}
xdrAsset, err := asset.ToXDR()
assert.NoError(t, err)
xdrLimit, err := amount.Parse("5000")
assert.NoError(t, err)
changeTrustOp := xdr.ChangeTrustOp{
Line: xdrAsset,
Limit: xdrLimit,
}

var opSource xdr.AccountId
err = opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)
xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeChangeTrust,
ChangeTrustOp: &changeTrustOp,
},
}

var ct ChangeTrust
err = ct.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", ct.SourceAccount.GetAccountID(), "source accounts should match")
assetType, e := ct.Line.GetType()
assert.NoError(t, e)

assert.Equal(t, AssetTypeCreditAlphanum4, assetType, "Asset type should match")
assert.Equal(t, "ABC", ct.Line.GetCode(), "Asset code should match")
assert.Equal(t, "GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3", ct.Line.GetIssuer(), "Asset issuer should match")
assert.Equal(t, "5000.0000000", ct.Limit, "Trustline limit should match")
}
}

func TestAllowTrustFromXDR(t *testing.T) {
xdrAsset := xdr.Asset{}
allowTrustAsset, err := xdrAsset.ToAllowTrustOpAsset("ABCXYZ")
assert.NoError(t, err)

var opSource xdr.AccountId
err = opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)

var trustor xdr.AccountId
err = trustor.SetAddress("GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3")
assert.NoError(t, err)

allowTrustOp := xdr.AllowTrustOp{
Trustor: trustor,
Asset: allowTrustAsset,
Authorize: true,
}

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeAllowTrust,
AllowTrustOp: &allowTrustOp,
},
}

var at AllowTrust
err = at.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", at.SourceAccount.GetAccountID(), "source accounts should match")

assetType, e := at.Type.GetType()
assert.NoError(t, e)
assert.Equal(t, AssetTypeCreditAlphanum12, assetType, "Asset type should match")
assert.Equal(t, "ABCXYZ", at.Type.GetCode(), "Asset code should match")
assert.Equal(t, "GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3", at.Trustor, "Trustor should match")
assert.Equal(t, true, at.Authorize, "Authorize value should match")
}
}

func TestAccountMergeFromXDR(t *testing.T) {
var opSource xdr.AccountId
err := opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)

var destination xdr.AccountId
err = destination.SetAddress("GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3")
assert.NoError(t, err)

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeAccountMerge,
Destination: &destination,
},
}

var am AccountMerge
err = am.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", am.SourceAccount.GetAccountID(), "source accounts should match")
assert.Equal(t, "GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3", am.Destination, "destination accounts should match")
}
}

func TestInflationFromXDR(t *testing.T) {
var opSource xdr.AccountId
err := opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{Type: xdr.OperationTypeInflation},
}

var inf Inflation
err = inf.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", inf.SourceAccount.GetAccountID(), "source accounts should match")
}
}

func TestManageDataFromXDR(t *testing.T) {
var opSource xdr.AccountId
err := opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)

dv := []byte("value")
xdrdv := xdr.DataValue(dv)
manageDataOp := xdr.ManageDataOp{
DataName: xdr.String64("data"),
DataValue: &xdrdv,
}

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeManageData,
ManageDataOp: &manageDataOp,
},
}

var md ManageData
err = md.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", md.SourceAccount.GetAccountID(), "source accounts should match")
assert.Equal(t, "data", md.Name, "Name should match")
assert.Equal(t, "value", string(md.Value), "Value should match")
}
}

func TestBumpSequenceFromXDR(t *testing.T) {
var opSource xdr.AccountId
err := opSource.SetAddress("GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H")
assert.NoError(t, err)

bsOp := xdr.BumpSequenceOp{
BumpTo: xdr.SequenceNumber(45),
}

xdrOp := xdr.Operation{
SourceAccount: &opSource,
Body: xdr.OperationBody{
Type: xdr.OperationTypeBumpSequence,
BumpSequenceOp: &bsOp,
},
}

var bs BumpSequence
err = bs.FromXDR(xdrOp)
if assert.NoError(t, err) {
assert.Equal(t, "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H", bs.SourceAccount.GetAccountID(), "source accounts should match")
assert.Equal(t, int64(45), bs.BumpTo, "BumpTo should match")
}
}
2 changes: 1 addition & 1 deletion txnbuild/set_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (so *SetOptions) handleClearFlagsXDR(flags *xdr.Uint32) {
if flags != nil {
for _, f := range []AccountFlag{AuthRequired, AuthRevocable, AuthImmutable} {
if f&AccountFlag(*flags) != 0 {
so.SetFlags = append(so.SetFlags, f)
so.ClearFlags = append(so.ClearFlags, f)
Copy link
Member

Choose a reason for hiding this comment

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

Another one I'm glad you caught! 👍

}
}
}
Expand Down
33 changes: 33 additions & 0 deletions txnbuild/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,37 @@ func TestFromXDR(t *testing.T) {
assert.Equal(t, "GATBMIXTHXYKSUZSZUEJKACZ2OS2IYUWP2AIF3CA32PIDLJ67CH6Y5UY", paymentOp.SourceAccount.GetAccountID(), "Operation source should match")
assert.Equal(t, "GDGEQS64ISS6Y2KDM5V67B6LXALJX4E7VE4MIA54NANSUX5MKGKBZM5G", paymentOp.Destination, "Operation destination should match")
assert.Equal(t, "874.0000000", paymentOp.Amount, "Operation amount should match")

txeB64 = "AAAAAGigiN2q4qBXAERImNEncpaADylyBRtzdqpEsku6CN0xAAABkAAADXYAAAABAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAABAAAABm5ldyB0eAAAAAAAAgAAAAEAAAAA+Q2efEMLNGF4i+aYfutUXGMSlf8tNevKeS1Jl/oCVGkAAAAGAAAAAVVTRAAAAAAAaKCI3arioFcAREiY0SdyloAPKXIFG3N2qkSyS7oI3TF//////////wAAAAAAAAAKAAAABHRlc3QAAAABAAAABXZhbHVlAAAAAAAAAAAAAAA="

newTx2, err := TransactionFromXDR(txeB64)
assert.NoError(t, err)
assert.Equal(t, "GBUKBCG5VLRKAVYAIREJRUJHOKLIADZJOICRW43WVJCLES52BDOTCQZU", newTx2.SourceAccount.GetAccountID(), "source accounts should match")
assert.Equal(t, int(200), int(newTx2.BaseFee), "Base fee should match")
sa2, ok := newTx2.SourceAccount.(*SimpleAccount)
assert.Equal(t, true, ok)
assert.Equal(t, int64(14800457302017), sa2.Sequence, "Sequence number should match")

memo, ok := newTx2.Memo.(MemoText)
assert.Equal(t, true, ok)
assert.Equal(t, MemoText("new tx"), memo, "memo should match")
assert.Equal(t, 2, len(newTx2.Operations), "Operations length should match")
assert.IsType(t, newTx2.Operations[0], &ChangeTrust{}, "Operation types should match")
assert.IsType(t, newTx2.Operations[1], &ManageData{}, "Operation types should match")
op1, ok1 := newTx2.Operations[0].(*ChangeTrust)
assert.Equal(t, true, ok1)
assert.Equal(t, "GD4Q3HT4IMFTIYLYRPTJQ7XLKROGGEUV74WTL26KPEWUTF72AJKGSJS7", op1.SourceAccount.GetAccountID(), "Operation source should match")
assetType, err := op1.Line.GetType()
assert.NoError(t, err)

assert.Equal(t, AssetTypeCreditAlphanum4, assetType, "Asset type should match")
assert.Equal(t, "USD", op1.Line.GetCode(), "Asset code should match")
assert.Equal(t, "GBUKBCG5VLRKAVYAIREJRUJHOKLIADZJOICRW43WVJCLES52BDOTCQZU", op1.Line.GetIssuer(), "Asset issuer should match")
assert.Equal(t, "922337203685.4775807", op1.Limit, "trustline limit should match")

op2, ok2 := newTx2.Operations[1].(*ManageData)
assert.Equal(t, true, ok2)
assert.Equal(t, nil, op2.SourceAccount, "Operation source should match")
assert.Equal(t, "test", op2.Name, "Name should match")
assert.Equal(t, "value", string(op2.Value), "Value should match")
}