diff --git a/.ci/ci_check.sh b/.ci/ci_check.sh index dde23094..894ef477 100644 --- a/.ci/ci_check.sh +++ b/.ci/ci_check.sh @@ -60,19 +60,10 @@ function stop_nodes() execute_cmd "./nodes/127.0.0.1/stop_all.sh" } -# test the common jsonRPC interface -function test_rpc_connection() -{ - LOG_INFO "## test commonRPCInterface..." - # rpc client testing - execute_cmd "go test -v -count=1 ./client" -} - function main() { build_blockchain start_nodes - test_rpc_connection } main \ No newline at end of file diff --git a/.ci/generate_coverage.sh b/.ci/generate_coverage.sh new file mode 100644 index 00000000..211e2503 --- /dev/null +++ b/.ci/generate_coverage.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +GOPATH_BIN=$(go env GOPATH)/bin + +LOG_ERROR() { + content=${1} + echo -e "\033[31m${content}\033[0m" +} + +LOG_INFO() { + content=${1} + echo -e "\033[32m${content}\033[0m" +} + +execute_cmd() { + command="${1}" + eval "${command}" + ret=$? + if [ $ret -ne 0 ]; then + LOG_ERROR "FAILED of command: ${command}" + exit 1 + else + LOG_INFO "SUCCESS of command: ${command}" + fi +} + +generate_hello() { + local struct="${1}" + local output="${2}" + go get -u github.com/sqs/goreturns # to format code style +cat << EOF >> "${output}" + +func main() { + configs := conf.ParseConfig("config.toml") + client, err := client.Dial(&configs[0]) + if err != nil { + fmt.Printf("Dial Client failed, err:%v", err) + return + } + address, tx, _, err := Deploy${struct}(client.GetTransactOpts(), client) + if err != nil { + fmt.Printf("Deploy failed, err:%v", err) + return + } + fmt.Println(address.Hex()) // the address should be saved + fmt.Println(tx.Hash().Hex()) +} +EOF + "${GOPATH_BIN}"/goreturns -w "${output}" +} + +calculate_coverage() { + # start blockchain demo + curl -LO https://raw.githubusercontent.com/FISCO-BCOS/FISCO-BCOS/master/tools/build_chain.sh && chmod u+x build_chain.sh + bash build_chain.sh -l 127.0.0.1:4 -o nodes + cp nodes/127.0.0.1/sdk/* ./ + bash nodes/127.0.0.1/start_all.sh + + # generate code coverage report + go test ./... -race -coverprofile=coverage.txt -covermode=atomic +} + +calculate_coverage \ No newline at end of file diff --git a/.ci/hello/HelloWorld.go b/.ci/hello/HelloWorld.go new file mode 100644 index 00000000..ee25c0c0 --- /dev/null +++ b/.ci/hello/HelloWorld.go @@ -0,0 +1,237 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package helloworld + +import ( + "math/big" + "strings" + + "github.com/FISCO-BCOS/go-sdk/abi" + "github.com/FISCO-BCOS/go-sdk/abi/bind" + "github.com/FISCO-BCOS/go-sdk/core/types" + "github.com/FISCO-BCOS/go-sdk/event" + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common" +) + +// Reference imports to suppress errors if they are not otherwise used. +var ( + _ = big.NewInt + _ = strings.NewReader + _ = ethereum.NotFound + _ = abi.U256 + _ = bind.Bind + _ = common.Big1 + _ = types.BloomLookup + _ = event.NewSubscription +) + +// HelloWorldABI is the input ABI used to generate the binding from. +const HelloWorldABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"v\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" + +// HelloWorldBin is the compiled bytecode used for deploying new contracts. +var HelloWorldBin = "0x608060405234801561001057600080fd5b506040805190810160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b6102d7806101166000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634ed3885e146100515780636d4ce63c146100ba575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061014a565b005b3480156100c657600080fd5b506100cf610164565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561010f5780820151818401526020810190506100f4565b50505050905090810190601f16801561013c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b8060009080519060200190610160929190610206565b5050565b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101fc5780601f106101d1576101008083540402835291602001916101fc565b820191906000526020600020905b8154815290600101906020018083116101df57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061024757805160ff1916838001178555610275565b82800160010185558215610275579182015b82811115610274578251825591602001919060010190610259565b5b5090506102829190610286565b5090565b6102a891905b808211156102a457600081600090555060010161028c565b5090565b905600a165627a7a723058206c4a89d94174d13802f6408f93d5ff68fd568ddaf00fde211da477a42352f7bb0029" + +// DeployHelloWorld deploys a new contract, binding an instance of HelloWorld to it. +func DeployHelloWorld(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *HelloWorld, error) { + parsed, err := abi.JSON(strings.NewReader(HelloWorldABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(HelloWorldBin), backend) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &HelloWorld{HelloWorldCaller: HelloWorldCaller{contract: contract}, HelloWorldTransactor: HelloWorldTransactor{contract: contract}, HelloWorldFilterer: HelloWorldFilterer{contract: contract}}, nil +} + +// HelloWorld is an auto generated Go binding around a Solidity contract. +type HelloWorld struct { + HelloWorldCaller // Read-only binding to the contract + HelloWorldTransactor // Write-only binding to the contract + HelloWorldFilterer // Log filterer for contract events +} + +// HelloWorldCaller is an auto generated read-only Go binding around a Solidity contract. +type HelloWorldCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HelloWorldTransactor is an auto generated write-only Go binding around a Solidity contract. +type HelloWorldTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HelloWorldFilterer is an auto generated log filtering Go binding around a Solidity contract events. +type HelloWorldFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HelloWorldSession is an auto generated Go binding around a Solidity contract, +// with pre-set call and transact options. +type HelloWorldSession struct { + Contract *HelloWorld // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// HelloWorldCallerSession is an auto generated read-only Go binding around a Solidity contract, +// with pre-set call options. +type HelloWorldCallerSession struct { + Contract *HelloWorldCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// HelloWorldTransactorSession is an auto generated write-only Go binding around a Solidity contract, +// with pre-set transact options. +type HelloWorldTransactorSession struct { + Contract *HelloWorldTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// HelloWorldRaw is an auto generated low-level Go binding around a Solidity contract. +type HelloWorldRaw struct { + Contract *HelloWorld // Generic contract binding to access the raw methods on +} + +// HelloWorldCallerRaw is an auto generated low-level read-only Go binding around a Solidity contract. +type HelloWorldCallerRaw struct { + Contract *HelloWorldCaller // Generic read-only contract binding to access the raw methods on +} + +// HelloWorldTransactorRaw is an auto generated low-level write-only Go binding around a Solidity contract. +type HelloWorldTransactorRaw struct { + Contract *HelloWorldTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewHelloWorld creates a new instance of HelloWorld, bound to a specific deployed contract. +func NewHelloWorld(address common.Address, backend bind.ContractBackend) (*HelloWorld, error) { + contract, err := bindHelloWorld(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &HelloWorld{HelloWorldCaller: HelloWorldCaller{contract: contract}, HelloWorldTransactor: HelloWorldTransactor{contract: contract}, HelloWorldFilterer: HelloWorldFilterer{contract: contract}}, nil +} + +// NewHelloWorldCaller creates a new read-only instance of HelloWorld, bound to a specific deployed contract. +func NewHelloWorldCaller(address common.Address, caller bind.ContractCaller) (*HelloWorldCaller, error) { + contract, err := bindHelloWorld(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &HelloWorldCaller{contract: contract}, nil +} + +// NewHelloWorldTransactor creates a new write-only instance of HelloWorld, bound to a specific deployed contract. +func NewHelloWorldTransactor(address common.Address, transactor bind.ContractTransactor) (*HelloWorldTransactor, error) { + contract, err := bindHelloWorld(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &HelloWorldTransactor{contract: contract}, nil +} + +// NewHelloWorldFilterer creates a new log filterer instance of HelloWorld, bound to a specific deployed contract. +func NewHelloWorldFilterer(address common.Address, filterer bind.ContractFilterer) (*HelloWorldFilterer, error) { + contract, err := bindHelloWorld(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &HelloWorldFilterer{contract: contract}, nil +} + +// bindHelloWorld binds a generic wrapper to an already deployed contract. +func bindHelloWorld(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := abi.JSON(strings.NewReader(HelloWorldABI)) + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_HelloWorld *HelloWorldRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _HelloWorld.Contract.HelloWorldCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_HelloWorld *HelloWorldRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HelloWorld.Contract.HelloWorldTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_HelloWorld *HelloWorldRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _HelloWorld.Contract.HelloWorldTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_HelloWorld *HelloWorldCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _HelloWorld.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_HelloWorld *HelloWorldTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HelloWorld.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_HelloWorld *HelloWorldTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _HelloWorld.Contract.contract.Transact(opts, method, params...) +} + +// Get is a free data retrieval call binding the contract method 0x6d4ce63c. +// +// Solidity: function get() constant returns(string) +func (_HelloWorld *HelloWorldCaller) Get(opts *bind.CallOpts) (string, error) { + var ( + ret0 = new(string) + ) + out := ret0 + err := _HelloWorld.contract.Call(opts, out, "get") + return *ret0, err +} + +// Get is a free data retrieval call binding the contract method 0x6d4ce63c. +// +// Solidity: function get() constant returns(string) +func (_HelloWorld *HelloWorldSession) Get() (string, error) { + return _HelloWorld.Contract.Get(&_HelloWorld.CallOpts) +} + +// Get is a free data retrieval call binding the contract method 0x6d4ce63c. +// +// Solidity: function get() constant returns(string) +func (_HelloWorld *HelloWorldCallerSession) Get() (string, error) { + return _HelloWorld.Contract.Get(&_HelloWorld.CallOpts) +} + +// Set is a paid mutator transaction binding the contract method 0x4ed3885e. +// +// Solidity: function set(string v) returns() +func (_HelloWorld *HelloWorldTransactor) Set(opts *bind.TransactOpts, v string) (*types.Transaction, error) { + return _HelloWorld.contract.Transact(opts, "set", v) +} + +// Set is a paid mutator transaction binding the contract method 0x4ed3885e. +// +// Solidity: function set(string v) returns() +func (_HelloWorld *HelloWorldSession) Set(v string) (*types.Transaction, error) { + return _HelloWorld.Contract.Set(&_HelloWorld.TransactOpts, v) +} + +// Set is a paid mutator transaction binding the contract method 0x4ed3885e. +// +// Solidity: function set(string v) returns() +func (_HelloWorld *HelloWorldTransactorSession) Set(v string) (*types.Transaction, error) { + return _HelloWorld.Contract.Set(&_HelloWorld.TransactOpts, v) +} diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 00000000..f9526743 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,27 @@ +name: Calculate Code Coverage +on: + push: + paths-ignore: + - "Changelog.md" + - "README.md" + pull_request: + +jobs: + coverage: + name: coverage + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 1 + - uses: actions/setup-go@v2 + with: + go-version: 1.14 + - name: generate code coverage report + run: bash .ci/generate_coverage.sh + - name: upload code coverage to Codecov + uses: codecov/codecov-action@v1 + with: + file: ./coverage.txt + name: go-sdk code coverage + fail_ci_if_error: true \ No newline at end of file diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index b2614f68..7c8dbb16 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -41,6 +41,4 @@ jobs: - name: check commit run: bash .ci/check-commit.sh - name: test - run: bash .ci/integration_test.sh - # - name: generate code coverage - # run: + run: bash .ci/integration_test.sh \ No newline at end of file diff --git a/.gitignore b/.gitignore index c2b4c5ba..4a214250 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,9 @@ ca.crt # Test binary, built with `go test -c` *.test +# code coverage file +coverage.txt + # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/client/goclient_test.go b/client/goclient_test.go index d26dde21..d8161e8c 100644 --- a/client/goclient_test.go +++ b/client/goclient_test.go @@ -2,11 +2,20 @@ package client import ( "context" + "fmt" + helloworld "github.com/FISCO-BCOS/go-sdk/.ci/hello" + "strings" "testing" "github.com/FISCO-BCOS/go-sdk/conf" ) +var ( + contractAddress = "" + blockHash = "" // get blockHash by TestBlockHashByNumber test case + transactionHash = "" +) + func GetClient(t *testing.T) *Client { config := &conf.Config{IsHTTP: true, ChainID: 1, IsSMCrypto: false, GroupID: 1, PrivateKey: "145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58", NodeURL: "http://localhost:8545"} @@ -17,6 +26,37 @@ func GetClient(t *testing.T) *Client { return c } +func deployHelloWorldContract(t *testing.T) error { + client := GetClient(t) + address, tx, instance, err := helloworld.DeployHelloWorld(client.GetTransactOpts(), client) // deploy contract + if err != nil { + return fmt.Errorf("deploy HelloWorld contract failed:%v", err) + } + contractAddress = address.Hex() + transactionHash = tx.Hash().Hex() + _ = instance + return nil +} + +// Get contractAddress、transactionHash、blockHash by this test +func TestBlockHashByNumber(t *testing.T) { + err := deployHelloWorldContract(t) + if err != nil { + t.Logf("excute TestBlockHashByNumber test case failed:%v", err) + } + + c := GetClient(t) + + bnum := "0x1" + raw, err := c.GetBlockHashByNumber(context.Background(), bnum) + if err != nil { + t.Fatalf("block hash not found: %v", err) + } + + t.Logf("block hash by number:\n%s", raw) + blockHash = strings.Trim(string(raw),"\"") +} + func TestClientVersion(t *testing.T) { c := GetClient(t) @@ -50,272 +90,255 @@ func TestPBFTView(t *testing.T) { t.Logf("PBFT view: \n%s", pv) } -// func TestBlockLimit(t *testing.T) { -// c := GetClient(t) -// // cannot use big.NewInt to construct json request -// // TODO: analysis the ethereum's big.NewInt -// bl, err := c.GetBlockLimit(context.Background()) -// if err != nil { -// t.Fatalf("blockLimit not found: %v", err) -// } - -// t.Logf("latest blockLimit: \n%s", bl) -// } - -// func TestGroupID(t *testing.T) { -// c := GetClient(t) -// // cannot use big.NewInt to construct json request -// // TODO: analysis the ethereum's big.NewInt -// groupid := c.GetGroupID() -// t.Logf("current groupID: \n%s", groupid) -// } - -// func TestChainID(t *testing.T) { -// c := GetClient(t) -// // cannot use big.NewInt to construct json request -// // TODO: analysis the ethereum's big.NewInt -// chainid, err := c.GetChainID(context.Background()) -// if err != nil { -// t.Fatalf("Chain ID not found: %v", err) -// } -// t.Logf("Chain ID: \n%s", chainid) -// } - -// func TestSealerList(t *testing.T) { -// c := GetClient(t) -// sl, err := c.GetSealerList(context.Background()) -// if err != nil { -// t.Fatalf("sealer list not found: %v", err) -// } - -// t.Logf("sealer list:\n%s", sl) -// } - -// func TestObserverList(t *testing.T) { -// c := GetClient(t) -// ol, err := c.GetObserverList(context.Background()) -// if err != nil { -// t.Fatalf("observer list not found: %v", err) -// } - -// t.Logf("observer list:\n%s", ol) -// } - -// func TestConsensusStatus(t *testing.T) { -// c := GetClient(t) -// status, err := c.GetConsensusStatus(context.Background()) -// if err != nil { -// t.Fatalf("consensus status not found: %v", err) -// } - -// t.Logf("consensus status:\n%s", status) -// } - -// func TestSyncStatus(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetSyncStatus(context.Background()) -// if err != nil { -// t.Fatalf("synchronization status not found: %v", err) -// } - -// t.Logf("synchronization Status:\n%s", raw) -// } - -// func TestPeers(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetPeers(context.Background()) -// if err != nil { -// t.Fatalf("peers not found: %v", err) -// } - -// t.Logf("peers:\n%s", raw) -// } - -// func TestGroupPeers(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetGroupPeers(context.Background()) -// if err != nil { -// t.Fatalf("group peers not found: %v", err) -// } - -// t.Logf("group peers:\n%s", raw) -// } - -// func TestNodeIDList(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetNodeIDList(context.Background()) -// if err != nil { -// t.Fatalf("nodeID list not found: %v", err) -// } - -// t.Logf("nodeID list:\n %s", raw) -// } - -// func TestGroupList(t *testing.T) { -// c := GetClient(t) -// raw, err := c.GetGroupList(context.Background()) -// if err != nil { -// t.Fatalf("group list not found: %v", err) -// } - -// t.Logf("group list:\n%s", raw) -// } - -// func TestBlockByHash(t *testing.T) { -// c := GetClient(t) - -// bhash := "0xc0b21d064b97bafda716e07785fe8bb20cc23506bb980f12c7f7a4f4ef50ce30" -// includeTx := false -// raw, err := c.GetBlockByHash(context.Background(), bhash, includeTx) -// if err != nil { -// t.Fatalf("block not found: %v", err) -// } - -// t.Logf("block by hash:\n%s", raw) -// } - -// func TestBlockByNumber(t *testing.T) { -// c := GetClient(t) - -// bnum := "0x1" -// includeTx := true -// raw, err := c.GetBlockByNumber(context.Background(), bnum, includeTx) -// if err != nil { -// t.Fatalf("block not found: %v", err) -// } - -// t.Logf("block by number:\n%s", raw) -// } - -// func TestBlockHashByNumber(t *testing.T) { -// c := GetClient(t) - -// bnum := "0x1" -// raw, err := c.GetBlockHashByNumber(context.Background(), bnum) -// if err != nil { -// t.Fatalf("block hash not found: %v", err) -// } - -// t.Logf("block hash by number:\n%s", raw) -// } - -// func TestTransactionByHash(t *testing.T) { -// c := GetClient(t) - -// txhash := "0xed51827558939e8d103cbf8f6ff37f8a99582f09afa29e5636d0e54a073d0893" -// raw, err := c.GetTransactionByHash(context.Background(), txhash) -// if err != nil { -// t.Fatalf("transaction not found: %v", err) -// } - -// t.Logf("transaction by hash:\n%s", raw) -// } - -// func TestTransactionByBlockHashAndIndex(t *testing.T) { -// c := GetClient(t) - -// bhash := "0xc0b21d064b97bafda716e07785fe8bb20cc23506bb980f12c7f7a4f4ef50ce30" -// txindex := "0x0" -// raw, err := c.GetTransactionByBlockHashAndIndex(context.Background(), bhash, txindex) -// if err != nil { -// t.Fatalf("transaction not found: %v", err) -// } - -// t.Logf("transaction by block hash and transaction index:\n%s", raw) -// } - -// func TestTransactionByBlockNumberAndIndex(t *testing.T) { -// c := GetClient(t) - -// bnum := "0x1" -// txindex := "0x0" -// raw, err := c.GetTransactionByBlockNumberAndIndex(context.Background(), bnum, txindex) -// if err != nil { -// t.Fatalf("transaction not found: %v", err) -// } - -// t.Logf("transaction by block number and transaction index:\n%s", raw) -// } - -// func TestTransactionReceipt(t *testing.T) { -// c := GetClient(t) - -// txhash := "0x6613da911621248ffc2983a4553d44fb299887ae2d803eb81d9890465fbf29e5" -// raw, err := c.GetTransactionReceipt(context.Background(), txhash) -// if err != nil { -// t.Fatalf("transaction receipt not found: %v", err) -// } -// t.Logf("transaction receipt by transaction hash:\n%s", raw) -// } - -// func TestContractAddress(t *testing.T) { -// c := GetClient(t) -// txhash := "0x4a2a4d878318a83491383d29d6550c088bdcf692e3055b060342dcd85177c621" -// ca, err := c.GetContractAddress(context.Background(), txhash) -// if err != nil { -// t.Fatalf("ContractAddress not found: %v", err) -// } - -// t.Logf("ContractAddress: \n%s", ca.String()) -// } - -// func TestPendingTransactions(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetPendingTransactions(context.Background()) -// if err != nil { -// t.Fatalf("pending transactions not found: %v", err) -// } - -// t.Logf("pending transactions:\n%s", raw) -// } - -// func TestPendingTxSize(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetPendingTxSize(context.Background()) -// if err != nil { -// t.Fatalf("pending transactions not found: %v", err) -// } - -// t.Logf("the amount of the pending transactions:\n%s", raw) -// } - -// func TestGetCode(t *testing.T) { -// c := GetClient(t) - -// addr := "0x27c1b5d9fe3ab035c2e9db7199d4beb139e12292" -// raw, err := c.GetCode(context.Background(), addr) -// if err != nil { -// t.Fatalf("contract not found: %v", err) -// } - -// t.Logf("the contract code:\n%s", raw) -// } - -// func TestTotalTransactionCount(t *testing.T) { -// c := GetClient(t) - -// raw, err := c.GetTotalTransactionCount(context.Background()) -// if err != nil { -// t.Fatalf("transactions not found: %v", err) -// } - -// t.Logf("the totoal transactions and present block height:\n%s", raw) -// } - -// func TestSystemConfigByKey(t *testing.T) { -// c := GetClient(t) - -// findkey := "tx_count_limit" -// raw, err := c.GetSystemConfigByKey(context.Background(), findkey) -// if err != nil { -// t.Fatalf("the value not found: %v", err) -// } - -// t.Logf("the value got by the key:\n%s", raw) -// } +func TestBlockLimit(t *testing.T) { + c := GetClient(t) + // cannot use big.NewInt to construct json request + // TODO: analysis the ethereum's big.NewInt + bl, err := c.GetBlockLimit(context.Background()) + if err != nil { + t.Fatalf("blockLimit not found: %v", err) + } + + t.Logf("latest blockLimit: \n%s", bl) +} + +func TestGroupID(t *testing.T) { + c := GetClient(t) + // cannot use big.NewInt to construct json request + // TODO: analysis the ethereum's big.NewInt + groupid := c.GetGroupID() + t.Logf("current groupID: \n%s", groupid) +} + +func TestChainID(t *testing.T) { + c := GetClient(t) + // cannot use big.NewInt to construct json request + // TODO: analysis the ethereum's big.NewInt + chainid, err := c.GetChainID(context.Background()) + if err != nil { + t.Fatalf("Chain ID not found: %v", err) + } + t.Logf("Chain ID: \n%s", chainid) +} + +func TestSealerList(t *testing.T) { + c := GetClient(t) + sl, err := c.GetSealerList(context.Background()) + if err != nil { + t.Fatalf("sealer list not found: %v", err) + } + + t.Logf("sealer list:\n%s", sl) +} + +func TestObserverList(t *testing.T) { + c := GetClient(t) + ol, err := c.GetObserverList(context.Background()) + if err != nil { + t.Fatalf("observer list not found: %v", err) + } + + t.Logf("observer list:\n%s", ol) +} + +func TestConsensusStatus(t *testing.T) { + c := GetClient(t) + status, err := c.GetConsensusStatus(context.Background()) + if err != nil { + t.Fatalf("consensus status not found: %v", err) + } + + t.Logf("consensus status:\n%s", status) +} + +func TestSyncStatus(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetSyncStatus(context.Background()) + if err != nil { + t.Fatalf("synchronization status not found: %v", err) + } + + t.Logf("synchronization Status:\n%s", raw) +} + +func TestPeers(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetPeers(context.Background()) + if err != nil { + t.Fatalf("peers not found: %v", err) + } + + t.Logf("peers:\n%s", raw) +} + +func TestGroupPeers(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetGroupPeers(context.Background()) + if err != nil { + t.Fatalf("group peers not found: %v", err) + } + + t.Logf("group peers:\n%s", raw) +} + +func TestNodeIDList(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetNodeIDList(context.Background()) + if err != nil { + t.Fatalf("nodeID list not found: %v", err) + } + + t.Logf("nodeID list:\n %s", raw) +} + +func TestGroupList(t *testing.T) { + c := GetClient(t) + raw, err := c.GetGroupList(context.Background()) + if err != nil { + t.Fatalf("group list not found: %v", err) + } + + t.Logf("group list:\n%s", raw) +} + +func TestBlockByHash(t *testing.T) { + c := GetClient(t) + + includeTx := false + raw, err := c.GetBlockByHash(context.Background(), blockHash, includeTx) + if err != nil { + t.Fatalf("block not found: %v", err) + } + + t.Logf("block by hash:\n%s", raw) +} + +func TestBlockByNumber(t *testing.T) { + c := GetClient(t) + + bnum := "0x1" + includeTx := true + raw, err := c.GetBlockByNumber(context.Background(), bnum, includeTx) + if err != nil { + t.Fatalf("block not found: %v", err) + } + + t.Logf("block by number:\n%s", raw) +} + +func TestTransactionByHash(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetTransactionByHash(context.Background(), transactionHash) + if err != nil { + t.Fatalf("transaction not found: %v", err) + } + + t.Logf("transaction by hash:\n%s", raw) +} + +func TestTransactionByBlockHashAndIndex(t *testing.T) { + c := GetClient(t) + + txindex := "0x0" + raw, err := c.GetTransactionByBlockHashAndIndex(context.Background(), blockHash, txindex) + if err != nil { + t.Fatalf("transaction not found: %v", err) + } + + t.Logf("transaction by block hash and transaction index:\n%s", raw) +} + +func TestTransactionByBlockNumberAndIndex(t *testing.T) { + c := GetClient(t) + + bnum := "0x1" + txindex := "0x0" + raw, err := c.GetTransactionByBlockNumberAndIndex(context.Background(), bnum, txindex) + if err != nil { + t.Fatalf("transaction not found: %v", err) + } + + t.Logf("transaction by block number and transaction index:\n%s", raw) +} + +func TestTransactionReceipt(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetTransactionReceipt(context.Background(), transactionHash) + if err != nil { + t.Fatalf("transaction receipt not found: %v", err) + } + t.Logf("transaction receipt by transaction hash:\n%s", raw) +} + +func TestContractAddress(t *testing.T) { + c := GetClient(t) + + ca, err := c.GetContractAddress(context.Background(), transactionHash) + if err != nil { + t.Fatalf("ContractAddress not found: %v", err) + } + + t.Logf("ContractAddress: \n%s", ca.String()) +} + +func TestPendingTransactions(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetPendingTransactions(context.Background()) + if err != nil { + t.Fatalf("pending transactions not found: %v", err) + } + + t.Logf("pending transactions:\n%s", raw) +} + +func TestPendingTxSize(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetPendingTxSize(context.Background()) + if err != nil { + t.Fatalf("pending transactions not found: %v", err) + } + + t.Logf("the amount of the pending transactions:\n%s", raw) +} + +func TestGetCode(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetCode(context.Background(), contractAddress) + if err != nil { + t.Fatalf("contract not found: %v", err) + } + + t.Logf("the contract code:\n%s", raw) +} + +func TestTotalTransactionCount(t *testing.T) { + c := GetClient(t) + + raw, err := c.GetTotalTransactionCount(context.Background()) + if err != nil { + t.Fatalf("transactions not found: %v", err) + } + + t.Logf("the totoal transactions and present block height:\n%s", raw) +} + +func TestSystemConfigByKey(t *testing.T) { + c := GetClient(t) + + findkey := "tx_count_limit" + raw, err := c.GetSystemConfigByKey(context.Background(), findkey) + if err != nil { + t.Fatalf("the value not found: %v", err) + } + + t.Logf("the value got by the key:\n%s", raw) +} diff --git a/conn/channel.go b/conn/channel.go index f24a2d9b..928d7d71 100644 --- a/conn/channel.go +++ b/conn/channel.go @@ -599,6 +599,7 @@ func (hc *channelSession) processMessages() { if err != nil { // fmt.Printf("channel Read error:%v", err) hc.Close() + continue } hc.buf = append(hc.buf, receiveBuf[:b]...) msg, err := decodeChannelMessage(hc.buf) diff --git a/precompiled/cns/cns_service_test.go b/precompiled/cns/cns_service_test.go index fa804b7e..6b70bde8 100644 --- a/precompiled/cns/cns_service_test.go +++ b/precompiled/cns/cns_service_test.go @@ -3,9 +3,9 @@ package cns import ( "context" "crypto/ecdsa" + "github.com/FISCO-BCOS/go-sdk/abi/bind" "testing" - "github.com/FISCO-BCOS/go-sdk/abi/bind" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" "github.com/ethereum/go-ethereum/crypto" @@ -41,11 +41,11 @@ func GetService(t *testing.T) *Service { return service } -func TestAll(t *testing.T) { - name := "store" - version := "5.0" - address := "0x0626918C51A1F36c7ad4354BB1197460A533a2B9" - abi := `[ +const ( + name = "store" + version = "5.0" + address = "0x0626918C51A1F36c7ad4354BB1197460A533a2B9" + testABI = `[ { "constant": true, "inputs": [ @@ -126,11 +126,13 @@ func TestAll(t *testing.T) { "type": "event" } ]` +) + +func TestRegisterCns(t *testing.T) { c := GetClient(t) service := GetService(t) - // test RegisterCns - tx, err := service.RegisterCns(name, version, address, abi) + tx, err := service.RegisterCns(name, version, address, testABI) if err != nil { t.Fatalf("Service RegisterCns failed: %+v\n", err) } @@ -140,6 +142,10 @@ func TestAll(t *testing.T) { t.Fatalf("tx mining error:%v\n", err) } t.Logf("transaction hash: %s\n", receipt.GetTransactionHash()) +} + +func TestGetAddressByContractNameAndVersion(t *testing.T) { + service := GetService(t) // test GetAddressByContractNameAndVersion addr, err := service.GetAddressByContractNameAndVersion(name + ":" + version) @@ -147,6 +153,10 @@ func TestAll(t *testing.T) { t.Fatalf("GetAddressByContractNameAndVersion failed: %v", err) } t.Logf("address: %s", addr) +} + +func TestQueryCnsByNameAndVersion(t *testing.T) { + service := GetService(t) // test QueryCnsByNameAndVersion cnsInfo, err := service.QueryCnsByNameAndVersion(name, version) @@ -154,6 +164,10 @@ func TestAll(t *testing.T) { t.Fatalf("QueryCnsByNameAndVersion failed: %v\n", err) } t.Logf("QueryCnsByNameAndVersion: %s", cnsInfo[0].String()) +} + +func TestQueryCnsByName(t *testing.T) { + service := GetService(t) // test QueryCnsByNameAndVersion cnsInfoByName, err := service.QueryCnsByName(name) diff --git a/precompiled/config/system_config_service_test.go b/precompiled/config/system_config_service_test.go index 668077e5..1e5e19e3 100644 --- a/precompiled/config/system_config_service_test.go +++ b/precompiled/config/system_config_service_test.go @@ -2,11 +2,10 @@ package config import ( "context" - "testing" - "github.com/FISCO-BCOS/go-sdk/abi/bind" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" + "testing" ) func TestSetValueByKey(t *testing.T) { diff --git a/precompiled/consensus/consensus_service_test.go b/precompiled/consensus/consensus_service_test.go index 88215844..c8c4c0fc 100644 --- a/precompiled/consensus/consensus_service_test.go +++ b/precompiled/consensus/consensus_service_test.go @@ -3,14 +3,19 @@ package consensus import ( "context" "crypto/ecdsa" + "github.com/FISCO-BCOS/go-sdk/abi/bind" + "regexp" "testing" - "github.com/FISCO-BCOS/go-sdk/abi/bind" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" "github.com/ethereum/go-ethereum/crypto" ) +var ( + nodeID = "" +) + func GetClient(t *testing.T) *client.Client { // config := &conf.ParseConfig("config.toml")[0] config := &conf.Config{IsHTTP: true, ChainID: 1, IsSMCrypto: false, GroupID: 1, @@ -41,6 +46,22 @@ func GetService(t *testing.T) *ConsensusService { return service } +// Get nodeID +// TODO: try to use TestMain function to init before excute test case +func TestGetNodeID(t *testing.T) { + c := GetClient(t) + sealerList, err := c.GetNodeIDList(context.Background()) + if err != nil { + t.Fatalf("sealer list not found: %v", err) + } + reg := regexp.MustCompile(`[\w]+`) + nodeList := reg.FindAllString(string(sealerList), -1) + if len(nodeList) < 4 { // pbft consensus needs 2f+1 + t.Fatalf("the number of nodes does not exceed 4") + } + nodeID = nodeList[1] +} + func TestAddObserver(t *testing.T) { c := GetClient(t) service := GetService(t) @@ -51,7 +72,6 @@ func TestAddObserver(t *testing.T) { } t.Logf("Observer list: %s\n", observer) - nodeID := "da72d42af7228b7fcbd0c2ca1128a9cf5b1a3a648c64878ebba4177a751507a0e1d686c2a6ccdfdadcfc60c1d6ec6d5d07797880f2f6a1f176d480b98ed5a13c" tx, err := service.AddObserver(nodeID) if err != nil { t.Fatalf("ConsensusService AddObserver failed: %+v\n", err) @@ -80,7 +100,6 @@ func TestAddSealer(t *testing.T) { } t.Logf("Sealer list: %s\n", observer) - nodeID := "da72d42af7228b7fcbd0c2ca1128a9cf5b1a3a648c64878ebba4177a751507a0e1d686c2a6ccdfdadcfc60c1d6ec6d5d07797880f2f6a1f176d480b98ed5a13c" tx, err := service.AddSealer(nodeID) if err != nil { t.Fatalf("ConsensusService AddSealer failed: %+v\n", err) @@ -109,8 +128,8 @@ func TestRemove(t *testing.T) { } t.Logf("Sealer list: %s\n", observer) - nodeID := "da72d42af7228b7fcbd0c2ca1128a9cf5b1a3a648c64878ebba4177a751507a0e1d686c2a6ccdfdadcfc60c1d6ec6d5d07797880f2f6a1f176d480b98ed5a13c" tx, err := service.RemoveNode(nodeID) + if err != nil { t.Fatalf("ConsensusService Remove failed: %+v\n", err) } diff --git a/precompiled/permission/permission_service_test.go b/precompiled/permission/permission_service_test.go index 1fbe32c8..4b34e5ec 100644 --- a/precompiled/permission/permission_service_test.go +++ b/precompiled/permission/permission_service_test.go @@ -2,10 +2,12 @@ package permission import ( "crypto/ecdsa" + "fmt" "testing" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" + "github.com/FISCO-BCOS/go-sdk/precompiled/crud" "github.com/ethereum/go-ethereum/crypto" ) @@ -15,6 +17,28 @@ const ( permisstionAdd = "0xFbb18d54e9Ee57529cda8c7c52242EFE879f064F" ) +func createUserTable(t *testing.T) error { + tableName := "t_test" + key := "name" + valueFields := "item_id, item_name" + table := &crud.Table{TableName: tableName, Key: key, ValueFields: valueFields} + + c := GetClient(t) + privateKey := GenerateKey(t) + service, err := crud.NewCRUDService(c, privateKey) + if err != nil { + return fmt.Errorf("init CRUDService failed: %+v", err) + } + + // create table + resultCreate, err := service.CreateTable(table) + if err != nil { + return fmt.Errorf("create table %v failed: %+v", tableName, err) + } + t.Logf("resultCreate: %d\n", resultCreate) + return nil +} + func GetClient(t *testing.T) *client.Client { // config := &conf.ParseConfig("config.toml")[0] config := &conf.Config{IsHTTP: true, ChainID: 1, IsSMCrypto: false, GroupID: 1, @@ -73,92 +97,118 @@ func TestGrant(t *testing.T) { t.Logf("ListPermissionManager: %v", listResult) } -// func TestUserTableManager(t *testing.T) { -// service := GetService(t) - -// result, err := service.GrantUserTableManager(tableName, permisstionAdd) -// if err != nil { -// t.Fatalf("TestUserTableManager failed: %v", err) -// } -// t.Logf("TestUserTableManager: %v", result) -// revokeResult, err := service.RevokeUserTableManager(tableName, permisstionAdd) -// if err != nil { -// t.Fatalf("TestUserTableManager failed: %v", err) -// } -// t.Logf("TestUserTableManager revoke result: %v", revokeResult) -// } - -// func TestDeployAndCreateManager(t *testing.T) { -// service := GetService(t) - -// result, err := service.GrantDeployAndCreateManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestDeployAndCreateManager failed: %v", err) -// } -// t.Logf("TestDeployAndCreateManager: %v", result) - -// revokeResult, err := service.RevokeDeployAndCreateManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestDeployAndCreateManager failed: %v", err) -// } -// t.Logf("TestDeployAndCreateManager revoke result: %v", revokeResult) -// } - -// func TestNodeManager(t *testing.T) { -// service := GetService(t) - -// result, err := service.GrantNodeManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestNodeManager failed: %v", err) -// } -// t.Logf("TestNodeManager: %v", result) - -// revokeResult, err := service.RevokeNodeManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestNodeManager failed: %v", err) -// } -// t.Logf("TestNodeManager revoke result: %v", revokeResult) -// } - -// func TestCNSManager(t *testing.T) { -// service := GetService(t) - -// result, err := service.GrantCNSManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestCNSManager failed: %v", err) -// } -// t.Logf("TestCNSManager: %v", result) - -// revokeResult, err := service.RevokeCNSManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestCNSManager failed: %v", err) -// } -// t.Logf("TestCNSManager revoke result: %v", revokeResult) -// } - -// func TestSysConfigManager(t *testing.T) { -// service := GetService(t) - -// result, err := service.GrantSysConfigManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestSysConfigManager failed: %v", err) -// } -// t.Logf("TestSysConfigManager: %v", result) - -// revokeResult, err := service.RevokeSysConfigManager(permisstionAdd) -// if err != nil { -// t.Fatalf("TestSysConfigManager failed: %v", err) -// } -// t.Logf("TestSysConfigManager revoke result: %v", revokeResult) -// t.Logf("Success result: %s", success) -// } - -// func TestListUser(t *testing.T) { -// service := GetService(t) - -// result, err := service.ListUserTableManager(tableName) -// if err != nil { -// t.Fatalf("ListUserTableManager failed: %v", err) -// } -// t.Logf("ListUserTableManager: %v", result) -// } +func TestUserTableManager(t *testing.T) { + err := createUserTable(t) + if err != nil { + t.Logf("TestUserTableManager failed: %v", err) + } + + service := GetService(t) + + result, err := service.GrantUserTableManager(tableName, permisstionAdd) + if err != nil { + t.Fatalf("TestUserTableManager failed: %v", err) + } + t.Logf("TestUserTableManager: %v", result) + + revokeResult, err := service.RevokeUserTableManager(tableName, permisstionAdd) + if err != nil { + t.Fatalf("TestUserTableManager failed: %v", err) + } + t.Logf("TestUserTableManager revoke result: %v", revokeResult) + + listResult, err := service.ListUserTableManager(tableName) + if err != nil { + t.Fatalf("ListUserTableManager failed: %v", err) + } + t.Logf("ListUserTableManager: %v", listResult) +} + +func TestDeployAndCreateManager(t *testing.T) { + service := GetService(t) + + result, err := service.GrantDeployAndCreateManager(permisstionAdd) + if err != nil { + t.Fatalf("TestDeployAndCreateManager failed: %v", err) + } + t.Logf("TestDeployAndCreateManager: %v", result) + + revokeResult, err := service.RevokeDeployAndCreateManager(permisstionAdd) + if err != nil { + t.Fatalf("TestDeployAndCreateManager failed: %v", err) + } + t.Logf("TestDeployAndCreateManager revoke result: %v", revokeResult) + + listResult, err := service.ListDeployAndCreateManager() + if err != nil { + t.Fatalf("ListDeployAndCreateManager failed: %v", err) + } + t.Logf("ListDeployAndCreateManager: %v", listResult) +} + +func TestNodeManager(t *testing.T) { + service := GetService(t) + + result, err := service.GrantNodeManager(permisstionAdd) + if err != nil { + t.Fatalf("TestNodeManager failed: %v", err) + } + t.Logf("TestNodeManager: %v", result) + + revokeResult, err := service.RevokeNodeManager(permisstionAdd) + if err != nil { + t.Fatalf("TestNodeManager failed: %v", err) + } + t.Logf("TestNodeManager revoke result: %v", revokeResult) + + listResult, err := service.ListNodeManager() + if err != nil { + t.Fatalf("ListNodeManager failed: %v", err) + } + t.Logf("ListNodeManager: %v", listResult) +} + +func TestCNSManager(t *testing.T) { + service := GetService(t) + + result, err := service.GrantCNSManager(permisstionAdd) + if err != nil { + t.Fatalf("TestCNSManager failed: %v", err) + } + t.Logf("TestCNSManager: %v", result) + + revokeResult, err := service.RevokeCNSManager(permisstionAdd) + if err != nil { + t.Fatalf("TestCNSManager failed: %v", err) + } + t.Logf("TestCNSManager revoke result: %v", revokeResult) + + listResult, err := service.ListCNSManager() + if err != nil { + t.Fatalf("ListCNSManager failed: %v", err) + } + t.Logf("ListCNSManager: %v", listResult) +} + +func TestSysConfigManager(t *testing.T) { + service := GetService(t) + + result, err := service.GrantSysConfigManager(permisstionAdd) + if err != nil { + t.Fatalf("TestSysConfigManager failed: %v", err) + } + t.Logf("TestSysConfigManager: %v", result) + + revokeResult, err := service.RevokeSysConfigManager(permisstionAdd) + if err != nil { + t.Fatalf("TestSysConfigManager failed: %v", err) + } + t.Logf("TestSysConfigManager revoke result: %v", revokeResult) + t.Logf("Success result: %s", success) + + listResult, err := service.ListSysConfigManager() + if err != nil { + t.Fatalf("ListSysConfigManager failed: %v", err) + } + t.Logf("ListSysConfigManager: %v", listResult) +}