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

Support chainsql #14

Open
wants to merge 1 commit into
base: enterprise
Choose a base branch
from
Open
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
161 changes: 161 additions & 0 deletions chains/chainsql/chainsql_invoker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package chainsql

import (
"encoding/json"
"fmt"
"github.com/ChainSQL/go-chainsql-api/common"
"github.com/ChainSQL/go-chainsql-api/core"
data "github.com/ChainSQL/go-chainsql-api/data"
eccd_abi "github.com/polynetwork/poly-io-test/chains/chainsql/eccd_abi"
eccm_abi "github.com/polynetwork/poly-io-test/chains/chainsql/eccm_abi"
eccmp_abi "github.com/polynetwork/poly-io-test/chains/chainsql/eccmp_abi"
config2 "github.com/polynetwork/poly-io-test/config"
"github.com/polynetwork/poly-io-test/log"
"io/ioutil"
"os"
)
type ChainsqlInvoker struct {
ChainsqlSdk *core.Chainsql
TransOpts *core.TransactOpts
}

type Account struct {
Address string
Secrect string
}
type Config struct {
URL string
ServerName string
RootCertPath string
ClientCertPath string
ClientKeyPath string
Account Account
}


func NewConfig(configFilePath string) *Config {

fileContent, err := ReadFile(configFilePath)
if err != nil {
log.Errorf("NewServiceConfig: failed, err: %s", err)
return nil
}
config := &Config{}
err = json.Unmarshal(fileContent, config)
if err != nil {
log.Errorf("NewServiceConfig: failed, err: %s", err)
return nil
}

return config
}

// Dial connects a client to the given URL and groupID.
func Dial(config *Config) (*core.Chainsql, error) {
node := core.NewChainsql()
node.Connect(
config.URL,
config.RootCertPath,
config.ClientCertPath,
config.ClientKeyPath,
config.ServerName)

node.As(config.Account.Address, config.Account.Secrect)
return node, nil
}

func NewChainsqlInvoker() (*ChainsqlInvoker, error) {
instance := &ChainsqlInvoker{}
cfg := NewConfig(config2.DefConfig.ChainsqlSdkConfFile)

chainsql, err := Dial(cfg)
if err != nil {
return nil, err
}
instance.ChainsqlSdk = chainsql
instance.TransOpts = &core.TransactOpts{
ContractValue: 0,
Gas: 30000000,
Expectation: "validate_success",
}
return instance, nil
}


func ReadFile(fileName string) ([]byte, error) {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0666)
if err != nil {
return nil, fmt.Errorf("ReadFile: open file %s error %s", fileName, err)
}
defer func() {
err := file.Close()
if err != nil {
log.Errorf("ReadFile: File %s close error %s", fileName, err)
}
}()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("ReadFile: ioutil.ReadAll %s error %s", fileName, err)
}
return data, nil
}

func (invoker *ChainsqlInvoker) DeployCrossChainDataContract() (string, error) {

ret,_,err := eccd_abi.DeployEthCrossChainData(invoker.ChainsqlSdk,invoker.TransOpts)
if err != nil{
return "",err
}

return ret.ContractAddress,nil
}

func (invoker *ChainsqlInvoker) DeployCrossChainManagerContract(ccdcAddress string, chainID uint64) (string, error) {
account, err := data.NewAccountFromAddress(ccdcAddress)
if err != nil {
return "", err
}
eccdAddress := common.BytesToAddress(account.Bytes())
ret,_,err := eccm_abi.DeployEthCrossChainManager(invoker.ChainsqlSdk,invoker.TransOpts,eccdAddress,chainID)
if err != nil{
return "",err
}

return ret.ContractAddress,nil
}

func (invoker *ChainsqlInvoker) TransaferOwnershipForECCD(ccdcAddress string,ownerAddress string) (*common.TxResult,error){
account, err := data.NewAccountFromAddress(ownerAddress)
if err != nil {
return nil, err
}
ccmcAddress := common.BytesToAddress(account.Bytes())

eccdContract,_ := eccd_abi.NewEthCrossChainData(invoker.ChainsqlSdk,ccdcAddress)
return eccdContract.TransferOwnership(invoker.TransOpts,ccmcAddress)
}

