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

DeployContract implementation for Tezos connector #1481

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 30 additions & 1 deletion internal/blockchain/tezos/tezos.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,36 @@ func (t *Tezos) SubmitNetworkAction(ctx context.Context, nsOpID string, signingK
}

func (t *Tezos) DeployContract(ctx context.Context, nsOpID, signingKey string, definition, contract *fftypes.JSONAny, input []interface{}, options map[string]interface{}) (submissionRejected bool, err error) {
return true, i18n.NewError(ctx, coremsgs.MsgNotSupportedByBlockchainPlugin)
if t.metrics.IsMetricsEnabled() {
t.metrics.BlockchainContractDeployment()
}
headers := TezosconnectMessageHeaders{
Type: "DeployContract",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to have as constant, as these types are used by several chains

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done

ID: nsOpID,
}
body := map[string]interface{}{
"headers": &headers,
"contract": contract,
}
if signingKey != "" {
body["from"] = signingKey
}
body, err = t.applyOptions(ctx, body, options)
if err != nil {
return true, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarification, let's use resErr.SubmissionRejected instead as we did it below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resErr.SubmissionRejected is a bool value, applyOptions returns error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, instead of 'true' value

}

var resErr common.BlockchainRESTError
res, err := t.client.R().
SetContext(ctx).
SetBody(body).
SetError(&resErr).
Post("/")
if err != nil || !res.IsSuccess() {
return resErr.SubmissionRejected, common.WrapRESTError(ctx, &resErr, res, err, coremsgs.MsgTezosconnectRESTErr)
}

return false, nil
}

func (t *Tezos) ValidateInvokeRequest(ctx context.Context, parsedMethod interface{}, input map[string]interface{}, hasMessage bool) error {
Expand Down
77 changes: 74 additions & 3 deletions internal/blockchain/tezos/tezos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func newTestTezos() (*Tezos, func()) {
mm.On("IsMetricsEnabled").Return(true)
mm.On("BlockchainTransaction", mock.Anything, mock.Anything).Return(nil)
mm.On("BlockchainQuery", mock.Anything, mock.Anything).Return(nil)
mm.On("BlockchainContractDeployment", mock.Anything, mock.Anything).Return(nil)
t := &Tezos{
ctx: ctx,
cancelCtx: cancel,
Expand Down Expand Up @@ -978,14 +979,84 @@ func TestDeployContractOK(t *testing.T) {
httpmock.ActivateNonDefault(tz.client.GetClient())
defer httpmock.DeactivateAndReset()
signingKey := "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN"
input := []interface{}{}
options := map[string]interface{}{}
definitionBytes, err := json.Marshal([]interface{}{})
contract := "{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"
contractBytes, err := json.Marshal(contract)
assert.NoError(t, err)
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
json.NewDecoder(req.Body).Decode(&body)
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, "123", headers["id"])
assert.Equal(t, contract, body["contract"])
return httpmock.NewJsonResponderOrPanic(200, "")(req)
})

_, err = tz.DeployContract(context.Background(), "123", signingKey, fftypes.JSONAnyPtrBytes(definitionBytes), fftypes.JSONAnyPtrBytes(contractBytes), input, options)

assert.NoError(t, err)
}

func TestDeployContractError(t *testing.T) {
tz, cancel := newTestTezos()
defer cancel()
httpmock.ActivateNonDefault(tz.client.GetClient())
defer httpmock.DeactivateAndReset()
signingKey := "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN"
input := []interface{}{}
options := map[string]interface{}{}
definitionBytes, err := json.Marshal([]interface{}{})
contractBytes, err := json.Marshal("KT123")
contract := "{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"
contractBytes, err := json.Marshal(contract)
assert.NoError(t, err)
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
json.NewDecoder(req.Body).Decode(&body)
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, "123", headers["id"])
assert.Equal(t, contract, body["contract"])
return httpmock.NewJsonResponderOrPanic(400, "error")(req)
})

_, err = tz.DeployContract(context.Background(), "123", signingKey, fftypes.JSONAnyPtrBytes(definitionBytes), fftypes.JSONAnyPtrBytes(contractBytes), input, options)

_, err = tz.DeployContract(context.Background(), "", signingKey, fftypes.JSONAnyPtrBytes(definitionBytes), fftypes.JSONAnyPtrBytes(contractBytes), input, options)
assert.Regexp(t, "FF10429", err)
assert.Regexp(t, "FF10283", err)
}

func TestDeployContractInvalidOption(t *testing.T) {
tz, cancel := newTestTezos()
defer cancel()
httpmock.ActivateNonDefault(tz.client.GetClient())
defer httpmock.DeactivateAndReset()
signingKey := "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN"
input := []interface{}{}
options := map[string]interface{}{
"contract": "shouldn't be allowed",
}
definitionBytes, err := json.Marshal([]interface{}{})
contract := "{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"
contractBytes, err := json.Marshal(contract)
assert.NoError(t, err)
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
json.NewDecoder(req.Body).Decode(&body)
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, "123", headers["id"])
assert.Equal(t, contract, body["contract"])
return httpmock.NewJsonResponderOrPanic(200, "")(req)
})

_, err = tz.DeployContract(context.Background(), "123", signingKey, fftypes.JSONAnyPtrBytes(definitionBytes), fftypes.JSONAnyPtrBytes(contractBytes), input, options)

assert.Regexp(t, "FF10398", err)
}

func TestInvokeContractOK(t *testing.T) {
Expand Down
Loading