Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move logger interface to types #633

Merged
merged 11 commits into from
Apr 8, 2024
103 changes: 34 additions & 69 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
"sync/atomic"
"time"

// Importing the general purpose Cosmos blockchain client

"code.cloudfoundry.org/go-diodes"

"github.com/avast/retry-go/v4"
"github.com/dymensionxyz/dymint/node/events"
"github.com/dymensionxyz/dymint/p2p"
"github.com/dymensionxyz/dymint/utils"
Expand All @@ -25,79 +22,55 @@ import (

"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/state"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
)

type blockSource string

const (
producedBlock blockSource = "produced"
gossipedBlock blockSource = "gossip"
daBlock blockSource = "da"
)

type blockMetaData struct {
source blockSource
daHeight uint64
}

// Manager is responsible for aggregating transactions into blocks.
type Manager struct {
Copy link
Contributor Author

@mtsitrin mtsitrin Apr 6, 2024

Choose a reason for hiding this comment

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

The only change here is that syncCache removed as it's not used.
grouping and comments other than that

pubsub *pubsub.Server

p2pClient *p2p.Client

lastState types.State

conf config.BlockManagerConfig
genesis *tmtypes.GenesisDoc

// Configuration
conf config.BlockManagerConfig
genesis *tmtypes.GenesisDoc
proposerKey crypto.PrivKey

store store.Store
executor *state.BlockExecutor
// Store and execution
store store.Store
lastState types.State
executor *state.BlockExecutor

// Clients and servers
pubsub *pubsub.Server
p2pClient *p2p.Client
dalc da.DataAvailabilityLayerClient
settlementClient settlement.LayerI
retriever da.BatchRetriever

// Data retrieval
retriever da.BatchRetriever

// Synchronization
syncTargetDiode diodes.Diode
syncTarget uint64
isSyncedCond sync.Cond

// Block production
shouldProduceBlocksCh chan bool
produceEmptyBlockCh chan bool

syncTarget uint64
lastSubmissionTime int64
batchInProcess atomic.Value
isSyncedCond sync.Cond

lastSubmissionTime int64
batchInProcess atomic.Value
produceBlockMutex sync.Mutex
applyCachedBlockMutex sync.Mutex

syncCache map[uint64]*types.Block

logger log.Logger
// Logging
logger types.Logger

// Previous data
prevBlock map[uint64]*types.Block
prevCommit map[uint64]*types.Commit
}

// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc.
func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger log.Logger) (types.State, error) {
s, err := store.LoadState()
if err == types.ErrNoStateFound {
logger.Info("failed to find state in the store, creating new state from genesis")
return types.NewFromGenesisDoc(genesis)
}

return s, err
}

