dione/node/network_service.go

96 lines
2.5 KiB
Go
Raw Normal View History

package node
import (
"context"
2021-06-02 19:45:55 +00:00
"errors"
"fmt"
2021-06-04 21:18:06 +00:00
"github.com/Secured-Finance/dione/blockchain"
2021-06-02 19:45:55 +00:00
gorpc "github.com/libp2p/go-libp2p-gorpc"
"github.com/sirupsen/logrus"
2021-06-02 19:45:55 +00:00
"github.com/Secured-Finance/dione/consensus/policy"
"github.com/Secured-Finance/dione/node/wire"
"github.com/Secured-Finance/dione/blockchain/pool"
)
type NetworkService struct {
2021-07-11 00:32:58 +00:00
blockchain *blockchain.BlockChain
mempool *pool.Mempool
rpcClient *gorpc.Client
}
2021-07-11 00:32:58 +00:00
func NewNetworkService(bc *blockchain.BlockChain, mp *pool.Mempool) *NetworkService {
return &NetworkService{
2021-07-11 00:32:58 +00:00
blockchain: bc,
mempool: mp,
}
}
2021-07-11 00:32:58 +00:00
func (s *NetworkService) LastBlockHeight(ctx context.Context, arg struct{}, reply *wire.LastBlockHeightReply) error {
height, err := s.blockchain.GetLatestBlockHeight()
if err != nil {
2021-07-11 00:32:58 +00:00
return err
}
reply.Height = height
2021-07-11 00:32:58 +00:00
return nil
}
2021-07-11 00:32:58 +00:00
func (s *NetworkService) GetRangeOfBlocks(ctx context.Context, arg wire.GetRangeOfBlocksArg, reply *wire.GetRangeOfBlocksReply) error {
if arg.From > arg.To {
2021-07-11 00:32:58 +00:00
return fmt.Errorf("incorrect arguments: from > to")
}
2021-06-02 19:45:55 +00:00
if arg.To-arg.From > policy.MaxBlockCountForRetrieving {
2021-07-11 00:32:58 +00:00
return fmt.Errorf("incorrect arguments: count of block for retrieving is exceeded the limit")
}
for i := arg.From; i <= arg.To; i++ {
2021-07-11 00:32:58 +00:00
block, err := s.blockchain.FetchBlockByHeight(i)
if err != nil {
logrus.Warnf("failed to retrieve block from blockpool with height %d", i)
reply.FailedBlockHeights = append(reply.FailedBlockHeights, i)
continue
}
reply.Blocks = append(reply.Blocks, *block)
}
2021-07-11 00:32:58 +00:00
return nil
}
2021-06-02 19:45:55 +00:00
2021-07-11 00:32:58 +00:00
func (s *NetworkService) Mempool(ctx context.Context, arg struct{}, reply *wire.InvMessage) error {
2021-06-02 19:45:55 +00:00
txs := s.mempool.GetAllTransactions()
// extract hashes of txs
for _, v := range txs {
reply.Inventory = append(reply.Inventory, wire.InvItem{
Type: wire.TxInvType,
Hash: v.Hash,
})
}
2021-07-11 00:32:58 +00:00
return nil
2021-06-02 19:45:55 +00:00
}
2021-07-11 00:32:58 +00:00
func (s *NetworkService) GetMempoolTxs(ctx context.Context, arg wire.GetMempoolTxsArg, reply *wire.GetMempoolTxsReply) error {
2021-07-10 22:07:04 +00:00
if len(arg.Items) > policy.MaxTransactionCountForRetrieving {
2021-06-02 19:45:55 +00:00
pid, _ := gorpc.GetRequestSender(ctx)
logrus.Warnf("Max tx count limit exceeded for GetMempoolTxs request of node %s", pid)
2021-07-11 00:32:58 +00:00
return fmt.Errorf("max tx count limit exceeded")
2021-06-02 19:45:55 +00:00
}
for _, v := range arg.Items {
tx, err := s.mempool.GetTransaction(v)
if err != nil {
if errors.Is(err, pool.ErrTxNotFound) {
reply.NotFoundTxs = append(reply.NotFoundTxs, v)
} else {
2021-07-11 00:32:58 +00:00
return err
2021-06-02 19:45:55 +00:00
}
}
reply.Transactions = append(reply.Transactions, *tx)
}
2021-07-11 00:32:58 +00:00
return nil
2021-06-02 19:45:55 +00:00
}