Commit c491d247 authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

feat: more blockchain related logging (#2211)

parent 8cb1d7c1
...@@ -67,13 +67,13 @@ func InitChain( ...@@ -67,13 +67,13 @@ func InitChain(
} }
// Sync the with the given Ethereum backend: // Sync the with the given Ethereum backend:
isSynced, err := transaction.IsSynced(ctx, backend, maxDelay) isSynced, _, err := transaction.IsSynced(ctx, backend, maxDelay)
if err != nil { if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("is synced: %w", err) return nil, common.Address{}, 0, nil, nil, fmt.Errorf("is synced: %w", err)
} }
if !isSynced { if !isSynced {
logger.Infof("waiting to sync with the Ethereum backend") logger.Infof("waiting to sync with the Ethereum backend")
err := transaction.WaitSynced(ctx, backend, maxDelay) err := transaction.WaitSynced(logger, ctx, backend, maxDelay)
if err != nil { if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("waiting backend sync: %w", err) return nil, common.Address{}, 0, nil, nil, fmt.Errorf("waiting backend sync: %w", err)
} }
...@@ -97,7 +97,7 @@ func InitChequebookFactory( ...@@ -97,7 +97,7 @@ func InitChequebookFactory(
foundFactory, foundLegacyFactories, found := chequebook.DiscoverFactoryAddress(chainID) foundFactory, foundLegacyFactories, found := chequebook.DiscoverFactoryAddress(chainID)
if factoryAddress == "" { if factoryAddress == "" {
if !found { if !found {
return nil, errors.New("no known factory address for this network") return nil, fmt.Errorf("no known factory address for this network (chain id: %d)", chainID)
} }
currentFactory = foundFactory currentFactory = foundFactory
logger.Infof("using default factory address for chain id %d: %x", chainID, currentFactory) logger.Infof("using default factory address for chain id %d: %x", chainID, currentFactory)
......
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/logging"
) )
// Backend is the minimum of blockchain backend functions we need. // Backend is the minimum of blockchain backend functions we need.
...@@ -35,28 +36,28 @@ type Backend interface { ...@@ -35,28 +36,28 @@ type Backend interface {
// is true if the current wall clock is after the block time of last block // is true if the current wall clock is after the block time of last block
// with the given maxDelay as the maximum duration we can be behind the block // with the given maxDelay as the maximum duration we can be behind the block
// time. // time.
func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, error) { func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, time.Time, error) {
number, err := backend.BlockNumber(ctx) number, err := backend.BlockNumber(ctx)
if err != nil { if err != nil {
return false, err return false, time.Time{}, err
} }
header, err := backend.HeaderByNumber(ctx, big.NewInt(int64(number))) header, err := backend.HeaderByNumber(ctx, big.NewInt(int64(number)))
if err != nil { if err != nil {
return false, err return false, time.Time{}, err
} }
blockTime := time.Unix(int64(header.Time), 0) blockTime := time.Unix(int64(header.Time), 0)
return blockTime.After(time.Now().UTC().Add(-maxDelay)), nil return blockTime.After(time.Now().UTC().Add(-maxDelay)), blockTime, nil
} }
// WaitSynced will wait until we are synced with the given blockchain backend, // WaitSynced will wait until we are synced with the given blockchain backend,
// with the given maxDelay duration as the maximum time we can be behind the // with the given maxDelay duration as the maximum time we can be behind the
// last block. // last block.
func WaitSynced(ctx context.Context, backend Backend, maxDelay time.Duration) error { func WaitSynced(logger logging.Logger, ctx context.Context, backend Backend, maxDelay time.Duration) error {
for { for {
synced, err := IsSynced(ctx, backend, maxDelay) synced, blockTime, err := IsSynced(ctx, backend, maxDelay)
if err != nil { if err != nil {
return err return err
} }
...@@ -65,6 +66,8 @@ func WaitSynced(ctx context.Context, backend Backend, maxDelay time.Duration) er ...@@ -65,6 +66,8 @@ func WaitSynced(ctx context.Context, backend Backend, maxDelay time.Duration) er
return nil return nil
} }
logger.Infof("still waiting for Ethereum to sync. Block time is %s.", blockTime)
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
......
...@@ -19,7 +19,7 @@ func TestIsSynced(t *testing.T) { ...@@ -19,7 +19,7 @@ func TestIsSynced(t *testing.T) {
blockNumber := uint64(100) blockNumber := uint64(100)
t.Run("synced", func(t *testing.T) { t.Run("synced", func(t *testing.T) {
synced, err := transaction.IsSynced( synced, _, err := transaction.IsSynced(
ctx, ctx,
backendmock.New( backendmock.New(
backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) { backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
...@@ -45,7 +45,7 @@ func TestIsSynced(t *testing.T) { ...@@ -45,7 +45,7 @@ func TestIsSynced(t *testing.T) {
}) })
t.Run("not synced", func(t *testing.T) { t.Run("not synced", func(t *testing.T) {
synced, err := transaction.IsSynced( synced, _, err := transaction.IsSynced(
ctx, ctx,
backendmock.New( backendmock.New(
backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) { backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
...@@ -72,7 +72,7 @@ func TestIsSynced(t *testing.T) { ...@@ -72,7 +72,7 @@ func TestIsSynced(t *testing.T) {
t.Run("error", func(t *testing.T) { t.Run("error", func(t *testing.T) {
expectedErr := errors.New("err") expectedErr := errors.New("err")
_, err := transaction.IsSynced( _, _, err := transaction.IsSynced(
ctx, ctx,
backendmock.New( backendmock.New(
backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) { backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment