Skip to content

Commit

Permalink
Merge pull request #1481 from alex-semenyuk/tezos_deploy_contract
Browse files Browse the repository at this point in the history
DeployContract implementation for Tezos connector
  • Loading branch information
nguyer authored Apr 8, 2024
2 parents 49410c5 + f4be7bf commit d460998
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/apiserver/route_post_contract_deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestPostContractDeploy(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mcm.On("DeployContract", mock.Anything, mock.MatchedBy(func(req *core.ContractDeployRequest) bool {
mcm.On(core.DeployContract, mock.Anything, mock.MatchedBy(func(req *core.ContractDeployRequest) bool {
return true
}), false).Return("banana", nil)
r.ServeHTTP(res, req)
Expand Down
6 changes: 2 additions & 4 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ func (e *Ethereum) DeployContract(ctx context.Context, nsOpID, signingKey string
e.metrics.BlockchainContractDeployment()
}
headers := EthconnectMessageHeaders{
Type: "DeployContract",
Type: core.DeployContract,
ID: nsOpID,
}
body := map[string]interface{}{
Expand All @@ -745,9 +745,7 @@ func (e *Ethereum) DeployContract(ctx context.Context, nsOpID, signingKey string
"definition": definition,
"contract": contract,
}
if signingKey != "" {
body["from"] = signingKey
}

body, err = e.applyOptions(ctx, body, options)
if err != nil {
return true, err
Expand Down
6 changes: 3 additions & 3 deletions internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -2604,7 +2604,7 @@ func TestDeployContractOK(t *testing.T) {
json.NewDecoder(req.Body).Decode(&body)
params := body["params"].([]interface{})
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, core.DeployContract, headers["type"])
assert.Equal(t, float64(1), params[0])
assert.Equal(t, "1000000000000000000000000", params[1])
assert.Equal(t, body["customOption"].(string), "customValue")
Expand Down Expand Up @@ -2686,7 +2686,7 @@ func TestDeployContractInvalidOption(t *testing.T) {
json.NewDecoder(req.Body).Decode(&body)
params := body["params"].([]interface{})
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, core.DeployContract, headers["type"])
assert.Equal(t, float64(1), params[0])
assert.Equal(t, "1000000000000000000000000", params[1])
assert.Equal(t, body["customOption"].(string), "customValue")
Expand Down
4 changes: 2 additions & 2 deletions internal/blockchain/fabric/fabric_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -2529,7 +2529,7 @@ func TestDeployContractOK(t *testing.T) {
json.NewDecoder(req.Body).Decode(&body)
params := body["params"].([]interface{})
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "DeployContract", headers["type"])
assert.Equal(t, core.DeployContract, headers["type"])
assert.Equal(t, float64(1), params[0])
assert.Equal(t, "1000000000000000000000000", params[1])
assert.Equal(t, body["customOption"].(string), "customValue")
Expand Down
30 changes: 29 additions & 1 deletion internal/blockchain/tezos/tezos.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,35 @@ 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: core.DeployContract,
ID: nsOpID,
}
body := map[string]interface{}{
"headers": &headers,
"contract": contract,
"from": signingKey,
}

body, err = t.applyOptions(ctx, body, options)
if err != nil {
return true, err
}

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
79 changes: 75 additions & 4 deletions internal/blockchain/tezos/tezos_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down 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, core.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, core.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
7 changes: 6 additions & 1 deletion pkg/core/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -62,3 +62,8 @@ const (
// SystemTagIdentityUpdate is the tag for messages that broadcast an identity update
SystemTagIdentityUpdate = "ff_identity_update"
)

const (
// DeployContract is the header for DeployContract request
DeployContract = "DeployContract"
)

0 comments on commit d460998

Please sign in to comment.