// NewManager creates new block Manager.
func NewManager(
proposerKey crypto.PrivKey,
Expand All @@ -111,7 +84,7 @@ func NewManager(
eventBus *tmtypes.EventBus,
pubsub *pubsub.Server,
p2pClient *p2p.Client,
logger log.Logger,
logger types.Logger,
) (*Manager, error) {

proposerAddress, err := getAddress(proposerKey)
Expand Down Expand Up @@ -145,7 +118,6 @@ func NewManager(
retriever: dalc.(da.BatchRetriever),
// channels are buffered to avoid blocking on input/output operations, buffer sizes are arbitrary
syncTargetDiode: diodes.NewOneToOne(1, nil),
syncCache: make(map[uint64]*types.Block),
isSyncedCond: *sync.NewCond(new(sync.Mutex)),
batchInProcess: batchInProcess,
shouldProduceBlocksCh: make(chan bool, 1),
Expand Down Expand Up @@ -279,23 +251,16 @@ func (m *Manager) applyBlockCallback(event pubsub.Message) {

// getLatestBatchFromSL gets the latest batch from the SL
func (m *Manager) getLatestBatchFromSL(ctx context.Context) (*settlement.ResultRetrieveBatch, error) {
var resultRetrieveBatch *settlement.ResultRetrieveBatch
var err error
// Get latest batch from SL
err = retry.Do(
func() error {
resultRetrieveBatch, err = m.settlementClient.RetrieveBatch()
if err != nil {
return err
}
return nil
},
retry.LastErrorOnly(true),
retry.Context(ctx),
retry.Attempts(1),
)
if err != nil {
return resultRetrieveBatch, err
return m.settlementClient.RetrieveBatch()
}

// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc.
func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger types.Logger) (types.State, error) {
s, err := store.LoadState()
if err == types.ErrNoStateFound {
logger.Info("failed to find state in the store, creating new state from genesis")
return types.NewFromGenesisDoc(genesis)
}
return resultRetrieveBatch, nil

return s, err
}
16 changes: 16 additions & 0 deletions block/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package block

//TODO: move to types package

type blockSource string

const (
producedBlock blockSource = "produced"
gossipedBlock blockSource = "gossip"
daBlock blockSource = "da"
)

type blockMetaData struct {
source blockSource
daHeight uint64
}
5 changes: 2 additions & 3 deletions da/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/avast/retry-go/v4"
"github.com/dymensionxyz/dymint/log"
"github.com/gogo/protobuf/proto"

"github.com/dymensionxyz/dymint/types"
Expand Down Expand Up @@ -59,7 +58,7 @@ type DataAvailabilityLayerClient struct {
client SubstrateApiI
pubsubServer *pubsub.Server
config Config
logger log.Logger
logger types.Logger
ctx context.Context
cancel context.CancelFunc
txInclusionTimeout time.Duration
Expand Down Expand Up @@ -99,7 +98,7 @@ func WithBatchRetryAttempts(attempts uint) da.Option {
}

// Init initializes DataAvailabilityLayerClient instance.
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...da.Option) error {
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...da.Option) error {
c.logger = logger

if len(config) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions da/avail/avail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
availtypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/da/avail"
"github.com/dymensionxyz/dymint/log/test"
mocks "github.com/dymensionxyz/dymint/mocks/da/avail"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestRetriveBatches(t *testing.T) {
assert.NoError(err)
// Start the DALC
dalc := avail.DataAvailabilityLayerClient{}
err = dalc.Init(configBytes, pubsubServer, nil, test.NewLogger(t), options...)
err = dalc.Init(configBytes, pubsubServer, nil, testutil.NewLogger(t), options...)
require.NoError(err)
err = dalc.Start()
require.NoError(err)
Expand Down
5 changes: 2 additions & 3 deletions da/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/dymensionxyz/dymint/da"
celtypes "github.com/dymensionxyz/dymint/da/celestia/types"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
pb "github.com/dymensionxyz/dymint/types/pb/dymint"
Expand All @@ -32,7 +31,7 @@ type DataAvailabilityLayerClient struct {

pubsubServer *pubsub.Server
config Config
logger log.Logger
logger types.Logger
ctx context.Context
cancel context.CancelFunc
rpcRetryDelay time.Duration
Expand Down Expand Up @@ -72,7 +71,7 @@ func WithSubmitRetryDelay(delay time.Duration) da.Option {
}

// Init initializes DataAvailabilityLayerClient instance.
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...da.Option) error {
func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...da.Option) error {
c.logger = logger

if len(config) <= 0 {
Expand Down
5 changes: 2 additions & 3 deletions da/celestia/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"github.com/celestiaorg/go-cnc"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/da/local"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
)
Expand All @@ -26,11 +25,11 @@
da *local.DataAvailabilityLayerClient
blockTime time.Duration
server *http.Server
logger log.Logger
logger types.Logger
}

// NewServer creates new instance of Server.
func NewServer(blockTime time.Duration, logger log.Logger) *Server {
func NewServer(blockTime time.Duration, logger types.Logger) *Server {

Check warning on line 32 in da/celestia/mock/server.go

View check run for this annotation

Codecov / codecov/patch

da/celestia/mock/server.go#L32

Added line #L32 was not covered by tests
return &Server{
da: new(local.DataAvailabilityLayerClient),
blockTime: blockTime,
Expand Down
3 changes: 1 addition & 2 deletions da/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/cometbft/cometbft/crypto/merkle"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
"github.com/rollkit/celestia-openrpc/types/blob"
Expand Down Expand Up @@ -182,7 +181,7 @@ type ResultRetrieveBatch struct {
// It also contains life-cycle methods.
type DataAvailabilityLayerClient interface {
// Init is called once to allow DA client to read configuration and initialize resources.
Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...Option) error
Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...Option) error

// Start is called once, after Init. It's implementation should start operation of DataAvailabilityLayerClient.
Start() error
Expand Down
5 changes: 2 additions & 3 deletions da/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"google.golang.org/grpc/credentials/insecure"

"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
"github.com/dymensionxyz/dymint/types/pb/dalc"
Expand All @@ -23,7 +22,7 @@
conn *grpc.ClientConn
client dalc.DALCServiceClient

logger log.Logger
logger types.Logger
}

// Config contains configuration options for DataAvailabilityLayerClient.
Expand All @@ -43,7 +42,7 @@
var _ da.BatchRetriever = &DataAvailabilityLayerClient{}

// Init sets the configuration options.
func (d *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, _ store.KVStore, logger log.Logger, options ...da.Option) error {
func (d *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, _ store.KVStore, logger types.Logger, options ...da.Option) error {

Check warning on line 45 in da/grpc/grpc.go

View check run for this annotation

Codecov / codecov/patch

da/grpc/grpc.go#L45

Added line #L45 was not covered by tests
d.logger = logger
if len(config) == 0 {
d.config = DefaultConfig
Expand Down
5 changes: 2 additions & 3 deletions da/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
"github.com/tendermint/tendermint/libs/pubsub"
Expand All @@ -17,7 +16,7 @@ import (
// DataAvailabilityLayerClient is intended only for usage in tests.
// It does actually ensures DA - it stores data in-memory.
type DataAvailabilityLayerClient struct {
logger log.Logger
logger types.Logger
dalcKV store.KVStore
daHeight uint64
config config
Expand All @@ -33,7 +32,7 @@ var _ da.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{}
var _ da.BatchRetriever = &DataAvailabilityLayerClient{}

// Init is called once to allow DA client to read configuration and initialize resources.
func (m *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, dalcKV store.KVStore, logger log.Logger, options ...da.Option) error {
func (m *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, dalcKV store.KVStore, logger types.Logger, options ...da.Option) error {
m.logger = logger
m.dalcKV = dalcKV
m.daHeight = 1
Expand Down
2 changes: 1 addition & 1 deletion mocks/settlement/settlement_layer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"go.uber.org/multierr"

"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/types"
)

// TODO(tzdybal): refactor to configuration parameters
Expand Down Expand Up @@ -71,14 +71,14 @@ type Client struct {
// it's required because of discovery.Advertise call
cancel context.CancelFunc

logger log.Logger
logger types.Logger
}

// NewClient creates new Client object.
//
// Basic checks on parameters are done, and default parameters are provided for unset-configuration
// TODO(tzdybal): consider passing entire config, not just P2P config, to reduce number of arguments
func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, logger log.Logger) (*Client, error) {
func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, logger types.Logger) (*Client, error) {
if privKey == nil {
return nil, errNoPrivKey
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/tendermint/tendermint/libs/log"

"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/log/test"
"github.com/dymensionxyz/dymint/testutil"
)

func TestClientStartup(t *testing.T) {
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestSeedStringParsing(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
logger := &test.MockLogger{}
logger := &testutil.MockLogger{}
client, err := NewClient(config.P2PConfig{
GossipCacheSize: 50,
BoostrapTime: 30 * time.Second}, privKey, "TestNetwork", logger)
Expand Down
Loading
Loading