Skip to content

Commit

Permalink
Update mcip_service.go
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 7aba246 commit 4d9819e
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions chain/mcip/advanced/mcip_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"sync"

"github.com/KOSASIH/pi-nexus-autonomous-banking-network/chain/mcip/pb"
"google.golang.org/grpc"
Expand All @@ -13,7 +14,12 @@ import (

type MCIPService struct {
pb.UnimplementedMCIPServer
chainMap map[string]*Chain
chainMap map[string]*Chain
chainLock sync.RWMutex
txPool map[string][]*types.Transaction
txPoolLock sync.RWMutex
blockchain *Blockchain
blockchainLock sync.RWMutex
}

type Chain struct {
Expand All @@ -23,6 +29,11 @@ type Chain struct {
Genesis *types.Block
}

type Blockchain struct {
chainID string
chain *types.Blockchain
}

func (s *MCIPService) RegisterChain(ctx context.Context, req *pb.RegisterChainRequest) (*pb.RegisterChainResponse, error) {
// Create new chain
chain := &Chain{
Expand All @@ -33,14 +44,18 @@ func (s *MCIPService) RegisterChain(ctx context.Context, req *pb.RegisterChainRe
}

// Store chain
s.chainLock.Lock()
s.chainMap[req.ChainID] = chain
s.chainLock.Unlock()

return &pb.RegisterChainResponse{Result: "success"}, nil
}

func (s *MCIPService) GetChain(ctx context.Context, req *pb.GetChainRequest) (*pb.GetChainResponse, error) {
// Retrieve chain from map
s.chainLock.RLock()
chain, ok := s.chainMap[req.ChainID]
s.chainLock.RUnlock()
if !ok {
return nil, fmt.Errorf("chain not found")
}
Expand All @@ -50,29 +65,60 @@ func (s *MCIPService) GetChain(ctx context.Context, req *pb.GetChainRequest) (*p

func (s *MCIPService) CrossChainTransfer(ctx context.Context, req *pb.CrossChainTransferRequest) (*pb.CrossChainTransferResponse, error) {
// Get source and destination chains
s.chainLock.RLock()
srcChain, ok := s.chainMap[req.SourceChainID]
if !ok {
return nil, fmt.Errorf("source chain not found")
}
dstChain, ok := s.chainMap[req.DestinationChainID]
s.chainLock.RUnlock()
if !ok {
return nil, fmt.Errorf("destination chain not found")
return nil, fmt.Errorf("chain not found")
}

// Perform cross-chain transfer logic
// ...
tx := &types.Transaction{
From: req.From,
To: req.To,
Value: req.Value,
Gas: req.Gas,
GasPrice: req.GasPrice,
}

s.txPoolLock.Lock()
s.txPool[req.SourceChainID] = append(s.txPool[req.SourceChainID], tx)
s.txPoolLock.Unlock()

return &pb.CrossChainTransferResponse{Result: "success"}, nil
}

func (s *MCIPService) MineBlock(ctx context.Context, req *pb.MineBlockRequest) (*pb.MineBlockResponse, error) {
// Get chain
s.chainLock.RLock()
chain, ok := s.chainMap[req.ChainID]
s.chainLock.RUnlock()
if !ok {
return nil, fmt.Errorf("chain not found")
}

// Mine block
block, err := s.blockchain.MineBlock(chain, req.Transactions)
if err != nil {
return nil, err
}

return &pb.MineBlockResponse{Block: block}, nil
}

func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

srv := grpc.NewServer()
pb.RegisterMCIPServer(srv, &MCIPService{chainMap: make(map[string]*Chain)})
pb.RegisterMCIPServer(srv, &MCIPService{
chainMap: make(map[string]*Chain),
txPool: make(map[string][]*types.Transaction),
blockchain: &Blockchain{},
})

log.Println("MCIP service listening on port 50051")
srv.Serve(lis)
Expand Down

0 comments on commit 4d9819e

Please sign in to comment.