Skip to content

Commit

Permalink
refactor: move logger interface to types (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Apr 8, 2024
1 parent 48bc6bf commit 3cd61e0
Show file tree
Hide file tree
Showing 27 changed files with 104 additions and 116 deletions.
84 changes: 34 additions & 50 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"sync/atomic"
"time"

// Importing the general purpose Cosmos blockchain client

"code.cloudfoundry.org/go-diodes"

"github.com/dymensionxyz/dymint/node/events"
Expand All @@ -24,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 {
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 @@ -110,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 @@ -144,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 @@ -280,3 +253,14 @@ func (m *Manager) applyBlockCallback(event pubsub.Message) {
func (m *Manager) getLatestBatchFromSL(ctx context.Context) (*settlement.ResultRetrieveBatch, error) {
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 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 @@ import (
"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 @@ type Server struct {
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 {
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 @@ import (
"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 @@ type DataAvailabilityLayerClient struct {
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.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{}
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 {
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
6 changes: 3 additions & 3 deletions p2p/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"

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

// GossipMessage represents message gossiped via P2P network (e.g. transaction, Block etc).
Expand All @@ -36,13 +36,13 @@ type Gossiper struct {
topic *pubsub.Topic
sub *pubsub.Subscription

logger log.Logger
logger types.Logger
}

// NewGossiper creates new, ready to use instance of Gossiper.
//
// Returned Gossiper object can be used for sending (Publishing) and receiving messages in topic identified by topicStr.
func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger log.Logger, options ...GossiperOption) (*Gossiper, error) {
func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger types.Logger, options ...GossiperOption) (*Gossiper, error) {
topic, err := ps.Join(topicStr)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 3cd61e0

Please sign in to comment.