Skip to content

Commit

Permalink
Remove unused log field from blockchain struct
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored and omerfirmak committed Jan 11, 2024
1 parent 0c588b5 commit 01101a3
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 47 deletions.
4 changes: 1 addition & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,16 @@ type Blockchain struct {
network utils.Network
database db.DB

log utils.SimpleLogger
listener EventListener

cachedPending atomic.Pointer[Pending]
}

func New(database db.DB, network utils.Network, log utils.SimpleLogger) *Blockchain {
func New(database db.DB, network utils.Network) *Blockchain {
RegisterCoreTypesToEncoder()
return &Blockchain{
database: database,
network: network,
log: log,
listener: &SelectiveListener{},
}
}
Expand Down
37 changes: 17 additions & 20 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ var emptyCommitments = core.BlockCommitments{}
func TestNew(t *testing.T) {
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
log := utils.NewNopZapLogger()
t.Run("empty blockchain's head is nil", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, log)
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)
assert.Equal(t, utils.Mainnet, chain.Network())
b, err := chain.Head()
assert.Nil(t, b)
Expand All @@ -40,10 +39,10 @@ func TestNew(t *testing.T) {
require.NoError(t, err)

testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Mainnet, log)
chain := blockchain.New(testDB, utils.Mainnet)
assert.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))

chain = blockchain.New(testDB, utils.Mainnet, log)
chain = blockchain.New(testDB, utils.Mainnet)
b, err := chain.Head()
require.NoError(t, err)
assert.Equal(t, block0, b)
Expand All @@ -53,9 +52,8 @@ func TestNew(t *testing.T) {
func TestHeight(t *testing.T) {
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
log := utils.NewNopZapLogger()
t.Run("return nil if blockchain is empty", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Goerli, log)
chain := blockchain.New(pebble.NewMemTest(t), utils.Goerli)
_, err := chain.Height()
assert.Error(t, err)
})
Expand All @@ -67,18 +65,18 @@ func TestHeight(t *testing.T) {
require.NoError(t, err)

testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Mainnet, log)
chain := blockchain.New(testDB, utils.Mainnet)
assert.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))

chain = blockchain.New(testDB, utils.Mainnet, log)
chain = blockchain.New(testDB, utils.Mainnet)
height, err := chain.Height()
require.NoError(t, err)
assert.Equal(t, block0.Number, height)
})
}

