Skip to content

Commit

Permalink
Display b64 fields if their content is zero bytes. (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolikzinovyev authored Nov 15, 2021
1 parent aff0804 commit e45af39
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
26 changes: 13 additions & 13 deletions api/converter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func msigToTransactionMsig(msig crypto.MultisigSig) *generated.TransactionSignat
subsigs := make([]generated.TransactionSignatureMultisigSubsignature, 0)
for _, subsig := range msig.Subsigs {
subsigs = append(subsigs, generated.TransactionSignatureMultisigSubsignature{
PublicKey: bytePtr(subsig.Key[:]),
PublicKey: byteSliceOmitZeroPtr(subsig.Key[:]),
Signature: sigToTransactionSig(subsig.Sig),
})
}
Expand Down Expand Up @@ -317,11 +317,11 @@ func signedTxnWithAdToTransaction(stxn *transactions.SignedTxnWithAD, extra rowD
case protocol.KeyRegistrationTx:
k := generated.TransactionKeyreg{
NonParticipation: boolPtr(stxn.Txn.Nonparticipation),
SelectionParticipationKey: bytePtr(stxn.Txn.SelectionPK[:]),
SelectionParticipationKey: byteSliceOmitZeroPtr(stxn.Txn.SelectionPK[:]),
VoteFirstValid: uint64Ptr(uint64(stxn.Txn.VoteFirst)),
VoteLastValid: uint64Ptr(uint64(stxn.Txn.VoteLast)),
VoteKeyDilution: uint64Ptr(stxn.Txn.VoteKeyDilution),
VoteParticipationKey: bytePtr(stxn.Txn.VotePK[:]),
VoteParticipationKey: byteSliceOmitZeroPtr(stxn.Txn.VotePK[:]),
}
keyreg = &k
case protocol.AssetConfigTx:
Expand All @@ -332,15 +332,15 @@ func signedTxnWithAdToTransaction(stxn *transactions.SignedTxnWithAD, extra rowD
DefaultFrozen: boolPtr(stxn.Txn.AssetParams.DefaultFrozen),
Freeze: addrPtr(stxn.Txn.AssetParams.Freeze),
Manager: addrPtr(stxn.Txn.AssetParams.Manager),
MetadataHash: bytePtr(stxn.Txn.AssetParams.MetadataHash[:]),
MetadataHash: byteSliceOmitZeroPtr(stxn.Txn.AssetParams.MetadataHash[:]),
Name: strPtr(util.PrintableUTF8OrEmpty(stxn.Txn.AssetParams.AssetName)),
NameB64: bytePtr([]byte(stxn.Txn.AssetParams.AssetName)),
NameB64: byteSlicePtr([]byte(stxn.Txn.AssetParams.AssetName)),
Reserve: addrPtr(stxn.Txn.AssetParams.Reserve),
Total: stxn.Txn.AssetParams.Total,
UnitName: strPtr(util.PrintableUTF8OrEmpty(stxn.Txn.AssetParams.UnitName)),
UnitNameB64: bytePtr([]byte(stxn.Txn.AssetParams.UnitName)),
UnitNameB64: byteSlicePtr([]byte(stxn.Txn.AssetParams.UnitName)),
Url: strPtr(util.PrintableUTF8OrEmpty(stxn.Txn.AssetParams.URL)),
UrlB64: bytePtr([]byte(stxn.Txn.AssetParams.URL)),
UrlB64: byteSlicePtr([]byte(stxn.Txn.AssetParams.URL)),
}
config := generated.TransactionAssetConfig{
AssetId: uint64Ptr(uint64(stxn.Txn.ConfigAsset)),
Expand Down Expand Up @@ -389,8 +389,8 @@ func signedTxnWithAdToTransaction(stxn *transactions.SignedTxnWithAD, extra rowD
Accounts: &accts,
ApplicationArgs: &args,
ApplicationId: uint64(stxn.Txn.ApplicationID),
ApprovalProgram: bytePtr(stxn.Txn.ApprovalProgram),
ClearStateProgram: bytePtr(stxn.Txn.ClearStateProgram),
ApprovalProgram: byteSliceOmitZeroPtr(stxn.Txn.ApprovalProgram),
ClearStateProgram: byteSliceOmitZeroPtr(stxn.Txn.ClearStateProgram),
ForeignApps: &apps,
ForeignAssets: &assets,
GlobalStateSchema: &generated.StateSchema{
Expand Down Expand Up @@ -491,12 +491,12 @@ func signedTxnWithAdToTransaction(stxn *transactions.SignedTxnWithAD, extra rowD
RoundTime: uint64Ptr(uint64(extra.RoundTime)),
Fee: stxn.Txn.Fee.Raw,
FirstValid: uint64(stxn.Txn.FirstValid),
GenesisHash: bytePtr(stxn.SignedTxn.Txn.GenesisHash[:]),
GenesisHash: byteSliceOmitZeroPtr(stxn.SignedTxn.Txn.GenesisHash[:]),
GenesisId: strPtr(stxn.SignedTxn.Txn.GenesisID),
Group: bytePtr(stxn.Txn.Group[:]),
Group: byteSliceOmitZeroPtr(stxn.Txn.Group[:]),
LastValid: uint64(stxn.Txn.LastValid),
Lease: bytePtr(stxn.Txn.Lease[:]),
Note: bytePtr(stxn.Txn.Note[:]),
Lease: byteSliceOmitZeroPtr(stxn.Txn.Lease[:]),
Note: byteSliceOmitZeroPtr(stxn.Txn.Note[:]),
Sender: stxn.Txn.Sender.String(),
ReceiverRewards: uint64Ptr(stxn.ReceiverRewards.Raw),
CloseRewards: uint64Ptr(stxn.CloseRewards.Raw),
Expand Down
8 changes: 4 additions & 4 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,13 @@ func (si *ServerImplementation) fetchAssets(ctx context.Context, options idb.Ass
Name: strPtr(util.PrintableUTF8OrEmpty(row.Params.AssetName)),
UnitName: strPtr(util.PrintableUTF8OrEmpty(row.Params.UnitName)),
Url: strPtr(util.PrintableUTF8OrEmpty(row.Params.URL)),
NameB64: bytePtr([]byte(row.Params.AssetName)),
UnitNameB64: bytePtr([]byte(row.Params.UnitName)),
UrlB64: bytePtr([]byte(row.Params.URL)),
NameB64: byteSlicePtr([]byte(row.Params.AssetName)),
UnitNameB64: byteSlicePtr([]byte(row.Params.UnitName)),
UrlB64: byteSlicePtr([]byte(row.Params.URL)),
Total: row.Params.Total,
Decimals: uint64(row.Params.Decimals),
DefaultFrozen: boolPtr(row.Params.DefaultFrozen),
MetadataHash: bytePtr(mdhash),
MetadataHash: byteSliceOmitZeroPtr(mdhash),
Clawback: strPtr(row.Params.Clawback.String()),
Reserve: strPtr(row.Params.Reserve.String()),
Freeze: strPtr(row.Params.Freeze.String()),
Expand Down
8 changes: 7 additions & 1 deletion api/pointer_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ func uint64PtrOrNil(x uint64) *uint64 {
return &x
}

func bytePtr(x []byte) *[]byte {
func byteSlicePtr(x []byte) *[]byte {
if len(x) == 0 {
return nil
}

xx := make([]byte, len(x))
copy(xx, x)
return &xx
}

func byteSliceOmitZeroPtr(x []byte) *[]byte {
// Don't return if it's all zero.
for _, v := range x {
if v != 0 {
Expand Down
17 changes: 12 additions & 5 deletions idb/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,12 +1192,12 @@ func (db *IndexerDb) yieldAccountsThread(req *getAccountsRequest) {
Decimals: uint64(ap.Decimals),
DefaultFrozen: boolPtr(ap.DefaultFrozen),
UnitName: stringPtr(util.PrintableUTF8OrEmpty(ap.UnitName)),
UnitNameB64: baPtr([]byte(ap.UnitName)),
UnitNameB64: byteSlicePtr([]byte(ap.UnitName)),
Name: stringPtr(util.PrintableUTF8OrEmpty(ap.AssetName)),
NameB64: baPtr([]byte(ap.AssetName)),
NameB64: byteSlicePtr([]byte(ap.AssetName)),
Url: stringPtr(util.PrintableUTF8OrEmpty(ap.URL)),
UrlB64: baPtr([]byte(ap.URL)),
MetadataHash: baPtr(ap.MetadataHash[:]),
UrlB64: byteSlicePtr([]byte(ap.URL)),
MetadataHash: byteSliceOmitZeroPtr(ap.MetadataHash[:]),
Manager: addrStr(ap.Manager),
Reserve: addrStr(ap.Reserve),
Freeze: addrStr(ap.Freeze),
Expand Down Expand Up @@ -1404,10 +1404,17 @@ func stringPtr(x string) *string {
return out
}

func baPtr(x []byte) *[]byte {
func byteSlicePtr(x []byte) *[]byte {
if len(x) == 0 {
return nil
}

xx := make([]byte, len(x))
copy(xx, x)
return &xx
}

func byteSliceOmitZeroPtr(x []byte) *[]byte {
allzero := true
for _, b := range x {
if b != 0 {
Expand Down

0 comments on commit e45af39

Please sign in to comment.