/
get_abi.go
86 lines (71 loc) · 2.23 KB
/
get_abi.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
package grpc
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/dfuse-io/derr"
pbstatedb "github.com/zhongshuwen/historyexp/pb/dfuse/eosio/statedb/v1"
"github.com/zhongshuwen/historyexp/statedb"
"github.com/dfuse-io/logging"
"github.com/dfuse-io/validator"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
)
func (s *Server) GetABI(ctx context.Context, request *pbstatedb.GetABIRequest) (*pbstatedb.GetABIResponse, error) {
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("get abi",
zap.String("contract", request.Contract),
zap.Uint64("block_num", request.BlockNum),
)
actualBlockNum, _, _, speculativeWrites, err := s.prepareRead(ctx, request.BlockNum, false)
if err != nil {
return nil, fmt.Errorf("speculative writes: %w", err)
}
entry, err := s.db.ReadSingletEntryAt(ctx, statedb.NewContractABISinglet(request.Contract), actualBlockNum, speculativeWrites)
if err != nil {
return nil, fmt.Errorf("db read: %w", err)
}
// FIXME: Is this the semantic we want for not found ABI call?
if entry == nil {
return &pbstatedb.GetABIResponse{}, nil
}
abiEntry := entry.(*statedb.ContractABIEntry)
if request.ToJson {
abi, _, err := abiEntry.ABI()
if err != nil {
return nil, derr.Statusf(codes.Internal, "failed to decode ABI to JSON: %s", err)
}
cnt, err := json.Marshal(abi)
if err != nil {
return nil, derr.Statusf(codes.Internal, "failed to marshal ABI to JSON: %s", err)
}
return &pbstatedb.GetABIResponse{
BlockNum: abiEntry.Height(),
JsonAbi: string(cnt),
}, nil
}
_, rawABI, err := abiEntry.ABI(statedb.ContractABIPackedOnly)
if err != nil {
return nil, derr.Statusf(codes.Internal, "unable to unmarshal contract abi value: %s", err)
}
return &pbstatedb.GetABIResponse{
BlockNum: abiEntry.Height(),
RawAbi: rawABI,
}, nil
}
func validateGetABIRequest(request *pbstatedb.GetABIRequest) error {
errors := validator.ValidateStruct(request, validator.Rules{
"contract": []string{"required", "fluxdb.eos.name"},
"block_num": []string{"fluxdb.eos.blockNum"},
"to_json": []string{"bool"},
})
if len(errors) == 0 {
return nil
}
msgs := []string{}
for _, errs := range errors {
msgs = append(msgs, errs...)
}
return fmt.Errorf("%s", strings.Join(msgs, ", "))
}