Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Nov 16, 2021
1 parent 3d0aeaa commit c517dad
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 66 deletions.
38 changes: 0 additions & 38 deletions xdr/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,44 +268,6 @@ func (a Asset) StringCanonical() string {
return fmt.Sprintf("%s:%s", c, i)
}

func trimRightZeros(b []byte) []byte {
if len(b) == 0 {
return b
}
i := len(b)
for ; i > 0; i-- {
if b[i-1] != 0 {
break
}
}
return b[:i]
}

func (e *EncodingBuffer) assetCompressEncodeTo(a Asset) error {
if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil {
return err
}

switch a.Type {
case AssetTypeAssetTypeNative:
return nil
case AssetTypeAssetTypeCreditAlphanum4:
code := trimRightZeros(a.AlphaNum4.AssetCode[:])
if _, err := e.xdrEncoderBuf.Write(code); err != nil {
return err
}
return e.accountIdCompressEncodeTo(a.AlphaNum4.Issuer)
case AssetTypeAssetTypeCreditAlphanum12:
code := trimRightZeros(a.AlphaNum12.AssetCode[:])
if _, err := e.xdrEncoderBuf.Write(code); err != nil {
return err
}
return e.accountIdCompressEncodeTo(a.AlphaNum12.Issuer)
default:
panic(fmt.Errorf("Unknown asset type: %v", a.Type))
}
}

// Equals returns true if `other` is equivalent to `a`
func (a Asset) Equals(other Asset) bool {
if a.Type != other.Type {
Expand Down
20 changes: 3 additions & 17 deletions xdr/asset_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package xdr
package xdr_test

import (
"testing"

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

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -111,22 +113,6 @@ func TestStringCanonical(t *testing.T) {
require.Equal(t, "USD:GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H", asset.StringCanonical())
}

func TestTrimRightZeros(t *testing.T) {
require.Equal(t, []byte(nil), trimRightZeros(nil))
require.Equal(t, []byte{}, trimRightZeros([]byte{}))
require.Equal(t, []byte{}, trimRightZeros([]byte{0x0}))
require.Equal(t, []byte{}, trimRightZeros([]byte{0x0, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0, 0x0}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0, 0x0}))
require.Equal(t, []byte{0x0, 0x2}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x0}))
require.Equal(t, []byte{0x0, 0x2, 0x0, 0x1}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x1, 0x0}))
}

var _ = Describe("xdr.Asset#Equals()", func() {
var (
issuer1 AccountId
Expand Down
26 changes: 21 additions & 5 deletions xdr/ledger_key_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package xdr_test
package xdr

import (
"encoding/base64"
"testing"

"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLedgerKeyTrustLineBinaryMaxLength(t *testing.T) {
key := &xdr.LedgerKey{}
key := &LedgerKey{}
err := key.SetTrustline(
xdr.MustAddress("GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF"),
xdr.MustNewCreditAsset("123456789012", "GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF").ToTrustLineAsset(),
MustAddress("GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF"),
MustNewCreditAsset("123456789012", "GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF").ToTrustLineAsset(),
)
assert.NoError(t, err)

Expand All @@ -22,3 +22,19 @@ func TestLedgerKeyTrustLineBinaryMaxLength(t *testing.T) {
bcompressed := base64.StdEncoding.EncodeToString(compressed)
assert.Equal(t, len(bcompressed), 124)
}

func TestTrimRightZeros(t *testing.T) {
require.Equal(t, []byte(nil), trimRightZeros(nil))
require.Equal(t, []byte{}, trimRightZeros([]byte{}))
require.Equal(t, []byte{}, trimRightZeros([]byte{0x0}))
require.Equal(t, []byte{}, trimRightZeros([]byte{0x0, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0}))
require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0, 0x0}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0}))
require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0, 0x0}))
require.Equal(t, []byte{0x0, 0x2}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x0}))
require.Equal(t, []byte{0x0, 0x2, 0x0, 0x1}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x1, 0x0}))
}
36 changes: 30 additions & 6 deletions xdr/trust_line_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,40 @@ func (a TrustLineAsset) MustExtract(typ interface{}, code interface{}, issuer in
}
}

func trimRightZeros(b []byte) []byte {
if len(b) == 0 {
return b
}
i := len(b)
for ; i > 0; i-- {
if b[i-1] != 0 {
break
}
}
return b[:i]
}

func (e *EncodingBuffer) assetTrustlineCompressEncodeTo(a TrustLineAsset) error {
if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil {
return err
}

switch a.Type {
case AssetTypeAssetTypeNative,
AssetTypeAssetTypeCreditAlphanum4,
AssetTypeAssetTypeCreditAlphanum12:
return e.assetCompressEncodeTo(a.ToAsset())
case AssetTypeAssetTypePoolShare:
if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil {
case AssetTypeAssetTypeNative:
return nil
case AssetTypeAssetTypeCreditAlphanum4:
code := trimRightZeros(a.AlphaNum4.AssetCode[:])
if _, err := e.xdrEncoderBuf.Write(code); err != nil {
return err
}
return e.accountIdCompressEncodeTo(a.AlphaNum4.Issuer)
case AssetTypeAssetTypeCreditAlphanum12:
code := trimRightZeros(a.AlphaNum12.AssetCode[:])
if _, err := e.xdrEncoderBuf.Write(code); err != nil {
return err
}
return e.accountIdCompressEncodeTo(a.AlphaNum12.Issuer)
case AssetTypeAssetTypePoolShare:
_, err := e.xdrEncoderBuf.Write(a.LiquidityPoolId[:])
return err
default:
Expand Down

0 comments on commit c517dad

Please sign in to comment.