Skip to content

Commit

Permalink
fix(ledger): automatic variable conversion on api v1 (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Feb 6, 2024
1 parent a3783be commit 33bfcf0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 31 deletions.
46 changes: 33 additions & 13 deletions components/ledger/internal/api/v1/controllers_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,36 @@ func getTransactions(w http.ResponseWriter, r *http.Request) {

type Script struct {
ledger.Script
Vars map[string]any `json:"vars"`
Vars map[string]json.RawMessage `json:"vars"`
}

func (s Script) ToCore() ledger.Script {
func (s Script) ToCore() (*ledger.Script, error) {
s.Script.Vars = map[string]string{}
for k, v := range s.Vars {
switch v := v.(type) {
case string:
s.Script.Vars[k] = v
case map[string]any:
s.Script.Vars[k] = fmt.Sprintf("%s %v", v["asset"], v["amount"])
default:
s.Script.Vars[k] = fmt.Sprint(v)

m := make(map[string]json.RawMessage)
if err := json.Unmarshal(v, &m); err != nil {
var rawValue string
if err := json.Unmarshal(v, &rawValue); err != nil {
panic(err)
}
s.Script.Vars[k] = rawValue
continue
}

// Is a monetary
var asset string
if err := json.Unmarshal(m["asset"], &asset); err != nil {
return nil, errors.Wrap(err, "unmarshalling asset")
}
amount := &big.Int{}
if err := json.Unmarshal(m["amount"], amount); err != nil {
return nil, errors.Wrap(err, "unmarshalling amount")
}

s.Script.Vars[k] = fmt.Sprintf("%s %s", asset, amount)
}
return s.Script
return &s.Script, nil
}

type PostTransactionRequest struct {
Expand Down Expand Up @@ -228,14 +242,20 @@ func postTransaction(w http.ResponseWriter, r *http.Request) {
return
}

script := ledger.RunScript{
Script: payload.Script.ToCore(),
script, err := payload.Script.ToCore()
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
}

runScript := ledger.RunScript{
Script: *script,
Timestamp: payload.Timestamp,
Reference: payload.Reference,
Metadata: payload.Metadata,
}

res, err := l.CreateTransaction(r.Context(), getCommandParameters(r), script)
res, err := l.CreateTransaction(r.Context(), getCommandParameters(r), runScript)
if err != nil {
switch {
case engine.IsCommandError(err):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1_test

import (
"encoding/json"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -64,8 +65,8 @@ func TestPostTransactions(t *testing.T) {
destination = @bank
)`,
},
Vars: map[string]any{
"val": "USD/2 100",
Vars: map[string]json.RawMessage{
"val": json.RawMessage(`"USD/2 100"`),
},
},
},
Expand Down Expand Up @@ -99,11 +100,11 @@ func TestPostTransactions(t *testing.T) {
destination = @bank
)`,
},
Vars: map[string]any{
"val": map[string]any{
Vars: map[string]json.RawMessage{
"val": json.RawMessage(`{
"asset": "USD/2",
"amount": 100,
},
"amount": 100
}`),
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions ee/agent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/url"
"os"
"path/filepath"

"github.com/formancehq/operator/api/formance.com/v1beta1"
"github.com/formancehq/stack/components/agent/internal"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
Expand All @@ -21,9 +25,6 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"net/url"
"os"
"path/filepath"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion ee/agent/internal/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package internal

import (
"context"
"net/http"

oidcclient "github.com/zitadel/oidc/v2/pkg/client"
"golang.org/x/oauth2/clientcredentials"
"google.golang.org/grpc/metadata"
"net/http"
)

type Authenticator interface {
Expand Down
3 changes: 2 additions & 1 deletion ee/agent/internal/informer_versions.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package internal

import (
"reflect"

"github.com/formancehq/operator/api/formance.com/v1beta1"
"github.com/formancehq/stack/components/agent/internal/generated"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down
5 changes: 3 additions & 2 deletions ee/agent/internal/membership_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package internal

import (
"context"
"io"
"time"

"github.com/formancehq/stack/components/agent/internal/generated"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"io"
"time"
)

const (
Expand Down
7 changes: 4 additions & 3 deletions ee/agent/internal/membership_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"strings"

"github.com/alitto/pond"
"github.com/formancehq/operator/api/formance.com/v1beta1"
"github.com/formancehq/stack/components/agent/internal/generated"
Expand All @@ -19,10 +23,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"net/url"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
)

type MembershipClient interface {
Expand Down
3 changes: 2 additions & 1 deletion ee/agent/internal/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package internal

import (
"context"
"time"

"github.com/formancehq/stack/libs/go-libs/logging"
"go.uber.org/fx"
"google.golang.org/grpc"
Expand All @@ -13,7 +15,6 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/cache"
"time"
)

func NewDynamicSharedInformerFactory(client *dynamic.DynamicClient) dynamicinformer.DynamicSharedInformerFactory {
Expand Down
2 changes: 1 addition & 1 deletion releases/sdks/go/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lockVersion: 2.0.0
id: 922a30f4-d7aa-4487-852e-ad9bd24f026e
id: 7edf123c-7114-4fd5-97a4-da1ad495c0ea
management:
docChecksum: e64f058f8ed1617c198d1fe84ea95670
docVersion: INTERNAL
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/suite/ledger-create-transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,43 @@ var _ = WithModules([]*Module{modules.Search, modules.Ledger}, func() {
Expect(err.(*sdkerrors.V2ErrorResponse).Details).To(Equal(pointer.For("https://play.numscript.org/?payload=eyJlcnJvciI6ImludmFsaWQgSlNPTiB2YWx1ZSBmb3IgdmFyaWFibGUgJGFtb3VudCBvZiB0eXBlIG1vbmV0YXJ5OiB2YWx1ZSBbVVNEIC0xMDBdOiBuZWdhdGl2ZSBhbW91bnQifQ==")))
})
})
When("creating a transaction on the ledger v1 with old variable format", func() {
var (
err error
response *operations.CreateTransactionResponse
)
BeforeEach(func() {
v, _ := big.NewInt(0).SetString("1320000000000000000000000000000000000000000000000001", 10)
response, err = Client().Ledger.CreateTransaction(
TestContext(),
operations.CreateTransactionRequest{
PostTransaction: shared.PostTransaction{
Metadata: map[string]any{},
Script: &shared.PostTransactionScript{
Plain: `vars {
monetary $amount
}
send $amount (
source = @world
destination = @bob
)`,
Vars: map[string]interface{}{
"amount": map[string]any{
"asset": "EUR/12",
"amount": v,
},
},
},
},
Ledger: "default",
},
)
})
It("should be ok", func() {
Expect(err).To(Succeed())
Expect(response.TransactionsResponse.Data[0].Txid).To(Equal(big.NewInt(0)))
})
})
When("creating a transaction on a ledger with error on script", func() {
var (
err error
Expand Down

0 comments on commit 33bfcf0

Please sign in to comment.