-
Notifications
You must be signed in to change notification settings - Fork 68
/
waitlist.go
77 lines (66 loc) · 2.11 KB
/
waitlist.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
package service
import (
"context"
"encoding/hex"
"github.com/MinterTeam/minter-go-node/core/state/waitlist"
"github.com/MinterTeam/minter-go-node/core/types"
pb "github.com/MinterTeam/node-grpc-gateway/api_pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strings"
)
// WaitList returns the list of address stakes in waitlist.
func (s *Service) WaitList(ctx context.Context, req *pb.WaitListRequest) (*pb.WaitListResponse, error) {
if !strings.HasPrefix(strings.Title(req.Address), "Mx") {
return nil, status.Error(codes.InvalidArgument, "invalid address")
}
decodeString, err := hex.DecodeString(req.Address[2:])
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid address")
}
address := types.BytesToAddress(decodeString)
cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
if req.Height != 0 {
cState.Lock()
cState.Candidates().LoadCandidates()
cState.Unlock()
}
cState.RLock()
defer cState.RUnlock()
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}
response := new(pb.WaitListResponse)
var items []waitlist.Item
publicKey := req.PublicKey
if publicKey != "" {
if !strings.HasPrefix(publicKey, "Mp") {
return nil, status.Error(codes.InvalidArgument, "public key don't has prefix 'Mp'")
}
items = cState.WaitList().GetByAddressAndPubKey(address, types.HexToPubkey(publicKey))
} else {
model := cState.WaitList().GetByAddress(address)
if model == nil {
return response, nil
}
items = model.List
}
response.List = make([]*pb.WaitListResponse_Wait, 0, len(items))
for _, item := range items {
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}
response.List = append(response.List, &pb.WaitListResponse_Wait{
PublicKey: cState.Candidates().PubKey(item.CandidateId).String(),
Coin: &pb.Coin{
Id: uint64(item.Coin),
Symbol: cState.Coins().GetCoin(item.Coin).GetFullSymbol(),
},
Value: item.Value.String(),
})
}
return response, nil
}