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

api: test and slightly improve protoFormat #1374

Merged
merged 2 commits into from
Sep 3, 2024
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 api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func (a *API) chainTxHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) er
return ErrVochainGetTxFailed.WithErr(err)
}
tx := &GenericTransactionWithInfo{
TxContent: []byte(protoFormat(stx.Tx)),
TxContent: protoTxAsJSON(stx.Tx),
Signature: stx.Signature,
TxInfo: *ref,
}
Expand Down
10 changes: 7 additions & 3 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ func (a *API) sendTx(tx []byte) (*cometcoretypes.ResultBroadcastTx, error) {
return resp, nil
}

func protoFormat(tx []byte) string {
func protoTxAsJSON(tx []byte) []byte {
ptx := models.Tx{}
if err := proto.Unmarshal(tx, &ptx); err != nil {
return ""
panic(err)
}
pj := protojson.MarshalOptions{
Multiline: false,
Indent: "",
EmitUnpopulated: true,
}
return pj.Format(&ptx)
asJSON, err := pj.Marshal(&ptx)
if err != nil {
panic(err)
}
return asJSON
}

// isTransactionType checks if the given transaction is of the given type.
Expand Down
33 changes: 33 additions & 0 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package api
import (
"bytes"
"encoding/json"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/proto/build/go/models"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func TestAPIHelpers_encodeEVMResultsArgs(t *testing.T) {
Expand Down Expand Up @@ -149,3 +154,31 @@ func TestConvertKeysToCamel(t *testing.T) {
t.Fatal(diff)
}
}

func TestProtoTxAsJSON(t *testing.T) {
wantJSON := strings.TrimSpace(`
{
"setProcess": {
"txtype": "SET_PROCESS_CENSUS",
"nonce": 1,
"processId": "sx3/YYFNq5DWw6m2XWyQgwSA5Lda0y50eUICAAAAAAA=",
"censusRoot": "zUU9BcTLBCnuXuGu/tAW9VO4AmtM7VsMNSkFv6U8foE=",
"censusURI": "ipfs://bafybeicyfukarcryrvy5oe37ligmxwf55sbfiojori4t25wencma4ymxfa",
"censusSize": "1000"
}
}
`)
var ptx models.Tx
err := protojson.Unmarshal([]byte(wantJSON), &ptx)
qt.Assert(t, err, qt.IsNil)

asProto, err := proto.Marshal(&ptx)
qt.Assert(t, err, qt.IsNil)

var dst bytes.Buffer
err = json.Indent(&dst, protoTxAsJSON(asProto), "", "\t")
qt.Assert(t, err, qt.IsNil)
gotJSON := dst.String()

qt.Assert(t, gotJSON, qt.Equals, wantJSON)
}