From 01e1d766a0419da0bc83e4030c62168ff5f43e68 Mon Sep 17 00:00:00 2001 From: Denio Date: Thu, 5 Dec 2019 21:37:35 +0100 Subject: [PATCH] add coinsupply rpc to lightwalletd --- cmd/server/main.go | 10 ++++ common/common.go | 34 ++++++++++++ frontend/service.go | 24 +++++++++ walletrpc/service.pb.go | 115 ++++++++++++++++++++++++++++++++++++++++ walletrpc/service.proto | 9 ++++ 5 files changed, 192 insertions(+) diff --git a/cmd/server/main.go b/cmd/server/main.go index 9c6470b..e56c058 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -179,6 +179,16 @@ func main() { log.Info("Got sapling height ", saplingHeight, " chain ", chainName, " branchID ", branchID," difficulty ", difficulty,longestchain, " longestchain ",notarized," notarized ") + // Get the Coinsupply from the RPC + result, coin, height, supply, zfunds, total, err := common.GetCoinsupply(rpcClient) + if err != nil { + log.WithFields(logrus.Fields{ + "error": err, + }).Warn("Unable to get coinsupply") + } + + log.Info( " result ", result, " coin ", coin," height", height, "supply", supply ,"zfunds", zfunds, "total", total) + // Initialize the cache cache := common.NewBlockCache(opts.cacheSize) diff --git a/common/common.go b/common/common.go index 6da4eaf..4bb53fd 100644 --- a/common/common.go +++ b/common/common.go @@ -55,6 +55,40 @@ func GetSaplingInfo(rpcClient *rpcclient.Client) (int, int, string, string, int, return int(saplingHeight), int(blockHeight), chainName, branchID, int(difficulty), int(longestchain), int(notarized), nil } +func GetCoinsupply(rpcClient *rpcclient.Client) (string, string, int, int, int,int, error) { + result1, rpcErr := rpcClient.RawRequest("coinsupply", make([]json.RawMessage, 0)) + + var err error + var errCode int64 + + + // For some reason, the error responses are not JSON + if rpcErr != nil { + errParts := strings.SplitN(rpcErr.Error(), ":", 2) + errCode, err = strconv.ParseInt(errParts[0], 10, 32) + //Check to see if we are requesting a height the hushd doesn't have yet + if err == nil && errCode == -8 { + return "","", -1, -1,-1,-1, nil + } + return "","", -1, -1,-1,-1, errors.Wrap(rpcErr, "error requesting coinsupply") + } + + var f interface{} + err = json.Unmarshal(result1, &f) + if err != nil { + return "","", -1, -1,-1,-1, errors.Wrap(err, "error reading JSON response") + } + + result := f.(map[string]interface{})["result"].(string) + coin := f.(map[string]interface{})["coin"].(string) + height := f.(map[string]interface{})["height"].(float64) + supply := f.(map[string]interface{})["supply"].(float64) + zfunds := f.(map[string]interface{})["zfunds"].(float64) + total := f.(map[string]interface{})["total"].(float64) + + return result,coin, int(height), int(supply),int(zfunds), int(total), nil +} + func getBlockFromRPC(rpcClient *rpcclient.Client, height int) (*walletrpc.CompactBlock, error) { params := make([]json.RawMessage, 2) params[0] = json.RawMessage("\"" + strconv.Itoa(height) + "\"") diff --git a/frontend/service.go b/frontend/service.go index e7325c9..61da90f 100644 --- a/frontend/service.go +++ b/frontend/service.go @@ -254,6 +254,30 @@ func (s *SqlStreamer) GetLightdInfo(ctx context.Context, in *walletrpc.Empty) (* }, nil } +// GetCoinsupply gets the Coinsupply info +func (s *SqlStreamer) GetCoinsupply(ctx context.Context, in *walletrpc.Empty) (*walletrpc.Coinsupply, error) { + result, coin, height, supply, zfunds,total, err := common.GetCoinsupply(s.client) + + if err != nil { + s.log.WithFields(logrus.Fields{ + "error": err, + }).Warn("Unable to get Coinsupply") + return nil, err + } + + // TODO these are called Error but they aren't at the moment. + // A success will return code 0 and message txhash. + return &walletrpc.Coinsupply{ + Result: result, + Coin: coin, + Height: uint64(height), + Supply: uint64(supply), + Zfunds: uint64(zfunds), + Total: uint64(total), + }, nil +} + + // SendTransaction forwards raw transaction bytes to a hushd instance over JSON-RPC func (s *SqlStreamer) SendTransaction(ctx context.Context, rawtx *walletrpc.RawTransaction) (*walletrpc.SendResponse, error) { // sendrawtransaction "hexstring" ( allowhighfees ) diff --git a/walletrpc/service.pb.go b/walletrpc/service.pb.go index 76831f7..f73ee41 100644 --- a/walletrpc/service.pb.go +++ b/walletrpc/service.pb.go @@ -447,6 +447,85 @@ func (m *LightdInfo) GetBlockHeight() uint64 { return 0 } +type Coinsupply struct { + Result string `protobuf:"bytes,1,opt,name=result,proto3" json:"result,omitempty"` + Coin string `protobuf:"bytes,2,opt,name=coin,proto3" json:"coin,omitempty"` + Height uint64 `protobuf:"varint,3,opt,name=height,proto3" json:"height,omitempty"` + Supply uint64 `protobuf:"varint,4,opt,name=supply,proto3" json:"supply,omitempty"` + Zfunds uint64 `protobuf:"varint,5,opt,name=zfunds,proto3" json:"zfunds,omitempty"` + Total uint64 `protobuf:"varint,6,opt,name=total,proto3" json:"total,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Coinsupply) Reset() { *m = Coinsupply{} } +func (m *Coinsupply) String() string { return proto.CompactTextString(m) } +func (*Coinsupply) ProtoMessage() {} +func (*Coinsupply) Descriptor() ([]byte, []int) { + return fileDescriptor_a0b84a42fa06f626, []int{7} +} + +func (m *Coinsupply) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Coinsupply.Unmarshal(m, b) +} +func (m *Coinsupply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Coinsupply.Marshal(b, m, deterministic) +} +func (m *Coinsupply) XXX_Merge(src proto.Message) { + xxx_messageInfo_Coinsupply.Merge(m, src) +} +func (m *Coinsupply) XXX_Size() int { + return xxx_messageInfo_Coinsupply.Size(m) +} +func (m *Coinsupply) XXX_DiscardUnknown() { + xxx_messageInfo_Coinsupply.DiscardUnknown(m) +} + +var xxx_messageInfo_Coinsupply proto.InternalMessageInfo + +func (m *Coinsupply) GetResult() string { + if m != nil { + return m.Result + } + return "" +} + +func (m *Coinsupply) GetCoin() string { + if m != nil { + return m.Coin + } + return "" +} + +func (m *Coinsupply) GetHeight() uint64 { + if m != nil { + return m.Height + } + return 0 +} + +func (m *Coinsupply) GetSupply() uint64 { + if m != nil { + return m.Supply + } + return 0 +} + +func (m *Coinsupply) GetZfunds() uint64 { + if m != nil { + return m.Zfunds + } + return 0 +} + +func (m *Coinsupply) GetTotal() uint64 { + if m != nil { + return m.Total + } + return 0 +} + type TransparentAddress struct { Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -542,6 +621,7 @@ func init() { proto.RegisterType((*ChainSpec)(nil), "cash.z.wallet.sdk.rpc.ChainSpec") proto.RegisterType((*Empty)(nil), "cash.z.wallet.sdk.rpc.Empty") proto.RegisterType((*LightdInfo)(nil), "cash.z.wallet.sdk.rpc.LightdInfo") + proto.RegisterType((*Coinsupply)(nil), "cash.z.wallet.sdk.rpc.Coinsupply") proto.RegisterType((*TransparentAddress)(nil), "cash.z.wallet.sdk.rpc.TransparentAddress") proto.RegisterType((*TransparentAddressBlockFilter)(nil), "cash.z.wallet.sdk.rpc.TransparentAddressBlockFilter") } @@ -616,6 +696,7 @@ type CompactTxStreamerClient interface { GetAddressTxids(ctx context.Context, in *TransparentAddressBlockFilter, opts ...grpc.CallOption) (CompactTxStreamer_GetAddressTxidsClient, error) // Misc GetLightdInfo(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*LightdInfo, error) + GetCoinsupply(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Coinsupply, error) } type compactTxStreamerClient struct { @@ -734,6 +815,14 @@ func (c *compactTxStreamerClient) GetLightdInfo(ctx context.Context, in *Empty, } return out, nil } +func (c *compactTxStreamerClient) GetCoinsupply(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Coinsupply, error) { + out := new(Coinsupply) + err := c.cc.Invoke(ctx, "/cash.z.wallet.sdk.rpc.CompactTxStreamer/GetCoinsupply", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} // CompactTxStreamerServer is the server API for CompactTxStreamer service. type CompactTxStreamerServer interface { @@ -748,6 +837,7 @@ type CompactTxStreamerServer interface { GetAddressTxids(*TransparentAddressBlockFilter, CompactTxStreamer_GetAddressTxidsServer) error // Misc GetLightdInfo(context.Context, *Empty) (*LightdInfo, error) + GetCoinsupply(context.Context, *Empty) (*Coinsupply, error) } // UnimplementedCompactTxStreamerServer can be embedded to have forward compatible implementations. @@ -775,6 +865,9 @@ func (*UnimplementedCompactTxStreamerServer) GetAddressTxids(req *TransparentAdd func (*UnimplementedCompactTxStreamerServer) GetLightdInfo(ctx context.Context, req *Empty) (*LightdInfo, error) { return nil, status.Errorf(codes.Unimplemented, "method GetLightdInfo not implemented") } +func (*UnimplementedCompactTxStreamerServer) GetCoinsupply(ctx context.Context, req *Empty) (*Coinsupply, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetCoinsupply not implemented") +} func RegisterCompactTxStreamerServer(s *grpc.Server, srv CompactTxStreamerServer) { s.RegisterService(&_CompactTxStreamer_serviceDesc, srv) @@ -912,6 +1005,24 @@ func _CompactTxStreamer_GetLightdInfo_Handler(srv interface{}, ctx context.Conte return interceptor(ctx, in, info, handler) } +func _CompactTxStreamer_GetCoinsupply_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CompactTxStreamerServer).GetCoinsupply(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cash.z.wallet.sdk.rpc.CompactTxStreamer/GetCoinsupply", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CompactTxStreamerServer).GetCoinsupply(ctx, req.(*Empty)) + } + return interceptor(ctx, in, info, handler) +} + var _CompactTxStreamer_serviceDesc = grpc.ServiceDesc{ ServiceName: "cash.z.wallet.sdk.rpc.CompactTxStreamer", HandlerType: (*CompactTxStreamerServer)(nil), @@ -936,6 +1047,10 @@ var _CompactTxStreamer_serviceDesc = grpc.ServiceDesc{ MethodName: "GetLightdInfo", Handler: _CompactTxStreamer_GetLightdInfo_Handler, }, + { + MethodName: "GetCoinsupply", + Handler: _CompactTxStreamer_GetCoinsupply_Handler, + }, }, Streams: []grpc.StreamDesc{ { diff --git a/walletrpc/service.proto b/walletrpc/service.proto index 322e56b..b5e0086 100644 --- a/walletrpc/service.proto +++ b/walletrpc/service.proto @@ -56,6 +56,14 @@ message LightdInfo { uint64 longestchain = 9; uint64 notarized = 10; } +message Coinsupply { + string result = 1; + string coin = 2; + uint64 height = 3; + uint64 supply = 4; + uint64 zfunds = 5; + uint64 total = 6; +} message TransparentAddress { string address = 1; @@ -81,4 +89,5 @@ service CompactTxStreamer { // Misc rpc GetLightdInfo(Empty) returns (LightdInfo) {} + rpc GetCoinsupply(Empty) returns (Coinsupply) {} }