forked from hyperledger-archives/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
executable file
·164 lines (138 loc) · 5.17 KB
/
api.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package openchain
import (
"errors"
"fmt"
"golang.org/x/net/context"
google_protobuf1 "google/protobuf"
"github.com/golang/protobuf/proto"
"github.com/openblockchain/obc-peer/openchain/ledger"
pb "github.com/openblockchain/obc-peer/protos"
)
var (
// ErrNotFound is returned if a requested resource does not exist
ErrNotFound = errors.New("openchain: resource not found")
)
// PeerInfo
type PeerInfo interface {
GetPeers() (*pb.PeersMessage, error)
}
// ServerOpenchain defines the Openchain server object, which holds the
// Ledger data structure and the pointer to the peerServer.
type ServerOpenchain struct {
ledger *ledger.Ledger
peerInfo PeerInfo
}
// NewOpenchainServer creates a new instance of the ServerOpenchain.
func NewOpenchainServer() (*ServerOpenchain, error) {
// Get a handle to the Ledger singleton.
ledger, err := ledger.GetLedger()
if err != nil {
return nil, err
}
s := &ServerOpenchain{ledger: ledger}
return s, nil
}
// NewOpenchainServerWithPeerInfo creates a new instance of the ServerOpenchain.
func NewOpenchainServerWithPeerInfo(peerServer PeerInfo) (*ServerOpenchain, error) {
// Get a handle to the Ledger singleton.
ledger, err := ledger.GetLedger()
if err != nil {
return nil, err
}
s := &ServerOpenchain{ledger: ledger, peerInfo: peerServer}
return s, nil
}
// GetBlockchainInfo returns information about the blockchain ledger such as
// height, current block hash, and previous block hash.
func (s *ServerOpenchain) GetBlockchainInfo(ctx context.Context, e *google_protobuf1.Empty) (*pb.BlockchainInfo, error) {
blockchainInfo, err := s.ledger.GetBlockchainInfo()
if blockchainInfo.Height == 0 {
return nil, fmt.Errorf("No blocks in blockchain.")
}
return blockchainInfo, err
}
// GetBlockByNumber returns the data contained within a specific block in the
// blockchain. The genesis block is block zero.
func (s *ServerOpenchain) GetBlockByNumber(ctx context.Context, num *pb.BlockNumber) (*pb.Block, error) {
block, err := s.ledger.GetBlockByNumber(num.Number)
if err != nil {
switch err {
case ledger.ErrOutOfBounds:
return nil, ErrNotFound
default:
return nil, fmt.Errorf("Error retrieving block from blockchain: %s", err)
}
}
// Remove payload from deploy transactions. This is done to make rest api
// calls more lightweight as the payload for these types of transactions
// can be very large. If the payload is needed, the caller should fetch the
// individual transaction.
blockTransactions := block.GetTransactions()
for _, transaction := range blockTransactions {
if transaction.Type == pb.Transaction_CHAINCODE_NEW {
deploymentSpec := &pb.ChaincodeDeploymentSpec{}
err := proto.Unmarshal(transaction.Payload, deploymentSpec)
if err != nil {
return nil, err
}
deploymentSpec.CodePackage = nil
deploymentSpecBytes, err := proto.Marshal(deploymentSpec)
if err != nil {
return nil, err
}
transaction.Payload = deploymentSpecBytes
}
}
return block, nil
}
// GetBlockCount returns the current number of blocks in the blockchain data
// structure.
func (s *ServerOpenchain) GetBlockCount(ctx context.Context, e *google_protobuf1.Empty) (*pb.BlockCount, error) {
// Total number of blocks in the blockchain.
size := s.ledger.GetBlockchainSize()
// Check the number of blocks in the blockchain. If the blockchain is empty,
// return error. There will always be at least one block in the blockchain,
// the genesis block.
if size > 0 {
count := &pb.BlockCount{Count: size}
return count, nil
}
return nil, fmt.Errorf("No blocks in blockchain.")
}
// GetState returns the value for a particular chaincode ID and key
func (s *ServerOpenchain) GetState(ctx context.Context, chaincodeID, key string) ([]byte, error) {
return s.ledger.GetState(chaincodeID, key, true)
}
// GetTransactionByUUID returns a transaction matching the specified UUID
func (s *ServerOpenchain) GetTransactionByUUID(ctx context.Context, txUUID string) (*pb.Transaction, error) {
transaction, err := s.ledger.GetTransactionByUUID(txUUID)
if err != nil {
switch err {
case ledger.ErrResourceNotFound:
return nil, ErrNotFound
default:
return nil, fmt.Errorf("Error retrieving transaction from blockchain: %s", err)
}
}
return transaction, nil
}
// GetPeers returns a list of all peer nodes currently connected to the target peer.
func (s *ServerOpenchain) GetPeers(ctx context.Context, e *google_protobuf1.Empty) (*pb.PeersMessage, error) {
return s.peerInfo.GetPeers()
}