func TestBlockByNumberAndHash(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Goerli, utils.NewNopZapLogger())
chain := blockchain.New(pebble.NewMemTest(t), utils.Goerli)
t.Run("same block is returned for both GetBlockByNumber and GetBlockByHash", func(t *testing.T) {
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -114,7 +112,7 @@ func TestVerifyBlock(t *testing.T) {
h1, err := new(felt.Felt).SetRandom()
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)

t.Run("error if chain is empty and incoming block number is not 0", func(t *testing.T) {
block := &core.Block{Header: &core.Header{Number: 10}}
Expand Down Expand Up @@ -176,7 +174,7 @@ func TestSanityCheckNewHeight(t *testing.T) {
h1, err := new(felt.Felt).SetRandom()
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)

client := feeder.NewTestClient(t, utils.Mainnet)

Expand Down Expand Up @@ -213,7 +211,6 @@ func TestSanityCheckNewHeight(t *testing.T) {
func TestStore(t *testing.T) {
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
log := utils.NewNopZapLogger()

block0, err := gw.BlockByNumber(context.Background(), 0)
require.NoError(t, err)
Expand All @@ -222,7 +219,7 @@ func TestStore(t *testing.T) {
require.NoError(t, err)

t.Run("add block to empty blockchain", func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, log)
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)
require.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))

headBlock, err := chain.Head()
Expand All @@ -248,7 +245,7 @@ func TestStore(t *testing.T) {
stateUpdate1, err := gw.StateUpdate(context.Background(), 1)
require.NoError(t, err)

chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, log)
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)
require.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))
require.NoError(t, chain.Store(block1, &emptyCommitments, stateUpdate1, nil))

Expand All @@ -271,7 +268,7 @@ func TestStore(t *testing.T) {
}

func TestTransactionAndReceipt(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)

client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -359,7 +356,7 @@ func TestTransactionAndReceipt(t *testing.T) {

func TestState(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(testDB, utils.Mainnet)

client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -422,7 +419,7 @@ func TestState(t *testing.T) {

func TestEvents(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Goerli2, utils.NewNopZapLogger())
chain := blockchain.New(testDB, utils.Goerli2)

client := feeder.NewTestClient(t, utils.Goerli2)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -541,7 +538,7 @@ func TestEvents(t *testing.T) {

func TestRevert(t *testing.T) {
testdb := pebble.NewMemTest(t)
chain := blockchain.New(testdb, utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(testdb, utils.Mainnet)

client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -625,7 +622,7 @@ func TestL1Update(t *testing.T) {

for _, head := range heads {
t.Run(fmt.Sprintf("update L1 head to block %d", head.BlockNumber), func(t *testing.T) {
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)
require.NoError(t, chain.SetL1Head(head))
got, err := chain.L1Head()
require.NoError(t, err)
Expand All @@ -636,7 +633,7 @@ func TestL1Update(t *testing.T) {

func TestPending(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(testDB, utils.Mainnet)
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)

Expand Down
4 changes: 2 additions & 2 deletions l1/l1_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestClient(t *testing.T) {
ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)
chain := blockchain.New(pebble.NewMemTest(t), network)

client := NewClient(nil, chain, nopLog).WithResubscribeDelay(0).WithPollFinalisedInterval(time.Nanosecond)

Expand Down Expand Up @@ -398,7 +398,7 @@ func TestUnreliableSubscription(t *testing.T) {
ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)
chain := blockchain.New(pebble.NewMemTest(t), network)
client := NewClient(nil, chain, nopLog).WithResubscribeDelay(0).WithPollFinalisedInterval(time.Nanosecond)

err := errors.New("test err")
Expand Down
6 changes: 3 additions & 3 deletions l1/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestFailToCreateSubscription(t *testing.T) {
network := utils.Mainnet
ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)
chain := blockchain.New(pebble.NewMemTest(t), network)

subscriber := mocks.NewMockSubscriber(ctrl)

Expand Down Expand Up @@ -80,7 +80,7 @@ func TestMismatchedChainID(t *testing.T) {
network := utils.Mainnet
ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)
chain := blockchain.New(pebble.NewMemTest(t), network)

subscriber := mocks.NewMockSubscriber(ctrl)

Expand All @@ -105,7 +105,7 @@ func TestEventListener(t *testing.T) {
ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)
chain := blockchain.New(pebble.NewMemTest(t), network)

subscriber := mocks.NewMockSubscriber(ctrl)
subscriber.
Expand Down
4 changes: 2 additions & 2 deletions migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRelocateContractStorageRootKeys(t *testing.T) {

func TestRecalculateBloomFilters(t *testing.T) {
testdb := pebble.NewMemTest(t)
chain := blockchain.New(testdb, utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(testdb, utils.Mainnet)
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestChangeTrieNodeEncoding(t *testing.T) {

func TestCalculateBlockCommitments(t *testing.T) {
testdb := pebble.NewMemTest(t)
chain := blockchain.New(testdb, utils.Mainnet, utils.NewNopZapLogger())
chain := blockchain.New(testdb, utils.Mainnet)
client := feeder.NewTestClient(t, utils.Mainnet)
gw := adaptfeeder.New(client)

Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen

services := make([]service.Service, 0)

chain := blockchain.New(database, cfg.Network, log)
chain := blockchain.New(database, cfg.Network)

// Verify that cfg.Network is compatible with the database.
head, err := chain.Head()
Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestNetworkVerificationOnNonEmptyDB(t *testing.T) {
log := utils.NewNopZapLogger()
database, err := pebble.New(dbPath, 1, 1, log)
require.NoError(t, err)
chain := blockchain.New(database, network, log)
chain := blockchain.New(database, network)
syncer := sync.New(chain, adaptfeeder.New(feeder.NewTestClient(t, network)), log, 0, false)
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
require.NoError(t, syncer.Run(ctx))
Expand Down
15 changes: 7 additions & 8 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestBlockWithTxHashes(t *testing.T) {
t.Run(description, func(t *testing.T) {
log := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, log)
chain := blockchain.New(pebble.NewMemTest(t), network)
handler := rpc.New(chain, nil, nil, "", log)

block, rpcErr := handler.BlockWithTxHashes(id)
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestBlockWithTxs(t *testing.T) {
t.Run(description, func(t *testing.T) {
log := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, log)
chain := blockchain.New(pebble.NewMemTest(t), network)
handler := rpc.New(chain, nil, nil, "", log)

block, rpcErr := handler.BlockWithTxs(id)
Expand Down Expand Up @@ -1754,8 +1754,7 @@ func TestStateUpdate(t *testing.T) {

for description, id := range errTests {
t.Run(description, func(t *testing.T) {
log := utils.NewNopZapLogger()
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet, log)
chain := blockchain.New(pebble.NewMemTest(t), utils.Mainnet)
handler := rpc.New(chain, nil, nil, "", nil)

update, rpcErr := handler.StateUpdate(id)
Expand Down Expand Up @@ -2331,7 +2330,7 @@ func TestClassAt(t *testing.T) {

func TestEvents(t *testing.T) {
testDB := pebble.NewMemTest(t)
chain := blockchain.New(testDB, utils.Goerli2, utils.NewNopZapLogger())
chain := blockchain.New(testDB, utils.Goerli2)

client := feeder.NewTestClient(t, utils.Goerli2)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -3286,7 +3285,7 @@ func TestTraceBlockTransactions(t *testing.T) {
t.Run(description, func(t *testing.T) {
log := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, log)
chain := blockchain.New(pebble.NewMemTest(t), network)
handler := rpc.New(chain, nil, nil, "", log)

update, rpcErr := handler.TraceBlockTransactions(context.Background(), id)
Expand Down Expand Up @@ -3490,7 +3489,7 @@ func TestSubscribeNewHeadsAndUnsubscribe(t *testing.T) {
gw := adaptfeeder.New(client)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
chain := blockchain.New(pebble.NewMemTest(t), network, log)
chain := blockchain.New(pebble.NewMemTest(t), network)
syncer := sync.New(chain, gw, log, 0, false)
handler := rpc.New(chain, syncer, nil, "", log)

Expand Down Expand Up @@ -3571,7 +3570,7 @@ func TestMultipleSubscribeNewHeadsAndUnsubscribe(t *testing.T) {
gw := adaptfeeder.New(feederClient)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
chain := blockchain.New(pebble.NewMemTest(t), network, log)
chain := blockchain.New(pebble.NewMemTest(t), network)
syncer := sync.New(chain, gw, log, 0, false)
handler := rpc.New(chain, syncer, nil, "", log)
go func() {
Expand Down
14 changes: 7 additions & 7 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSyncBlocks(t *testing.T) {
t.Run("sync multiple blocks in an empty db", func(t *testing.T) {
t.Parallel()
testDB := pebble.NewMemTest(t)
bc := blockchain.New(testDB, utils.Mainnet, log)
bc := blockchain.New(testDB, utils.Mainnet)
synchronizer := sync.New(bc, gw, log, time.Duration(0), false)
ctx, cancel := context.WithTimeout(context.Background(), timeout)

Expand All @@ -71,7 +71,7 @@ func TestSyncBlocks(t *testing.T) {
t.Run("sync multiple blocks in a non-empty db", func(t *testing.T) {
t.Parallel()
testDB := pebble.NewMemTest(t)
bc := blockchain.New(testDB, utils.Mainnet, log)
bc := blockchain.New(testDB, utils.Mainnet)
b0, err := gw.BlockByNumber(context.Background(), 0)
require.NoError(t, err)
s0, err := gw.StateUpdate(context.Background(), 0)
Expand All @@ -90,7 +90,7 @@ func TestSyncBlocks(t *testing.T) {
t.Run("sync multiple blocks, with an unreliable gw", func(t *testing.T) {
t.Parallel()
testDB := pebble.NewMemTest(t)
bc := blockchain.New(testDB, utils.Mainnet, log)
bc := blockchain.New(testDB, utils.Mainnet)

mockSNData := mocks.NewMockStarknetData(mockCtrl)

Expand Down Expand Up @@ -152,7 +152,7 @@ func TestReorg(t *testing.T) {
testDB := pebble.NewMemTest(t)

// sync to integration for 2 blocks
bc := blockchain.New(testDB, utils.Integration, utils.NewNopZapLogger())
bc := blockchain.New(testDB, utils.Integration)
synchronizer := sync.New(bc, integGw, utils.NewNopZapLogger(), time.Duration(0), false)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -161,7 +161,7 @@ func TestReorg(t *testing.T) {

t.Run("resync to mainnet with the same db", func(t *testing.T) {
t.Parallel()
bc = blockchain.New(testDB, utils.Mainnet, utils.NewNopZapLogger())
bc = blockchain.New(testDB, utils.Mainnet)

// Ensure current head is Integration head
head, err := bc.HeadsHeader()
Expand All @@ -188,7 +188,7 @@ func TestPending(t *testing.T) {

testDB := pebble.NewMemTest(t)
log := utils.NewNopZapLogger()
bc := blockchain.New(testDB, utils.Mainnet, log)
bc := blockchain.New(testDB, utils.Mainnet)
synchronizer := sync.New(bc, gw, log, time.Millisecond*100, false)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)

Expand All @@ -207,7 +207,7 @@ func TestSubscribeNewHeads(t *testing.T) {
testDB := pebble.NewMemTest(t)
log := utils.NewNopZapLogger()
integration := utils.Integration
chain := blockchain.New(testDB, integration, log)
chain := blockchain.New(testDB, integration)
integrationClient := feeder.NewTestClient(t, integration)
gw := adaptfeeder.New(integrationClient)
syncer := sync.New(chain, gw, log, 0, false)
Expand Down

0 comments on commit 01101a3

Please sign in to comment.