Return all transactions in a block

This commit is contained in:
Patrick O'Grady 2020-10-27 11:14:53 -07:00
parent 5e9343a662
commit 9fa814a74a
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
4 changed files with 78 additions and 49 deletions

View file

@ -7,7 +7,7 @@
"http_timeout": 300,
"max_retries": 5,
"retry_elapsed_time": 0,
"max_online_connections": 0,
"max_online_connections": 1000,
"max_sync_concurrency": 0,
"tip_delay": 1800,
"log_configuration": false,

View file

@ -7,7 +7,7 @@
"http_timeout": 300,
"max_retries": 5,
"retry_elapsed_time": 0,
"max_online_connections": 0,
"max_online_connections": 1000,
"max_sync_concurrency": 0,
"tip_delay": 1800,
"log_configuration": false,

View file

@ -54,6 +54,22 @@ func (s *BlockAPIService) Block(
return nil, wrapErr(ErrBlockNotFound, err)
}
txs := make([]*types.Transaction, len(blockResponse.OtherTransactions))
for i, otherTx := range blockResponse.OtherTransactions {
transaction, err := s.i.GetBlockTransaction(
ctx,
blockResponse.Block.BlockIdentifier,
otherTx,
)
if err != nil {
return nil, wrapErr(ErrTransactionNotFound, err)
}
txs[i] = transaction
}
blockResponse.Block.Transactions = txs
blockResponse.OtherTransactions = nil
return blockResponse, nil
}
@ -62,20 +78,5 @@ func (s *BlockAPIService) BlockTransaction(
ctx context.Context,
request *types.BlockTransactionRequest,
) (*types.BlockTransactionResponse, *types.Error) {
if s.config.Mode != configuration.Online {
return nil, wrapErr(ErrUnavailableOffline, nil)
}
transaction, err := s.i.GetBlockTransaction(
ctx,
request.BlockIdentifier,
request.TransactionIdentifier,
)
if err != nil {
return nil, wrapErr(ErrTransactionNotFound, err)
}
return &types.BlockTransactionResponse{
Transaction: transaction,
}, nil
return nil, ErrUnimplemented
}

View file

@ -40,8 +40,8 @@ func TestBlockService_Offline(t *testing.T) {
blockTransaction, err := servicer.BlockTransaction(ctx, &types.BlockTransactionRequest{})
assert.Nil(t, blockTransaction)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
assert.Equal(t, ErrUnavailableOffline.Message, err.Message)
assert.Equal(t, ErrUnimplemented.Code, err.Code)
assert.Equal(t, ErrUnimplemented.Message, err.Message)
mockIndexer.AssertExpectations(t)
}
@ -54,51 +54,49 @@ func TestBlockService_Online(t *testing.T) {
servicer := NewBlockAPIService(cfg, mockIndexer)
ctx := context.Background()
block := &types.Block{
rawBlock := &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: 100,
Hash: "block 100",
},
}
blockResponse := &types.BlockResponse{
Block: block,
OtherTransactions: []*types.TransactionIdentifier{
{
Hash: "tx1",
},
},
}
transaction := &types.Transaction{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "tx1",
},
}
block := &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: 100,
Hash: "block 100",
},
Transactions: []*types.Transaction{
transaction,
},
}
blockResponse := &types.BlockResponse{
Block: block,
}
t.Run("nil identifier", func(t *testing.T) {
mockIndexer.On(
"GetBlockLazy",
ctx,
(*types.PartialBlockIdentifier)(nil),
).Return(
blockResponse,
&types.BlockResponse{
Block: rawBlock,
OtherTransactions: []*types.TransactionIdentifier{
{
Hash: "tx1",
},
},
},
nil,
).Once()
b, err := servicer.Block(ctx, &types.BlockRequest{})
assert.Nil(t, err)
assert.Equal(t, blockResponse, b)
})
t.Run("populated identifier", func(t *testing.T) {
pbIdentifier := types.ConstructPartialBlockIdentifier(block.BlockIdentifier)
mockIndexer.On("GetBlockLazy", ctx, pbIdentifier).Return(blockResponse, nil).Once()
b, err := servicer.Block(ctx, &types.BlockRequest{
BlockIdentifier: pbIdentifier,
})
assert.Nil(t, err)
assert.Equal(t, blockResponse, b)
mockIndexer.On(
"GetBlockTransaction",
ctx,
@ -108,12 +106,42 @@ func TestBlockService_Online(t *testing.T) {
transaction,
nil,
).Once()
blockTransaction, err := servicer.BlockTransaction(ctx, &types.BlockTransactionRequest{
BlockIdentifier: blockResponse.Block.BlockIdentifier,
TransactionIdentifier: transaction.TransactionIdentifier,
b, err := servicer.Block(ctx, &types.BlockRequest{})
assert.Nil(t, err)
assert.Equal(t, blockResponse, b)
})
t.Run("populated identifier", func(t *testing.T) {
pbIdentifier := types.ConstructPartialBlockIdentifier(block.BlockIdentifier)
mockIndexer.On(
"GetBlockLazy",
ctx,
pbIdentifier,
).Return(
&types.BlockResponse{
Block: rawBlock,
OtherTransactions: []*types.TransactionIdentifier{
{
Hash: "tx1",
},
},
},
nil,
).Once()
mockIndexer.On(
"GetBlockTransaction",
ctx,
blockResponse.Block.BlockIdentifier,
transaction.TransactionIdentifier,
).Return(
transaction,
nil,
).Once()
b, err := servicer.Block(ctx, &types.BlockRequest{
BlockIdentifier: pbIdentifier,
})
assert.Nil(t, err)
assert.Equal(t, transaction, blockTransaction.Transaction)
assert.Equal(t, blockResponse, b)
})
mockIndexer.AssertExpectations(t)