Skip to content

Commit

Permalink
add some tx method and update c-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Dec 22, 2023
1 parent a54007d commit 3700637
Show file tree
Hide file tree
Showing 14 changed files with 1,070 additions and 160 deletions.
23 changes: 18 additions & 5 deletions .ci/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

c_sdk_version="v3.4.0"
c_sdk_version="v3.5.0"
start_time=15
macOS=
ldflags="-ldflags=\"-r /usr/local/lib/\""
Expand Down Expand Up @@ -336,7 +336,9 @@ integration_std()
head build_chain.sh
bash build_chain.sh -l 127.0.0.1:2 -o nodes -a 0x83309d045a19c44dc3722d15a6abd472f95866ac
bash nodes/127.0.0.1/start_all.sh && sleep "${start_time}"
mkdir -p conf
cp nodes/127.0.0.1/sdk/* ./
cp nodes/127.0.0.1/sdk/* ./conf/
cp nodes/127.0.0.1/sdk/* ./client/

# abigen std
Expand Down Expand Up @@ -364,6 +366,7 @@ integration_std()
if [[ "${check_amop}" == "true" ]];then
integration_amop
fi
integration_examples
bash nodes/127.0.0.1/stop_all.sh
LOG_INFO "integration_std testing pass."
}
Expand All @@ -376,6 +379,8 @@ integration_gm()
bash build_chain.sh -l 127.0.0.1:2 -s -o nodes_gm -a 0x791a0073e6dfd9dc5e5061aebc43ab4f7aa4ae8b
cp -r nodes_gm/127.0.0.1/sdk/* ./conf/
bash nodes_gm/127.0.0.1/start_all.sh && sleep "${start_time}"
mkdir -p conf
cp nodes/127.0.0.1/sdk/* ./conf
cp nodes/127.0.0.1/sdk/* ./
cp nodes/127.0.0.1/sdk/* ./client/

Expand All @@ -399,20 +404,28 @@ integration_gm()
integration_amop() {
# nodes should be started
LOG_INFO "amop unicast testing..."
execute_cmd "go build ${ldflags} -o subscriber examples/amop/sub/subscriber.go"
execute_cmd "go build ${ldflags} -o unicast_publisher examples/amop/unicast_pub/publisher.go"
execute_cmd "go build ${ldflags} -o subscriber ./examples/amop/sub/subscriber.go"
execute_cmd "go build ${ldflags} -o unicast_publisher ./examples/amop/unicast_pub/publisher.go"
./subscriber 127.0.0.1:20201 hello &
sleep 2
./unicast_publisher 127.0.0.1:20200 hello

LOG_INFO "amop broadcast testing..."
execute_cmd "go build ${ldflags} -o broadcast_publisher examples/amop/broadcast_pub/publisher.go"
execute_cmd "go build ${ldflags} -o broadcast_publisher examples/amop/broadcast_pub/publisher.go"
execute_cmd "go build ${ldflags} -o broadcast_publisher ./examples/amop/broadcast_pub/publisher.go"
execute_cmd "go build ${ldflags} -o broadcast_publisher ./examples/amop/broadcast_pub/publisher.go"
./subscriber 127.0.0.1:20201 hello1 &
sleep 2
./broadcast_publisher 127.0.0.1:20200 hello1
}

integration_examples() {
# nodes should be started
execute_cmd "go build ${ldflags} -o hello ./examples/hello_world/wrapper/"
./hello
execute_cmd "go build ${ldflags} -o hello ./examples/hello_world/manual/"
./hello
}

parse_params()
{
echo "parse_params $#"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 1
- uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.21
- name: mod tidy
run: go mod tidy
- name: generate code coverage report
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
go: ["1.17", "1.19"]
go: ["1.21"]
steps:
- uses: actions/checkout@v2
with:
Expand Down
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ conf
sdk.crt
sdk.key
ca.crt
*.nodeid
*.cnf

# Test binary, built with `go test -c`
*.test
Expand Down Expand Up @@ -62,3 +64,10 @@ config.toml
# library
libs
lib

# csdk log
*.log

# solc output
*.bin
*.abi
76 changes: 22 additions & 54 deletions client/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,8 @@ type Connection struct {
writeConn jsonWriter

// for dispatch
close chan struct{}
closing chan struct{} // closed when client is quitting
didClose chan struct{} // closed when client quits
reconnected chan ServerCodec // where write/reconnect sends the new connection
readOp chan readOp // read messages
readErr chan error // errors from read
reqInit chan *requestOp // register response IDs, takes write lock
reqSent chan error // signals write completion, releases write lock
reqTimeout chan *requestOp // removes response IDs when call timeout expires
}

type reconnectFunc func(ctx context.Context) (ServerCodec, error)
Expand All @@ -131,11 +124,6 @@ func (cc *clientConn) close(err error, inflightReq *requestOp) {
cc.codec.Close()
}

type readOp struct {
msgs []*jsonrpcMessage
batch bool
}

type requestOp struct {
ids []json.RawMessage
err error
Expand Down Expand Up @@ -266,15 +254,8 @@ func NewConnection(config *Config) (*Connection, error) {
func initClient(conn ServerCodec) *Connection {
c := &Connection{
writeConn: conn,
close: make(chan struct{}),
closing: make(chan struct{}),
didClose: make(chan struct{}),
reconnected: make(chan ServerCodec),
readOp: make(chan readOp),
readErr: make(chan error),
reqInit: make(chan *requestOp),
reqSent: make(chan error, 1),
reqTimeout: make(chan *requestOp),
}
return c
}
Expand Down Expand Up @@ -455,15 +436,29 @@ func (c *Connection) CallContext(ctx context.Context, result interface{}, method
case "getPendingTxSize":
c.csdk.GetPendingTxSize(op.respChanData)
case "sendTransaction":
data := hexutil.Encode(args[0].([]byte))
contractAddress := args[1].(string)
fallthrough
case "SendEncodedTransaction":
var handler func(*types.Receipt, error)
if len(args) >= 3 {
handler = args[2].(func(*types.Receipt, error))
}
_, err := c.csdk.CreateAndSendTransaction(op.respChanData, contractAddress, data, "", true)
if err != nil {
return err
if method == "sendTransaction" {
data := hexutil.Encode(args[0].([]byte))
contractAddress := args[1].(string)
if len(args) >= 3 {
handler = args[2].(func(*types.Receipt, error))
}
_, err := c.csdk.CreateAndSendTransaction(op.respChanData, contractAddress, data, "", true)
if err != nil {
return err
}
} else { // SendEncodedTransaction
encodedTransaction := args[0].([]byte)
withProof := args[1].(bool)
if len(args) >= 3 {
handler = args[2].(func(*types.Receipt, error))
}
err := c.csdk.SendEncodedTransaction(op.respChanData, encodedTransaction, withProof)
if err != nil {
return err
}
}
// async send transaction
if handler != nil {
Expand Down Expand Up @@ -509,22 +504,6 @@ func (c *Connection) CallContext(ctx context.Context, result interface{}, method
}
}

//func (c *Connection) UnsubscribeBlockNumberNotify(groupID uint64) error {
// hc := c.writeConn.(*channelSession)
// return hc.unSubscribeBlockNumberNotify(groupID)
//}

//func (c *Connection) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
// msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method}
// if paramsIn != nil { // prevent sending "params":null
// var err error
// if msg.Params, err = json.Marshal(paramsIn); err != nil {
// return nil, err
// }
// }
// return msg, nil
//}

func (c *Connection) reconnect(ctx context.Context) error {
if c.reconnectFunc == nil {
return errDead
Expand All @@ -549,14 +528,3 @@ func (c *Connection) reconnect(ctx context.Context) error {
return ErrClientQuit
}
}

// drainRead drops read messages until an error occurs.
func (c *Connection) drainRead() {
for {
select {
case <-c.readOp:
case <-c.readErr:
return
}
}
}
57 changes: 52 additions & 5 deletions client/go_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *Client) WaitMined(tx *types.Transaction) (*types.Receipt, error) {

// SMCrypto returns true if use sm crypto
func (c *Client) SMCrypto() bool {
return c.smCrypto
return c.conn.GetCSDK().SMCrypto()
}

// CodeAt returns the contract code of the given account.
Expand Down Expand Up @@ -194,10 +194,7 @@ func (c *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNu
if cr.Status != 0 {
var errorMessage string
if len(cr.Output) >= 138 {
outputBytes, err := hex.DecodeString(cr.Output[2:])
if err != nil {
return nil, fmt.Errorf("call error of status %d, hex.DecodeString failed", cr.Status)
}
outputBytes := common.FromHex(cr.Output)
errorMessage = string(outputBytes[68:])
}
return nil, fmt.Errorf("call error of status %d, %v", cr.Status, errorMessage)
Expand Down Expand Up @@ -273,6 +270,56 @@ func (c *Client) AsyncSendTransaction(ctx context.Context, tx *types.Transaction
return nil
}

func (c *Client) CreateEncodedTransactionDataV1(to *common.Address, input []byte, blockLimit int64, abi string) ([]byte, []byte, error) {
addressHex := ""
if to != nil {
addressHex = strings.ToLower(to.String()[2:])
}
return c.conn.GetCSDK().CreateEncodedTransactionDataV1(blockLimit, addressHex, input, abi)
}

func (c *Client) CreateEncodedSignature(hash []byte) ([]byte, error) {
return c.conn.GetCSDK().CreateEncodedSignature(hash)
}

func (c *Client) CreateEncodedTransaction(transactionData, transactionDataHash, signature []byte, attribute int32, extraData string) ([]byte, error) {
return c.conn.GetCSDK().CreateEncodedTransaction(transactionData, transactionDataHash, signature, attribute, extraData)
}

func (c *Client) SendEncodedTransaction(ctx context.Context, encodedTransaction []byte, withProof bool) (*types.Receipt, error) {
var err error
var anonymityReceipt = &struct {
types.Receipt
}{}
err = c.conn.CallContext(ctx, anonymityReceipt, "SendEncodedTransaction", encodedTransaction, withProof)
if err != nil {
errorStr := fmt.Sprintf("%s", err)
if strings.Contains(errorStr, "connection refused") {
log.Println("connection refused err:", err)
return nil, err
}
return nil, err
}
return &anonymityReceipt.Receipt, nil
}

func (c *Client) AsyncSendEncodedTransaction(ctx context.Context, encodedTransaction []byte, withProof bool, handler func(*types.Receipt, error)) error {
err := c.conn.CallContext(ctx, nil, "SendEncodedTransaction", encodedTransaction, withProof, handler)
if err != nil {
errorStr := fmt.Sprintf("%s", err)
if strings.Contains(errorStr, "connection refused") {
log.Println("connection refused err:", err)
return err
}
return err
}
return nil
}

func (c *Client) SetPrivateKey(privateKey []byte) error {
return c.conn.GetCSDK().SetPrivateKey(privateKey)
}

// TransactionReceipt returns the receipt of a transaction by transaction hash.
// Note that the receipt is not available for pending transactions.
func (c *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
Expand Down
Loading

0 comments on commit 3700637

Please sign in to comment.