-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #560 from stellar/build-bump-sequence
Add BumpSequence to "build" package
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package build | ||
|
||
import ( | ||
"github.com/stellar/go/support/errors" | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
// BumpSequence groups the creation of a new BumpSequenceBuilder with a call | ||
// to Mutate. Requires the BumpTo mutator to be set. | ||
func BumpSequence(muts ...interface{}) (result BumpSequenceBuilder) { | ||
result.Mutate(muts...) | ||
return | ||
} | ||
|
||
// BumpSequenceMutator is a interface that wraps the | ||
// MutateBumpSequence operation. types may implement this interface to | ||
// specify how they modify an xdr.BumpSequenceOp object | ||
type BumpSequenceMutator interface { | ||
MutateBumpSequence(*xdr.BumpSequenceOp) error | ||
} | ||
|
||
// BumpSequenceBuilder helps to build BumpSequenceOp structs. | ||
type BumpSequenceBuilder struct { | ||
O xdr.Operation | ||
BS xdr.BumpSequenceOp | ||
Err error | ||
} | ||
|
||
// Mutate applies the provided mutators to this builder's payment or operation. | ||
func (b *BumpSequenceBuilder) Mutate(muts ...interface{}) { | ||
for _, m := range muts { | ||
var err error | ||
switch mut := m.(type) { | ||
case BumpSequenceMutator: | ||
err = mut.MutateBumpSequence(&b.BS) | ||
case OperationMutator: | ||
err = mut.MutateOperation(&b.O) | ||
default: | ||
err = errors.New("Mutator type not allowed") | ||
} | ||
|
||
if err != nil { | ||
b.Err = errors.Wrap(err, "BumpSequenceBuilder error") | ||
return | ||
} | ||
} | ||
} | ||
|
||
// MutateBumpSequence for BumpTo sets the BumpSequenceOp's | ||
// StartingBalance field | ||
func (m BumpTo) MutateBumpSequence(o *xdr.BumpSequenceOp) (err error) { | ||
o.BumpTo = xdr.SequenceNumber(m) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package build | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
var _ = Describe("BumpSequenceBuilder Mutators", func() { | ||
|
||
var ( | ||
subject BumpSequenceBuilder | ||
mut interface{} | ||
|
||
address = "GAXEMCEXBERNSRXOEKD4JAIKVECIXQCENHEBRVSPX2TTYZPMNEDSQCNQ" | ||
bad = "foo" | ||
) | ||
|
||
JustBeforeEach(func() { | ||
subject = BumpSequenceBuilder{} | ||
subject.Mutate(mut) | ||
}) | ||
|
||
Describe("BumpTo", func() { | ||
BeforeEach(func() { mut = BumpTo(9223372036854775807) }) | ||
|
||
It("succeeds", func() { | ||
Expect(subject.Err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("sets the value to the correct xdr.SequenceNumber", func() { | ||
Expect(subject.BS.BumpTo).To(Equal(xdr.SequenceNumber(9223372036854775807))) | ||
}) | ||
}) | ||
|
||
Describe("SourceAccount", func() { | ||
Context("using a valid stellar address", func() { | ||
BeforeEach(func() { mut = SourceAccount{address} }) | ||
|
||
It("succeeds", func() { | ||
Expect(subject.Err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("sets the destination to the correct xdr.AccountId", func() { | ||
var aid xdr.AccountId | ||
aid.SetAddress(address) | ||
Expect(subject.O.SourceAccount.MustEd25519()).To(Equal(aid.MustEd25519())) | ||
}) | ||
}) | ||
|
||
Context("using an invalid value", func() { | ||
BeforeEach(func() { mut = SourceAccount{bad} }) | ||
It("failed", func() { Expect(subject.Err).To(HaveOccurred()) }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters