mirror of
https://github.com/hanzoai/vm.git
synced 2026-10-01 00:47:56 +00:00
- Rename Go module from github.com/casvisor/casvisor to github.com/hanzoai/visor - Update all copyright headers from casbin to Hanzo Industries Inc - Remove all hardcoded passwords and credentials from config files - All credentials now sourced from environment variables / Hanzo KMS - Switch default database from MySQL to PostgreSQL (matching production) - Rename binary output from server/casvisor to visor - Update Dockerfile, build.sh, docker-compose.yml, docker-entrypoint.sh - Replace casdoor* config key references with iam* equivalents - Update CI to use PostgreSQL service and mark CI-only test password - Add visor and casvisor binaries to .gitignore - Add envFrom secretRef for visor-env in K8s deployment - Add ExternalSecret template for Hanzo KMS credential provisioning Co-authored-by: Hanzo Dev <[email protected]>
164 lines
5.2 KiB
Go
164 lines
5.2 KiB
Go
// Copyright 2024 Hanzo Industries Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package chain
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/hanzoai/visor/util"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
tbaas "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tbaas/v20180416"
|
|
)
|
|
|
|
type ChainTencentChainmakerDemoClient struct {
|
|
ClientId string
|
|
ClientSecret string
|
|
Region string
|
|
NetworkId string
|
|
ChainId string
|
|
Client *tbaas.Client
|
|
}
|
|
|
|
func newChainTencentChainmakerDemoClient(clientId, clientSecret, region, networkId, chainId string) (*ChainTencentChainmakerDemoClient, error) {
|
|
credential := common.NewCredential(clientId, clientSecret)
|
|
cpf := profile.NewClientProfile()
|
|
cpf.HttpProfile.Endpoint = "tbaas.tencentcloudapi.com"
|
|
|
|
client, err := tbaas.NewClient(credential, region, cpf)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("newChainTencentChainmakerClient() error: %v", err)
|
|
}
|
|
|
|
return &ChainTencentChainmakerDemoClient{
|
|
ClientId: clientId,
|
|
ClientSecret: clientSecret,
|
|
Region: region,
|
|
NetworkId: networkId,
|
|
ChainId: chainId,
|
|
Client: client,
|
|
}, nil
|
|
}
|
|
|
|
func (client ChainTencentChainmakerDemoClient) getQueryResult(txId string) (*tbaas.ChainMakerTransactionResult, error) {
|
|
request := tbaas.NewQueryChainMakerDemoTransactionRequest()
|
|
request.ClusterId = common.StringPtr(client.NetworkId)
|
|
request.ChainId = common.StringPtr(client.ChainId)
|
|
request.TxID = common.StringPtr(txId)
|
|
|
|
response, err := client.Client.QueryChainMakerDemoTransaction(request)
|
|
if err != nil {
|
|
if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
return nil, fmt.Errorf("TencentCloudSDKError: %v", sdkErr)
|
|
}
|
|
|
|
return nil, fmt.Errorf("ChainTencentChainmakerDemoClient.Client.InvokeChainMakerDemoContract() error: %v", err)
|
|
}
|
|
if *(response.Response.Result.Code) != 0 {
|
|
return nil, fmt.Errorf("TencentCloudSDKError, code = %d, message = %s", *(response.Response.Result.Code), *(response.Response.Result.Message))
|
|
}
|
|
|
|
return response.Response.Result, nil
|
|
}
|
|
|
|
func (client *ChainTencentChainmakerDemoClient) Commit(data string) (string, string, error) {
|
|
request := tbaas.NewInvokeChainMakerDemoContractRequest()
|
|
request.ClusterId = common.StringPtr(client.NetworkId)
|
|
request.ChainId = common.StringPtr(client.ChainId)
|
|
request.ContractName = common.StringPtr("ChainMakerDemo")
|
|
request.FuncName = common.StringPtr("save")
|
|
request.FuncParam = common.StringPtr(data)
|
|
|
|
response, err := client.Client.InvokeChainMakerDemoContract(request)
|
|
if err != nil {
|
|
if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
return "", "", fmt.Errorf("TencentCloudSDKError: %v", sdkErr)
|
|
}
|
|
|
|
return "", "", fmt.Errorf("ChainTencentChainmakerDemoClient.Client.InvokeChainMakerDemoContract() error: %v", err)
|
|
}
|
|
if *(response.Response.Result.Code) != 0 {
|
|
return "", "", fmt.Errorf("TencentCloudSDKError, code = %d, message = %s", *(response.Response.Result.Code), *(response.Response.Result.Message))
|
|
}
|
|
|
|
txId := *(response.Response.Result.TxId)
|
|
|
|
queryResult, err := client.getQueryResult(txId)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
blockId := strconv.FormatInt(*(queryResult.BlockHeight), 10)
|
|
return blockId, txId, nil
|
|
}
|
|
|
|
func (client ChainTencentChainmakerDemoClient) Query(txId string, data string) (string, error) {
|
|
queryResult, err := client.getQueryResult(txId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
blockId := strconv.FormatInt(*(queryResult.BlockHeight), 10)
|
|
|
|
type ContractEvent struct {
|
|
ContractName string `json:"contract_name"`
|
|
ContractVersion string `json:"contract_version"`
|
|
EventData []string `json:"event_data"`
|
|
Topic string `json:"topic"`
|
|
TxId string `json:"tx_id"`
|
|
}
|
|
|
|
contractEvents := []ContractEvent{}
|
|
err = util.JsonToStruct(*(queryResult.ContractEvent), &contractEvents)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
type Param struct {
|
|
Key string `json:"key"`
|
|
Field string `json:"field"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
key := contractEvents[0].EventData[0]
|
|
field := contractEvents[0].EventData[1]
|
|
value := contractEvents[0].EventData[2]
|
|
param := Param{Key: key, Field: field, Value: value}
|
|
chainData := util.StructToJson(param)
|
|
|
|
res := "Mismatched"
|
|
if chainData == data {
|
|
res = fmt.Sprintf(`Matched
|
|
******************************************************
|
|
Data:
|
|
|
|
%s`, chainData)
|
|
} else {
|
|
res = fmt.Sprintf(`Mismatched
|
|
******************************************************
|
|
Chain data:
|
|
|
|
%s
|
|
******************************************************
|
|
Local data:
|
|
|
|
%s`, chainData, data)
|
|
}
|
|
|
|
return fmt.Sprintf("The query result for block [%s] is: %s", blockId, res), nil
|
|
}
|