Skip to content

Commit

Permalink
add rebroadcast via infura public node
Browse files Browse the repository at this point in the history
  • Loading branch information
tranvictor committed Jun 18, 2017
1 parent 914a99e commit 6a2184a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions ethereum/geth/etherscan_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package geth

import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
)

func SendRawTransaction(data []byte) error {
client, err := rpc.DialHTTP("https://mainnet.infura.io/0BRKxQ0SFvAxGL72cbXi")
// client, err := rpc.DialHTTP("https://ropsten.infura.io/0BRKxQ0SFvAxGL72cbXi")
if err != nil {
return err
}
return client.Call(nil, "eth_sendRawTransaction",
fmt.Sprintf("0x%s", common.Bytes2Hex(data)))
}
22 changes: 18 additions & 4 deletions ethereum/geth/txwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ func (tw *TxWatcher) newTx(oldTx *types.Transaction) (*types.Transaction, error)
return tw.transactor.Signer(types.HomesteadSigner{}, tw.transactor.From, newTx)
}

func (tw *TxWatcher) rebroadcastByPublicNode(signedTx *types.Transaction) error {
buff := bytes.NewBuffer([]byte{})
if err := signedTx.EncodeRLP(buff); err != nil {
return err
}
return SendRawTransaction(buff.Bytes())
}

func (tw *TxWatcher) rebroadcast(oldTx, signedTx *types.Transaction) error {
buff := bytes.NewBuffer([]byte{})
if err := signedTx.EncodeRLP(buff); err != nil {
Expand Down Expand Up @@ -105,10 +113,16 @@ func (tw *TxWatcher) WaitAndRetry() (*big.Int, *big.Int, error) {
if err != nil {
return nil, nil, err
}
return tw.Wait()
} else {
return errCode, errInfo, err
errCode, errInfo, err = tw.Wait()
if err != nil {
err = tw.rebroadcastByPublicNode(signedTx)
if err != nil {
smartpool.Output.Printf("Rebroadcast by public node of tx: %s failed. Error: %s\n", signedTx.Hash().Hex(), err)
}
}
errCode, errInfo, err = tw.Wait()
}
return errCode, errInfo, err
}

func (tw *TxWatcher) Wait() (*big.Int, *big.Int, error) {
Expand All @@ -120,7 +134,7 @@ func (tw *TxWatcher) Wait() (*big.Int, *big.Int, error) {
timeout := make(chan bool, 1)
go tw.loop(timeout)
go func() {
time.Sleep(2 * time.Minute)
time.Sleep(12 * time.Second)
// push 2 timeouts for the above loop and below select
timeout <- true
timeout <- true
Expand Down

1 comment on commit 6a2184a

@tranvictor
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve #13

Please sign in to comment.