Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Mar 4, 2024
1 parent 0304331 commit aebab94
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .ci/generate_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

GOPATH_BIN=$(go env GOPATH)/bin

c_sdk_version="v3.5.0"
c_sdk_version="v3.6.0"

LOG_ERROR() {
content=${1}
Expand Down
1 change: 1 addition & 0 deletions .ci/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ precompiled_test(){
# TODO: cns
# TODO: permission
precompileds=(config crud auth config)
sleep 10
for pkg in ${precompileds[*]}; do
cp -R nodes/127.0.0.1/sdk/* "./precompiled/${pkg}"
execute_cmd "go test ${ldflags} -v ./precompiled/${pkg}"
Expand Down
179 changes: 91 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Config struct {

1. 搭建FISCO BCOS 3.2以上版本节点,请[参考这里](https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/quick_start/air_installation.html)
1. 请拷贝对应的SDK证书到conf文件夹,证书名为`ca.crt/sdk.key/sdk.crt`,国密时证书名为`sm_ca.crt/sm_sdk.key/sm_sdk.crt/sm_ensdk.key/sm_ensdk.crt`
1. go-sdk需要依赖csdk的动态库,[下载地址](https://github.com/FISCO-BCOS/bcos-c-sdk/releases/tag/v3.5.0),将动态库放在`/usr/local/lib`目录下。在其他机器使用时也需要通过`export LD_LIBRARY_PATH=${PWD}/lib`设置动态库的搜索路径,其中`${PWD}/lib`需替换为bcos-c-sdk的动态库所在文件夹。如果编译后在其他机器运行,也可以在编译时使用`-ldflags`指定动态库搜索路径,如`go build -ldflags="-r ${PWD}/lib"`
1. go-sdk需要依赖csdk的动态库,[下载地址](https://github.com/FISCO-BCOS/bcos-c-sdk/releases/tag/v3.6.0),将动态库放在`/usr/local/lib`目录下。在其他机器使用时也需要通过`export LD_LIBRARY_PATH=${PWD}/lib`设置动态库的搜索路径,其中`${PWD}/lib`需替换为bcos-c-sdk的动态库所在文件夹。如果编译后在其他机器运行,也可以在编译时使用`-ldflags`指定动态库搜索路径,如`go build -ldflags="-r ${PWD}/lib"`

```bash
# 下面的脚本帮助用户下载bcos-c-sdk的动态库到/usr/local/lib目录下
Expand Down Expand Up @@ -163,12 +163,15 @@ cp .ci/hello/HelloWorld.sol ./hello
```

在MacOS下运行`./solc-0.8.11`时如果出现找不到`libz3.dylib`的错误,例如:

```bash
dyld[42564]: Library not loaded: /opt/homebrew/opt/z3/lib/libz3.dylib
Referenced from: <08BAD135-54EC-3430-A170-26E7B4A5BA96> xxxxxx/.fisco/solc/solc-0.8.11
Reason: tried xxxxxx
```

可尝试以下命令安装`libz3`

```bash
brew installz3
```
Expand Down Expand Up @@ -198,115 +201,115 @@ HelloWorld.abi HelloWorld.bin HelloWorld.go HelloWorld.sol
写入智能合约需要我们用私钥来对交易事务进行签名,我们创建的智能合约有一个名为`Set`的方法,它接受`string`类型的参数,然后将其设置为`value`,并且将`version`加1。
新建一个go工程,目录结构如下
```bash
hello
|—— HelloWorld.go
ca.crt
go.mod
go.sum
hello_main.go
sdk.crt
sdk.key
```

`hello_main.go`代码如下

```go
package main

import (
"context"
"encoding/hex"
"fmt"
"log"
"context"
"encoding/hex"
"fmt"
"log"

"example/go-sdk/hello"
"example/go-sdk/hello"

"github.com/FISCO-BCOS/go-sdk/v3/client"
"github.com/FISCO-BCOS/go-sdk/v3/types"
"github.com/FISCO-BCOS/go-sdk/v3/client"
"github.com/FISCO-BCOS/go-sdk/v3/types"
)

func main() {
privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
config := &client.Config{IsSMCrypto: false, GroupID: "group0",
PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"}
client, err := client.DialContext(context.Background(), config)
if err != nil {
log.Fatal(err)
}
input := "HelloWorld deployment 1.0"
fmt.Println("=================DeployHelloWorld===============")
address, receipt, instance, err := hello.DeployHelloWorld(client.GetTransactOpts(), client, input)
if err != nil {
log.Fatal(err)
}
fmt.Println("contract address: ", address.Hex()) // the address should be saved, will use in next example
fmt.Println("transaction hash: ", receipt.TransactionHash)

// load the contract
// contractAddress := common.HexToAddress("contract address in hex String")
// instance, err := hello.NewHelloWorld(contractAddress, client)
// if err != nil {
// log.Fatal(err)
// }

fmt.Println("================================")
helloSession := &hello.HelloWorldSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}

version, err := helloSession.Version()
if err != nil {
log.Fatal(err)
}

fmt.Println("version :", version) // "HelloWorld deployment 1.0"

ret, err := helloSession.Get()
if err != nil {
fmt.Printf("hello.Get() failed: %v", err)
return
}
done := make(chan bool)
currentBlock, err := client.GetBlockNumber(context.Background())
if err != nil {
fmt.Printf("GetBlockNumber() failed: %v", err)
return
}
_, err = helloSession.WatchAllSetValue(&currentBlock, func(ret int, logs []types.Log) {
fmt.Printf("WatchAllSetValue receive statud: %d, logs: %v\n", ret, logs)
setValue, err := helloSession.ParseSetValue(logs[0])
if err != nil {
fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
panic("WatchAllSetValue hello.WatchAllSetValue() failed")
}
fmt.Printf("receive setValue: %+v\n", *setValue)
done <- true
})
if err != nil {
fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
return
}
fmt.Printf("Get: %s\n", ret)
fmt.Println("================================")

oldValue, _, receipt, err := helloSession.Set("hello fisco")
fmt.Println("old value is: ", oldValue)
if err != nil {
log.Fatal(err)
}

fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())

ret, err = helloSession.Get()
if err != nil {
fmt.Printf("hello.Get() failed: %v", err)
return
}
fmt.Printf("Get: %s\n", ret)
<-done
privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
config := &client.Config{IsSMCrypto: false, GroupID: "group0",
PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"}
client, err := client.DialContext(context.Background(), config)
if err != nil {
log.Fatal(err)
}
input := "HelloWorld deployment 1.0"
fmt.Println("=================DeployHelloWorld===============")
address, receipt, instance, err := hello.DeployHelloWorld(client.GetTransactOpts(), client, input)
if err != nil {
log.Fatal(err)
}
fmt.Println("contract address: ", address.Hex()) // the address should be saved, will use in next example
fmt.Println("transaction hash: ", receipt.TransactionHash)

// load the contract
// contractAddress := common.HexToAddress("contract address in hex String")
// instance, err := hello.NewHelloWorld(contractAddress, client)
// if err != nil {
// log.Fatal(err)
// }

fmt.Println("================================")
helloSession := &hello.HelloWorldSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}

version, err := helloSession.Version()
if err != nil {
log.Fatal(err)
}

fmt.Println("version :", version) // "HelloWorld deployment 1.0"

ret, err := helloSession.Get()
if err != nil {
fmt.Printf("hello.Get() failed: %v", err)
return
}
done := make(chan bool)
currentBlock, err := client.GetBlockNumber(context.Background())
if err != nil {
fmt.Printf("GetBlockNumber() failed: %v", err)
return
}
_, err = helloSession.WatchAllSetValue(&currentBlock, func(ret int, logs []types.Log) {
fmt.Printf("WatchAllSetValue receive statud: %d, logs: %v\n", ret, logs)
setValue, err := helloSession.ParseSetValue(logs[0])
if err != nil {
fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
panic("WatchAllSetValue hello.WatchAllSetValue() failed")
}
fmt.Printf("receive setValue: %+v\n", *setValue)
done <- true
})
if err != nil {
fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
return
}
fmt.Printf("Get: %s\n", ret)
fmt.Println("================================")

oldValue, _, receipt, err := helloSession.Set("hello fisco")
fmt.Println("old value is: ", oldValue)
if err != nil {
log.Fatal(err)
}

fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())

ret, err = helloSession.Get()
if err != nil {
fmt.Printf("hello.Get() failed: %v", err)
return
}
fmt.Printf("Get: %s\n", ret)
<-done
}

```

在项目目录下运行`hello_world.go`

```bash
go run -ldflags="-r /usr/local/lib" main.go
go run -ldflags="-r /usr/local/lib" hello_main.go
```
2 changes: 1 addition & 1 deletion tools/download_csdk_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

install_path="/usr/local/lib/"
version="3.5.0"
version="3.6.0"
OS="linux"
versions=(3.2.0 3.4.0 3.5.0 3.6.0)

Expand Down

0 comments on commit aebab94

Please sign in to comment.