-
Notifications
You must be signed in to change notification settings - Fork 12
/
get_block.go
111 lines (94 loc) · 5.11 KB
/
get_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package publicapi
import (
"context"
"github.com/orbs-network/orbs-network-go/instrumentation/log"
"github.com/orbs-network/orbs-network-go/instrumentation/trace"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/orbs-network/orbs-spec/types/go/protocol/client"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/pkg/errors"
)
func (s *service) GetBlock(parentCtx context.Context, input *services.GetBlockInput) (*services.GetBlockOutput, error) {
ctx := trace.NewContext(parentCtx, "PublicApi.GetBlock")
if input.ClientRequest == nil {
err := errors.Errorf("client request is nil")
s.logger.Info("get block received missing input", log.Error(err))
return nil, err
}
logger := s.logger.WithTags(trace.LogFieldFrom(ctx), log.BlockHeight(input.ClientRequest.BlockHeight()), log.String("flow", "checkpoint"))
if _, err := validateRequest(s.config, input.ClientRequest.ProtocolVersion(), input.ClientRequest.VirtualChainId()); err != nil {
logger.Info("get block received input failed", log.Error(err))
return toGetBlockErrOutput(protocol.REQUEST_STATUS_BAD_REQUEST, 0, 0), err
}
if input.ClientRequest.BlockHeight() == 0 {
logger.Info("requested block height 0 is not valid")
return s.toGetBlockErrOutputAddHeight(ctx, logger, protocol.REQUEST_STATUS_BAD_REQUEST)
}
logger.Info("get block request received")
bpc, err := s.blockStorage.GetBlockPair(ctx, &services.GetBlockPairInput{
BlockHeight: input.ClientRequest.BlockHeight(),
})
if err != nil {
logger.Info("block storage failed", log.Error(err))
return toGetBlockErrOutput(protocol.REQUEST_STATUS_SYSTEM_ERROR, 0, 0), err
}
if bpc.BlockPair == nil {
logger.Info("get block failed to get requested block height", log.BlockHeight(input.ClientRequest.BlockHeight()))
return s.toGetBlockErrOutputAddHeight(ctx, logger, protocol.REQUEST_STATUS_NOT_FOUND)
}
return toGetBlockOutput(bpc.BlockPair), nil
}
func toGetBlockOutput(bpc *protocol.BlockPairContainer) *services.GetBlockOutput {
signedTransactionBuilders := make([]*protocol.SignedTransactionBuilder, len(bpc.TransactionsBlock.SignedTransactions))
for i, stx := range bpc.TransactionsBlock.SignedTransactions {
signedTransactionBuilders[i] = protocol.SignedTransactionBuilderFromRaw(stx.Raw())
}
transactionReceiptBuilders := make([]*protocol.TransactionReceiptBuilder, len(bpc.ResultsBlock.TransactionReceipts))
for i, txr := range bpc.ResultsBlock.TransactionReceipts {
transactionReceiptBuilders[i] = protocol.TransactionReceiptBuilderFromRaw(txr.Raw())
}
contractStateDiffBuilders := make([]*protocol.ContractStateDiffBuilder, len(bpc.ResultsBlock.ContractStateDiffs))
for i, csd := range bpc.ResultsBlock.ContractStateDiffs {
contractStateDiffBuilders[i] = protocol.ContractStateDiffBuilderFromRaw(csd.Raw())
}
response := client.GetBlockResponseBuilder{
RequestResult: &client.RequestResultBuilder{
RequestStatus: protocol.REQUEST_STATUS_COMPLETED,
BlockHeight: bpc.TransactionsBlock.Header.BlockHeight(),
BlockTimestamp: bpc.TransactionsBlock.Header.Timestamp(),
},
TransactionsBlockHeader: protocol.TransactionsBlockHeaderBuilderFromRaw(bpc.TransactionsBlock.Header.Raw()),
TransactionsBlockMetadata: protocol.TransactionsBlockMetadataBuilderFromRaw(bpc.TransactionsBlock.Metadata.Raw()),
SignedTransactions: signedTransactionBuilders,
TransactionsBlockProof: protocol.TransactionsBlockProofBuilderFromRaw(bpc.TransactionsBlock.BlockProof.Raw()),
ResultsBlockHeader: protocol.ResultsBlockHeaderBuilderFromRaw(bpc.ResultsBlock.Header.Raw()),
TransactionReceipts: transactionReceiptBuilders,
ContractStateDiffs: contractStateDiffBuilders,
ResultsBlockProof: protocol.ResultsBlockProofBuilderFromRaw(bpc.ResultsBlock.BlockProof.Raw()),
}
return &services.GetBlockOutput{ClientResponse: response.Build()}
}
func (s *service) toGetBlockErrOutputAddHeight(ctx context.Context, logger log.BasicLogger, status protocol.RequestStatus) (*services.GetBlockOutput, error) {
bk, err := s.blockStorage.GetLastCommittedBlockHeight(ctx, &services.GetLastCommittedBlockHeightInput{})
if err != nil {
logger.Info("block storage failed while getting last block", log.Error(err))
return toGetBlockErrOutput(protocol.REQUEST_STATUS_SYSTEM_ERROR, 0, 0), err
}
return toGetBlockErrOutput(status, bk.LastCommittedBlockHeight, bk.LastCommittedBlockTimestamp), nil
}
func toGetBlockErrOutput(status protocol.RequestStatus, height primitives.BlockHeight, nano primitives.TimestampNano) *services.GetBlockOutput {
response := client.GetBlockResponseBuilder{
RequestResult: &client.RequestResultBuilder{
RequestStatus: status,
BlockHeight: height,
BlockTimestamp: nano,
},
}
return &services.GetBlockOutput{ClientResponse: response.Build()}
}