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 21, 2023
1 parent a54007d commit b9e3a60
Show file tree
Hide file tree
Showing 11 changed files with 1,051 additions and 153 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,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 b9e3a60

Please sign in to comment.