2023-10-09 04:52:19 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-10-12 18:55:17 +00:00
|
|
|
"math/big"
|
2023-10-09 04:52:19 +00:00
|
|
|
"net/http"
|
2024-10-14 01:37:19 +00:00
|
|
|
"strings"
|
2024-05-27 05:10:15 +00:00
|
|
|
|
2024-03-24 08:11:58 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/config"
|
2024-10-14 01:37:19 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/token/application"
|
2023-10-09 04:52:19 +00:00
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
"github.com/iden3/go-iden3-crypto/poseidon"
|
2024-02-24 08:35:13 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
mn "github.com/multiformats/go-multiaddr/net"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/grpc/reflection"
|
2024-10-14 01:37:19 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2024-03-04 03:20:24 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/consensus/master"
|
2023-10-09 04:52:19 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution"
|
2023-10-28 02:23:55 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/keys"
|
2023-10-09 04:52:19 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RPCServer struct {
|
|
|
|
protobufs.UnimplementedNodeServiceServer
|
|
|
|
listenAddrGRPC string
|
|
|
|
listenAddrHTTP string
|
|
|
|
logger *zap.Logger
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProofStore store.DataProofStore
|
2023-10-09 04:52:19 +00:00
|
|
|
clockStore store.ClockStore
|
2024-10-12 18:55:17 +00:00
|
|
|
coinStore store.CoinStore
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager keys.KeyManager
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub p2p.PubSub
|
2024-03-04 03:20:24 +00:00
|
|
|
masterClock *master.MasterClockConsensusEngine
|
2023-10-09 04:52:19 +00:00
|
|
|
executionEngines []execution.ExecutionEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFrameInfo implements protobufs.NodeServiceServer.
|
|
|
|
func (r *RPCServer) GetFrameInfo(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetFrameInfoRequest,
|
|
|
|
) (*protobufs.FrameInfoResponse, error) {
|
2024-10-12 18:55:17 +00:00
|
|
|
if bytes.Equal(req.Filter, make([]byte, 32)) {
|
2023-10-09 04:52:19 +00:00
|
|
|
frame, err := r.clockStore.GetMasterClockFrame(
|
|
|
|
req.Filter,
|
|
|
|
req.FrameNumber,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frame info")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protobufs.FrameInfoResponse{
|
|
|
|
ClockFrame: frame,
|
|
|
|
}, nil
|
|
|
|
} else if req.Selector == nil {
|
|
|
|
frame, _, err := r.clockStore.GetDataClockFrame(
|
|
|
|
req.Filter,
|
|
|
|
req.FrameNumber,
|
2024-02-24 08:35:13 +00:00
|
|
|
false,
|
2023-10-09 04:52:19 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frame info")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protobufs.FrameInfoResponse{
|
|
|
|
ClockFrame: frame,
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.Wrap(errors.New("not found"), "get frame info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFrames implements protobufs.NodeServiceServer.
|
|
|
|
func (r *RPCServer) GetFrames(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetFramesRequest,
|
|
|
|
) (*protobufs.FramesResponse, error) {
|
2024-10-12 18:55:17 +00:00
|
|
|
if bytes.Equal(req.Filter, make([]byte, 32)) {
|
2023-10-09 04:52:19 +00:00
|
|
|
iter, err := r.clockStore.RangeMasterClockFrames(
|
|
|
|
req.Filter,
|
|
|
|
req.FromFrameNumber,
|
|
|
|
req.ToFrameNumber,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frames")
|
|
|
|
}
|
|
|
|
|
|
|
|
frames := []*protobufs.ClockFrame{}
|
|
|
|
for iter.First(); iter.Valid(); iter.Next() {
|
|
|
|
frame, err := iter.Value()
|
|
|
|
if err != nil {
|
|
|
|
iter.Close()
|
|
|
|
return nil, errors.Wrap(err, "get frames")
|
|
|
|
}
|
|
|
|
frames = append(frames, frame)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frames")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protobufs.FramesResponse{
|
|
|
|
TruncatedClockFrames: frames,
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
iter, err := r.clockStore.RangeDataClockFrames(
|
|
|
|
req.Filter,
|
|
|
|
req.FromFrameNumber,
|
|
|
|
req.ToFrameNumber,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frame info")
|
|
|
|
}
|
|
|
|
|
|
|
|
frames := []*protobufs.ClockFrame{}
|
|
|
|
for iter.First(); iter.Valid(); iter.Next() {
|
|
|
|
frame, err := iter.TruncatedValue()
|
|
|
|
if err != nil {
|
|
|
|
iter.Close()
|
|
|
|
return nil, errors.Wrap(err, "get frames")
|
|
|
|
}
|
|
|
|
frames = append(frames, frame)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get frames")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protobufs.FramesResponse{
|
|
|
|
TruncatedClockFrames: frames,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkInfo implements protobufs.NodeServiceServer.
|
|
|
|
func (r *RPCServer) GetNetworkInfo(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetNetworkInfoRequest,
|
|
|
|
) (*protobufs.NetworkInfoResponse, error) {
|
|
|
|
return r.pubSub.GetNetworkInfo(), nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 07:00:50 +00:00
|
|
|
// GetNodeInfo implements protobufs.NodeServiceServer.
|
|
|
|
func (r *RPCServer) GetNodeInfo(
|
2024-01-29 21:09:48 +00:00
|
|
|
ctx context.Context,
|
2024-02-13 07:00:50 +00:00
|
|
|
req *protobufs.GetNodeInfoRequest,
|
|
|
|
) (*protobufs.NodeInfoResponse, error) {
|
2024-01-29 21:09:48 +00:00
|
|
|
peerID, err := peer.IDFromBytes(r.pubSub.GetPeerID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting id from bytes")
|
|
|
|
}
|
2024-02-13 07:00:50 +00:00
|
|
|
peerScore := r.pubSub.GetPeerScore(r.pubSub.GetPeerID())
|
|
|
|
|
2024-05-27 05:10:15 +00:00
|
|
|
return &protobufs.NodeInfoResponse{
|
|
|
|
PeerId: peerID.String(),
|
|
|
|
MaxFrame: r.masterClock.GetFrame().GetFrameNumber(),
|
|
|
|
PeerScore: uint64(peerScore),
|
2024-06-21 17:46:36 +00:00
|
|
|
Version: append(
|
|
|
|
append([]byte{}, config.GetVersion()...), config.GetPatchNumber(),
|
|
|
|
),
|
2024-05-27 05:10:15 +00:00
|
|
|
}, nil
|
2024-01-29 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
// GetPeerInfo implements protobufs.NodeServiceServer.
|
|
|
|
func (r *RPCServer) GetPeerInfo(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetPeerInfoRequest,
|
|
|
|
) (*protobufs.PeerInfoResponse, error) {
|
|
|
|
resp := &protobufs.PeerInfoResponse{}
|
2024-05-27 05:10:15 +00:00
|
|
|
manifests := r.masterClock.GetPeerManifests()
|
|
|
|
for _, m := range manifests.PeerManifests {
|
|
|
|
multiaddr := r.pubSub.GetMultiaddrOfPeer(m.PeerId)
|
|
|
|
addrs := []string{}
|
|
|
|
if multiaddr != "" {
|
|
|
|
addrs = append(addrs, multiaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.PeerInfo = append(resp.PeerInfo, &protobufs.PeerInfo{
|
|
|
|
PeerId: m.PeerId,
|
|
|
|
Multiaddrs: addrs,
|
|
|
|
MaxFrame: m.MasterHeadFrame,
|
|
|
|
Timestamp: m.LastSeen,
|
|
|
|
// We can get away with this for this release only, we will want to add
|
|
|
|
// version info in manifests.
|
|
|
|
Version: config.GetVersion(),
|
|
|
|
})
|
2023-10-09 04:52:19 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
func (r *RPCServer) SendMessage(
|
2023-10-28 02:23:55 +00:00
|
|
|
ctx context.Context,
|
2024-10-14 01:37:19 +00:00
|
|
|
req *protobufs.TokenRequest,
|
|
|
|
) (*protobufs.SendMessageResponse, error) {
|
|
|
|
any := &anypb.Any{}
|
|
|
|
if err := any.MarshalFrom(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "publish message")
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
// annoying protobuf any hack
|
|
|
|
any.TypeUrl = strings.Replace(
|
|
|
|
any.TypeUrl,
|
|
|
|
"type.googleapis.com",
|
|
|
|
"types.quilibrium.com",
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(any)
|
2024-10-12 18:55:17 +00:00
|
|
|
if err != nil {
|
2024-10-14 01:37:19 +00:00
|
|
|
return nil, errors.Wrap(err, "publish message")
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
h, err := poseidon.HashBytes(payload)
|
2024-10-12 18:55:17 +00:00
|
|
|
if err != nil {
|
2024-10-14 01:37:19 +00:00
|
|
|
return nil, errors.Wrap(err, "publish message")
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
intrinsicFilter := p2p.GetBloomFilter(application.TOKEN_ADDRESS, 256, 3)
|
2023-10-28 02:23:55 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
msg := &protobufs.Message{
|
|
|
|
Hash: h.Bytes(),
|
|
|
|
Address: intrinsicFilter,
|
|
|
|
Payload: payload,
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
2024-10-14 01:37:19 +00:00
|
|
|
data, err := proto.Marshal(msg)
|
2024-10-12 18:55:17 +00:00
|
|
|
if err != nil {
|
2024-10-14 01:37:19 +00:00
|
|
|
return nil, errors.Wrap(err, "publish message")
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
2024-10-14 01:37:19 +00:00
|
|
|
return &protobufs.SendMessageResponse{}, r.pubSub.PublishToBitmask(
|
|
|
|
intrinsicFilter,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
}
|
2024-01-03 08:13:51 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
func (r *RPCServer) GetTokensByAccount(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetTokensByAccountRequest,
|
|
|
|
) (*protobufs.TokensByAccountResponse, error) {
|
|
|
|
frameNumbers, coins, err := r.coinStore.GetCoinsForOwner(req.Address)
|
2023-10-28 02:23:55 +00:00
|
|
|
if err != nil {
|
2024-10-14 01:37:19 +00:00
|
|
|
return nil, err
|
2023-10-28 02:23:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
return &protobufs.TokensByAccountResponse{
|
|
|
|
Coins: coins,
|
|
|
|
FrameNumbers: frameNumbers,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-10-12 18:55:17 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
func (r *RPCServer) GetTokenInfo(
|
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetTokenInfoRequest,
|
|
|
|
) (*protobufs.TokenInfoResponse, error) {
|
|
|
|
// 1 QUIL = 0x1DCD65000 units
|
|
|
|
if req.Address != nil {
|
|
|
|
_, coins, err := r.coinStore.GetCoinsForOwner(req.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("no coins found for address")
|
|
|
|
}
|
2024-10-12 18:55:17 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
total := big.NewInt(0)
|
|
|
|
for _, coin := range coins {
|
|
|
|
total.Add(total, new(big.Int).SetBytes(coin.Amount))
|
|
|
|
}
|
2023-10-28 02:23:55 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
return &protobufs.TokenInfoResponse{
|
|
|
|
OwnedTokens: total.FillBytes(make([]byte, 32)),
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
provingKey, err := r.keyManager.GetRawKey(
|
|
|
|
"default-proving-key",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get token info")
|
|
|
|
}
|
|
|
|
|
|
|
|
peerBytes := r.pubSub.GetPeerID()
|
|
|
|
peerAddr, err := poseidon.HashBytes(peerBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := poseidon.HashBytes(provingKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrBytes := addr.FillBytes(make([]byte, 32))
|
|
|
|
peerAddrBytes := peerAddr.FillBytes(make([]byte, 32))
|
|
|
|
|
|
|
|
_, coins, err := r.coinStore.GetCoinsForOwner(addrBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, otherCoins, err := r.coinStore.GetCoinsForOwner(peerAddrBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
total := big.NewInt(0)
|
|
|
|
for _, coin := range coins {
|
|
|
|
total.Add(total, new(big.Int).SetBytes(coin.Amount))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coin := range otherCoins {
|
|
|
|
total.Add(total, new(big.Int).SetBytes(coin.Amount))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protobufs.TokenInfoResponse{
|
|
|
|
OwnedTokens: total.FillBytes(make([]byte, 32)),
|
|
|
|
}, nil
|
|
|
|
}
|
2023-10-28 02:23:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 05:05:04 +00:00
|
|
|
func (r *RPCServer) GetPeerManifests(
|
2024-03-04 03:20:24 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *protobufs.GetPeerManifestsRequest,
|
|
|
|
) (*protobufs.PeerManifestsResponse, error) {
|
|
|
|
return r.masterClock.GetPeerManifests(), nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
func NewRPCServer(
|
|
|
|
listenAddrGRPC string,
|
|
|
|
listenAddrHTTP string,
|
|
|
|
logger *zap.Logger,
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProofStore store.DataProofStore,
|
2023-10-09 04:52:19 +00:00
|
|
|
clockStore store.ClockStore,
|
2024-10-14 21:41:40 +00:00
|
|
|
coinStore store.CoinStore,
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager keys.KeyManager,
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub p2p.PubSub,
|
2024-03-04 03:20:24 +00:00
|
|
|
masterClock *master.MasterClockConsensusEngine,
|
2023-10-09 04:52:19 +00:00
|
|
|
executionEngines []execution.ExecutionEngine,
|
|
|
|
) (*RPCServer, error) {
|
|
|
|
return &RPCServer{
|
|
|
|
listenAddrGRPC: listenAddrGRPC,
|
|
|
|
listenAddrHTTP: listenAddrHTTP,
|
|
|
|
logger: logger,
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProofStore: dataProofStore,
|
2023-10-09 04:52:19 +00:00
|
|
|
clockStore: clockStore,
|
2024-10-14 21:41:40 +00:00
|
|
|
coinStore: coinStore,
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager: keyManager,
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub: pubSub,
|
2024-03-04 03:20:24 +00:00
|
|
|
masterClock: masterClock,
|
2023-10-09 04:52:19 +00:00
|
|
|
executionEngines: executionEngines,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPCServer) Start() error {
|
|
|
|
s := grpc.NewServer(
|
2023-10-27 00:19:19 +00:00
|
|
|
grpc.MaxRecvMsgSize(600*1024*1024),
|
|
|
|
grpc.MaxSendMsgSize(600*1024*1024),
|
2023-10-09 04:52:19 +00:00
|
|
|
)
|
|
|
|
protobufs.RegisterNodeServiceServer(s, r)
|
|
|
|
reflection.Register(s)
|
|
|
|
|
|
|
|
mg, err := multiaddr.NewMultiaddr(r.listenAddrGRPC)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := mn.Listen(mg)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(mn.NetListener(lis)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if r.listenAddrHTTP != "" {
|
|
|
|
m, err := multiaddr.NewMultiaddr(r.listenAddrHTTP)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
ma, err := mn.ToNetAddr(m)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
mga, err := mn.ToNetAddr(mg)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
mux := runtime.NewServeMux()
|
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
grpc.WithDefaultCallOptions(
|
2023-10-27 00:19:19 +00:00
|
|
|
grpc.MaxCallRecvMsgSize(600*1024*1024),
|
|
|
|
grpc.MaxCallSendMsgSize(600*1024*1024),
|
2023-10-09 04:52:19 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := protobufs.RegisterNodeServiceHandlerFromEndpoint(
|
|
|
|
context.Background(),
|
|
|
|
mux,
|
|
|
|
mga.String(),
|
|
|
|
opts,
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(ma.String(), mux); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|