github.com/MetalBlockchain/metalgo@v1.11.9/chains/atomic/gsharedmemory/shared_memory_client.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package gsharedmemory
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/chains/atomic"
    10  	"github.com/MetalBlockchain/metalgo/database"
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  
    13  	sharedmemorypb "github.com/MetalBlockchain/metalgo/proto/pb/sharedmemory"
    14  )
    15  
    16  var _ atomic.SharedMemory = (*Client)(nil)
    17  
    18  // Client is atomic.SharedMemory that talks over RPC.
    19  type Client struct {
    20  	client sharedmemorypb.SharedMemoryClient
    21  }
    22  
    23  // NewClient returns shared memory connected to remote shared memory
    24  func NewClient(client sharedmemorypb.SharedMemoryClient) *Client {
    25  	return &Client{client: client}
    26  }
    27  
    28  func (c *Client) Get(peerChainID ids.ID, keys [][]byte) ([][]byte, error) {
    29  	resp, err := c.client.Get(context.Background(), &sharedmemorypb.GetRequest{
    30  		PeerChainId: peerChainID[:],
    31  		Keys:        keys,
    32  	})
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return resp.Values, nil
    37  }
    38  
    39  func (c *Client) Indexed(
    40  	peerChainID ids.ID,
    41  	traits [][]byte,
    42  	startTrait,
    43  	startKey []byte,
    44  	limit int,
    45  ) (
    46  	[][]byte,
    47  	[]byte,
    48  	[]byte,
    49  	error,
    50  ) {
    51  	resp, err := c.client.Indexed(context.Background(), &sharedmemorypb.IndexedRequest{
    52  		PeerChainId: peerChainID[:],
    53  		Traits:      traits,
    54  		StartTrait:  startTrait,
    55  		StartKey:    startKey,
    56  		Limit:       int32(limit),
    57  	})
    58  	if err != nil {
    59  		return nil, nil, nil, err
    60  	}
    61  	return resp.Values, resp.LastTrait, resp.LastKey, nil
    62  }
    63  
    64  func (c *Client) Apply(requests map[ids.ID]*atomic.Requests, batches ...database.Batch) error {
    65  	req := &sharedmemorypb.ApplyRequest{
    66  		Requests: make([]*sharedmemorypb.AtomicRequest, 0, len(requests)),
    67  		Batches:  make([]*sharedmemorypb.Batch, len(batches)),
    68  	}
    69  	for key, value := range requests {
    70  		key := key
    71  
    72  		chainReq := &sharedmemorypb.AtomicRequest{
    73  			RemoveRequests: value.RemoveRequests,
    74  			PutRequests:    make([]*sharedmemorypb.Element, len(value.PutRequests)),
    75  			PeerChainId:    key[:],
    76  		}
    77  		for i, v := range value.PutRequests {
    78  			chainReq.PutRequests[i] = &sharedmemorypb.Element{
    79  				Key:    v.Key,
    80  				Value:  v.Value,
    81  				Traits: v.Traits,
    82  			}
    83  		}
    84  		req.Requests = append(req.Requests, chainReq)
    85  	}
    86  	for i, batch := range batches {
    87  		batch := batch.Inner()
    88  		fb := filteredBatch{
    89  			writes: make(map[string][]byte),
    90  		}
    91  		if err := batch.Replay(&fb); err != nil {
    92  			return err
    93  		}
    94  		req.Batches[i] = &sharedmemorypb.Batch{
    95  			Puts:    fb.PutRequests(),
    96  			Deletes: fb.DeleteRequests(),
    97  		}
    98  	}
    99  
   100  	_, err := c.client.Apply(context.Background(), req)
   101  	return err
   102  }