diff --git a/client/client.go b/client/client.go index 4fb23e9b65719626168cb91baeac937fc07052dd..f8cdfd042a951705fabe9786599d0be54eeefe6a 100644 --- a/client/client.go +++ b/client/client.go @@ -51,7 +51,7 @@ type NodeWebsocketClient interface { Subscribe(eventId string) error Unsubscribe(eventId string) error - WaitForConfirmation(eventid string) (chan Confirmation, error) + WaitForConfirmation(tx txs.Tx, chainId string, inputAddr []byte) (chan Confirmation, error) Close() } diff --git a/client/cmd/transaction.go b/client/cmd/transaction.go index 13076b0ec366a21a1e06082de52ff2dbf99f8e17..87a3e10c5ee99aeb84a97062280d98c4047d0016 100644 --- a/client/cmd/transaction.go +++ b/client/cmd/transaction.go @@ -149,7 +149,7 @@ func addTransactionPersistentFlags() { // TransactionCmd.PersistentFlags().BoolVarP(&clientDo.SignFlag, "sign", "s", false, "sign the transaction using the eris-keys daemon") TransactionCmd.PersistentFlags().BoolVarP(&clientDo.BroadcastFlag, "broadcast", "b", true, "broadcast the transaction to the blockchain") - TransactionCmd.PersistentFlags().BoolVarP(&clientDo.WaitFlag, "wait", "w", false, "wait for the transaction to be committed in a block") + TransactionCmd.PersistentFlags().BoolVarP(&clientDo.WaitFlag, "wait", "w", true, "wait for the transaction to be committed in a block") } //------------------------------------------------------------------------------ diff --git a/client/core/transaction_factory.go b/client/core/transaction_factory.go index fef9dc97384322ae4c8afd1c47de8dc5666635d4..15dd140b11a52e299fb9eb85060e5ff8f72e63f0 100644 --- a/client/core/transaction_factory.go +++ b/client/core/transaction_factory.go @@ -20,8 +20,7 @@ import ( "encoding/hex" "fmt" "strconv" - // "strings" - "time" + log "github.com/eris-ltd/eris-logger" ptypes "github.com/eris-ltd/eris-db/permission/types" @@ -274,9 +273,9 @@ type TxResult struct { // Preserve func SignAndBroadcast(chainID string, nodeClient client.NodeClient, keyClient keys.KeyClient, tx txs.Tx, sign, broadcast, wait bool) (txResult *TxResult, err error) { - // var inputAddr []byte + var inputAddr []byte if sign { - _, tx, err = signTx(keyClient, chainID, tx) + inputAddr, tx, err = signTx(keyClient, chainID, tx) if err != nil { return nil, err } @@ -286,32 +285,41 @@ func SignAndBroadcast(chainID string, nodeClient client.NodeClient, keyClient ke } if broadcast { - // if wait { - // var ch chan Msg - // ch, err = subscribeAndWait(tx, chainID, nodeAddr, inputAddr) - // if err != nil { - // return nil, err - // } else { - // defer func() { - // if err != nil { - // // if broadcast threw an error, just return - // return - // } - // log.WithFields(log.Fields{ - // "", - // }).Debug("Waiting for tx to be committed") - // msg := <-ch - // if msg.Error != nil { - // logger.Infof("Encountered error waiting for event: %v\n", msg.Error) - // err = msg.Error - // } else { - // txResult.BlockHash = msg.BlockHash - // txResult.Return = msg.Value - // txResult.Exception = msg.Exception - // } - // }() - // } - // } + if wait { + wsClient, err := nodeClient.DeriveWebsocketClient() + if err != nil { + return nil, err + } + var confirmationChannel chan client.Confirmation + confirmationChannel, err = wsClient.WaitForConfirmation(tx, chainID, inputAddr) + if err != nil { + return nil, err + } else { + defer func() { + if err != nil { + // if broadcast threw an error, just return + return + } + log.Debug("Waiting for transaction to be confirmed.") + confirmation := <- confirmationChannel + if confirmation.Error != nil { + log.Errorf("Encountered error waiting for event: %s\n", confirmation.Error) + err = confirmation.Error + return + } + eventDataTx, ok := confirmation.Event.(txs.EventDataTx) + if !ok { + log.Errorf("Received wrong event type.") + err = fmt.Errorf("Received wrong event type.") + return + } + txResult.BlockHash = confirmation.BlockHash + txResult.Return = eventDataTx.Return + txResult.Exception = confirmation.Exception.Error() + }() + } + } + var receipt *txs.Receipt receipt, err = nodeClient.Broadcast(tx) if err != nil { @@ -329,21 +337,20 @@ func SignAndBroadcast(chainID string, nodeClient client.NodeClient, keyClient ke } } } - time.Sleep(2*time.Second) return } //------------------------------------------------------------------------------------ // wait for events -type Msg struct { - BlockHash []byte - Value []byte - Exception string - Error error -} +// type Msg struct { +// BlockHash []byte +// Value []byte +// Exception string +// Error error +// } -// func subscribeAndWait(tx txs.Tx, chainID, nodeAddr string, inputAddr []byte) (chan Msg, error) { +// func subscribeAndWait(tx txs.Tx, chainID string, nodeAddr string, inputAddr []byte) (chan Msg, error) { // // subscribe to event and wait for tx to be committed // var wsAddr string // if strings.HasPrefix(nodeAddr, "http://") { diff --git a/client/websocket_client.go b/client/websocket_client.go index 05cc464af213db574a55e69d6a09136b56061dda..627de09986b953e612eb7422c5930f8c0db24c9a 100644 --- a/client/websocket_client.go +++ b/client/websocket_client.go @@ -63,19 +63,16 @@ func (erisNodeWebsocketClient *ErisNodeWebsocketClient) Unsubscribe(subscription // Returns a channel that will receive a confirmation with a result or the exception that // has been confirmed; or an error is returned and the confirmation channel is nil. -func (erisNodeWebsocketClient *ErisNodeWebsocketClient) WaitForConfirmation(eventid string) (chan Confirmation, error) { +func (erisNodeWebsocketClient *ErisNodeWebsocketClient) WaitForConfirmation(tx txs.Tx, chainId string, inputAddr []byte) (chan Confirmation, error) { // check no errors are reported on the websocket if err := erisNodeWebsocketClient.assertNoErrors(); err != nil { return nil, err } - // Setup the confirmation channel to be returned confirmationChannel := make(chan Confirmation, 1) var latestBlockHash []byte - var inputAddr []byte - var chainId string - var tx txs.Tx + eid := txs.EventStringAccInput(inputAddr) // Read the incoming events