Skip to content

Commit

Permalink
Sync check on startup (#142)
Browse files Browse the repository at this point in the history
* InitEthConn fails if the endpoint is not synced. Refactored errors

* update dependencies
  • Loading branch information
hamdiallam authored Apr 26, 2019
1 parent d324138 commit dee03e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
41 changes: 32 additions & 9 deletions eth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
"math/big"
)
Expand All @@ -21,7 +21,8 @@ type Client struct {
logger log.Logger
}

// Instantiate a connection and bind the go plasma contract wrapper with this client
// Instantiate a connection and bind the go plasma contract wrapper with this client.
// Will return an error if the ethereum client is not fully sycned
func InitEthConn(nodeUrl string, logger log.Logger) (Client, error) {
// Connect to a remote etheruem client
//
Expand All @@ -33,13 +34,36 @@ func InitEthConn(nodeUrl string, logger log.Logger) (Client, error) {
}
ec := ethclient.NewClient(c)

return Client{c, ec, logger}, nil
// check if the client is synced
client := Client{c, ec, logger}
if synced, err := client.Synced(); !synced || err != nil {
if err != nil {
return client, err
} else {
return client, errors.New("geth endpoint is not fully synced")
}
}

return client, nil
}

func (client Client) Synced() (bool, error) {
var res json.RawMessage
if err := client.rpc.Call(&res, "eth_syncing"); err != nil {
return false, errors.Wrap(err, "rpc")
}

if string(res) != "false" {
return false, nil
}

return true, nil
}

func (client Client) LatestBlockNum() (*big.Int, error) {
var hexStr string
if err := client.rpc.Call(&hexStr, "eth_blockNumber"); err != nil {
return nil, fmt.Errorf("RPC error { %s }", err)
return nil, errors.Wrap(err, "rpc")
}

hexStr = utils.RemoveHexPrefix(hexStr)
Expand All @@ -51,7 +75,7 @@ func (client Client) LatestBlockNum() (*big.Int, error) {

hexBytes, err := hex.DecodeString(hexStr)
if err != nil {
return nil, fmt.Errorf("Hex decoding error: { %s }", err)
return nil, errors.Wrap(err, "hex decoding")
}

return new(big.Int).SetBytes(hexBytes), nil
Expand Down Expand Up @@ -81,14 +105,13 @@ func (client Client) SubscribeToHeads() (<-chan *types.Header, error) {
// used for testing when running against a local client like ganache
func (client Client) accounts() ([]common.Address, error) {
var res json.RawMessage
err := client.rpc.Call(&res, "eth_accounts")
if err != nil {
return nil, err
if err := client.rpc.Call(&res, "eth_accounts"); err != nil {
return nil, errors.Wrap(err, "rpc")
}

var addrs []string
if err := json.Unmarshal(res, &addrs); err != nil {
return nil, err
return nil, errors.Wrap(err, "json unmarshaling")
}

// convert to the correct type
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.8.0
github.com/prometheus/client_golang v0.9.2 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.2.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ github.com/syndtr/goleveldb v0.0.0-20190226153722-4217c9f31f58 h1:DLVQCtatLabge7
github.com/syndtr/goleveldb v0.0.0-20190226153722-4217c9f31f58/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/tendermint/btcd v0.1.0 h1:2bR8bGTlOLEiO9eoz81Upbs8LFSRF2MVT42WiyW88eU=
github.com/tendermint/btcd v0.1.0/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U=
github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 h1:u8i49c+BxloX3XQ55cvzFNXplizZP/q00i+IlttUjAU=
github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk=
github.com/tendermint/go-amino v0.14.1 h1:o2WudxNfdLNBwMyl2dqOJxiro5rfrEaU0Ugs6offJMk=
github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso=
Expand Down

0 comments on commit dee03e9

Please sign in to comment.