diff --git a/blockchain/v0/reactor.go b/blockchain/v0/reactor.go index d4fa62f8ae..4010c83cc5 100644 --- a/blockchain/v0/reactor.go +++ b/blockchain/v0/reactor.go @@ -1,6 +1,7 @@ package v0 import ( + "context" "fmt" "reflect" "time" @@ -178,7 +179,7 @@ func (bcR *BlockchainReactor) RemovePeer(peer p2p.Peer, reason interface{}) { func (bcR *BlockchainReactor) respondToPeer(msg *bcproto.BlockRequest, src p2p.Peer) (queued bool) { - block, err := bcR.store.LoadBlock(msg.Height) + block, err := bcR.store.LoadBlock(context.TODO(), msg.Height) if err != nil { bcR.Logger.Error("failure to load block", "err", err) return false @@ -422,7 +423,7 @@ FOR_LOOP: bcR.pool.PopRequest() // TODO: batch saves so we dont persist to disk every block - err = bcR.store.SaveBlock(first, firstParts, second.LastCommit) + err = bcR.store.SaveBlock(context.TODO(), first, firstParts, second.LastCommit) if err != nil { panic(err) } diff --git a/blockchain/v0/reactor_test.go b/blockchain/v0/reactor_test.go index 4bd2a3bc6a..65f422f5c0 100644 --- a/blockchain/v0/reactor_test.go +++ b/blockchain/v0/reactor_test.go @@ -1,6 +1,7 @@ package v0 import ( + "context" "crypto/sha256" "fmt" "os" @@ -100,7 +101,7 @@ func newBlockchainReactor( lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil) if blockHeight > 1 { lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1) - lastBlock, err := blockStore.LoadBlock(blockHeight - 1) + lastBlock, err := blockStore.LoadBlock(context.TODO(), blockHeight-1) if err != nil { panic(err) } @@ -130,7 +131,7 @@ func newBlockchainReactor( panic(fmt.Errorf("error apply block: %w", err)) } - err = blockStore.SaveBlock(thisBlock, thisParts, lastCommit) + err = blockStore.SaveBlock(context.TODO(), thisBlock, thisParts, lastCommit) if err != nil { panic(err) } @@ -190,7 +191,7 @@ func TestNoBlockResponse(t *testing.T) { assert.Equal(t, maxBlockHeight, reactorPairs[0].reactor.store.Height()) for _, tt := range tests { - block, err := reactorPairs[1].reactor.store.LoadBlock(tt.height) + block, err := reactorPairs[1].reactor.store.LoadBlock(context.TODO(), tt.height) if tt.existent { require.NoError(t, err) assert.True(t, block != nil) diff --git a/consensus/replay.go b/consensus/replay.go index 615f6a1f55..7eca15b7e5 100644 --- a/consensus/replay.go +++ b/consensus/replay.go @@ -463,7 +463,7 @@ func (h *Handshaker) replayBlocks( } for i := firstBlock; i <= finalBlock; i++ { h.logger.Info("Applying block", "height", i) - block, err := h.store.LoadBlock(i) + block, err := h.store.LoadBlock(context.TODO(), i) if err != nil { return nil, err } @@ -495,7 +495,7 @@ func (h *Handshaker) replayBlocks( // ApplyBlock on the proxyApp with the last block. func (h *Handshaker) replayBlock(state sm.State, height int64, proxyApp proxy.AppConnConsensus) (sm.State, error) { - block, err := h.store.LoadBlock(height) + block, err := h.store.LoadBlock(context.TODO(), height) if err != nil { return sm.State{}, err } diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 18ff64f3d5..ecf0d5e6c6 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -548,7 +548,7 @@ func TestSimulateValidatorsChange(t *testing.T) { sim.Chain = make([]*types.Block, 0) sim.Commits = make([]*types.Commit, 0) for i := 1; i <= numBlocks; i++ { - block, err := css[0].blockStore.LoadBlock(int64(i)) + block, err := css[0].blockStore.LoadBlock(context.TODO(), int64(i)) if err != nil { panic(err) } @@ -1203,10 +1203,10 @@ func (bs *mockBlockStore) Height() int64 { return int64(len(bs. func (bs *mockBlockStore) Base() int64 { return bs.base } func (bs *mockBlockStore) Size() int64 { return bs.Height() - bs.Base() + 1 } func (bs *mockBlockStore) LoadBaseMeta() *types.BlockMeta { return bs.LoadBlockMeta(bs.base) } -func (bs *mockBlockStore) LoadBlock(height int64) (*types.Block, error) { +func (bs *mockBlockStore) LoadBlock(ctx context.Context, height int64) (*types.Block, error) { return bs.chain[height-1], nil } -func (bs *mockBlockStore) LoadBlockByHash(hash []byte) (*types.Block, error) { +func (bs *mockBlockStore) LoadBlockByHash(ctx context.Context, hash []byte) (*types.Block, error) { return bs.chain[int64(len(bs.chain))-1], nil } func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { @@ -1217,7 +1217,12 @@ func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { } } func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } -func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) error { +func (bs *mockBlockStore) SaveBlock( + ctx context.Context, + block *types.Block, + blockParts *types.PartSet, + seenCommit *types.Commit, +) error { return nil } func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit { diff --git a/consensus/state.go b/consensus/state.go index a172de7f93..ea3f36da4c 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1559,7 +1559,7 @@ func (cs *State) finalizeCommit(height int64) { // but may differ from the LastCommit included in the next block precommits := cs.Votes.Precommits(cs.CommitRound) seenCommit := precommits.MakeCommit() - err := cs.blockStore.SaveBlock(block, blockParts, seenCommit) + err := cs.blockStore.SaveBlock(context.TODO(), block, blockParts, seenCommit) if err != nil { panic(err) } diff --git a/evidence/pool_test.go b/evidence/pool_test.go index 194f40d7a7..259a979bed 100644 --- a/evidence/pool_test.go +++ b/evidence/pool_test.go @@ -1,6 +1,7 @@ package evidence_test import ( + "context" "os" "testing" "time" @@ -408,7 +409,7 @@ func initializeBlockStore(db dbm.DB, state sm.State, valAddr []byte) *store.Bloc partSet := block.MakePartSet(parts) seenCommit := makeCommit(i, valAddr) - err := blockStore.SaveBlock(block, partSet, seenCommit) + err := blockStore.SaveBlock(context.TODO(), block, partSet, seenCommit) if err != nil { panic(err) } diff --git a/rpc/core/blocks.go b/rpc/core/blocks.go index dc51f33fef..b370fb5bab 100644 --- a/rpc/core/blocks.go +++ b/rpc/core/blocks.go @@ -1,6 +1,7 @@ package core import ( + "context" "fmt" tmmath "github.com/lazyledger/lazyledger-core/libs/math" @@ -89,7 +90,7 @@ func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) return nil, err } - block, err := env.BlockStore.LoadBlock(height) + block, err := env.BlockStore.LoadBlock(context.TODO(), height) if err != nil { return nil, err } @@ -103,7 +104,7 @@ func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) // BlockByHash gets block by hash. // More: https://docs.tendermint.com/master/rpc/#/Info/block_by_hash func BlockByHash(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultBlock, error) { - block, err := env.BlockStore.LoadBlockByHash(hash) + block, err := env.BlockStore.LoadBlockByHash(context.TODO(), hash) if err != nil { return nil, err } @@ -153,7 +154,7 @@ func DataAvailabilityHeader(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.Re // depends on either: // - https://github.com/lazyledger/lazyledger-core/pull/312, or // - https://github.com/lazyledger/lazyledger-core/pull/218 - block, err := env.BlockStore.LoadBlock(height) + block, err := env.BlockStore.LoadBlock(context.TODO(), height) if err != nil { return nil, err } diff --git a/rpc/core/blocks_test.go b/rpc/core/blocks_test.go index 266f313bab..8ae560ff16 100644 --- a/rpc/core/blocks_test.go +++ b/rpc/core/blocks_test.go @@ -1,6 +1,7 @@ package core import ( + "context" "fmt" "testing" @@ -118,17 +119,26 @@ type mockBlockStore struct { height int64 } -func (mockBlockStore) Base() int64 { return 1 } -func (store mockBlockStore) Height() int64 { return store.height } -func (store mockBlockStore) Size() int64 { return store.height } -func (mockBlockStore) LoadBaseMeta() *types.BlockMeta { return nil } -func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil } -func (mockBlockStore) LoadBlock(height int64) (*types.Block, error) { return nil, nil } -func (mockBlockStore) LoadBlockByHash(hash []byte) (*types.Block, error) { return nil, nil } +func (mockBlockStore) Base() int64 { return 1 } +func (store mockBlockStore) Height() int64 { return store.height } +func (store mockBlockStore) Size() int64 { return store.height } +func (mockBlockStore) LoadBaseMeta() *types.BlockMeta { return nil } +func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil } +func (mockBlockStore) LoadBlock(ctx context.Context, height int64) (*types.Block, error) { + return nil, nil +} +func (mockBlockStore) LoadBlockByHash(ctx context.Context, hash []byte) (*types.Block, error) { + return nil, nil +} func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return nil } func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { return nil } func (mockBlockStore) PruneBlocks(height int64) (uint64, error) { return 0, nil } -func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) error { +func (mockBlockStore) SaveBlock( + ctx context.Context, + block *types.Block, + blockParts *types.PartSet, + seenCommit *types.Commit, +) error { return nil } diff --git a/rpc/core/tx.go b/rpc/core/tx.go index 12cb48e50c..14aaa84970 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -1,6 +1,7 @@ package core import ( + "context" "errors" "fmt" "sort" @@ -37,7 +38,7 @@ func Tx(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error var proof types.TxProof if prove { - block, err := env.BlockStore.LoadBlock(height) + block, err := env.BlockStore.LoadBlock(context.TODO(), height) if err != nil { return nil, err } @@ -111,7 +112,7 @@ func TxSearch(ctx *rpctypes.Context, query string, prove bool, pagePtr, perPageP var proof types.TxProof if prove { // TODO(evan): do something with this error - block, _ := env.BlockStore.LoadBlock(r.Height) + block, _ := env.BlockStore.LoadBlock(context.TODO(), r.Height) proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines } diff --git a/state/services.go b/state/services.go index 880019352f..45aba30329 100644 --- a/state/services.go +++ b/state/services.go @@ -1,6 +1,8 @@ package state import ( + "context" + "github.com/lazyledger/lazyledger-core/types" ) @@ -20,13 +22,13 @@ type BlockStore interface { LoadBaseMeta() *types.BlockMeta LoadBlockMeta(height int64) *types.BlockMeta - LoadBlock(height int64) (*types.Block, error) + LoadBlock(ctx context.Context, height int64) (*types.Block, error) - SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) error + SaveBlock(ctx context.Context, block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) error PruneBlocks(height int64) (uint64, error) - LoadBlockByHash(hash []byte) (*types.Block, error) + LoadBlockByHash(ctx context.Context, hash []byte) (*types.Block, error) LoadBlockPart(height int64, index int) *types.Part LoadBlockCommit(height int64) *types.Commit diff --git a/store/store.go b/store/store.go index e4fc5dc7ff..ec4d557cfb 100644 --- a/store/store.go +++ b/store/store.go @@ -98,7 +98,7 @@ func (bs *BlockStore) LoadBaseMeta() *types.BlockMeta { // LoadBlock returns the block with the given height. // If no block is found for that height, it returns nil. -func (bs *BlockStore) LoadBlock(height int64) (*types.Block, error) { +func (bs *BlockStore) LoadBlock(ctx context.Context, height int64) (*types.Block, error) { blockMeta := bs.LoadBlockMeta(height) if blockMeta == nil { // TODO(evan): return an error @@ -107,7 +107,7 @@ func (bs *BlockStore) LoadBlock(height int64) (*types.Block, error) { lastCommit := bs.LoadBlockCommit(height - 1) - data, err := ipld.RetrieveBlockData(context.TODO(), &blockMeta.DAHeader, bs.dag, rsmt2d.NewRSGF8Codec()) + data, err := ipld.RetrieveBlockData(ctx, &blockMeta.DAHeader, bs.dag, rsmt2d.NewRSGF8Codec()) if err != nil { return nil, err } @@ -125,7 +125,7 @@ func (bs *BlockStore) LoadBlock(height int64) (*types.Block, error) { // LoadBlockByHash returns the block with the given hash. // If no block is found for that hash, it returns nil. // Panics if it fails to parse height associated with the given hash. -func (bs *BlockStore) LoadBlockByHash(hash []byte) (*types.Block, error) { +func (bs *BlockStore) LoadBlockByHash(ctx context.Context, hash []byte) (*types.Block, error) { bz, err := bs.db.Get(calcBlockHashKey(hash)) if err != nil { panic(err) @@ -140,7 +140,7 @@ func (bs *BlockStore) LoadBlockByHash(hash []byte) (*types.Block, error) { if err != nil { panic(fmt.Sprintf("failed to extract height from %s: %v", s, err)) } - return bs.LoadBlock(height) + return bs.LoadBlock(ctx, height) } // LoadBlockPart returns the Part at the given index @@ -328,7 +328,12 @@ func (bs *BlockStore) PruneBlocks(height int64) (uint64, error) { // If all the nodes restart after committing a block, // we need this to reload the precommits to catch-up nodes to the // most recent height. Otherwise they'd stall at H-1. -func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) error { +func (bs *BlockStore) SaveBlock( + ctx context.Context, + block *types.Block, + blockParts *types.PartSet, + seenCommit *types.Commit, +) error { if block == nil { panic("BlockStore can only save a non-nil block") } @@ -352,7 +357,7 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s bs.saveBlockPart(height, i, part) } - err := ipld.PutBlock(context.TODO(), bs.dag, block) + err := ipld.PutBlock(ctx, bs.dag, block) if err != nil { return err } diff --git a/store/store_test.go b/store/store_test.go index 9bf232e728..9c0309a5b1 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -2,6 +2,7 @@ package store import ( "bytes" + "context" "crypto/sha256" "fmt" "os" @@ -175,7 +176,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { // check there are no blocks at various heights noBlockHeights := []int64{0, -1, 100, 1000, 2} for i, height := range noBlockHeights { - if g, _ := bs.LoadBlock(height); g != nil { + if g, _ := bs.LoadBlock(context.TODO(), height); g != nil { t.Errorf("#%d: height(%d) got a block; want nil", i, height) } } @@ -184,7 +185,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { block := makeBlock(bs.Height()+1, state, new(types.Commit)) validPartSet := block.MakePartSet(2) seenCommit := makeTestCommit(10, tmtime.Now()) - err := bs.SaveBlock(block, partSet, seenCommit) + err := bs.SaveBlock(context.TODO(), block, partSet, seenCommit) require.NoError(t, err) require.EqualValues(t, 1, bs.Base(), "expecting the new height to be changed") require.EqualValues(t, block.Header.Height, bs.Height(), "expecting the new height to be changed") @@ -306,7 +307,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { bs, db := freshBlockStore() // SaveBlock res, err, panicErr := doFn(func() (interface{}, error) { - err = bs.SaveBlock(tuple.block, tuple.parts, tuple.seenCommit) + err = bs.SaveBlock(context.TODO(), tuple.block, tuple.parts, tuple.seenCommit) require.NoError(t, err) if tuple.block == nil { return nil, nil @@ -316,7 +317,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { err := db.Set(calcBlockMetaKey(tuple.block.Height), []byte("block-bogus")) require.NoError(t, err) } - bBlock, err := bs.LoadBlock(tuple.block.Height) + bBlock, err := bs.LoadBlock(context.TODO(), tuple.block.Height) require.NoError(t, err) bBlockMeta := bs.LoadBlockMeta(tuple.block.Height) @@ -390,7 +391,7 @@ func TestLoadBaseMeta(t *testing.T) { block := makeBlock(h, state, new(types.Commit)) partSet := block.MakePartSet(2) seenCommit := makeTestCommit(h, tmtime.Now()) - err = bs.SaveBlock(block, partSet, seenCommit) + err = bs.SaveBlock(context.TODO(), block, partSet, seenCommit) require.NoError(t, err) } @@ -459,7 +460,7 @@ func TestPruneBlocks(t *testing.T) { block := makeBlock(h, state, new(types.Commit)) partSet := block.MakePartSet(2) seenCommit := makeTestCommit(h, tmtime.Now()) - err = bs.SaveBlock(block, partSet, seenCommit) + err = bs.SaveBlock(context.TODO(), block, partSet, seenCommit) require.NoError(t, err) } @@ -467,7 +468,7 @@ func TestPruneBlocks(t *testing.T) { assert.EqualValues(t, 1500, bs.Height()) assert.EqualValues(t, 1500, bs.Size()) - prunedBlock, err := bs.LoadBlock(1199) + prunedBlock, err := bs.LoadBlock(context.TODO(), 1199) assert.NoError(t, err) // Check that basic pruning works @@ -482,22 +483,22 @@ func TestPruneBlocks(t *testing.T) { Height: 1500, }, LoadBlockStoreState(db)) - block, _ := bs.LoadBlock(1200) + block, _ := bs.LoadBlock(context.TODO(), 1200) require.NotNil(t, block) - block, _ = bs.LoadBlock(1199) + block, _ = bs.LoadBlock(context.TODO(), 1199) require.Nil(t, block) - block, _ = bs.LoadBlockByHash(prunedBlock.Hash()) + block, _ = bs.LoadBlockByHash(context.TODO(), prunedBlock.Hash()) require.Nil(t, block) require.Nil(t, bs.LoadBlockCommit(1199)) require.Nil(t, bs.LoadBlockMeta(1199)) require.Nil(t, bs.LoadBlockPart(1199, 1)) for i := int64(1); i < 1200; i++ { - block, _ = bs.LoadBlock(i) + block, _ = bs.LoadBlock(context.TODO(), i) require.Nil(t, block) } for i := int64(1200); i <= 1500; i++ { - block, _ = bs.LoadBlock(i) + block, _ = bs.LoadBlock(context.TODO(), i) require.NotNil(t, block) } @@ -524,11 +525,11 @@ func TestPruneBlocks(t *testing.T) { pruned, err = bs.PruneBlocks(1500) require.NoError(t, err) assert.EqualValues(t, 200, pruned) - block, _ = bs.LoadBlock(1499) + block, _ = bs.LoadBlock(context.TODO(), 1499) assert.Nil(t, block) - block, _ = bs.LoadBlock(1500) + block, _ = bs.LoadBlock(context.TODO(), 1500) assert.NotNil(t, block) - block, _ = bs.LoadBlock(1501) + block, _ = bs.LoadBlock(context.TODO(), 1501) assert.Nil(t, block) } @@ -582,11 +583,11 @@ func TestBlockFetchAtHeight(t *testing.T) { partSet := block.MakePartSet(2) seenCommit := makeTestCommit(10, tmtime.Now()) - err := bs.SaveBlock(block, partSet, seenCommit) + err := bs.SaveBlock(context.TODO(), block, partSet, seenCommit) require.NoError(t, err) require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed") - blockAtHeight, _ := bs.LoadBlock(bs.Height()) + blockAtHeight, _ := bs.LoadBlock(context.TODO(), bs.Height()) b1, err := block.ToProto() require.NoError(t, err) b2, err := blockAtHeight.ToProto() @@ -597,10 +598,10 @@ func TestBlockFetchAtHeight(t *testing.T) { require.Equal(t, block.Hash(), blockAtHeight.Hash(), "expecting a successful load of the last saved block") - blockAtHeightPlus1, err := bs.LoadBlock(bs.Height() + 1) + blockAtHeightPlus1, err := bs.LoadBlock(context.TODO(), bs.Height()+1) require.NoError(t, err) require.Nil(t, blockAtHeightPlus1, "expecting an unsuccessful load of Height()+1") - blockAtHeightPlus2, err := bs.LoadBlock(bs.Height() + 2) + blockAtHeightPlus2, err := bs.LoadBlock(context.TODO(), bs.Height()+2) require.NoError(t, err) require.Nil(t, blockAtHeightPlus2, "expecting an unsuccessful load of Height()+2") }