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(
}
// Sync the with the given Ethereum backend:
isSynced, err := transaction.IsSynced(ctx, backend, maxDelay)
isSynced, _, err := transaction.IsSynced(ctx, backend, maxDelay)
if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("is synced: %w", err)
}
if !isSynced {
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 {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("waiting backend sync: %w", err)
}
......@@ -97,7 +97,7 @@ func InitChequebookFactory(
foundFactory, foundLegacyFactories, found := chequebook.DiscoverFactoryAddress(chainID)
if factoryAddress == "" {
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
logger.Infof("using default factory address for chain id %d: %x", chainID, currentFactory)
......
......@@ -17,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/logging"
)
// Backend is the minimum of blockchain backend functions we need.
......@@ -35,28 +36,28 @@ type Backend interface {
// 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
// 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)
if err != nil {
return false, err
return false, time.Time{}, err
}
header, err := backend.HeaderByNumber(ctx, big.NewInt(int64(number)))
if err != nil {
return false, err
return false, time.Time{}, err
}
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,
// with the given maxDelay duration as the maximum time we can be behind the
// 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 {
synced, err := IsSynced(ctx, backend, maxDelay)
synced, blockTime, err := IsSynced(ctx, backend, maxDelay)
if err != nil {
return err
}
......@@ -65,6 +66,8 @@ func WaitSynced(ctx context.Context, backend Backend, maxDelay time.Duration) er
return nil
}
logger.Infof("still waiting for Ethereum to sync. Block time is %s.", blockTime)
select {
case <-ctx.Done():
return ctx.Err()
......
......@@ -19,7 +19,7 @@ func TestIsSynced(t *testing.T) {
blockNumber := uint64(100)
t.Run("synced", func(t *testing.T) {
synced, err := transaction.IsSynced(
synced, _, err := transaction.IsSynced(
ctx,
backendmock.New(
backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
......@@ -45,7 +45,7 @@ func TestIsSynced(t *testing.T) {
})
t.Run("not synced", func(t *testing.T) {
synced, err := transaction.IsSynced(
synced, _, err := transaction.IsSynced(
ctx,
backendmock.New(
backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
......@@ -72,7 +72,7 @@ func TestIsSynced(t *testing.T) {
t.Run("error", func(t *testing.T) {
expectedErr := errors.New("err")
_, err := transaction.IsSynced(
_, _, err := transaction.IsSynced(
ctx,
backendmock.New(
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