Skip to content

Commit

Permalink
adapt shutdown signal in solo init function (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony authored May 22, 2024
1 parent 74e275d commit e39091e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 11 additions & 7 deletions cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *Solo) Run(ctx context.Context) error {

log.Info("prepared to pack block")

if err := s.init(); err != nil {
if err := s.init(ctx); err != nil {
return err
}

Expand Down Expand Up @@ -181,10 +181,6 @@ func (s *Solo) packing(pendingTxs tx.Transactions, onDemand bool) error {
}
realElapsed := mclock.Now() - startTime

if err := s.repo.SetBestBlockID(b.Header().ID()); err != nil {
return errors.WithMessage(err, "set best block")
}

if !s.skipLogs {
w := s.logDB.NewWriter()
if err := w.Write(b, receipts); err != nil {
Expand All @@ -196,6 +192,10 @@ func (s *Solo) packing(pendingTxs tx.Transactions, onDemand bool) error {
}
}

if err := s.repo.SetBestBlockID(b.Header().ID()); err != nil {
return errors.WithMessage(err, "set best block")
}

commitElapsed := mclock.Now() - startTime - execElapsed

if v, updated := s.bandwidth.Update(b.Header(), time.Duration(realElapsed)); updated {
Expand All @@ -215,7 +215,7 @@ func (s *Solo) packing(pendingTxs tx.Transactions, onDemand bool) error {
}

// The init function initializes the chain parameters.
func (s *Solo) init() error {
func (s *Solo) init(ctx context.Context) error {
best := s.repo.BestBlockSummary()
newState := s.stater.NewState(best.Header.StateRoot(), best.Header.Number(), best.Conflicts, best.SteadyNum)
currentBGP, err := builtin.Params.Native(newState).Get(thor.KeyBaseGasPrice)
Expand Down Expand Up @@ -244,7 +244,11 @@ func (s *Solo) init() error {

if !s.onDemand {
// wait for the next block interval if not on-demand
time.Sleep(time.Duration(10-time.Now().Unix()%10) * time.Second)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Duration(int64(s.blockInterval)-time.Now().Unix()%int64(s.blockInterval)) * time.Second):
}
}

return s.packing(tx.Transactions{baseGasePriceTx}, false)
Expand Down
3 changes: 2 additions & 1 deletion cmd/thor/solo/solo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package solo

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -36,7 +37,7 @@ func TestInitSolo(t *testing.T) {
solo := newSolo()

// init solo -> this should mine a block with the gas price tx
err := solo.init()
err := solo.init(context.Background())
assert.Nil(t, err)

// check the gas price
Expand Down

0 comments on commit e39091e

Please sign in to comment.