func (invoker *ChainsqlInvoker) DeployCrossChainManagerProxyContract(ccmcAddress string) (string, error) {
account, err := data.NewAccountFromAddress(ccmcAddress)
if err != nil {
return "", err
}
eccmAddress := common.BytesToAddress(account.Bytes())
ret,_,err := eccmp_abi.DeployEccmpAbi(invoker.ChainsqlSdk,invoker.TransOpts,eccmAddress)
if err != nil{
return "",err
}

return ret.ContractAddress,nil
}

func (invoker *ChainsqlInvoker) TransferOwnershipForECCM(ccmcAddress string,ccmp string) (*common.TxResult,error){
account, err := data.NewAccountFromAddress(ccmp)
if err != nil {
return nil, err
}
ccmpAddress := common.BytesToAddress(account.Bytes())

eccmContract,_ := eccm_abi.NewEthCrossChainManager(invoker.ChainsqlSdk,ccmcAddress)
return eccmContract.TransferOwnership(invoker.TransOpts,ccmpAddress)
}
1,328 changes: 1,328 additions & 0 deletions chains/chainsql/eccd_abi/eccd_abi.go

Large diffs are not rendered by default.

1,750 changes: 1,750 additions & 0 deletions chains/chainsql/eccm_abi/eccm_abi.go

Large diffs are not rendered by default.

997 changes: 997 additions & 0 deletions chains/chainsql/eccmp_abi/eccmp_abi.go

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions cmd/chainsql_deployer/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"flag"
"github.com/ChainSQL/go-chainsql-api/data"
"github.com/polynetwork/poly-io-test/chains/chainsql"
"github.com/polynetwork/poly-io-test/config"
"github.com/polynetwork/poly-io-test/log"
)

var (
fnEth string
configPath string
)

func init() {
flag.StringVar(&configPath, "conf", "./config.json", "Config of poly-io-test")
flag.StringVar(&fnEth, "func", "deploy", "choose function to run: deploy or setup")
flag.Parse()
}

func main() {
err := config.DefConfig.Init(configPath)
if err != nil {
panic(err)
}

switch fnEth {
case "deploy":
DeployChainsqlSmartContract()
case "setup":
break
}
}

func DeployChainsqlSmartContract() {

var (
eccdAddr string
eccmAddr string
err error
)

invoker, err := chainsql.NewChainsqlInvoker()
if err != nil {
panic(err)
}

eccdAddr,err = invoker.DeployCrossChainDataContract()
if err != nil{
panic(err)
}

account, err := data.NewAccountFromAddress(eccdAddr)
if err != nil {
panic(err)
}

log.Infof("eccd_address:%s, hex:0x%x",eccdAddr,account.Bytes())

eccmAddr,err = invoker.DeployCrossChainManagerContract(eccdAddr,config.DefConfig.ChainsqlChainID)

result,err := invoker.TransaferOwnershipForECCD(eccdAddr,eccmAddr)

if err != nil{
panic(err)
}
if result.Status != "validate_success"{
panic(result.ErrorMessage)
}
account, err = data.NewAccountFromAddress(eccmAddr)
if err != nil {
panic(err)
}

log.Infof("eccm_address:%s, hex:0x%x",eccmAddr,account.Bytes())
eccmpAddr,err := invoker.DeployCrossChainManagerProxyContract(eccmAddr)
if err != nil{
panic(err)
}
result,err = invoker.TransferOwnershipForECCM(eccmAddr,eccmpAddr)
if result.Status != "validate_success"{
panic(result.ErrorMessage)
}

account, err = data.NewAccountFromAddress(eccmpAddr)
if err != nil {
panic(err)
}
log.Infof("eccmp_address:%s, hex:0x%x",eccmpAddr,account.Bytes())
}
Loading