Skip to content

Commit

Permalink
#4909: fix asset loader on null terminated codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Oct 20, 2023
1 parent 7c3842b commit bf0f64c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion services/horizon/internal/db2/history/asset_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"
"sort"
"strings"

sq "github.com/Masterminds/squirrel"

Expand All @@ -25,7 +26,7 @@ type AssetKey struct {
func AssetKeyFromXDR(asset xdr.Asset) AssetKey {
return AssetKey{
Type: xdr.AssetTypeToString[asset.Type],
Code: asset.GetCode(),
Code: strings.TrimRight(asset.GetCode(), "\x00"),
Issuer: asset.GetIssuer(),
}
}
Expand Down
23 changes: 16 additions & 7 deletions services/horizon/internal/db2/history/asset_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ func TestAssetLoader(t *testing.T) {
for i := 0; i < 100; i++ {
var key AssetKey
if i == 0 {
key.Type = "native"
key = AssetKeyFromXDR(xdr.Asset{Type: xdr.AssetTypeAssetTypeNative})
} else if i%2 == 0 {
key.Type = "credit_alphanum4"
key.Code = fmt.Sprintf("ab%d", i)
key.Issuer = keypair.MustRandom().Address()
code := [4]byte{0,0,0,0}

Check failure on line 27 in services/horizon/internal/db2/history/asset_loader_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gofmt`-ed with `-s` (gofmt)
copy(code[:], fmt.Sprintf("ab%d", i))
key = AssetKeyFromXDR(xdr.Asset{
Type: xdr.AssetTypeAssetTypeCreditAlphanum4,
AlphaNum4: &xdr.AlphaNum4{
AssetCode: code,
Issuer: xdr.MustAddress(keypair.MustRandom().Address())}})
} else {
key.Type = "credit_alphanum12"
key.Code = fmt.Sprintf("abcdef%d", i)
key.Issuer = keypair.MustRandom().Address()
code := [12]byte{0,0,0,0,0,0,0,0,0,0,0,0}
copy(code[:], fmt.Sprintf("abcdef%d", i))
key = AssetKeyFromXDR(xdr.Asset{
Type: xdr.AssetTypeAssetTypeCreditAlphanum12,
AlphaNum12: &xdr.AlphaNum12{
AssetCode: code,
Issuer: xdr.MustAddress(keypair.MustRandom().Address())}})

}
keys = append(keys, key)
}
Expand Down

0 comments on commit bf0f64c

Please sign in to comment.