diff --git a/.circleci/config.yml b/.circleci/config.yml index 4bb92faa..4299d4a5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -55,19 +55,28 @@ jobs: command: | make lotus-images - - run: make itest-poc1 - - run: make itest-poc2js +# - run: make itest-poc1 +# - run: make itest-poc2js +# +# - run: docker build -t consensys/fc-retrieval/itest -f itest/Dockerfile . - - run: docker build -t consensys/fc-retrieval/itest -f itest/Dockerfile . + ## debugging + - run: docker build -t consensys/fc-retrieval/gateway -f gateway/Dockerfile . + - run: docker build -t consensys/fc-retrieval/provider -f provider/Dockerfile . + - run: docker build -t consensys/fc-retrieval/register -f register/Dockerfile . + - run: sudo sh -c 'cat itest/.hosts >> /etc/hosts' + - run: cat /etc/hosts - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/lotus-full-node - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/client-gateway - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/client-init - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/provider-admin - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc1 + - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_group_offer + - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_new_gateway + - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_dht_offer - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_dht_offer_ack - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_dht_offer_new_gateway - - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2_new_gateway - run: cd itest; go test -p 1 -count=1 -v github.com/ConsenSys/fc-retrieval/itest/pkg/poc2js - run: diff --git a/client/pkg/api/clientapi/client_api.go b/client/pkg/api/clientapi/client_api.go index edbf257a..85dd080a 100644 --- a/client/pkg/api/clientapi/client_api.go +++ b/client/pkg/api/clientapi/client_api.go @@ -15,6 +15,7 @@ type Client struct { type ClientApi interface { RequestDHTOfferDiscover( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, gatewayIDs []nodeid.NodeID, contentID *cid.ContentID, @@ -25,6 +26,7 @@ type ClientApi interface { ) ([]GatewaySubOffers, error) RequestDHTDiscover( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -36,6 +38,7 @@ type ClientApi interface { ) ([]nodeid.NodeID, []fcrmessages.FCRMessage, []nodeid.NodeID, error) RequestDHTDiscoverV2( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -47,12 +50,14 @@ type ClientApi interface { ) ([]nodeid.NodeID, []fcrmessages.FCRMessage, []nodeid.NodeID, bool, string, error) RequestDHTOfferAck( + clientApiEndpoint string, providerInfo register.ProviderRegistrar, contentID *cid.ContentID, gatewayID *nodeid.NodeID, ) (bool, *fcrmessages.FCRMessage, *fcrmessages.FCRMessage, error) RequestEstablishment( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, challenge []byte, clientID *nodeid.NodeID, @@ -60,6 +65,7 @@ type ClientApi interface { ) error RequestStandardDiscoverOffer( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -70,6 +76,7 @@ type ClientApi interface { ) ([]cidoffer.SubCIDOffer, error) RequestStandardDiscover( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -79,6 +86,7 @@ type ClientApi interface { ) ([]cidoffer.SubCIDOffer, error) RequestStandardDiscoverV2( + clientApiEndpoint string, gatewayInfo register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, diff --git a/client/pkg/api/clientapi/dht_discover_offer_requester.go b/client/pkg/api/clientapi/dht_discover_offer_requester.go index d53d98a1..5506e97b 100644 --- a/client/pkg/api/clientapi/dht_discover_offer_requester.go +++ b/client/pkg/api/clientapi/dht_discover_offer_requester.go @@ -39,6 +39,7 @@ type GatewaySubOffers struct { } func (c *Client) RequestDHTOfferDiscover( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, gatewayIDs []nodeid.NodeID, contentID *cid.ContentID, @@ -54,7 +55,7 @@ func (c *Client) RequestDHTOfferDiscover( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, err } diff --git a/client/pkg/api/clientapi/dht_discover_requester.go b/client/pkg/api/clientapi/dht_discover_requester.go index 30eaa57f..331406a2 100644 --- a/client/pkg/api/clientapi/dht_discover_requester.go +++ b/client/pkg/api/clientapi/dht_discover_requester.go @@ -28,6 +28,7 @@ import ( // RequestDHTDiscover requests a dht discover to a given gateway for a given contentID, nonce and ttl. func (c *Client) RequestDHTDiscover( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -45,7 +46,7 @@ func (c *Client) RequestDHTDiscover( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, nil, nil, err } diff --git a/client/pkg/api/clientapi/dht_discover_requester_v2.go b/client/pkg/api/clientapi/dht_discover_requester_v2.go index 76e3a26e..4e4b3a32 100644 --- a/client/pkg/api/clientapi/dht_discover_requester_v2.go +++ b/client/pkg/api/clientapi/dht_discover_requester_v2.go @@ -26,6 +26,7 @@ import ( // RequestDHTDiscoverV2 requests a dht discover to a given gateway for a given contentID, nonce and ttl. func (c *Client) RequestDHTDiscoverV2( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -42,7 +43,7 @@ func (c *Client) RequestDHTDiscoverV2( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, nil, nil, false, "", fmt.Errorf("error sending DHT discover message to gateway ID: %s, error: %s", gatewayRegistrar.GetNodeID(), err.Error()) } diff --git a/client/pkg/api/clientapi/dht_offer_ack_requester.go b/client/pkg/api/clientapi/dht_offer_ack_requester.go index f6af6633..14132b6d 100644 --- a/client/pkg/api/clientapi/dht_offer_ack_requester.go +++ b/client/pkg/api/clientapi/dht_offer_ack_requester.go @@ -27,6 +27,7 @@ import ( // RequestDHTOfferAck requests a dht offer ack to a given provider for a pair of cid and gateway id func (c *Client) RequestDHTOfferAck( + clientApiEndpoint string, gatewayRegistrar register.ProviderRegistrar, contentID *cid.ContentID, gatewayID *nodeid.NodeID, @@ -39,7 +40,7 @@ func (c *Client) RequestDHTOfferAck( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return false, nil, nil, err } diff --git a/client/pkg/api/clientapi/establishment_requester.go b/client/pkg/api/clientapi/establishment_requester.go index 14c95fdb..2af33c4d 100644 --- a/client/pkg/api/clientapi/establishment_requester.go +++ b/client/pkg/api/clientapi/establishment_requester.go @@ -27,6 +27,7 @@ import ( // RequestEstablishment requests an establishment to a given gateway for a given challenge, client id and ttl. func (c *Client) RequestEstablishment( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, challenge []byte, clientID *nodeid.NodeID, @@ -45,7 +46,7 @@ func (c *Client) RequestEstablishment( return err } - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return err } diff --git a/client/pkg/api/clientapi/standard_discover_offer_requester.go b/client/pkg/api/clientapi/standard_discover_offer_requester.go index 926feb3a..b91e0af0 100644 --- a/client/pkg/api/clientapi/standard_discover_offer_requester.go +++ b/client/pkg/api/clientapi/standard_discover_offer_requester.go @@ -28,6 +28,7 @@ import ( // RequestStandardDiscoverOffer requests a standard discover to a given gateway for a given contentID, nonce and ttl. func (c *Client) RequestStandardDiscoverOffer( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -44,7 +45,7 @@ func (c *Client) RequestStandardDiscoverOffer( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, err } diff --git a/client/pkg/api/clientapi/standard_discover_requester.go b/client/pkg/api/clientapi/standard_discover_requester.go index fbe2ebb8..91dbb69b 100644 --- a/client/pkg/api/clientapi/standard_discover_requester.go +++ b/client/pkg/api/clientapi/standard_discover_requester.go @@ -27,6 +27,7 @@ import ( // RequestStandardDiscover requests a standard discover to a given gateway for a given contentID, nonce and ttl. func (c *Client) RequestStandardDiscover( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -42,7 +43,7 @@ func (c *Client) RequestStandardDiscover( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, err } diff --git a/client/pkg/api/clientapi/standard_discover_requester_v2.go b/client/pkg/api/clientapi/standard_discover_requester_v2.go index c3396000..ea5b5856 100644 --- a/client/pkg/api/clientapi/standard_discover_requester_v2.go +++ b/client/pkg/api/clientapi/standard_discover_requester_v2.go @@ -26,6 +26,7 @@ import ( // RequestStandardDiscoverV2 requests a standard discover to a given gateway for a given contentID, nonce and ttl. func (c *Client) RequestStandardDiscoverV2( + clientApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, contentID *cid.ContentID, nonce int64, @@ -41,7 +42,7 @@ func (c *Client) RequestStandardDiscoverV2( } // Send request and get response - response, err := c.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoClient(), request) + response, err := c.httpCommunicator.SendMessage(clientApiEndpoint, request) if err != nil { return nil, false, "", fmt.Errorf("error sending message to gateway ID: %s, error: %s", gatewayRegistrar.GetNodeID(), err.Error()) } diff --git a/client/pkg/fcrclient/filecoin_retrieval_client.go b/client/pkg/fcrclient/filecoin_retrieval_client.go index 23d0d915..fe416f16 100644 --- a/client/pkg/fcrclient/filecoin_retrieval_client.go +++ b/client/pkg/fcrclient/filecoin_retrieval_client.go @@ -210,7 +210,7 @@ func (c *FilecoinRetrievalClient) GetGatewaysToUse() []*nodeid.NodeID { // AddActiveGateways adds one or more gateways to active gateway map. // Returns the number of gateways added. -func (c *FilecoinRetrievalClient) AddActiveGateways(gwNodeIDs []*nodeid.NodeID) int { +func (c *FilecoinRetrievalClient) AddActiveGateways(clientApiEndpoint string, gwNodeIDs []*nodeid.NodeID) int { numAdded := 0 for _, gwToAddID := range gwNodeIDs { c.ActiveGatewaysLock.RLock() @@ -230,7 +230,7 @@ func (c *FilecoinRetrievalClient) AddActiveGateways(gwNodeIDs []*nodeid.NodeID) challenge := make([]byte, 32) rand.Read(challenge) ttl := time.Now().Unix() + c.Settings.EstablishmentTTL() - err := c.clientApi.RequestEstablishment(gatewayRegistrar, challenge, c.Settings.ClientID(), ttl) + err := c.clientApi.RequestEstablishment(clientApiEndpoint, gatewayRegistrar, challenge, c.Settings.ClientID(), ttl) if err != nil { logging.Error("Error in initial establishment: %v", err.Error()) continue @@ -291,7 +291,7 @@ func (c *FilecoinRetrievalClient) GetActiveGateways() []*nodeid.NodeID { } // FindOffersStandardDiscovery finds offer using standard discovery from given gateways -func (c *FilecoinRetrievalClient) FindOffersStandardDiscovery(contentID *cid.ContentID, gatewayID *nodeid.NodeID) ([]cidoffer.SubCIDOffer, error) { +func (c *FilecoinRetrievalClient) FindOffersStandardDiscovery(clientApiEndpoint string, contentID *cid.ContentID, gatewayID *nodeid.NodeID) ([]cidoffer.SubCIDOffer, error) { c.ActiveGatewaysLock.RLock() defer c.ActiveGatewaysLock.RUnlock() @@ -300,7 +300,7 @@ func (c *FilecoinRetrievalClient) FindOffersStandardDiscovery(contentID *cid.Con return make([]cidoffer.SubCIDOffer, 0), errors.New("given gatewayID is not in active nodes map") } // TODO need to do nonce management - offers, err := c.clientApi.RequestStandardDiscover(gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), "", "") + offers, err := c.clientApi.RequestStandardDiscover(clientApiEndpoint, gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), "", "") if err != nil { logging.Warn("GatewayStdDiscovery error. Gateway: %s, Error: %s", gw.GetNodeID(), err) return make([]cidoffer.SubCIDOffer, 0), errors.New("error in requesting standard discovery") @@ -340,7 +340,7 @@ func (c *FilecoinRetrievalClient) FindOffersStandardDiscovery(contentID *cid.Con } // FindOffersDHTDiscovery finds offer using dht discovery from given gateways -func (c *FilecoinRetrievalClient) FindOffersDHTDiscovery(contentID *cid.ContentID, gatewayID *nodeid.NodeID, numDHT int64) (map[string]*[]cidoffer.SubCIDOffer, error) { +func (c *FilecoinRetrievalClient) FindOffersDHTDiscovery(clientApiEndpoint string, contentID *cid.ContentID, gatewayID *nodeid.NodeID, numDHT int64) (map[string]*[]cidoffer.SubCIDOffer, error) { c.ActiveGatewaysLock.RLock() defer c.ActiveGatewaysLock.RUnlock() @@ -351,7 +351,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscovery(contentID *cid.ContentI return offersMap, errors.New("given gatewayID is not in active nodes map") } // TODO need to do nonce management - contacted, contactedResp, uncontactable, err := c.clientApi.RequestDHTDiscover(gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), numDHT, false, "", "") + contacted, contactedResp, uncontactable, err := c.clientApi.RequestDHTDiscover(clientApiEndpoint, gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), numDHT, false, "", "") if err != nil { logging.Warn("GatewayDHTDiscovery error. Gateway: %s, Error: %s", gw.GetNodeID(), err) return offersMap, errors.New("error in requesting dht discovery") @@ -428,7 +428,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscovery(contentID *cid.ContentI // FindOffersDHTDiscoveryV2 finds offer using dht discovery from given gateway with maximum number of offers // offersNumberLimit - maximum number of offers the client asking to have -func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(contentID *cid.ContentID, gatewayID *nodeid.NodeID, numDHT int64, offersNumberLimit int) (map[string]*[]cidoffer.SubCIDOffer, error) { +func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(clientApiEndpoint string, contentID *cid.ContentID, gatewayID *nodeid.NodeID, numDHT int64, offersNumberLimit int) (map[string]*[]cidoffer.SubCIDOffer, error) { offersMap := make(map[string]*[]cidoffer.SubCIDOffer) c.ActiveGatewaysLock.RLock() @@ -456,7 +456,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(contentID *cid.Conten // TODO need to do nonce management nonce := rand.Int63() ttl := time.Now().Unix() + c.Settings.EstablishmentTTL() - contactedGateways, contactedResp, uncontactable, paymentRequired, paymentChannel, discoverErr := c.clientApi.RequestDHTDiscoverV2(entryGateway, contentID, nonce, ttl, numDHT, false, paymentChannel, voucher) + contactedGateways, contactedResp, uncontactable, paymentRequired, paymentChannel, discoverErr := c.clientApi.RequestDHTDiscoverV2(clientApiEndpoint, entryGateway, contentID, nonce, ttl, numDHT, false, paymentChannel, voucher) if discoverErr != nil { logging.Warn("GatewayDHTDiscovery error. Gateway: %s, Error: %s", entryGateway.GetNodeID(), discoverErr) return nil, fmt.Errorf("error in requesting dht discovery: %s", discoverErr.Error()) @@ -467,7 +467,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(contentID *cid.Conten return nil, fmt.Errorf("topup error while requesting DHT discovery: %s", err.Error()) } // retry - contactedGateways, contactedResp, uncontactable, paymentRequired, paymentChannel, discoverErr = c.clientApi.RequestDHTDiscoverV2(entryGateway, contentID, nonce, ttl, numDHT, false, paymentChannel, voucher) + contactedGateways, contactedResp, uncontactable, paymentRequired, paymentChannel, discoverErr = c.clientApi.RequestDHTDiscoverV2(clientApiEndpoint, entryGateway, contentID, nonce, ttl, numDHT, false, paymentChannel, voucher) if discoverErr != nil { logging.Warn("gateway DHT discovery problem; gateway: %s, error: %s", entryGateway.GetNodeID(), discoverErr) return nil, fmt.Errorf("error in requesting dht discovery: %s", discoverErr.Error()) @@ -551,7 +551,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(contentID *cid.Conten } logging.Info("Successful initial payment for DHT offers discovery, payment channel: %s, voucher: %s", paymentChannel, voucher) - allGatewaysOffers, discoverError := c.clientApi.RequestDHTOfferDiscover(entryGateway, contactedGateways, contentID, nonce, offersDigestsFromAllGateways, paymentChannel, voucher) + allGatewaysOffers, discoverError := c.clientApi.RequestDHTOfferDiscover(clientApiEndpoint, entryGateway, contactedGateways, contentID, nonce, offersDigestsFromAllGateways, paymentChannel, voucher) if discoverError != nil { return nil, fmt.Errorf("error getting sub-offers from their digests: %s", discoverError) } @@ -563,7 +563,7 @@ func (c *FilecoinRetrievalClient) FindOffersDHTDiscoveryV2(contentID *cid.Conten } // FindDHTOfferAck finds offer ack for a cid, gateway pair -func (c *FilecoinRetrievalClient) FindDHTOfferAck(contentID *cid.ContentID, gatewayID *nodeid.NodeID, providerID *nodeid.NodeID) (bool, error) { +func (c *FilecoinRetrievalClient) FindDHTOfferAck(clientApiEndpoint string, contentID *cid.ContentID, gatewayID *nodeid.NodeID, providerID *nodeid.NodeID) (bool, error) { provider := c.registerMgr.GetProvider(providerID) if provider == nil { logging.Error("Error getting registered provider %v", providerID) @@ -574,7 +574,7 @@ func (c *FilecoinRetrievalClient) FindDHTOfferAck(contentID *cid.ContentID, gate return false, errors.New("invalid register info") } - found, request, ack, err := c.clientApi.RequestDHTOfferAck(provider, contentID, gatewayID) + found, request, ack, err := c.clientApi.RequestDHTOfferAck(clientApiEndpoint, provider, contentID, gatewayID) if err != nil { return false, err } @@ -644,7 +644,7 @@ func (c *FilecoinRetrievalClient) FindDHTOfferAck(contentID *cid.ContentID, gate } // FindOffersStandardDiscoveryV2 finds offer using standard discovery from given gateways -func (c *FilecoinRetrievalClient) FindOffersStandardDiscoveryV2(contentID *cid.ContentID, gatewayID *nodeid.NodeID, maxOffers int) ([]cidoffer.SubCIDOffer, error) { +func (c *FilecoinRetrievalClient) FindOffersStandardDiscoveryV2(clientApiEndpoint string, contentID *cid.ContentID, gatewayID *nodeid.NodeID, maxOffers int) ([]cidoffer.SubCIDOffer, error) { c.ActiveGatewaysLock.RLock() defer c.ActiveGatewaysLock.RUnlock() @@ -673,7 +673,7 @@ func (c *FilecoinRetrievalClient) FindOffersStandardDiscoveryV2(contentID *cid.C // It pays for the first request to get a list of offer digests. // TODO need to do nonce management - offerDigests, paymentRequired, _, discoverErr := c.clientApi.RequestStandardDiscoverV2(gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), paychAddr, voucher) + offerDigests, paymentRequired, _, discoverErr := c.clientApi.RequestStandardDiscoverV2(clientApiEndpoint, gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), paychAddr, voucher) if discoverErr != nil { return make([]cidoffer.SubCIDOffer, 0), fmt.Errorf("error getting offer from gateway: %s; error: %s", gw.GetNodeID(), discoverErr.Error()) } @@ -683,7 +683,7 @@ func (c *FilecoinRetrievalClient) FindOffersStandardDiscoveryV2(contentID *cid.C return nil, fmt.Errorf("topup error while requesting standard discovery: %s", err.Error()) } // retry - offerDigests, paymentRequired, _, err = c.clientApi.RequestStandardDiscoverV2(gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), paychAddr, voucher) + offerDigests, paymentRequired, _, err = c.clientApi.RequestStandardDiscoverV2(clientApiEndpoint, gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), paychAddr, voucher) if err != nil { return make([]cidoffer.SubCIDOffer, 0), fmt.Errorf("error getting offer from gateway: %s; error: %s", gw.GetNodeID(), err.Error()) } @@ -718,7 +718,7 @@ func (c *FilecoinRetrievalClient) FindOffersStandardDiscoveryV2(contentID *cid.C } } - offers, err := c.clientApi.RequestStandardDiscoverOffer(gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), offerDigests, paychAddr, voucher) + offers, err := c.clientApi.RequestStandardDiscoverOffer(clientApiEndpoint, gw, contentID, rand.Int63(), time.Now().Unix()+c.Settings.EstablishmentTTL(), offerDigests, paychAddr, voucher) var validOffers []cidoffer.SubCIDOffer // Verify the offer one by one diff --git a/common/pkg/cidoffer/cidoffer.go b/common/pkg/cidoffer/cidoffer.go index 60c4a2e6..3aef9cd3 100644 --- a/common/pkg/cidoffer/cidoffer.go +++ b/common/pkg/cidoffer/cidoffer.go @@ -26,13 +26,15 @@ import ( "encoding/binary" "encoding/json" "errors" + "fmt" "time" + "github.com/cbergoon/merkletree" + "github.com/ConsenSys/fc-retrieval/common/pkg/cid" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmerkletree" "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/cbergoon/merkletree" ) const CIDOfferDigestSize = sha512.Size256 @@ -159,14 +161,14 @@ func (c *CIDOffer) Sign(privKey *fcrcrypto.KeyPair, keyVer *fcrcrypto.KeyVersion func (c *CIDOffer) Verify(pubKey *fcrcrypto.KeyPair) error { raw, err := c.MarshalToSign() if err != nil { - return err + return fmt.Errorf("offer does not pass signature verification, marshaling error: %s", err) } res, err := fcrcrypto.VerifyMessage(pubKey, c.signature, raw) if err != nil { - return err + return fmt.Errorf("offer does not pass signature verification, error: %s", err) } if !res { - return errors.New("Offer does not pass signature verification") + return errors.New("offer does not pass signature verification") } return nil } diff --git a/common/pkg/cidoffer/subcidoffer.go b/common/pkg/cidoffer/subcidoffer.go index 478e0578..e42563ed 100644 --- a/common/pkg/cidoffer/subcidoffer.go +++ b/common/pkg/cidoffer/subcidoffer.go @@ -18,6 +18,7 @@ package cidoffer import ( "encoding/json" "errors" + "fmt" "time" "github.com/ConsenSys/fc-retrieval/common/pkg/cid" @@ -126,14 +127,14 @@ func (c *SubCIDOffer) HasExpired() bool { func (c *SubCIDOffer) Verify(pubKey *fcrcrypto.KeyPair) error { raw, err := c.MarshalToSign() if err != nil { - return err + return fmt.Errorf("offer does not pass signature verification, marshaling error: %s", err) } res, err := fcrcrypto.VerifyMessage(pubKey, c.signature, raw) if err != nil { - return err + return fmt.Errorf("offer does not pass signature verification, error: %s", err) } if !res { - return errors.New("Offer does not pass signature verification") + return errors.New("offer does not pass signature verification") } return nil } diff --git a/common/pkg/fcrcrypto/msg_signing.go b/common/pkg/fcrcrypto/msg_signing.go index 2fce9ef0..3387b525 100644 --- a/common/pkg/fcrcrypto/msg_signing.go +++ b/common/pkg/fcrcrypto/msg_signing.go @@ -3,6 +3,7 @@ package fcrcrypto import ( "encoding/hex" "errors" + "fmt" ) /** @@ -60,7 +61,7 @@ func ExtractKeyVersionFromMessage(signature string) (*KeyVersion, error) { func VerifyMessage(pubKey *KeyPair, signature string, msg []byte) (bool, error) { sigBytes, err := hex.DecodeString(signature) if err != nil { - return false, err + return false, fmt.Errorf("error decoding signature during message verification: %s", err.Error()) } if len(sigBytes) < sigOfsRawSig { diff --git a/gateway-admin/pkg/api/adminapi/admin_api.go b/gateway-admin/pkg/api/adminapi/admin_api.go index 65ca208a..53834ad5 100644 --- a/gateway-admin/pkg/api/adminapi/admin_api.go +++ b/gateway-admin/pkg/api/adminapi/admin_api.go @@ -1,79 +1,86 @@ package adminapi import ( - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" - "github.com/ConsenSys/fc-retrieval/common/pkg/request" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/request" ) type Admin struct { - httpCommunicator request.HttpCommunications + httpCommunicator request.HttpCommunications } type AdminApi interface { - RequestForceRefresh( - gatewayRegistrar register.GatewayRegistrar, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) error + RequestForceRefresh( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) error - RequestGetReputation( - gatewayRegistrar register.GatewayRegistrar, - clientID *nodeid.NodeID, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) (int64, error) + RequestGetReputation( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + clientID *nodeid.NodeID, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) (int64, error) - RequestInitialiseKey( - gatewayRegistrar register.GatewayRegistrar, - gatewayPrivKey *fcrcrypto.KeyPair, - gatewayPrivKeyVer *fcrcrypto.KeyVersion, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) error + RequestInitialiseKey( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + gatewayPrivKey *fcrcrypto.KeyPair, + gatewayPrivKeyVer *fcrcrypto.KeyVersion, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) error - RequestInitialiseKeyV2( - gatewayRegistrar register.GatewayRegistrar, - gatewayPrivKey *fcrcrypto.KeyPair, - gatewayPrivKeyVer *fcrcrypto.KeyVersion, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - lotusWalletPrivateKey string, - lotusAP string, - lotusAuthToken string, - ) error + RequestInitialiseKeyV2( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + gatewayPrivKey *fcrcrypto.KeyPair, + gatewayPrivKeyVer *fcrcrypto.KeyVersion, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + lotusWalletPrivateKey string, + lotusAP string, + lotusAuthToken string, + ) error - RequestListDHTOffer( - gatewayRegistrar register.GatewayRegistrar, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) error + RequestListDHTOffer( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) error - SetGroupCIDOfferSupportedForProviders( - gatewayRegistrar register.GatewayRegistrar, - providerIDs []nodeid.NodeID, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) error + SetGroupCIDOfferSupportedForProviders( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + providerIDs []nodeid.NodeID, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) error - RequestSetReputation( - gatewayRegistrar register.GatewayRegistrar, - clientID *nodeid.NodeID, - reputation int64, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, - ) (bool, error) + RequestSetReputation( + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + clientID *nodeid.NodeID, + reputation int64, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, + ) (bool, error) } func NewAdminApi() AdminApi { - return &Admin{ - httpCommunicator: request.NewHttpCommunicator(), - } + return &Admin{ + httpCommunicator: request.NewHttpCommunicator(), + } } func NewAdminApiWithDep(httpCommunicator request.HttpCommunications) AdminApi { - return &Admin{ - httpCommunicator: httpCommunicator, - } + return &Admin{ + httpCommunicator: httpCommunicator, + } } diff --git a/gateway-admin/pkg/api/adminapi/force_refresh_requester.go b/gateway-admin/pkg/api/adminapi/force_refresh_requester.go index 6cb15a69..06e8d07e 100644 --- a/gateway-admin/pkg/api/adminapi/force_refresh_requester.go +++ b/gateway-admin/pkg/api/adminapi/force_refresh_requester.go @@ -19,17 +19,18 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestForceRefresh forces a given gateway to refresh its internal register func (a *Admin) RequestForceRefresh( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion) error { // First, Get pubkey @@ -50,7 +51,7 @@ func (a *Admin) RequestForceRefresh( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/gateway-admin/pkg/api/adminapi/get_reputation_requester.go b/gateway-admin/pkg/api/adminapi/get_reputation_requester.go index a5c6923b..4a2f0dc7 100644 --- a/gateway-admin/pkg/api/adminapi/get_reputation_requester.go +++ b/gateway-admin/pkg/api/adminapi/get_reputation_requester.go @@ -16,18 +16,19 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestGetReputation gets the reputation for a given client id func (a *Admin) RequestGetReputation( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, clientID *nodeid.NodeID, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion, @@ -50,7 +51,7 @@ func (a *Admin) RequestGetReputation( return 0, errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return 0, err diff --git a/gateway-admin/pkg/api/adminapi/initialise_key_requester.go b/gateway-admin/pkg/api/adminapi/initialise_key_requester.go index e871ac86..25375782 100644 --- a/gateway-admin/pkg/api/adminapi/initialise_key_requester.go +++ b/gateway-admin/pkg/api/adminapi/initialise_key_requester.go @@ -16,45 +16,46 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestInitialiseKey initialise a given gateway func (a *Admin) RequestInitialiseKey( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, gatewayPrivKey *fcrcrypto.KeyPair, gatewayPrivKeyVer *fcrcrypto.KeyVersion, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion) error { - // First, Get pubkey + // First, Get pubkey pubKey, err := gatewayRegistrar.GetSigningKey() if err != nil { logging.Error("Error in obtaining signing key from register info.") return err } - nodeID, err := nodeid.NewNodeIDFromHexString(gatewayRegistrar.GetNodeID()) + nodeID, err := nodeid.NewNodeIDFromHexString(gatewayRegistrar.GetNodeID()) if err != nil { logging.Error("Error in generating nodeID.") return err } - // Second, send key exchange to activate the given gateway + // Second, send key exchange to activate the given gateway request, err := fcrmessages.EncodeGatewayAdminInitialiseKeyRequest(nodeID, gatewayPrivKey, gatewayPrivKeyVer) if err != nil { logging.Error("Error in encoding message.") return err } - // Sign the request + // Sign the request if request.Sign(signingPrivkey, signingPrivKeyVer) != nil { return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err @@ -64,12 +65,12 @@ func (a *Admin) RequestInitialiseKey( if response.Verify(pubKey) != nil { return errors.New("fail to verify the response") } - ok, err := fcrmessages.DecodeGatewayAdminInitialiseKeyResponse(response) + ok, err := fcrmessages.DecodeGatewayAdminInitialiseKeyResponse(response) if err != nil { logging.Error("Error in decoding the message.") return err } - if !ok { + if !ok { logging.Error("Initialise gateway failed.") return errors.New("fail to initialise gateway") } diff --git a/gateway-admin/pkg/api/adminapi/initialise_key_requester_v2.go b/gateway-admin/pkg/api/adminapi/initialise_key_requester_v2.go index fbe343e6..6da2157f 100644 --- a/gateway-admin/pkg/api/adminapi/initialise_key_requester_v2.go +++ b/gateway-admin/pkg/api/adminapi/initialise_key_requester_v2.go @@ -16,18 +16,19 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestInitialiseKeyV2 initialise a given gateway func (a *Admin) RequestInitialiseKeyV2( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, gatewayPrivKey *fcrcrypto.KeyPair, gatewayPrivKeyVer *fcrcrypto.KeyVersion, signingPrivkey *fcrcrypto.KeyPair, @@ -65,7 +66,7 @@ func (a *Admin) RequestInitialiseKeyV2( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/gateway-admin/pkg/api/adminapi/list_dht_offer_requester.go b/gateway-admin/pkg/api/adminapi/list_dht_offer_requester.go index 6e1e22f9..aad2de0a 100644 --- a/gateway-admin/pkg/api/adminapi/list_dht_offer_requester.go +++ b/gateway-admin/pkg/api/adminapi/list_dht_offer_requester.go @@ -16,17 +16,18 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestListDHTOffer asks a given gateway to list dht offer func (a *Admin) RequestListDHTOffer( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion) error { // First, Get pubkey @@ -47,7 +48,7 @@ func (a *Admin) RequestListDHTOffer( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/gateway-admin/pkg/api/adminapi/set_group_cid_support.go b/gateway-admin/pkg/api/adminapi/set_group_cid_support.go index e57b2353..94f31278 100644 --- a/gateway-admin/pkg/api/adminapi/set_group_cid_support.go +++ b/gateway-admin/pkg/api/adminapi/set_group_cid_support.go @@ -16,23 +16,24 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) func (a *Admin) SetGroupCIDOfferSupportedForProviders( - gatewayRegistrar register.GatewayRegistrar, - providerIDs []nodeid.NodeID, - signingPrivkey *fcrcrypto.KeyPair, - signingPrivKeyVer *fcrcrypto.KeyVersion, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, + providerIDs []nodeid.NodeID, + signingPrivkey *fcrcrypto.KeyPair, + signingPrivKeyVer *fcrcrypto.KeyVersion, ) error { - pubKey, err := gatewayRegistrar.GetSigningKey() + pubKey, err := gatewayRegistrar.GetSigningKey() if err != nil { logging.Error("Error in obtaining signing key from register info.") return err @@ -53,7 +54,7 @@ func (a *Admin) SetGroupCIDOfferSupportedForProviders( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/gateway-admin/pkg/api/adminapi/set_reputation_requester.go b/gateway-admin/pkg/api/adminapi/set_reputation_requester.go index 50b8d7af..becb853b 100644 --- a/gateway-admin/pkg/api/adminapi/set_reputation_requester.go +++ b/gateway-admin/pkg/api/adminapi/set_reputation_requester.go @@ -16,18 +16,19 @@ package adminapi */ import ( - "errors" + "errors" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" ) // RequestSetReputation sets the reputation for a given client id func (a *Admin) RequestSetReputation( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, clientID *nodeid.NodeID, reputation int64, signingPrivkey *fcrcrypto.KeyPair, @@ -51,7 +52,7 @@ func (a *Admin) RequestSetReputation( return false, errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(gatewayRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return false, err diff --git a/gateway-admin/pkg/fcrgatewayadmin/filecoin-gateway-admin.go b/gateway-admin/pkg/fcrgatewayadmin/filecoin-gateway-admin.go index 6a3d3de9..3803fe7b 100644 --- a/gateway-admin/pkg/fcrgatewayadmin/filecoin-gateway-admin.go +++ b/gateway-admin/pkg/fcrgatewayadmin/filecoin-gateway-admin.go @@ -16,14 +16,14 @@ package fcrgatewayadmin */ import ( - "errors" - "sync" - - "github.com/ConsenSys/fc-retrieval/common/pkg/cidoffer" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" - "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/api/adminapi" + "errors" + "sync" + + "github.com/ConsenSys/fc-retrieval/common/pkg/cidoffer" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/api/adminapi" ) // FilecoinRetrievalGatewayAdmin is an example implementation using the api, @@ -36,7 +36,7 @@ type FilecoinRetrievalGatewayAdmin struct { ActiveGateways map[string]register.GatewayRegistrar ActiveGatewaysLock sync.RWMutex - AdminApiCaller adminapi.AdminApi + AdminApiCaller adminapi.AdminApi } // NewFilecoinRetrievalGatewayAdmin initialise the Filecoin Retreival Gateway Admin library @@ -45,13 +45,13 @@ func NewFilecoinRetrievalGatewayAdmin(settings GatewayAdminSettings) *FilecoinRe Settings: settings, ActiveGateways: make(map[string]register.GatewayRegistrar), ActiveGatewaysLock: sync.RWMutex{}, - AdminApiCaller: adminapi.NewAdminApi(), + AdminApiCaller: adminapi.NewAdminApi(), } } // InitialiseGateway initialise a given gateway -func (c *FilecoinRetrievalGatewayAdmin) InitialiseGateway(gatewayRegistrar register.GatewayRegistrar, gatewayPrivKey *fcrcrypto.KeyPair, gatewayPrivKeyVer *fcrcrypto.KeyVersion) error { - err := c.AdminApiCaller.RequestInitialiseKey(gatewayRegistrar, gatewayPrivKey, gatewayPrivKeyVer, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) +func (c *FilecoinRetrievalGatewayAdmin) InitialiseGateway(adminApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, gatewayPrivKey *fcrcrypto.KeyPair, gatewayPrivKeyVer *fcrcrypto.KeyVersion) error { + err := c.AdminApiCaller.RequestInitialiseKey(adminApiEndpoint, gatewayRegistrar, gatewayPrivKey, gatewayPrivKeyVer, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) if err != nil { return err } @@ -59,13 +59,14 @@ func (c *FilecoinRetrievalGatewayAdmin) InitialiseGateway(gatewayRegistrar regis // Add this gateway to the active gateways list c.ActiveGatewaysLock.Lock() c.ActiveGateways[gatewayRegistrar.GetNodeID()] = gatewayRegistrar - c.ActiveGatewaysLock.Unlock() + c.ActiveGatewaysLock.Unlock() return nil } // InitialiseGatewayV2 initialise a given v2 gateway func (c *FilecoinRetrievalGatewayAdmin) InitialiseGatewayV2( - gatewayRegistrar register.GatewayRegistrar, + adminApiEndpoint string, + gatewayRegistrar register.GatewayRegistrar, gatewayPrivKey *fcrcrypto.KeyPair, gatewayPrivKeyVer *fcrcrypto.KeyVersion, lotusWalletPrivateKey string, @@ -73,7 +74,8 @@ func (c *FilecoinRetrievalGatewayAdmin) InitialiseGatewayV2( lotusAuthToken string, ) error { err := c.AdminApiCaller.RequestInitialiseKeyV2( - gatewayRegistrar, + adminApiEndpoint, + gatewayRegistrar, gatewayPrivKey, gatewayPrivKeyVer, c.Settings.gatewayAdminPrivateKey, @@ -109,31 +111,31 @@ func (c *FilecoinRetrievalGatewayAdmin) GetCIDOffersList() ([]cidoffer.CIDOffer, } // ForceUpdate forces the provider to update its internal register -func (c *FilecoinRetrievalGatewayAdmin) ForceUpdate(gatewayID *nodeid.NodeID) error { +func (c *FilecoinRetrievalGatewayAdmin) ForceUpdate(adminApiEndpoint string, gatewayID *nodeid.NodeID) error { c.ActiveGatewaysLock.RLock() defer c.ActiveGatewaysLock.RUnlock() - gatewayRegistrar, exists := c.ActiveGateways[gatewayID.ToString()] + gatewayRegistrar, exists := c.ActiveGateways[gatewayID.ToString()] if !exists { return errors.New("unable to find the gateway in admin storage") } - return c.AdminApiCaller.RequestForceRefresh(gatewayRegistrar, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) + return c.AdminApiCaller.RequestForceRefresh(adminApiEndpoint, gatewayRegistrar, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) } // ListDHTOffer asks the gateway to list dht offer from providers -func (c *FilecoinRetrievalGatewayAdmin) ListDHTOffer(gatewayID *nodeid.NodeID) error { +func (c *FilecoinRetrievalGatewayAdmin) ListDHTOffer(adminApiEndpoint string, gatewayID *nodeid.NodeID) error { c.ActiveGatewaysLock.RLock() defer c.ActiveGatewaysLock.RUnlock() - gatewayRegistrar, exists := c.ActiveGateways[gatewayID.ToString()] + gatewayRegistrar, exists := c.ActiveGateways[gatewayID.ToString()] if !exists { return errors.New("unable to find the gateway in admin storage") } - return c.AdminApiCaller.RequestListDHTOffer(gatewayRegistrar, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) + return c.AdminApiCaller.RequestListDHTOffer(adminApiEndpoint, gatewayRegistrar, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) } -func (c *FilecoinRetrievalGatewayAdmin) UpdateGatewaySupportedFeatures(gatewayRegistrar register.GatewayRegistrar, providers []nodeid.NodeID) error { +func (c *FilecoinRetrievalGatewayAdmin) UpdateGatewaySupportedFeatures(adminApiEndpoint string, gatewayRegistrar register.GatewayRegistrar, providers []nodeid.NodeID) error { c.ActiveGatewaysLock.Lock() defer c.ActiveGatewaysLock.Unlock() - err := c.AdminApiCaller.SetGroupCIDOfferSupportedForProviders(gatewayRegistrar, providers, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) + err := c.AdminApiCaller.SetGroupCIDOfferSupportedForProviders(adminApiEndpoint, gatewayRegistrar, providers, c.Settings.gatewayAdminPrivateKey, c.Settings.gatewayAdminPrivateKeyVer) if err != nil { return err } diff --git a/gateway/config/config.go b/gateway/config/config.go index 435b3993..610d985d 100644 --- a/gateway/config/config.go +++ b/gateway/config/config.go @@ -4,16 +4,16 @@ Package config - combines operations used to setup parameters for Gateway node i package config import ( - "flag" - "fmt" - "math/big" - "time" + "flag" + "fmt" + "math/big" + "time" - "github.com/spf13/pflag" - "github.com/spf13/viper" + "github.com/spf13/pflag" + "github.com/spf13/viper" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/gateway/internal/util/settings" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/gateway/internal/util/settings" ) // NewConfig creates a new configuration @@ -81,13 +81,13 @@ func Map(conf *viper.Viper) settings.AppSettings { RegisterRefreshDuration: registerRefreshDuration, GatewayAddress: conf.GetString("GATEWAY_ADDRESS"), - NetworkInfoGateway: conf.GetString("IP") + ":" + conf.GetString("BIND_GATEWAY_API"), GatewayRegionCode: conf.GetString("GATEWAY_REGION_CODE"), GatewayRootSigningKey: conf.GetString("GATEWAY_ROOT_SIGNING_KEY"), GatewaySigningKey: conf.GetString("GATEWAY_SIGNING_KEY"), NetworkInfoClient: conf.GetString("IP") + ":" + conf.GetString("BIND_REST_API"), NetworkInfoProvider: conf.GetString("IP") + ":" + conf.GetString("BIND_PROVIDER_API"), + NetworkInfoGateway: conf.GetString("IP") + ":" + conf.GetString("BIND_GATEWAY_API"), NetworkInfoAdmin: conf.GetString("IP") + ":" + conf.GetString("BIND_ADMIN_API"), TCPInactivityTimeout: tcpInactivityTimeout, @@ -99,7 +99,7 @@ func Map(conf *viper.Viper) settings.AppSettings { } } -func defineFlags(conf *viper.Viper) { +func defineFlags(_ *viper.Viper) { flag.String("host", "0.0.0.0", "help message for host") flag.String("ip", "127.0.0.1", "help message for ip") } @@ -108,8 +108,8 @@ func bindFlags(conf *viper.Viper) { pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() if err := conf.BindPFlags(pflag.CommandLine); err != nil { - logging.Error("can't bind a command line flag") - } + logging.Error("can't bind a command line flag") + } } func setValues(conf *viper.Viper) { diff --git a/itest/.env.gateway b/itest/.env.gateway index 1e28a481..74990c91 100644 --- a/itest/.env.gateway +++ b/itest/.env.gateway @@ -6,7 +6,7 @@ LOG_SERVICE_NAME=gateway LOG_LEVEL=debug LOG_TIME_FORMAT=RFC3339 LOG_TARGET=STDOUT -LOG_DIR=/var/log/fc-retrieval/fc-retrieval/gateway +LOG_DIR=/var/log/fc-retrieval/gateway LOG_FILE=gateway.log LOG_MAX_BACKUPS=3 LOG_MAX_AGE=28 diff --git a/itest/.env.provider b/itest/.env.provider index f4933f7b..3e40e80c 100644 --- a/itest/.env.provider +++ b/itest/.env.provider @@ -6,7 +6,7 @@ LOG_LEVEL=debug LOG_SERVICE_NAME=provider LOG_TIME_FORMAT=RFC3339 LOG_TARGET=STDOUT -LOG_DIR=/var/log/fc-retrieval/fc-retrieval/provider +LOG_DIR=/var/log/fc-retrieval/provider LOG_FILE=provider.log LOG_MAX_BACKUPS=3 LOG_MAX_AGE=28 diff --git a/itest/.env.register b/itest/.env.register index 3330cfbe..4e89be2e 100644 --- a/itest/.env.register +++ b/itest/.env.register @@ -1,3 +1,5 @@ +BIND_API=9020 + SERVICE_NAME="Filecoin Retrieval Register" REDIS_URL=redis diff --git a/itest/.hosts b/itest/.hosts new file mode 100644 index 00000000..281ffdec --- /dev/null +++ b/itest/.hosts @@ -0,0 +1,3 @@ +127.0.0.1 register lotus +127.0.0.1 gateway-0 gateway-1 gateway-2 gateway-3 gateway-4 gateway-5 gateway-6 gateway-7 gateway-8 gateway-9 gateway-10 gateway-11 gateway-12 gateway-13 gateway-14 gateway-15 gateway-16 gateway-17 gateway-18 gateway-19 gateway-20 gateway-21 gateway-22 gateway-23 gateway-24 gateway-25 gateway-26 gateway-27 gateway-28 gateway-29 gateway-30 gateway-31 gateway-32 gateway-33 gateway-34 gateway-35 gateway-36 gateway-37 gateway-38 gateway-39 gateway-40 +127.0.0.1 provider-0 provider-1 provider-2 provider-3 provider-4 provider-5 provider-6 provider-7 provider-8 provider-9 provider-10 provider-11 provider-12 provider-13 provider-14 provider-15 provider-16 provider-17 provider-18 provider-19 provider-20 provider-21 provider-22 provider-23 provider-24 provider-25 provider-26 provider-27 provider-28 provider-29 provider-30 provider-31 provider-32 provider-33 provider-34 provider-35 provider-36 provider-37 provider-38 provider-39 provider-40 diff --git a/itest/demo/client-cli/main.go b/itest/demo/client-cli/main.go index 50123feb..347f6872 100644 --- a/itest/demo/client-cli/main.go +++ b/itest/demo/client-cli/main.go @@ -12,6 +12,13 @@ import ( "strings" "time" + "github.com/c-bata/go-prompt" + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-jsonrpc" + "github.com/filecoin-project/lotus/api/apistruct" + "github.com/filecoin-project/lotus/chain/types" + cid2 "github.com/ipfs/go-cid" + "github.com/ConsenSys/fc-retrieval/client/pkg/fcrclient" "github.com/ConsenSys/fc-retrieval/common/pkg/cid" "github.com/ConsenSys/fc-retrieval/common/pkg/cidoffer" @@ -19,12 +26,6 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/c-bata/go-prompt" - "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/lotus/api/apistruct" - "github.com/filecoin-project/lotus/chain/types" - cid2 "github.com/ipfs/go-cid" ) var localLotusAP = "http://127.0.0.1:1234/rpc/v0" @@ -135,7 +136,7 @@ func executor(in string) { return } client.AddGatewaysToUse([]*nodeid.NodeID{id}) - added := client.AddActiveGateways([]*nodeid.NodeID{id}) + added := client.AddActiveGateways("127.0.0.1:9010", []*nodeid.NodeID{id}) if added != 1 { fmt.Println("Fail to use gateway.") return @@ -164,7 +165,7 @@ func executor(in string) { temp := make(map[string]*cidoffer.SubCIDOffer) gws := client.GetActiveGateways() for _, gw := range gws { - res, err := client.FindOffersStandardDiscoveryV2(id, gw, 5) + res, err := client.FindOffersStandardDiscoveryV2("127.0.0.1:9010", id, gw, 5) if err != nil { fmt.Printf("Error querying gateway %v: %v\n", gw.ToString(), err.Error()) continue @@ -197,7 +198,7 @@ func executor(in string) { fmt.Printf("Invalid contentID: %s\n", blocks[2]) return } - res, err := client.FindOffersDHTDiscoveryV2(id, node, 4, 4) + res, err := client.FindOffersDHTDiscoveryV2("127.0.0.1:9010", id, node, 4, 4) if err != nil { fmt.Printf("Error in dht discovery: %s\n", err.Error()) return diff --git a/itest/demo/gatewayadmin-cli/main.go b/itest/demo/gatewayadmin-cli/main.go index dfe61b4e..8db4d3a7 100644 --- a/itest/demo/gatewayadmin-cli/main.go +++ b/itest/demo/gatewayadmin-cli/main.go @@ -12,17 +12,18 @@ import ( "strings" "time" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" - "github.com/ConsenSys/fc-retrieval/common/pkg/register" - "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" "github.com/c-bata/go-prompt" "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/lotus/api/apistruct" "github.com/filecoin-project/lotus/chain/types" cid2 "github.com/ipfs/go-cid" + + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" + "github.com/ConsenSys/fc-retrieval/common/pkg/register" + "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" ) var localLotusAP = "http://127.0.0.1:1234/rpc/v0" @@ -117,12 +118,12 @@ func executor(in string) { gatewayRootSigningKey, gatewayRetrievalSigningKey, "au", - fmt.Sprintf("gateway%v:9012", i), // NetworkInfoGateway - fmt.Sprintf("gateway%v:9011", i), // NetworkInfoProvider + fmt.Sprintf("gateway%v:9012", i), // NetworkInfoGateway + fmt.Sprintf("gateway%v:9011", i), // NetworkInfoProvider fmt.Sprintf("127.0.0.1:%v", 8018+i), // NetworkInfoClient fmt.Sprintf("127.0.0.1:%v", 7013+i), // NetworkInfoAdmin ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), key, networkLotusAP, token) + err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar.GetNetworkInfoAdmin(), gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), key, networkLotusAP, token) if err != nil { fmt.Printf("Fail to initialise gateway: %s\n", err.Error()) return diff --git a/itest/demo/provideradmin-cli/main.go b/itest/demo/provideradmin-cli/main.go index 47e49107..703135d3 100644 --- a/itest/demo/provideradmin-cli/main.go +++ b/itest/demo/provideradmin-cli/main.go @@ -13,6 +13,13 @@ import ( "strings" "time" + "github.com/c-bata/go-prompt" + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-jsonrpc" + "github.com/filecoin-project/lotus/api/apistruct" + "github.com/filecoin-project/lotus/chain/types" + cid2 "github.com/ipfs/go-cid" + "github.com/ConsenSys/fc-retrieval/common/pkg/cid" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" @@ -20,12 +27,6 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" - "github.com/c-bata/go-prompt" - "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/lotus/api/apistruct" - "github.com/filecoin-project/lotus/chain/types" - cid2 "github.com/ipfs/go-cid" ) var localLotusAP = "http://127.0.0.1:1234/rpc/v0" @@ -113,12 +114,12 @@ func executor(in string) { address, providerRootSigningKey, providerRetrievalSigningKey, - "au", // RegionCode - "provider:9032", // NetworkInfoGateway + "au", // RegionCode + "provider:9032", // NetworkInfoGateway "127.0.0.1:9030", // NetworkInfoClient "127.0.0.1:9033", // NetworkInfoAdmin ) - err = pAdmin.InitialiseProviderV2(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), key, networkLotusAP, token) + err = pAdmin.InitialiseProviderV2("127.0.0.1:9033", providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), key, networkLotusAP, token) if err != nil { fmt.Printf("Fail to initialise provider: %s\n", err.Error()) return @@ -164,7 +165,7 @@ func executor(in string) { cid3 := cid.NewRandomContentID() expiryDate := time.Now().Local().Add(time.Hour * 24).Unix() price := rand.Intn(100-1) + 1 - err = pAdmin.PublishGroupCID(id, []cid.ContentID{*cid1, *cid2, *cid3}, uint64(price), expiryDate, 42) + err = pAdmin.PublishGroupCID("127.0.0.1:9033", id, []cid.ContentID{*cid1, *cid2, *cid3}, uint64(price), expiryDate, 42) if err != nil { fmt.Println("Error in publishing offer.") return @@ -201,7 +202,7 @@ func executor(in string) { } expiryDate := time.Now().Local().Add(time.Hour * 24).Unix() price := 44 - err = pAdmin.PublishGroupCID(id, []cid.ContentID{*cid1, *cid2, *cid3}, uint64(price), expiryDate, 42) + err = pAdmin.PublishGroupCID("127.0.0.1:9033", id, []cid.ContentID{*cid1, *cid2, *cid3}, uint64(price), expiryDate, 42) if err != nil { fmt.Println("Error in publishing offer.") return @@ -227,7 +228,7 @@ func executor(in string) { return } expiryDate := time.Now().Local().Add(time.Hour * 24).Unix() - err = pAdmin.PublishDHTCID(id, []cid.ContentID{*ccid}, []uint64{45}, []int64{expiryDate}, []uint64{43}) + err = pAdmin.PublishDHTCID("127.0.0.1:9033", id, []cid.ContentID{*ccid}, []uint64{45}, []int64{expiryDate}, []uint64{43}) if err != nil { fmt.Println("Error in publishing DHT offer.") return diff --git a/itest/go.mod b/itest/go.mod index caaa6c06..694fc090 100644 --- a/itest/go.mod +++ b/itest/go.mod @@ -18,6 +18,7 @@ require ( github.com/c-bata/go-prompt v0.2.6 github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect github.com/docker/docker v20.10.5+incompatible + github.com/docker/go-connections v0.4.0 // indirect github.com/filecoin-project/go-address v0.0.5 github.com/filecoin-project/go-jsonrpc v0.1.4-0.20210217175800-45ea43ac2bec github.com/filecoin-project/lotus v1.8.0 diff --git a/itest/pkg/client-gateway/client1_gw_test.go b/itest/pkg/client-gateway/client1_gw_test.go index 24a9d2fe..1fdd3bb4 100644 --- a/itest/pkg/client-gateway/client1_gw_test.go +++ b/itest/pkg/client-gateway/client1_gw_test.go @@ -24,6 +24,10 @@ import ( "testing" "time" + "github.com/testcontainers/testcontainers-go" + + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" + "github.com/stretchr/testify/assert" "github.com/ConsenSys/fc-retrieval/client/pkg/fcrclient" @@ -34,64 +38,26 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" ) -func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register and a gateway - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") +var containers tc.AllContainers - // Create shared net +func TestMain(m *testing.M) { + const testName = "client-gateway" ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start gateway - gatewayContainer := util.StartGateway(ctx, "gateway", networkName, util.ColorBlue, gwEnv, true) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := gatewayContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) + var gatewayConfig = config.NewConfig(".env.gateway") + var providerConfig = config.NewConfig(".env.provider") + var registerConfig = config.NewConfig(".env.register") + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 1, 1, testName, true, gatewayConfig, providerConfig, registerConfig) + if err != nil { + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } func TestOneGateway(t *testing.T) { @@ -104,7 +70,7 @@ func TestOneGateway(t *testing.T) { confBuilder := fcrgatewayadmin.CreateSettings() confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL("http://register:9020") + confBuilder.SetRegisterURL("http://" + containers.Register.GetRegisterHostApiEndpoint()) conf := confBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*conf) @@ -131,24 +97,28 @@ func TestOneGateway(t *testing.T) { } gatewayID := nodeid.NewRandomNodeID() + gatewayName := "gateway-0" + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootSigningKey, gatewayRetrievalSigningKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - if err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)); err != nil { - t.Errorf("can't initialise gateway") + if err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)); err != nil { + logging.Error("gateway initialisation error: %s", err.Error()) + t.FailNow() } if err = rm.RegisterGateway(gatewayRegistrar); err != nil { - t.Errorf("can't register gateway") + logging.Error("gateway registration error: %s", err.Error()) + t.FailNow() } logging.Info("Adding to client config gateway: %s", gatewayID.ToString()) @@ -172,7 +142,7 @@ func TestOneGateway(t *testing.T) { gws := client.GetGatewaysToUse() assert.Equal(t, 1, len(gws)) - numAdded = client.AddActiveGateways(newGatewaysToBeAdded) + numAdded = client.AddActiveGateways(gatewayClientApiEndpoint, newGatewaysToBeAdded) assert.Equal(t, 1, numAdded, "expecting the new Gateway be added to the list of gateways in use") ga := client.GetActiveGateways() assert.Equal(t, 1, len(ga)) diff --git a/itest/pkg/client-init/client0_init_test.go b/itest/pkg/client-init/client0_init_test.go index cd12e60e..3b1df231 100644 --- a/itest/pkg/client-init/client0_init_test.go +++ b/itest/pkg/client-init/client0_init_test.go @@ -25,6 +25,11 @@ import ( "testing" "time" + "github.com/testcontainers/testcontainers-go" + + "github.com/ConsenSys/fc-retrieval/itest/config" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" + "github.com/stretchr/testify/assert" "github.com/ConsenSys/fc-retrieval/client/pkg/fcrclient" @@ -32,46 +37,30 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" ) // Tests in this file use the Client API, but don't need the rest of the system to be // configured. These tests need to be run prior to the other client tests. var fClient *fcrclient.FilecoinRetrievalClient -func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - m.Run() - return - } - // Env is not set, we are calling from host - // We don't need any running instance +var containers tc.AllContainers - // Create shared net +func TestMain(m *testing.M) { + const testName = "client-init" ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) + var gatewayConfig = config.NewConfig(".env.gateway") + var providerConfig = config.NewConfig(".env.provider") + var registerConfig = config.NewConfig(".env.register") + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 1, 1, testName, true, gatewayConfig, providerConfig, registerConfig) + if err != nil { + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } func TestGetClientVersion(t *testing.T) { diff --git a/itest/pkg/lotus-full-node/lotus_basic_test.go b/itest/pkg/lotus-full-node/lotus_basic_test.go index 4edf565f..85a6cc5a 100644 --- a/itest/pkg/lotus-full-node/lotus_basic_test.go +++ b/itest/pkg/lotus-full-node/lotus_basic_test.go @@ -30,59 +30,43 @@ import ( "testing" "time" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + "github.com/testcontainers/testcontainers-go" + + "github.com/ConsenSys/fc-retrieval/itest/config" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" + "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/lotus/api/apistruct" "github.com/stretchr/testify/assert" -) -func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - m.Run() - return - } - // Env is not set, we are calling from host - // We need a lotus + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" +) - // Create shared net - bgCtx := context.Background() - ctx, _ := context.WithTimeout(bgCtx, time.Minute*2) - network, networkName := util.CreateNetwork(ctx) - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, true) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } +var containers tc.AllContainers - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) +func TestMain(m *testing.M) { + const testName = "lotus-connectivity" + ctx := context.Background() + var gatewayConfig = config.NewConfig(".env.gateway") + var providerConfig = config.NewConfig(".env.provider") + var registerConfig = config.NewConfig(".env.register") + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 1, 1, testName, true, gatewayConfig, providerConfig, registerConfig) + if err != nil { + logging.Error("%s test failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } func TestLotusFullNodeConnectivityHttp(t *testing.T) { - method := "Filecoin.ChainHead" id := 1 - lotusUrl := "http://lotus-full-node:1234/rpc/v0" + lotusDaemonEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + lotusUrl := "http://" + lotusDaemonEndpoint + "/rpc/v0" requestBody := `{ "jsonrpc": "2.0", "method": "` + method + `", @@ -93,6 +77,7 @@ func TestLotusFullNodeConnectivityHttp(t *testing.T) { resp, err := http.Post(lotusUrl, "application/json", bytes.NewBuffer([]byte(requestBody))) if err != nil { t.Errorf("Can't establish connection to Lotus %s", lotusUrl) + t.FailNow() } defer resp.Body.Close() @@ -100,11 +85,13 @@ func TestLotusFullNodeConnectivityHttp(t *testing.T) { body, err := ioutil.ReadAll(resp.Body) if err != nil { t.Errorf("Can't read response from Lotus, method: %s", method) + t.FailNow() } var fields map[string]interface{} err = json.Unmarshal(body, &fields) if err != nil { t.Errorf("Can't parse json response from Lotus, method: %s", method) + t.FailNow() } assert.Equal(t, float64(id), fields["id"]) @@ -115,9 +102,10 @@ func TestLotusFullNodeConnectivityWs(t *testing.T) { bgCtx := context.Background() ctx, _ := context.WithTimeout(bgCtx, time.Minute*3) + lotusDaemonEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() clientClose, err := jsonrpc.NewMergeClient( ctx, - "ws://lotus-full-node:1234/rpc/v0", + "ws://"+lotusDaemonEndpoint+"/rpc/v0", "Filecoin", []interface{}{ &lotusApi.CommonStruct.Internal, @@ -126,12 +114,14 @@ func TestLotusFullNodeConnectivityWs(t *testing.T) { http.Header{}) if err != nil { t.Errorf("Can't construct a Lotus client, error: %s", err.Error()) + t.FailNow() } defer clientClose() head, err := lotusApi.ChainHead(context.Background()) if err != nil { t.Errorf("Can't call method ChainHead of Lotus API, error: %s", err.Error()) + t.FailNow() } assert.Greater(t, len(head.Cids()), 0) assert.NotEqualf(t, "", head.Cids()[0].KeyString(), "Head CID Key is empty") @@ -142,9 +132,10 @@ func TestLotusMinerConnectivityWs(t *testing.T) { bgCtx := context.Background() ctx, _ := context.WithTimeout(bgCtx, time.Minute*3) + _, lotusMinerEndpoint := containers.Lotus.GetLostHostApiEndpoints() clientClose, err := jsonrpc.NewMergeClient( ctx, - "ws://lotus-full-node:2345/rpc/v0", + "ws://"+lotusMinerEndpoint+"/rpc/v0", "Filecoin", []interface{}{ &lotusApi.CommonStruct.Internal, @@ -159,6 +150,7 @@ func TestLotusMinerConnectivityWs(t *testing.T) { id, err := lotusApi.ID(context.Background()) if err != nil { t.Errorf("Can't call method ChainHead of Lotus API, error: %s", err.Error()) + t.FailNow() } assert.NotNil(t, id) } diff --git a/itest/pkg/lotus/lotus_basic_test.go b/itest/pkg/lotus/lotus_basic_test.go deleted file mode 100644 index 0219f807..00000000 --- a/itest/pkg/lotus/lotus_basic_test.go +++ /dev/null @@ -1,158 +0,0 @@ -/* -Package lotus - end-to-end tests, calling private, locally hosted Lotus network without running Lotus Miner. - -Avoiding running Lotus Miner for tests, that don't need miner functionality significantly reduces CPU load. -*/ -package lotus - -/* - * Copyright 2021 ConsenSys Software Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -import ( - "bytes" - "context" - "encoding/json" - "io/ioutil" - "net/http" - "os" - "strconv" - "testing" - "time" - - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" - "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/lotus/api/apistruct" - "github.com/stretchr/testify/assert" -) - -func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - m.Run() - return - } - // Env is not set, we are calling from host - // We need a lotus - - // Create shared net - bgCtx := context.Background() - ctx, _ := context.WithTimeout(bgCtx, time.Minute*2) - network, networkName := util.CreateNetwork(ctx) - - // Start lotus - lotusContainer := util.StartLotusDaemon(ctx, networkName, true) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -func TestLotusConnectivityHttp(t *testing.T) { - - method := "Filecoin.ChainHead" - id := 1 - lotusUrl := "http://lotus-daemon:1234/rpc/v0" - requestBody := `{ - "jsonrpc": "2.0", - "method": "` + method + `", - "id": ` + strconv.Itoa(id) + `, - "params": [] - }` - - resp, err := http.Post(lotusUrl, "application/json", bytes.NewBuffer([]byte(requestBody))) - if err != nil { - t.Errorf("Can't establish connection to Lotus %s", lotusUrl) - } - defer resp.Body.Close() - - assert.Equal(t, http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - t.Errorf("Can't read response from Lotus, method: %s", method) - } - var fields map[string]interface{} - err = json.Unmarshal(body, &fields) - if err != nil { - t.Errorf("Can't parse json response from Lotus, method: %s", method) - } - - assert.Equal(t, float64(id), fields["id"]) -} - -func TestLotusConnectivityWs(t *testing.T) { - var lotusApi apistruct.FullNodeStruct - bgCtx := context.Background() - ctx, _ := context.WithTimeout(bgCtx, time.Minute*3) - - clientClose, err := jsonrpc.NewMergeClient( - ctx, - "ws://lotus-daemon:1234/rpc/v0", - "Filecoin", - []interface{}{ - &lotusApi.CommonStruct.Internal, - &lotusApi.Internal, - }, - http.Header{}) - if err != nil { - t.Errorf("Can't construct a Lotus client, error: %s", err.Error()) - } - defer clientClose() - - head, err := lotusApi.ChainHead(context.Background()) - if err != nil { - t.Errorf("Can't call method ChainHead of Lotus API, error: %s", err.Error()) - } - assert.Greater(t, len(head.Cids()), 0) - assert.NotEqualf(t, "", head.Cids()[0].KeyString(), "Head CID Key is empty") -} - -func generateKeyPair() (privateKey []byte, publicKey []byte) { - // Generate Private-Public pairs. Public key will be used as address - var signer fcrpaymentmgr.SecpSigner - privateKey, err := signer.GenPrivate() - if err != nil { - logging.Error("Error generating private key, while creating address %s", err.Error()) - os.Exit(1) - } - - publicKey, err = signer.ToPublic(privateKey) - if err != nil { - logging.Error("Error generating public key, while creating address %s", err.Error()) - os.Exit(1) - } - return -} diff --git a/itest/pkg/poc1/poc1usage_test.go b/itest/pkg/poc1/poc1usage_test.go index 4030163e..75b4c89d 100644 --- a/itest/pkg/poc1/poc1usage_test.go +++ b/itest/pkg/poc1/poc1usage_test.go @@ -5,16 +5,19 @@ package poc1 import ( "context" - "errors" "fmt" "os" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/testcontainers/testcontainers-go" "github.com/ConsenSys/fc-retrieval/client/pkg/fcrclient" "github.com/ConsenSys/fc-retrieval/common/pkg/cid" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" @@ -22,7 +25,6 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -43,70 +45,22 @@ import ( var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") +var registerConfig = config.NewConfig(".env.register") +var containers tc.AllContainers func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, a gateway and a provider - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net + const testName = "poc1" ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start gateway - gatewayContainer := util.StartGateway(ctx, "gateway", networkName, util.ColorBlue, gwEnv, true) - - // Start provider - providerContainer := util.StartProvider(ctx, "provider", networkName, util.ColorPurple, pvEnv, true) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := providerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := gatewayContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 1, 1, testName, true, gatewayConfig, providerConfig, registerConfig) + if err != nil { + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } func TestInitialiseGateway(t *testing.T) { @@ -122,7 +76,8 @@ func TestInitialiseGateway(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), false, true, 10*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -132,37 +87,39 @@ func TestInitialiseGateway(t *testing.T) { // Configure gateway admin confBuilder := fcrgatewayadmin.CreateSettings() confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL("http://" + containers.Register.GetRegisterHostApiEndpoint()) conf := confBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*conf) // Configure gateway register - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } gatewayID := nodeid.NewRandomNodeID() + gatewayName := "gateway-0" + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) // Initialise the gateway using gateway admin - err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } @@ -184,7 +141,8 @@ func TestInitialiseProvider(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, 2*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, true, true, 2*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -194,30 +152,32 @@ func TestInitialiseProvider(t *testing.T) { // Configure provider admin confBuilder := fcrprovideradmin.CreateSettings() confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL(registerApiEndpoint) conf := confBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*conf) // Configure provider register - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } providerID := nodeid.NewRandomNodeID() + providerName := "provider-0" + _, _, adminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() providerRegistrar := register.NewProviderRegister( providerID.ToString(), providerConfig.GetString("PROVIDER_ADDRESS"), providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY"), - providerConfig.GetString("NETWORK_INFO_CLIENT"), - providerConfig.GetString("NETWORK_INFO_ADMIN"), + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) // Initialise the provider using provider admin - err = pAdmin.InitialiseProvider(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = pAdmin.InitialiseProvider(adminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { logging.Error("error initialising provider: %s", err.Error()) t.FailNow() @@ -247,7 +207,8 @@ func TestPublishGroupCID(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, true, true, time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -257,63 +218,67 @@ func TestPublishGroupCID(t *testing.T) { // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Configure provider admin pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL(registerApiEndpoint) pConf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*pConf) // Configure gateway register - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } gatewayID := nodeid.NewRandomNodeID() + gatewayName := "gateway-0" + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } // Configure provider register - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } providerID := nodeid.NewRandomNodeID() + providerName := "provider-0" + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() providerRegistrar := register.NewProviderRegister( providerID.ToString(), providerConfig.GetString("PROVIDER_ADDRESS"), providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY"), - providerConfig.GetString("NETWORK_INFO_CLIENT"), - providerConfig.GetString("NETWORK_INFO_ADMIN"), + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) - err = pAdmin.InitialiseProvider(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = pAdmin.InitialiseProvider(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { logging.Error("error initialising provider: %s", err.Error()) t.FailNow() @@ -324,12 +289,12 @@ func TestPublishGroupCID(t *testing.T) { } // Force provider and gateway to update - err = pAdmin.ForceUpdate(providerID) + err = pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { logging.Error("error forcing update provider: %s", err.Error()) t.FailNow() } - err = gwAdmin.ForceUpdate(gatewayID) + err = gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { logging.Error("error forcing update gateway: %s", err.Error()) t.FailNow() @@ -343,7 +308,7 @@ func TestPublishGroupCID(t *testing.T) { expiryDate := time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() // Publish Group CID - err = pAdmin.PublishGroupCID(providerID, pieceCIDs, 42, expiryDate, 42) + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providerID, pieceCIDs, 42, expiryDate, 42) if err != nil { logging.Error("error publishing group CID: %s", err.Error()) t.FailNow() @@ -352,18 +317,19 @@ func TestPublishGroupCID(t *testing.T) { // Test get all offers gatewayIDs := make([]nodeid.NodeID, 0) logging.Info("Get all offers") - _, cidgroupInfo, err := pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err := pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.Error("error getting group CID offer: %s", err.Error()) t.FailNow() } + fmt.Printf("<<<<<<<<<<<<< cidgroupInfo: %#v", cidgroupInfo) if !assert.GreaterOrEqual(t, len(cidgroupInfo), 1, "Offers should be found") { t.FailNow() } // Add a gateway and verify gatewayIDs = append(gatewayIDs, *gatewayID) logging.Info("Get offers by gatewayID=%s", gatewayID.ToString()) - _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.Error("error getting group CID offer: %s", err.Error()) t.FailNow() @@ -376,7 +342,7 @@ func TestPublishGroupCID(t *testing.T) { fakeNodeID, _ := nodeid.NewNodeIDFromHexString("101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2DFA43") gatewayIDs[0] = *fakeNodeID logging.Info("Get offers by gatewayID=%s", fakeNodeID.ToString()) - _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.Error("error getting group CID offer: %s", err.Error()) t.FailNow() @@ -401,7 +367,8 @@ func TestInitClient(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), false, false, 2*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, false, 2*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -412,7 +379,7 @@ func TestInitClient(t *testing.T) { confBuilder := fcrclient.CreateSettings() confBuilder.SetEstablishmentTTL(101) confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL(registerApiEndpoint) conf := confBuilder.Build() // Create client and verify @@ -440,7 +407,8 @@ func TestClientAddGateway(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, true, true, time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) } @@ -449,7 +417,7 @@ func TestClientAddGateway(t *testing.T) { // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) @@ -457,7 +425,7 @@ func TestClientAddGateway(t *testing.T) { confBuilder := fcrclient.CreateSettings() confBuilder.SetEstablishmentTTL(101) confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL(registerApiEndpoint) conf := confBuilder.Build() client, err := fcrclient.NewFilecoinRetrievalClient(*conf, rm) if err != nil { @@ -466,30 +434,32 @@ func TestClientAddGateway(t *testing.T) { } // Configure gateway register - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } gatewayID := nodeid.NewRandomNodeID() + gatewayName := "gateway-0" + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } @@ -499,7 +469,7 @@ func TestClientAddGateway(t *testing.T) { t.FailNow() } // Make the gateway active, this involves doing an establishment - added = client.AddActiveGateways([]*nodeid.NodeID{gatewayID}) + added = client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) assert.Equal(t, 1, added, "One gateway should be added") logging.Info("/*******************************************************/") @@ -520,7 +490,8 @@ func TestClientStdContentDiscover(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, true, true, time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) } @@ -529,63 +500,67 @@ func TestClientStdContentDiscover(t *testing.T) { // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Configure provider admin pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL(registerApiEndpoint) pConf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*pConf) // Configure gateway register - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } gatewayID := nodeid.NewRandomNodeID() + gatewayName := "gateway-0" + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } // Configure provider register - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { logging.Error("can't generate key pairs %s", err.Error()) t.FailNow() } providerID := nodeid.NewRandomNodeID() + providerName := "provider-0" + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() providerRegistrar := register.NewProviderRegister( providerID.ToString(), providerConfig.GetString("PROVIDER_ADDRESS"), providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY"), - providerConfig.GetString("NETWORK_INFO_CLIENT"), - providerConfig.GetString("NETWORK_INFO_ADMIN"), + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) - err = pAdmin.InitialiseProvider(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = pAdmin.InitialiseProvider(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { logging.Error("error initialising provider: %s", err.Error()) t.FailNow() @@ -599,7 +574,7 @@ func TestClientStdContentDiscover(t *testing.T) { clientConfBuilder := fcrclient.CreateSettings() clientConfBuilder.SetEstablishmentTTL(101) clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) clientConf := clientConfBuilder.Build() client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) if err != nil { @@ -612,16 +587,16 @@ func TestClientStdContentDiscover(t *testing.T) { t.FailNow() } // Make the gateway active, this involves doing an establishment - added = client.AddActiveGateways([]*nodeid.NodeID{gatewayID}) + added = client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) assert.Equal(t, 1, added, "One gateway should be added") // Force provider and gateway to update - err = pAdmin.ForceUpdate(providerID) + err = pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { logging.Error("error forcing update provider: %s", err.Error()) t.FailNow() } - err = gwAdmin.ForceUpdate(gatewayID) + err = gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { logging.Error("error forcing update gateway: %s", err.Error()) t.FailNow() @@ -635,14 +610,14 @@ func TestClientStdContentDiscover(t *testing.T) { expiryDate := time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() // Publish Group CID - err = pAdmin.PublishGroupCID(providerID, pieceCIDs, 42, expiryDate, 42) + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providerID, pieceCIDs, 42, expiryDate, 42) if err != nil { logging.Error("error publishing group CID: %s", err.Error()) t.FailNow() } // Find published offers - offers, err := client.FindOffersStandardDiscovery(&(pieceCIDs[0]), gatewayID) + offers, err := client.FindOffersStandardDiscovery(gatewayClientApiEndpoint, &(pieceCIDs[0]), gatewayID) if err != nil { logging.Error("error finding offer via standard discovery: %s", err.Error()) t.FailNow() @@ -651,7 +626,7 @@ func TestClientStdContentDiscover(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscovery(&(pieceCIDs[1]), gatewayID) + offers, err = client.FindOffersStandardDiscovery(gatewayClientApiEndpoint, &(pieceCIDs[1]), gatewayID) if err != nil { logging.Error("error finding offer via standard discovery: %s", err.Error()) t.FailNow() @@ -660,7 +635,7 @@ func TestClientStdContentDiscover(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscovery(&(pieceCIDs[2]), gatewayID) + offers, err = client.FindOffersStandardDiscovery(gatewayClientApiEndpoint, &(pieceCIDs[2]), gatewayID) if err != nil { logging.Error("error finding offer via standard discovery: %s", err.Error()) t.FailNow() @@ -671,7 +646,7 @@ func TestClientStdContentDiscover(t *testing.T) { // Negative test using random content ID randomCID := cid.NewRandomContentID() - offers, err = client.FindOffersStandardDiscovery(randomCID, gatewayID) + offers, err = client.FindOffersStandardDiscovery(gatewayClientApiEndpoint, randomCID, gatewayID) if err != nil { logging.Error("error finding offer for random content ID via standard discovery: %s", err.Error()) t.FailNow() @@ -682,33 +657,3 @@ func TestClientStdContentDiscover(t *testing.T) { logging.Info("/* End TestClientStdContentDiscover */") logging.Info("/*******************************************************/") } - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) - } - return -} diff --git a/itest/pkg/poc2_dht_offer/main_test.go b/itest/pkg/poc2_dht_offer/main_test.go index efe943f8..26135b7b 100644 --- a/itest/pkg/poc2_dht_offer/main_test.go +++ b/itest/pkg/poc2_dht_offer/main_test.go @@ -5,141 +5,33 @@ package poc2_dht_offer import ( "context" - "errors" - "fmt" "os" "testing" - tc "github.com/wcgcyx/testcontainers-go" + "github.com/testcontainers/testcontainers-go" + + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" ) -const lotusAP = "http://lotus-full-node:1234/rpc/v0" - -var lotusToken string -var superAcct string var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") +var registerConfig = config.NewConfig(".env.register") +var containers tc.AllContainers func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net + const testName = "poc2-dht-offer" ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start 3 providers - var providerContainers []*tc.Container - for i := 0; i < 3; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - - // Start 33 gateways - var gatewayContainers []*tc.Container - for i := 0; i < 33; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() - logging.Info("Lotus token is: %s", lotusToken) - logging.Info("Super Acct is %s", superAcct) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating Lotus test container: %s", err.Error()) - } - for _, c := range gatewayContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating gateway test container: %s", err.Error()) - } - } - for _, c := range providerContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating provider test container: %s", err.Error()) - } - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 33, 3, testName, true, gatewayConfig, providerConfig, registerConfig) if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } - return + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } diff --git a/itest/pkg/poc2_dht_offer/publish_dht_offer_test.go b/itest/pkg/poc2_dht_offer/publish_dht_offer_test.go index f678bce1..d180bcfa 100644 --- a/itest/pkg/poc2_dht_offer/publish_dht_offer_test.go +++ b/itest/pkg/poc2_dht_offer/publish_dht_offer_test.go @@ -1,6 +1,7 @@ package poc2_dht_offer import ( + "context" "fmt" "testing" "time" @@ -15,7 +16,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -26,8 +28,17 @@ func TestPublishDHTOffer(t *testing.T) { t.Log("/* Start TestPublishDHTOffer */") t.Log("/*******************************************************/") + ctx := context.Background() + + lotusToken, superAcct := fil.GetLotusToken() + logging.Info("Lotus token is: %s", lotusToken) + logging.Info("Super Acct is %s", superAcct) + + lotusDaemonEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + lotusAP := "http://" + lotusDaemonEndpoint + "/rpc/v0" + var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -39,23 +50,51 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } + registerHostPort := containers.Register.GetRegisterHostApiEndpoint() + registerApiEndpoint := "http://" + registerHostPort + // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, 10*time.Second) + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, true, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() } defer rm.ShutdownAndWait() + walletKey := privateKeys[0] + // walletAddress := accountAddrs[0] + privateKeys = privateKeys[1:] + accountAddrs = accountAddrs[1:] + + // Configure client + clientConfBuilder := fcrclient.CreateSettings() + clientConfBuilder.SetEstablishmentTTL(101) + clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) + clientConfBuilder.SetWalletPrivateKey(walletKey) + clientConfBuilder.SetLotusAP(lotusAP) + clientConfBuilder.SetLotusAuthToken(lotusToken) + clientConf := clientConfBuilder.Build() + client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) + if !assert.Nil(t, err, "Error should be nil") { + t.Fatal(err) + } + res := client.PaymentMgr() + if !assert.NotNil(t, res, "Fail to initialise a payment manager") { + t.FailNow() + } + // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Initialise gateways - var gwIDs []*nodeid.NodeID + + // map between gateway ID and gateway name + var gateways = make(map[string]*nodeid.NodeID) // Only initialise 32 gateways, with one extra to initialise later to test list single cid offer for i := 0; i < 32; i++ { walletKey := privateKeys[0] @@ -63,7 +102,7 @@ func TestPublishDHTOffer(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -74,30 +113,44 @@ func TestPublishDHTOffer(t *testing.T) { gatewayID, err := nodeid.NewNodeIDFromHexString(idStr) if err != nil { - panic(err) + logging.Error("node ID generation error: %s", err.Error()) + t.FailNow() } - gwIDs = append(gwIDs, gatewayID) + fmt.Printf("+++ TestPublishDHTOffer, new gatewayID %+v %+v\n", gatewayID, *gatewayID) + + gatewayName := fmt.Sprintf("gateway-%v", i) + gateways[gatewayName] = gatewayID + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() - identifier := fmt.Sprintf("-%v", i) gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), walletAddress, gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[7:], - gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[7:], - gatewayConfig.GetString("NETWORK_INFO_CLIENT")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_CLIENT")[7:], - gatewayConfig.GetString("NETWORK_INFO_ADMIN")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_ADMIN")[7:], + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = gwAdmin.InitialiseGatewayV2(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) + t.FailNow() + } + + // add to client + added := client.AddGatewaysToUse([]*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, added, "1 gateway should be added") { + t.FailNow() + } + activatedCount := client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, activatedCount, "1 gateway should be active") { t.FailNow() } } @@ -105,33 +158,38 @@ func TestPublishDHTOffer(t *testing.T) { // Initialise providers pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL(registerApiEndpoint) pConf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*pConf) - var pIDs []*nodeid.NodeID + + // map between provider ID and provider name + var providers = make(map[string]*nodeid.NodeID) for i := 0; i < 3; i++ { walletKey := privateKeys[0] walletAddress := accountAddrs[0] privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() - pIDs = append(pIDs, providerID) - identifier := fmt.Sprintf("-%v", i) + providerName := fmt.Sprintf("provider-%v", i) + providers[providerName] = providerID + // get endpoint for Docker host + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + providerRegistrar := register.NewProviderRegister( providerID.ToString(), walletAddress, providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_GATEWAY")[8:], - providerConfig.GetString("NETWORK_INFO_CLIENT")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_CLIENT")[8:], - providerConfig.GetString("NETWORK_INFO_ADMIN")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_ADMIN")[8:], + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) // Initialise the provider using provider admin - err = pAdmin.InitialiseProviderV2(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = pAdmin.InitialiseProviderV2(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } @@ -142,49 +200,18 @@ func TestPublishDHTOffer(t *testing.T) { } } - walletKey := privateKeys[0] - // walletAddress := accountAddrs[0] - privateKeys = privateKeys[1:] - accountAddrs = accountAddrs[1:] - - // Configure client - clientConfBuilder := fcrclient.CreateSettings() - clientConfBuilder.SetEstablishmentTTL(101) - clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) - clientConfBuilder.SetWalletPrivateKey(walletKey) - clientConfBuilder.SetLotusAP(lotusAP) - clientConfBuilder.SetLotusAuthToken(lotusToken) - clientConf := clientConfBuilder.Build() - client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) - if !assert.Nil(t, err, "Error should be nil") { - t.Fatal(err) - } - res := client.PaymentMgr() - if !assert.NotNil(t, res, "Fail to initialise a payment manager") { - t.FailNow() - } - - added := client.AddGatewaysToUse(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be added") { - t.FailNow() - } - - added = client.AddActiveGateways(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be active") { - t.FailNow() - } - // Force providers and gateways to update - for _, p := range pIDs { - err := pAdmin.ForceUpdate(p) + for providerName, providerID := range providers { + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + err := pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { logging.Error("provider update error: %s", err) t.FailNow() } } - for _, g := range gwIDs { - err := gwAdmin.ForceUpdate(g) + for gatewayName, gatewayID := range gateways { + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() + err := gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { logging.Error("gateway update error: %s", err) t.FailNow() @@ -205,7 +232,8 @@ func TestPublishDHTOffer(t *testing.T) { t.Fatal(err) } expiryDate := time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishGroupCID(pIDs[0], []cid.ContentID{*contentID01, *contentID02, *contentID03}, 42, expiryDate, 42) + _, _, providerAdminApiEndpoint := containers.Providers["provider-0"].GetProviderHostApiEndpoints() + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providers["provider-0"], []cid.ContentID{*contentID01, *contentID02, *contentID03}, 42, expiryDate, 42) if err != nil { logging.Error("error to publish group offer for content IDs: %s, %s, %s; error: %s", contentID01.ToString(), contentID02.ToString(), contentID03.ToString(), err.Error()) t.FailNow() @@ -225,7 +253,8 @@ func TestPublishDHTOffer(t *testing.T) { t.Fatal(err) } expiryDate = time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishGroupCID(pIDs[1], []cid.ContentID{*contentID11, *contentID12, *contentID13}, 42, expiryDate, 42) + _, _, providerAdminApiEndpoint = containers.Providers["provider-1"].GetProviderHostApiEndpoints() + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providers["provider-1"], []cid.ContentID{*contentID11, *contentID12, *contentID13}, 42, expiryDate, 42) if err != nil { logging.Error("error to publish group offer for content IDs: %s, %s, %s; error: %s", contentID11.ToString(), contentID12.ToString(), contentID13.ToString(), err.Error()) t.FailNow() @@ -238,7 +267,8 @@ func TestPublishDHTOffer(t *testing.T) { t.Fatal(err) } expiryDate = time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishDHTCID(pIDs[0], []cid.ContentID{*contentID0}, []uint64{42}, []int64{expiryDate}, []uint64{42}) + _, _, providerAdminApiEndpoint = containers.Providers["provider-0"].GetProviderHostApiEndpoints() + err = pAdmin.PublishDHTCID(providerAdminApiEndpoint, providers["provider-0"], []cid.ContentID{*contentID0}, []uint64{42}, []int64{expiryDate}, []uint64{42}) if err != nil { logging.Error("error to publish DHT offer for content ID: %s; error: %s", contentID0.ToString(), err.Error()) t.FailNow() @@ -251,14 +281,16 @@ func TestPublishDHTOffer(t *testing.T) { t.Fatal(err) } expiryDate = time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishDHTCID(pIDs[0], []cid.ContentID{*contentID1}, []uint64{42}, []int64{expiryDate}, []uint64{42}) + _, _, providerAdminApiEndpoint = containers.Providers["provider-0"].GetProviderHostApiEndpoints() + err = pAdmin.PublishDHTCID(providerAdminApiEndpoint, providers["provider-0"], []cid.ContentID{*contentID1}, []uint64{42}, []int64{expiryDate}, []uint64{42}) if err != nil { logging.Error("error to publish DHT offer for content ID: %s; error: %s", contentID1.ToString(), err.Error()) t.FailNow() } // Try Standard Discovery for contentID0 - offers, err := client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[5], 1) + _, _, gatewayClientApiEndpoint, _ := containers.Gateways["gateway-5"].GetGatewayHostApiEndpoints() + offers, err := client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-5"], 1) if err != nil { t.Fatal(err) } @@ -266,7 +298,8 @@ func TestPublishDHTOffer(t *testing.T) { t.Fatal() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[6], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-6"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-6"], 1) if err != nil { t.Fatal(err) } @@ -274,7 +307,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[12], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-12"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-12"], 1) if err != nil { t.Fatal(err) } @@ -282,7 +316,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[21], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-21"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-21"], 1) if err != nil { t.Fatal(err) } @@ -290,7 +325,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[22], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-22"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-22"], 1) if err != nil { t.Fatal(err) } @@ -299,7 +335,8 @@ func TestPublishDHTOffer(t *testing.T) { } // Try Standard Discovery for contentID1 - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[24], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-24"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-24"], 1) if err != nil { t.Fatal(err) } @@ -307,7 +344,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[25], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-25"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-25"], 1) if err != nil { t.Fatal(err) } @@ -315,7 +353,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[31], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-31"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-31"], 1) if err != nil { t.Fatal(err) } @@ -323,7 +362,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[0], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-0"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-0"], 1) if err != nil { t.Fatal(err) } @@ -331,7 +371,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[8], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-8"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-8"], 1) if err != nil { t.Fatal(err) } @@ -339,7 +380,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[9], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-9"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-9"], 1) if err != nil { t.Fatal(err) } @@ -348,7 +390,8 @@ func TestPublishDHTOffer(t *testing.T) { } // Try DHT Search for content 0 - offers, err = client.FindOffersStandardDiscoveryV2(contentID0, gwIDs[0], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-0"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-0"], 1) if err != nil { t.Fatal(err) } @@ -356,7 +399,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offersMap, err := client.FindOffersDHTDiscoveryV2(contentID0, gwIDs[0], 4, 4) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-0"].GetGatewayHostApiEndpoints() + offersMap, err := client.FindOffersDHTDiscoveryV2(gatewayClientApiEndpoint, contentID0, gateways["gateway-0"], 4, 4) if err != nil { t.Fatal(err) } @@ -365,44 +409,45 @@ func TestPublishDHTOffer(t *testing.T) { } // It should contact gateway 12, 13, 14, 15 - _, exists := offersMap[gwIDs[12].ToString()] + _, exists := offersMap[gateways["gateway-12"].ToString()] if !assert.True(t, exists, "Should query gateway 12.") { t.FailNow() } - offers = *(offersMap[gwIDs[12].ToString()]) + offers = *(offersMap[gateways["gateway-12"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 12.") { t.FailNow() } - _, exists = offersMap[gwIDs[13].ToString()] + _, exists = offersMap[gateways["gateway-13"].ToString()] if !assert.True(t, exists, "Should query gateway 13.") { t.FailNow() } - offers = *(offersMap[gwIDs[13].ToString()]) + offers = *(offersMap[gateways["gateway-13"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 13.") { t.FailNow() } - _, exists = offersMap[gwIDs[14].ToString()] + _, exists = offersMap[gateways["gateway-14"].ToString()] if !assert.True(t, exists, "Should query gateway 14.") { t.FailNow() } - offers = *(offersMap[gwIDs[14].ToString()]) + offers = *(offersMap[gateways["gateway-14"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 14.") { t.FailNow() } - _, exists = offersMap[gwIDs[15].ToString()] + _, exists = offersMap[gateways["gateway-15"].ToString()] if !assert.True(t, exists, "Should query gateway 15.") { t.FailNow() } - offers = *(offersMap[gwIDs[15].ToString()]) + offers = *(offersMap[gateways["gateway-15"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 15.") { t.FailNow() } // Try DHT Search for content 1 - offers, err = client.FindOffersStandardDiscoveryV2(contentID1, gwIDs[15], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-15"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-15"], 1) if err != nil { t.Fatal(err) } @@ -410,7 +455,8 @@ func TestPublishDHTOffer(t *testing.T) { t.FailNow() } - offersMap, err = client.FindOffersDHTDiscoveryV2(contentID1, gwIDs[15], 3, 3) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-15"].GetGatewayHostApiEndpoints() + offersMap, err = client.FindOffersDHTDiscoveryV2(gatewayClientApiEndpoint, contentID1, gateways["gateway-15"], 3, 3) if err != nil { t.Fatal(err) } @@ -419,29 +465,29 @@ func TestPublishDHTOffer(t *testing.T) { } // It should contact gateway 0, 1 and 31 - _, exists = offersMap[gwIDs[0].ToString()] + _, exists = offersMap[gateways["gateway-0"].ToString()] if !assert.True(t, exists, "Should query gateway 0.") { t.FailNow() } - offers = *(offersMap[gwIDs[0].ToString()]) + offers = *(offersMap[gateways["gateway-0"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 0.") { t.FailNow() } - _, exists = offersMap[gwIDs[1].ToString()] + _, exists = offersMap[gateways["gateway-1"].ToString()] if !assert.True(t, exists, "Should query gateway 1.") { t.FailNow() } - offers = *(offersMap[gwIDs[1].ToString()]) + offers = *(offersMap[gateways["gateway-1"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 1.") { t.FailNow() } - _, exists = offersMap[gwIDs[31].ToString()] + _, exists = offersMap[gateways["gateway-31"].ToString()] if !assert.True(t, exists, "Should query gateway 31.") { t.FailNow() } - offers = *(offersMap[gwIDs[31].ToString()]) + offers = *(offersMap[gateways["gateway-31"].ToString()]) if !assert.Equal(t, 1, len(offers), "Should find offer with cid 0 from gateway 31.") { t.FailNow() } diff --git a/itest/pkg/poc2_dht_offer_ack/dht_offer_ack_test.go b/itest/pkg/poc2_dht_offer_ack/dht_offer_ack_test.go index 7e2087e1..a645130a 100644 --- a/itest/pkg/poc2_dht_offer_ack/dht_offer_ack_test.go +++ b/itest/pkg/poc2_dht_offer_ack/dht_offer_ack_test.go @@ -15,7 +15,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + crypto_facade "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -24,8 +25,9 @@ func TestDHTOfferAck(t *testing.T) { t.Log("/* Start TestDHTOfferAck */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -61,7 +63,7 @@ func TestDHTOfferAck(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := crypto_facade.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -95,7 +97,7 @@ func TestDHTOfferAck(t *testing.T) { } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } } @@ -113,7 +115,7 @@ func TestDHTOfferAck(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := crypto_facade.GenerateKeys() providerID := nodeid.NewRandomNodeID() pIDs = append(pIDs, providerID) diff --git a/itest/pkg/poc2_dht_offer_ack/main_test.go b/itest/pkg/poc2_dht_offer_ack/main_test.go index 690a0507..5fe316ca 100644 --- a/itest/pkg/poc2_dht_offer_ack/main_test.go +++ b/itest/pkg/poc2_dht_offer_ack/main_test.go @@ -5,17 +5,13 @@ package poc2_dht_offer_ack import ( "context" - "errors" - "fmt" "os" "testing" - tc "github.com/wcgcyx/testcontainers-go" - - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" ) const lotusAP = "http://lotus-full-node:1234/rpc/v0" @@ -26,120 +22,18 @@ var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net - ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start 3 providers - var providerContainers []*tc.Container - for i := 0; i < 3; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - - // Start 33 gateways - var gatewayContainers []*tc.Container - for i := 0; i < 33; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() + lotusToken, superAcct = fil.GetLotusToken() logging.Info("Lotus token is: %s", lotusToken) logging.Info("Super Acct is %s", superAcct) - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating Lotus test container: %s", err.Error()) - } - for _, c := range gatewayContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating gateway test container: %s", err.Error()) - } - } - for _, c := range providerContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating provider test container: %s", err.Error()) - } - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + const testName = "poc2-dht-offer-ack" + ctx := context.Background() + containersAndPorts, network, err := tc.StartContainers(ctx, 33, 3, testName, true) if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, containersAndPorts, network) + os.Exit(1) } - return + defer tc.StopContainers(ctx, containersAndPorts, network) + m.Run() } diff --git a/itest/pkg/poc2_dht_offer_new_gateway/main_test.go b/itest/pkg/poc2_dht_offer_new_gateway/main_test.go index f8c109aa..56481efd 100644 --- a/itest/pkg/poc2_dht_offer_new_gateway/main_test.go +++ b/itest/pkg/poc2_dht_offer_new_gateway/main_test.go @@ -5,17 +5,13 @@ package poc2_dht_offer_new_gateway import ( "context" - "errors" - "fmt" "os" "testing" - tc "github.com/wcgcyx/testcontainers-go" - - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" ) const lotusAP = "http://lotus-full-node:1234/rpc/v0" @@ -26,120 +22,18 @@ var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net - ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start 3 providers - var providerContainers []*tc.Container - for i := 0; i < 3; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - - // Start 33 gateways - var gatewayContainers []*tc.Container - for i := 0; i < 33; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() + lotusToken, superAcct = fil.GetLotusToken() logging.Info("Lotus token is: %s", lotusToken) logging.Info("Super Acct is %s", superAcct) - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating Lotus test container: %s", err.Error()) - } - for _, c := range gatewayContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating gateway test container: %s", err.Error()) - } - } - for _, c := range providerContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating provider test container: %s", err.Error()) - } - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + const testName = "poc2-dht-offer-new-gateway" + ctx := context.Background() + containersAndPorts, network, err := tc.StartContainers(ctx, 33, 3, testName, true) if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, containersAndPorts, network) + os.Exit(1) } - return + defer tc.StopContainers(ctx, containersAndPorts, network) + m.Run() } diff --git a/itest/pkg/poc2_dht_offer_new_gateway/publish_dht_offer_with_new_gateway_test.go b/itest/pkg/poc2_dht_offer_new_gateway/publish_dht_offer_with_new_gateway_test.go index 2257cc4f..559bf67c 100644 --- a/itest/pkg/poc2_dht_offer_new_gateway/publish_dht_offer_with_new_gateway_test.go +++ b/itest/pkg/poc2_dht_offer_new_gateway/publish_dht_offer_with_new_gateway_test.go @@ -1,6 +1,7 @@ package poc2_dht_offer_new_gateway import ( + "context" "fmt" "testing" "time" @@ -15,7 +16,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -28,8 +30,9 @@ func TestPublishDHTOfferWithNewGateway(t *testing.T) { t.Log("/* Start TestPublishDHTOfferWithNewGateway */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -65,7 +68,7 @@ func TestPublishDHTOfferWithNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -99,7 +102,7 @@ func TestPublishDHTOfferWithNewGateway(t *testing.T) { } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } } @@ -117,7 +120,7 @@ func TestPublishDHTOfferWithNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() pIDs = append(pIDs, providerID) @@ -198,7 +201,7 @@ func TestPublishDHTOfferWithNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() newGatewayID, err := nodeid.NewNodeIDFromHexString("3880000000000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) diff --git a/itest/pkg/poc2_group_offer/force_update_test.go b/itest/pkg/poc2_group_offer/force_update_test.go index 8af1f3ef..fcc3bd53 100644 --- a/itest/pkg/poc2_group_offer/force_update_test.go +++ b/itest/pkg/poc2_group_offer/force_update_test.go @@ -1,6 +1,7 @@ package poc2_group_offer import ( + "context" "fmt" "testing" "time" @@ -14,7 +15,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -23,8 +25,12 @@ func TestForceUpdate(t *testing.T) { t.Log("/* Start TestForceUpdate */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -37,22 +43,48 @@ func TestForceUpdate(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, 10*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() } defer rm.ShutdownAndWait() + walletKey := privateKeys[0] + // walletAddress := accountAddrs[0] + privateKeys = privateKeys[1:] + accountAddrs = accountAddrs[1:] + + // Configure client + clientConfBuilder := fcrclient.CreateSettings() + clientConfBuilder.SetEstablishmentTTL(101) + clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) + clientConfBuilder.SetWalletPrivateKey(walletKey) + clientConfBuilder.SetLotusAP(lotusAP) + clientConfBuilder.SetLotusAuthToken(lotusToken) + clientConf := clientConfBuilder.Build() + client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) + if !assert.Nil(t, err, "Error should be nil") { + t.Fatal(err) + } + res := client.PaymentMgr() + if !assert.NotNil(t, res, "Fail to initialise a payment manager") { + t.FailNow() + } + // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL("http://" + containers.Register.GetRegisterHostApiEndpoint()) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Initialise gateways - var gwIDs []*nodeid.NodeID + + // map between gateway ID and gateway name + var gateways = make(map[string]*nodeid.NodeID) // Only initialise 32 gateways, with one extra to initialise later to test list single cid offer for i := 0; i < 32; i++ { walletKey := privateKeys[0] @@ -60,7 +92,7 @@ func TestForceUpdate(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -73,28 +105,39 @@ func TestForceUpdate(t *testing.T) { if err != nil { panic(err) } - gwIDs = append(gwIDs, gatewayID) - identifier := fmt.Sprintf("-%v", i) + gatewayName := fmt.Sprintf("gateway-%v", i) + gateways[gatewayName] = gatewayID + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), walletAddress, gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[7:], - gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[7:], - gatewayConfig.GetString("NETWORK_INFO_CLIENT")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_CLIENT")[7:], - gatewayConfig.GetString("NETWORK_INFO_ADMIN")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_ADMIN")[7:], + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = gwAdmin.InitialiseGatewayV2(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) + t.FailNow() + } + + // add to client + added := client.AddGatewaysToUse([]*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, added, "1 gateway should be added") { + t.FailNow() + } + activatedCount := client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, activatedCount, "1 gateway should be active") { t.FailNow() } } @@ -102,33 +145,38 @@ func TestForceUpdate(t *testing.T) { // Initialise providers pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL("http://" + containers.Register.GetRegisterHostApiEndpoint()) pConf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*pConf) - var pIDs []*nodeid.NodeID + + // map between provider ID and provider name + var providers = make(map[string]*nodeid.NodeID) for i := 0; i < 3; i++ { walletKey := privateKeys[0] walletAddress := accountAddrs[0] privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() - pIDs = append(pIDs, providerID) - identifier := fmt.Sprintf("-%v", i) + providerName := fmt.Sprintf("provider-%v", i) + providers[providerName] = providerID + // get endpoint for Docker host + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + providerRegistrar := register.NewProviderRegister( providerID.ToString(), walletAddress, providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_GATEWAY")[8:], - providerConfig.GetString("NETWORK_INFO_CLIENT")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_CLIENT")[8:], - providerConfig.GetString("NETWORK_INFO_ADMIN")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_ADMIN")[8:], + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) // Initialise the provider using provider admin - err = pAdmin.InitialiseProviderV2(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = pAdmin.InitialiseProviderV2(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } @@ -139,49 +187,18 @@ func TestForceUpdate(t *testing.T) { } } - walletKey := privateKeys[0] - // walletAddress := accountAddrs[0] - privateKeys = privateKeys[1:] - accountAddrs = accountAddrs[1:] - - // Configure client - clientConfBuilder := fcrclient.CreateSettings() - clientConfBuilder.SetEstablishmentTTL(101) - clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) - clientConfBuilder.SetWalletPrivateKey(walletKey) - clientConfBuilder.SetLotusAP(lotusAP) - clientConfBuilder.SetLotusAuthToken(lotusToken) - clientConf := clientConfBuilder.Build() - client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) - if !assert.Nil(t, err, "Error should be nil") { - t.Fatal(err) - } - res := client.PaymentMgr() - if !assert.NotNil(t, res, "Fail to initialise a payment manager") { - t.FailNow() - } - - added := client.AddGatewaysToUse(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be added") { - t.FailNow() - } - - added = client.AddActiveGateways(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be active") { - t.FailNow() - } - // Force providers and gateways to update - for _, p := range pIDs { - err := pAdmin.ForceUpdate(p) + for providerName, providerID := range providers { + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + err := pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { logging.Error("provider update error: %s", err) t.FailNow() } } - for _, g := range gwIDs { - err := gwAdmin.ForceUpdate(g) + for gatewayName, gatewayID := range gateways { + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() + err := gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { logging.Error("gateway update error: %s", err) t.FailNow() diff --git a/itest/pkg/poc2_group_offer/initialise_client_test.go b/itest/pkg/poc2_group_offer/initialise_client_test.go index 83d90198..92b456a0 100644 --- a/itest/pkg/poc2_group_offer/initialise_client_test.go +++ b/itest/pkg/poc2_group_offer/initialise_client_test.go @@ -1,6 +1,7 @@ package poc2_group_offer import ( + "context" "fmt" "testing" "time" @@ -14,7 +15,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" ) func TestInitialiseClient(t *testing.T) { @@ -22,8 +24,12 @@ func TestInitialiseClient(t *testing.T) { t.Log("/* Start TestInitialiseClient */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -36,7 +42,8 @@ func TestInitialiseClient(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, 10*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -46,11 +53,13 @@ func TestInitialiseClient(t *testing.T) { // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) - // Initialise gateways + // map between gateway ID and gateway name + var gateways = make(map[string]*nodeid.NodeID) + var gwIDs []*nodeid.NodeID // Only initialise 32 gateways, with one extra to initialise later to test list single cid offer for i := 0; i < 32; i++ { @@ -59,7 +68,7 @@ func TestInitialiseClient(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -72,28 +81,30 @@ func TestInitialiseClient(t *testing.T) { if err != nil { panic(err) } - gwIDs = append(gwIDs, gatewayID) - identifier := fmt.Sprintf("-%v", i) + gatewayName := fmt.Sprintf("gateway-%v", i) + gateways[gatewayName] = gatewayID + gwIDs = append(gwIDs, gatewayID) + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), walletAddress, gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[7:], - gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[7:], - gatewayConfig.GetString("NETWORK_INFO_CLIENT")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_CLIENT")[7:], - gatewayConfig.GetString("NETWORK_INFO_ADMIN")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_ADMIN")[7:], + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = gwAdmin.InitialiseGatewayV2(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } } @@ -107,7 +118,7 @@ func TestInitialiseClient(t *testing.T) { clientConfBuilder := fcrclient.CreateSettings() clientConfBuilder.SetEstablishmentTTL(101) clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) clientConfBuilder.SetWalletPrivateKey(walletKey) clientConfBuilder.SetLotusAP(lotusAP) clientConfBuilder.SetLotusAuthToken(lotusToken) @@ -126,8 +137,13 @@ func TestInitialiseClient(t *testing.T) { t.Fatal() } - added = client.AddActiveGateways(gwIDs) - assert.Equal(t, 32, added, "32 gateways should be active") + for gatewayName, gatewayID := range gateways { + _, _, gatewayClientApiEndpoint, _ := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() + activatedCount := client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, activatedCount, "gateway should be active") { + t.FailNow() + } + } t.Log("/*******************************************************/") t.Log("/* End TestInitialiseClient */") diff --git a/itest/pkg/poc2_group_offer/initialise_gateways_test.go b/itest/pkg/poc2_group_offer/initialise_gateways_test.go index 746109d6..adc67764 100644 --- a/itest/pkg/poc2_group_offer/initialise_gateways_test.go +++ b/itest/pkg/poc2_group_offer/initialise_gateways_test.go @@ -1,6 +1,7 @@ package poc2_group_offer import ( + "context" "fmt" "testing" @@ -9,7 +10,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" ) func TestInitialiseGateways(t *testing.T) { @@ -17,8 +19,12 @@ func TestInitialiseGateways(t *testing.T) { t.Log("/* Start TestInitialiseGateways */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -28,9 +34,11 @@ func TestInitialiseGateways(t *testing.T) { panic(err) } + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + confBuilder := fcrgatewayadmin.CreateSettings() confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL(registerApiEndpoint) conf := confBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*conf) @@ -43,7 +51,7 @@ func TestInitialiseGateways(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() if err != nil { panic(err) } @@ -61,22 +69,23 @@ func TestInitialiseGateways(t *testing.T) { } gwIDs = append(gwIDs, gatewayID) - identifier := fmt.Sprintf("-%v", i) + gatewayName := fmt.Sprintf("gateway-%v", i) gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), walletAddress, gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[7:], - gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[7:], - gatewayConfig.GetString("NETWORK_INFO_CLIENT")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_CLIENT")[7:], - gatewayConfig.GetString("NETWORK_INFO_ADMIN")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_ADMIN")[7:], + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() + err = gwAdmin.InitialiseGatewayV2(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } } diff --git a/itest/pkg/poc2_group_offer/initialise_providers_test.go b/itest/pkg/poc2_group_offer/initialise_providers_test.go index dde574d4..ce9364c3 100644 --- a/itest/pkg/poc2_group_offer/initialise_providers_test.go +++ b/itest/pkg/poc2_group_offer/initialise_providers_test.go @@ -1,6 +1,7 @@ package poc2_group_offer import ( + "context" "fmt" "testing" @@ -8,7 +9,7 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -17,8 +18,12 @@ func TestInitialiseProviders(t *testing.T) { t.Log("/* Start TestInitialiseProviders */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -28,14 +33,13 @@ func TestInitialiseProviders(t *testing.T) { panic(err) } + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() confBuilder := fcrprovideradmin.CreateSettings() confBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - confBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + confBuilder.SetRegisterURL(registerApiEndpoint) conf := confBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*conf) - var pIDs []*nodeid.NodeID - for i := 0; i < 3; i++ { walletKey := privateKeys[0] @@ -60,21 +64,22 @@ func TestInitialiseProviders(t *testing.T) { panic(err) } providerID := nodeid.NewRandomNodeID() - pIDs = append(pIDs, providerID) - identifier := fmt.Sprintf("-%v", i) + providerName := fmt.Sprintf("provider-%v", i) + // get endpoint for Docker host + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() providerRegistrar := register.NewProviderRegister( providerID.ToString(), walletAddress, providerRootSigningKey, providerRetrievalSigningKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_GATEWAY")[8:], - providerConfig.GetString("NETWORK_INFO_CLIENT")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_CLIENT")[8:], - providerConfig.GetString("NETWORK_INFO_ADMIN")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_ADMIN")[8:], + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) - err = pAdmin.InitialiseProviderV2(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = pAdmin.InitialiseProviderV2(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { logging.Error("error initialising provider: %s", err.Error()) t.FailNow() diff --git a/itest/pkg/poc2_group_offer/main_test.go b/itest/pkg/poc2_group_offer/main_test.go index 65c504b7..fd68e1d8 100644 --- a/itest/pkg/poc2_group_offer/main_test.go +++ b/itest/pkg/poc2_group_offer/main_test.go @@ -5,141 +5,32 @@ package poc2_group_offer import ( "context" - "errors" - "fmt" "os" "testing" - tc "github.com/wcgcyx/testcontainers-go" + "github.com/testcontainers/testcontainers-go" - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" ) -const lotusAP = "http://lotus-full-node:1234/rpc/v0" - -var lotusToken string -var superAcct string var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") +var registerConfig = config.NewConfig(".env.register") +var containers tc.AllContainers func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net + const testName = "poc2-group-offer" + var network *testcontainers.Network + var err error ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start 3 providers - var providerContainers []*tc.Container - for i := 0; i < 3; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - - // Start 33 gateways - var gatewayContainers []*tc.Container - for i := 0; i < 33; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() - logging.Info("Lotus token is: %s", lotusToken) - logging.Info("Super Acct is %s", superAcct) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating Lotus test container: %s", err.Error()) - } - for _, c := range gatewayContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating gateway test container: %s", err.Error()) - } - } - for _, c := range providerContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating provider test container: %s", err.Error()) - } - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + containers, network, err = tc.StartContainers(ctx, 33, 3, testName, true, gatewayConfig, providerConfig, registerConfig) if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } - return + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } diff --git a/itest/pkg/poc2_group_offer/new_accounts_test.go b/itest/pkg/poc2_group_offer/new_accounts_test.go index 77483772..0ae33886 100644 --- a/itest/pkg/poc2_group_offer/new_accounts_test.go +++ b/itest/pkg/poc2_group_offer/new_accounts_test.go @@ -1,9 +1,10 @@ package poc2_group_offer import ( + "context" "testing" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" ) func TestNewAccounts(t *testing.T) { @@ -11,8 +12,12 @@ func TestNewAccounts(t *testing.T) { t.Log("/* Start TestNewAccounts */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } diff --git a/itest/pkg/poc2_group_offer/publish_group_offer_test.go b/itest/pkg/poc2_group_offer/publish_group_offer_test.go index 02f95ad8..d187a0cb 100644 --- a/itest/pkg/poc2_group_offer/publish_group_offer_test.go +++ b/itest/pkg/poc2_group_offer/publish_group_offer_test.go @@ -1,6 +1,7 @@ package poc2_group_offer import ( + "context" "fmt" "testing" "time" @@ -15,7 +16,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -24,8 +26,12 @@ func TestPublishGroupOffer(t *testing.T) { t.Log("/* Start TestPublishGroupOffer */") t.Log("/*******************************************************/") + ctx := context.Background() + lotusToken, superAcct := fil.GetLotusToken() + lotusDaemonApiEndpoint, _ := containers.Lotus.GetLostHostApiEndpoints() + var lotusAP = "http://" + lotusDaemonApiEndpoint + "/rpc/v0" var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -38,22 +44,49 @@ func TestPublishGroupOffer(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(gatewayConfig.GetString("REGISTER_API_URL"), true, true, 10*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() } defer rm.ShutdownAndWait() + walletKey := privateKeys[0] + // walletAddress := accountAddrs[0] + privateKeys = privateKeys[1:] + accountAddrs = accountAddrs[1:] + + // Configure client + clientConfBuilder := fcrclient.CreateSettings() + clientConfBuilder.SetEstablishmentTTL(101) + clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) + clientConfBuilder.SetWalletPrivateKey(walletKey) + clientConfBuilder.SetLotusAP(lotusAP) + clientConfBuilder.SetLotusAuthToken(lotusToken) + clientConf := clientConfBuilder.Build() + client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) + if !assert.Nil(t, err, "Error should be nil") { + t.Fatal(err) + } + res := client.PaymentMgr() + if !assert.NotNil(t, res, "Fail to initialise a payment manager") { + t.FailNow() + } + // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Initialise gateways - var gwIDs []*nodeid.NodeID + + // map between gateway ID and gateway name + var gateways = make(map[string]*nodeid.NodeID) + // Only initialise 32 gateways, with one extra to initialise later to test list single cid offer for i := 0; i < 32; i++ { walletKey := privateKeys[0] @@ -61,7 +94,7 @@ func TestPublishGroupOffer(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -74,29 +107,40 @@ func TestPublishGroupOffer(t *testing.T) { if err != nil { panic(err) } - gwIDs = append(gwIDs, gatewayID) - identifier := fmt.Sprintf("-%v", i) + gatewayName := fmt.Sprintf("gateway-%v", i) + gateways[gatewayName] = gatewayID + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), walletAddress, gatewayRootPubKey, gatewayRetrievalPubKey, gatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_GATEWAY")[7:], - gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_PROVIDER")[7:], - gatewayConfig.GetString("NETWORK_INFO_CLIENT")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_CLIENT")[7:], - gatewayConfig.GetString("NETWORK_INFO_ADMIN")[:7]+identifier+gatewayConfig.GetString("NETWORK_INFO_ADMIN")[7:], + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = gwAdmin.InitialiseGatewayV2(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) + t.FailNow() + } + + // add to client + added := client.AddGatewaysToUse([]*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, added, "1 gateway should be added") { + t.FailNow() + } + activatedCount := client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, activatedCount, "1 gateway should be active") { t.FailNow() } } @@ -104,33 +148,38 @@ func TestPublishGroupOffer(t *testing.T) { // Initialise providers pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL(registerApiEndpoint) pConf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*pConf) - var pIDs []*nodeid.NodeID + + // map between provider ID and provider name + var providers = make(map[string]*nodeid.NodeID) for i := 0; i < 3; i++ { walletKey := privateKeys[0] walletAddress := accountAddrs[0] privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() - pIDs = append(pIDs, providerID) - identifier := fmt.Sprintf("-%v", i) + providerName := fmt.Sprintf("provider-%v", i) + providers[providerName] = providerID + // get endpoint for Docker host + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + providerRegistrar := register.NewProviderRegister( providerID.ToString(), walletAddress, providerRootPubKey, providerRetrievalPubKey, providerConfig.GetString("PROVIDER_REGION_CODE"), - providerConfig.GetString("NETWORK_INFO_GATEWAY")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_GATEWAY")[8:], - providerConfig.GetString("NETWORK_INFO_CLIENT")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_CLIENT")[8:], - providerConfig.GetString("NETWORK_INFO_ADMIN")[:8]+identifier+providerConfig.GetString("NETWORK_INFO_ADMIN")[8:], + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) // Initialise the provider using provider admin - err = pAdmin.InitialiseProviderV2(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) + err = pAdmin.InitialiseProviderV2(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { panic(err) } @@ -141,49 +190,18 @@ func TestPublishGroupOffer(t *testing.T) { } } - walletKey := privateKeys[0] - // walletAddress := accountAddrs[0] - privateKeys = privateKeys[1:] - accountAddrs = accountAddrs[1:] - - // Configure client - clientConfBuilder := fcrclient.CreateSettings() - clientConfBuilder.SetEstablishmentTTL(101) - clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(gatewayConfig.GetString("REGISTER_API_URL")) - clientConfBuilder.SetWalletPrivateKey(walletKey) - clientConfBuilder.SetLotusAP(lotusAP) - clientConfBuilder.SetLotusAuthToken(lotusToken) - clientConf := clientConfBuilder.Build() - client, err := fcrclient.NewFilecoinRetrievalClient(*clientConf, rm) - if !assert.Nil(t, err, "Error should be nil") { - t.Fatal(err) - } - res := client.PaymentMgr() - if !assert.NotNil(t, res, "Fail to initialise a payment manager") { - t.FailNow() - } - - added := client.AddGatewaysToUse(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be added") { - t.FailNow() - } - - added = client.AddActiveGateways(gwIDs) - if !assert.Equal(t, 32, added, "32 gateways should be active") { - t.FailNow() - } - // Force providers and gateways to update - for _, p := range pIDs { - err := pAdmin.ForceUpdate(p) + for providerName, providerID := range providers { + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() + err := pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { logging.Error("provider update error: %s", err) t.FailNow() } } - for _, g := range gwIDs { - err := gwAdmin.ForceUpdate(g) + for gatewayName, gatewayID := range gateways { + _, _, _, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() + err := gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { logging.Error("gateway update error: %s", err) t.FailNow() @@ -204,7 +222,8 @@ func TestPublishGroupOffer(t *testing.T) { t.Fatal(err) } expiryDate := time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishGroupCID(pIDs[0], []cid.ContentID{*contentID01, *contentID02, *contentID03}, 42, expiryDate, 42) + _, _, providerAdminApiEndpoint := containers.Providers["provider-0"].GetProviderHostApiEndpoints() + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providers["provider-0"], []cid.ContentID{*contentID01, *contentID02, *contentID03}, 42, expiryDate, 42) if err != nil { t.Fatal(err) } @@ -223,13 +242,15 @@ func TestPublishGroupOffer(t *testing.T) { t.Fatal(err) } expiryDate = time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() - err = pAdmin.PublishGroupCID(pIDs[1], []cid.ContentID{*contentID11, *contentID12, *contentID13}, 42, expiryDate, 42) + _, _, providerAdminApiEndpoint = containers.Providers["provider-1"].GetProviderHostApiEndpoints() + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providers["provider-1"], []cid.ContentID{*contentID11, *contentID12, *contentID13}, 42, expiryDate, 42) if err != nil { t.Fatal(err) } // Query gateway 0 for offer 0 - offers, err := client.FindOffersStandardDiscoveryV2(contentID01, gwIDs[0], 1) + _, _, gatewayClientApiEndpoint, _ := containers.Gateways["gateway-0"].GetGatewayHostApiEndpoints() + offers, err := client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID01, gateways["gateway-0"], 1) if err != nil { panic(err) } @@ -238,7 +259,7 @@ func TestPublishGroupOffer(t *testing.T) { } // Query gateway 0 for offer 1 - offers, err = client.FindOffersStandardDiscoveryV2(contentID11, gwIDs[0], 1) + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID11, gateways["gateway-0"], 1) if err != nil { panic(err) } @@ -248,7 +269,8 @@ func TestPublishGroupOffer(t *testing.T) { } // Query gateway 1 for offer 0 - offers, err = client.FindOffersStandardDiscoveryV2(contentID01, gwIDs[1], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-1"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID01, gateways["gateway-1"], 1) if err != nil { panic(err) } @@ -258,7 +280,7 @@ func TestPublishGroupOffer(t *testing.T) { } // Query gateway 1 for offer 1 - offers, err = client.FindOffersStandardDiscoveryV2(contentID11, gwIDs[1], 1) + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID11, gateways["gateway-1"], 1) if err != nil { panic(err) } @@ -267,7 +289,8 @@ func TestPublishGroupOffer(t *testing.T) { } // Query gateway 2 for offer 0 - offers, err = client.FindOffersStandardDiscoveryV2(contentID01, gwIDs[2], 1) + _, _, gatewayClientApiEndpoint, _ = containers.Gateways["gateway-2"].GetGatewayHostApiEndpoints() + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID01, gateways["gateway-2"], 1) if err != nil { panic(err) } @@ -277,7 +300,7 @@ func TestPublishGroupOffer(t *testing.T) { } // Query gateway 2 for offer 1 - offers, err = client.FindOffersStandardDiscoveryV2(contentID11, gwIDs[2], 1) + offers, err = client.FindOffersStandardDiscoveryV2(gatewayClientApiEndpoint, contentID11, gateways["gateway-2"], 1) if err != nil { panic(err) } diff --git a/itest/pkg/poc2_new_gateway/main_test.go b/itest/pkg/poc2_new_gateway/main_test.go index 957ef87d..1c5be28c 100644 --- a/itest/pkg/poc2_new_gateway/main_test.go +++ b/itest/pkg/poc2_new_gateway/main_test.go @@ -5,17 +5,13 @@ package poc2_new_gateway import ( "context" - "errors" - "fmt" "os" "testing" - tc "github.com/wcgcyx/testcontainers-go" - - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/logging" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" ) const lotusAP = "http://lotus-full-node:1234/rpc/v0" @@ -26,120 +22,18 @@ var gatewayConfig = config.NewConfig(".env.gateway") var providerConfig = config.NewConfig(".env.provider") func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net - ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start 3 providers - var providerContainers []*tc.Container - for i := 0; i < 3; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - - // Start 33 gateways - var gatewayContainers []*tc.Container - for i := 0; i < 33; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() + lotusToken, superAcct = fil.GetLotusToken() logging.Info("Lotus token is: %s", lotusToken) logging.Info("Super Acct is %s", superAcct) - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, "") - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := lotusContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating Lotus test container: %s", err.Error()) - } - for _, c := range gatewayContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating gateway test container: %s", err.Error()) - } - } - for _, c := range providerContainers { - if err := (*c).Terminate(ctx); err != nil { - logging.Error("error while terminating provider test container: %s", err.Error()) - } - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) - } -} - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + const testName = "poc2-new-gateway" + ctx := context.Background() + containersAndPorts, network, err := tc.StartContainers(ctx, 33, 3, testName, true) if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, containersAndPorts, network) + os.Exit(1) } - return + defer tc.StopContainers(ctx, containersAndPorts, network) + m.Run() } diff --git a/itest/pkg/poc2_new_gateway/new_gateway_test.go b/itest/pkg/poc2_new_gateway/new_gateway_test.go index e160a2b8..e3c8ff38 100644 --- a/itest/pkg/poc2_new_gateway/new_gateway_test.go +++ b/itest/pkg/poc2_new_gateway/new_gateway_test.go @@ -1,6 +1,7 @@ package poc2_new_gateway import ( + "context" "fmt" "testing" "time" @@ -15,7 +16,8 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/nodeid" "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -27,8 +29,9 @@ func TestNewGateway(t *testing.T) { t.Log("/* Start TestNewGateway */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -64,7 +67,7 @@ func TestNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -98,7 +101,7 @@ func TestNewGateway(t *testing.T) { } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } } @@ -116,7 +119,7 @@ func TestNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() pIDs = append(pIDs, providerID) @@ -266,7 +269,7 @@ func TestNewGateway(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() newGatewayID, err := nodeid.NewNodeIDFromHexString("3880000000000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) diff --git a/itest/pkg/poc2js/poc2js_test.go b/itest/pkg/poc2js/poc2js_test.go index 38165f0f..c0b806c9 100644 --- a/itest/pkg/poc2js/poc2js_test.go +++ b/itest/pkg/poc2js/poc2js_test.go @@ -2,15 +2,12 @@ package poc2v2 import ( "context" - "errors" "fmt" "os" - "strings" "testing" "time" "github.com/stretchr/testify/assert" - tc "github.com/wcgcyx/testcontainers-go" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrregistermgr" @@ -19,7 +16,11 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + fil "github.com/ConsenSys/fc-retrieval/itest/pkg/util/filecoin-facade" + js "github.com/ConsenSys/fc-retrieval/itest/pkg/util/jsclient-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" + "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) @@ -36,82 +37,20 @@ const ( ) func TestMain(m *testing.M) { - - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - lotusToken = os.Getenv("LOTUS_TOKEN") - superAcct = os.Getenv("SUPER_ACCT") - // Env is set, we are calling from docker container - // This logging should be only called after all tests finished. - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, 17 gateways and 3 providers - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net - ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - defer (*network).Remove(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - defer redisContainer.Terminate(ctx) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - defer registerContainer.Terminate(ctx) - // Start providers - var providerContainers []*tc.Container - for i := 0; i < nProviderContainers; i++ { - c := util.StartProvider(ctx, fmt.Sprintf("provider-%v", i), networkName, util.ColorBlue, pvEnv, true) - providerContainers = append(providerContainers, &c) - } - defer func() { - for _, c := range providerContainers { - (*c).Terminate(ctx) - } - }() - // Start gateways - var gatewayContainers []*tc.Container - - for i := 0; i < nGateways; i++ { - c := util.StartGateway(ctx, fmt.Sprintf("gateway-%v", i), networkName, util.ColorCyan, gwEnv, true) - gatewayContainers = append(gatewayContainers, &c) - } - defer func() { - for _, c := range gatewayContainers { - (*c).Terminate(ctx) - } - }() - - // Start lotus - lotusContainer := util.StartLotusFullNode(ctx, networkName, false) - defer lotusContainer.Terminate(ctx) - - reloadJsTests := os.Getenv("RELOAD_JS_TESTS") - // Get lotus token - lotusToken, superAcct = util.GetLotusToken() + lotusToken, superAcct = fil.GetLotusToken() logging.Info("Lotus token is: %s", lotusToken) logging.Info("Super Acct is %s", superAcct) - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, lotusToken, superAcct, done, true, reloadJsTests) - defer itestContainer.Terminate(ctx) - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Fatal("Tests failed, shutdown...") + const testName = "poc2-new-gateway" + ctx := context.Background() + containersAndPorts, network, err := tc.StartContainers(ctx, nGateways, nProviderContainers, testName, true) + if err != nil { + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, containersAndPorts, network) + os.Exit(1) } + defer tc.StopContainers(ctx, containersAndPorts, network) + m.Run() } func TestNewAccounts(t *testing.T) { @@ -119,8 +58,9 @@ func TestNewAccounts(t *testing.T) { t.Log("/* Start POC2-JS TestNewAccounts */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, nGateways+nProviderContainers+3) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, nGateways+nProviderContainers+3) if err != nil { t.Fatal(err) } @@ -138,8 +78,9 @@ func TestInitialiseProviders(t *testing.T) { t.Log("/* Start POC2-JS TestInitialiseProviders */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -212,8 +153,9 @@ func TestInitialiseGateways(t *testing.T) { t.Log("/* Start POC2-JS TestInitialiseGateways */") t.Log("/*******************************************************/") + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -237,7 +179,7 @@ func TestInitialiseGateways(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -267,7 +209,7 @@ func TestInitialiseGateways(t *testing.T) { err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } } @@ -283,10 +225,11 @@ func TestClientJS(t *testing.T) { t.Log("/* Start POC2-JS TestClientJS */") t.Log("/*******************************************************/") - assert.Nil(t, util.CallClientJsInstall()) + assert.Nil(t, js.CallClientJsInstall()) + ctx := context.Background() var err error - privateKeys, accountAddrs, err := util.GenerateAccount(lotusAP, lotusToken, superAcct, 37) + privateKeys, accountAddrs, err := fil.GenerateAccount(ctx, lotusAP, lotusToken, superAcct, 37) if err != nil { t.Fatal(err) } @@ -317,7 +260,7 @@ func TestClientJS(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID := nodeid.NewRandomNodeID() pIDs = append(pIDs, providerID) @@ -358,7 +301,7 @@ func TestClientJS(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() var idStr string if i%2 == 0 { idStr = fmt.Sprintf("%X000000000000000000000000000000000000000000000000000000000000000", i/2) @@ -387,12 +330,12 @@ func TestClientJS(t *testing.T) { ) err = gwAdmin.InitialiseGatewayV2(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1), walletKey, lotusAP, lotusToken) if err != nil { - logging.Error("error initialising gateway: %s", err.Error()) + logging.Error("gateway initialising error: %s", err.Error()) t.FailNow() } // Enroll the gateway in the Register srv. if err := rm.RegisterGateway(gatewayRegistrar); err != nil { - logging.Error("error registering gateway: %s", err.Error()) + logging.Error("gateway registering error: %s", err.Error()) t.FailNow() } } @@ -405,17 +348,7 @@ func TestClientJS(t *testing.T) { privateKeys = privateKeys[1:] accountAddrs = accountAddrs[1:] - var gtws []string - for _, d := range gwIDs { - gtws = append(gtws, d.ToString()) - } - - var provs []string - for _, d := range pIDs { - provs = append(provs, d.ToString()) - } - - err = util.CallClientJsE2E(key, walletKey, gatewayConfig.GetString("REGISTER_API_URL"), lotusAP, lotusToken, strings.Join(gtws, ","), strings.Join(provs, ",")) + err = js.CallClientJsE2E(key, walletKey, gatewayConfig.GetString("REGISTER_API_URL"), lotusAP, lotusToken) if err != nil { logging.Error("error calling JS client E2E: %s", err.Error()) t.FailNow() @@ -425,33 +358,3 @@ func TestClientJS(t *testing.T) { t.Log("/* End POC2-JS TestClientJS */") t.Log("/*******************************************************/") } - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) - } - return -} diff --git a/itest/pkg/provider-admin/provideradmin_test.go b/itest/pkg/provider-admin/provideradmin_test.go index b17909c2..345a3f7a 100644 --- a/itest/pkg/provider-admin/provideradmin_test.go +++ b/itest/pkg/provider-admin/provideradmin_test.go @@ -20,14 +20,13 @@ package provider_admin import ( "context" - "errors" - "fmt" "os" "strconv" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/testcontainers/testcontainers-go" "github.com/ConsenSys/fc-retrieval/client/pkg/fcrclient" "github.com/ConsenSys/fc-retrieval/common/pkg/cid" @@ -38,76 +37,30 @@ import ( "github.com/ConsenSys/fc-retrieval/common/pkg/register" "github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/fcrgatewayadmin" "github.com/ConsenSys/fc-retrieval/itest/config" - "github.com/ConsenSys/fc-retrieval/itest/pkg/util" + cr "github.com/ConsenSys/fc-retrieval/itest/pkg/util/crypto-facade" + tc "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers" "github.com/ConsenSys/fc-retrieval/provider-admin/pkg/fcrprovideradmin" ) // Test the Provider Admin API. -var providerTestProviderConfig = config.NewConfig(".env.provider") -var gatewayConfigGatewayConfig = config.NewConfig(".env.gateway") +var providerConfig = config.NewConfig(".env.provider") +var gatewayConfig = config.NewConfig(".env.gateway") +var registerConfig = config.NewConfig(".env.register") +var containers tc.AllContainers func TestMain(m *testing.M) { - // Need to make sure this env is not set in host machine - itestEnv := os.Getenv("ITEST_CALLING_FROM_CONTAINER") - - if itestEnv != "" { - // Env is set, we are calling from docker container - m.Run() - return - } - // Env is not set, we are calling from host - // We need a redis, a register, a gateway and a provider - - // Get env - rgEnv := util.GetEnvMap("../../.env.register") - gwEnv := util.GetEnvMap("../../.env.gateway") - pvEnv := util.GetEnvMap("../../.env.provider") - - // Create shared net + const testName = "provider-admin" ctx := context.Background() - network, networkName := util.CreateNetwork(ctx) - - // Start redis - redisContainer := util.StartRedis(ctx, networkName, true) - - // Start register - registerContainer := util.StartRegister(ctx, networkName, util.ColorYellow, rgEnv, true) - - // Start gateway - gatewayContainer := util.StartGateway(ctx, "gateway", networkName, util.ColorBlue, gwEnv, true) - - // Start provider - providerContainer := util.StartProvider(ctx, "provider", networkName, util.ColorPurple, pvEnv, true) - - // Start itest - done := make(chan bool) - itestContainer := util.StartItest(ctx, networkName, util.ColorGreen, "", "", done, true, "") - - // Block until done. - if <-done { - logging.Info("Tests passed, shutdown...") - } else { - logging.Error("Tests failed, shutdown...") - } - - if err := itestContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := providerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := gatewayContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := registerContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := redisContainer.Terminate(ctx); err != nil { - logging.Error("error while terminating test container: %s", err.Error()) - } - if err := (*network).Remove(ctx); err != nil { - logging.Error("error while terminating test container network: %s", err.Error()) + var network *testcontainers.Network + var err error + containers, network, err = tc.StartContainers(ctx, 1, 1, testName, true, gatewayConfig, providerConfig, registerConfig) + if err != nil { + logging.Error("%s failed, container starting error: %s", testName, err.Error()) + tc.StopContainers(ctx, testName, containers, network) + os.Exit(1) } + defer tc.StopContainers(ctx, testName, containers, network) + m.Run() } func TestGetProviderAdminVersion(t *testing.T) { @@ -125,7 +78,6 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { logging.Info("/*******************************************************/") logging.Info("/* Start TestInitProviderAdminNoRetrievalKey */") logging.Info("/*******************************************************/") - logging.Error("Wait two seconds for the provider to deploy and be ready for requests") // Main key used across the chain blockchainPrivateKey, err := fcrcrypto.GenerateBlockchainKeyPair() @@ -135,7 +87,8 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { } // Create and start register manager - var rm = fcrregistermgr.NewFCRRegisterMgr(providerTestProviderConfig.GetString("REGISTER_API_URL"), true, true, 2*time.Second) + registerApiEndpoint := "http://" + containers.Register.GetRegisterHostApiEndpoint() + var rm = fcrregistermgr.NewFCRRegisterMgr(registerApiEndpoint, false, true, 10*time.Second) if err := rm.Start(); err != nil { logging.Error("error starting Register Manager: %s", err.Error()) t.FailNow() @@ -145,14 +98,14 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { // Configure gateway admin gConfBuilder := fcrgatewayadmin.CreateSettings() gConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - gConfBuilder.SetRegisterURL(providerTestProviderConfig.GetString("REGISTER_API_URL")) + gConfBuilder.SetRegisterURL(registerApiEndpoint) gConf := gConfBuilder.Build() gwAdmin := fcrgatewayadmin.NewFilecoinRetrievalGatewayAdmin(*gConf) // Configure provider admin pConfBuilder := fcrprovideradmin.CreateSettings() pConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - pConfBuilder.SetRegisterURL(providerTestProviderConfig.GetString("REGISTER_API_URL")) + pConfBuilder.SetRegisterURL(registerApiEndpoint) conf := pConfBuilder.Build() pAdmin := fcrprovideradmin.NewFilecoinRetrievalProviderAdmin(*conf) @@ -160,7 +113,7 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { clientConfBuilder := fcrclient.CreateSettings() clientConfBuilder.SetEstablishmentTTL(101) clientConfBuilder.SetBlockchainPrivateKey(blockchainPrivateKey) - clientConfBuilder.SetRegisterURL(providerTestProviderConfig.GetString("REGISTER_API_URL")) + clientConfBuilder.SetRegisterURL(registerApiEndpoint) clientConf := clientConfBuilder.Build() // Create client @@ -171,24 +124,26 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { } // Initialise gateway - gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := generateKeys() + gatewayRootPubKey, gatewayRetrievalPubKey, gatewayRetrievalPrivateKey, err := cr.GenerateKeys() gatewayID, err := nodeid.NewNodeIDFromHexString("ebc134a429ba7dc4811bf64ccb67057f5bd57ca4676800e2f71731cbcc5eb518") if err != nil { logging.Error("error generating gateway id") os.Exit(1) } + gatewayName := "gateway-0" + _, _, gatewayClientApiEndpoint, gatewayAdminApiEndpoint := containers.Gateways[gatewayName].GetGatewayHostApiEndpoints() gatewayRegistrar := register.NewGatewayRegister( gatewayID.ToString(), - gatewayConfigGatewayConfig.GetString("GATEWAY_ADDRESS"), + gatewayConfig.GetString("GATEWAY_ADDRESS"), gatewayRootPubKey, gatewayRetrievalPubKey, - gatewayConfigGatewayConfig.GetString("GATEWAY_REGION_CODE"), - gatewayConfigGatewayConfig.GetString("NETWORK_INFO_GATEWAY"), - gatewayConfigGatewayConfig.GetString("NETWORK_INFO_PROVIDER"), - gatewayConfigGatewayConfig.GetString("NETWORK_INFO_CLIENT"), - gatewayConfigGatewayConfig.GetString("NETWORK_INFO_ADMIN"), + gatewayConfig.GetString("GATEWAY_REGION_CODE"), + gatewayName+":"+gatewayConfig.GetString("BIND_GATEWAY_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_PROVIDER_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_REST_API"), + gatewayName+":"+gatewayConfig.GetString("BIND_ADMIN_API"), ) - err = gwAdmin.InitialiseGateway(gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = gwAdmin.InitialiseGateway(gatewayAdminApiEndpoint, gatewayRegistrar, gatewayRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { panic(err) } @@ -198,34 +153,36 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { } // Add the gateways to the passive list added := client.AddGatewaysToUse([]*nodeid.NodeID{gatewayID}) - if !assert.Equal(t, 1, added, "32 gateways should be added") { + if !assert.Equal(t, 1, added, "1 gateway should be added") { t.FailNow() } // Make the gateways active, this involves doing an establishment - addedActive := client.AddActiveGateways([]*nodeid.NodeID{gatewayID}) - if !assert.Equal(t, 1, addedActive, "32 gateways should be activated") { + addedActive := client.AddActiveGateways(gatewayClientApiEndpoint, []*nodeid.NodeID{gatewayID}) + if !assert.Equal(t, 1, addedActive, "1 gateway should be activated") { t.FailNow() } // Initialise provider - providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := generateKeys() + providerRootPubKey, providerRetrievalPubKey, providerRetrievalPrivateKey, err := cr.GenerateKeys() providerID, err := nodeid.NewNodeIDFromHexString("ebc134a429ba7dc4811bf64ccb67057f5bd57ca4676800e2f71731cbcc5eb518") if err != nil { logging.Error("can't generate provider ID: %s", err.Error()) os.Exit(1) } + providerName := "provider-0" + _, _, providerAdminApiEndpoint := containers.Providers[providerName].GetProviderHostApiEndpoints() providerRegistrar := register.NewProviderRegister( providerID.ToString(), - providerTestProviderConfig.GetString("PROVIDER_ADDRESS"), + providerConfig.GetString("PROVIDER_ADDRESS"), providerRootPubKey, providerRetrievalPubKey, - providerTestProviderConfig.GetString("PROVIDER_REGION_CODE"), - providerTestProviderConfig.GetString("NETWORK_INFO_GATEWAY"), - providerTestProviderConfig.GetString("NETWORK_INFO_CLIENT"), - providerTestProviderConfig.GetString("NETWORK_INFO_ADMIN"), + providerConfig.GetString("PROVIDER_REGION_CODE"), + providerName+":"+providerConfig.GetString("BIND_GATEWAY_API"), + providerName+":"+providerConfig.GetString("BIND_REST_API"), + providerName+":"+providerConfig.GetString("BIND_ADMIN_API"), ) // Initialise the provider using provider admin - err = pAdmin.InitialiseProvider(providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) + err = pAdmin.InitialiseProvider(providerAdminApiEndpoint, providerRegistrar, providerRetrievalPrivateKey, fcrcrypto.DecodeKeyVersion(1)) if err != nil { logging.ErrorAndPanic(err.Error()) } @@ -242,17 +199,17 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { expiryDate := time.Now().Local().Add(time.Hour * time.Duration(24)).Unix() // Force update - err = pAdmin.ForceUpdate(providerID) + err = pAdmin.ForceUpdate(providerAdminApiEndpoint, providerID) if err != nil { panic(err) } - err = gwAdmin.ForceUpdate(gatewayID) + err = gwAdmin.ForceUpdate(gatewayAdminApiEndpoint, gatewayID) if err != nil { panic(err) } // Publish Group CID - err = pAdmin.PublishGroupCID(providerID, pieceCIDs, 42, expiryDate, 42) + err = pAdmin.PublishGroupCID(providerAdminApiEndpoint, providerID, pieceCIDs, 42, expiryDate, 42) if err != nil { logging.ErrorAndPanic(err.Error()) } @@ -261,7 +218,7 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { var gatewayIDs []nodeid.NodeID gatewayIDs = make([]nodeid.NodeID, 0) logging.Info("Get all offers") - _, cidgroupInfo, err := pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err := pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.ErrorAndPanic(err.Error()) } @@ -281,7 +238,7 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { } gatewayIDs = append(gatewayIDs, *realNodeID) // Add a gateway logging.Info("Get offers by real gatewayID=%s", realNodeID.ToString()) - _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.ErrorAndPanic(err.Error()) } @@ -292,7 +249,7 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { fakeNodeID, _ := nodeid.NewNodeIDFromHexString("101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2DFA43") gatewayIDs[0] = *fakeNodeID logging.Info("Get offers by fake gatewayID=%s", fakeNodeID.ToString()) - _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerID, gatewayIDs) + _, cidgroupInfo, err = pAdmin.GetGroupCIDOffer(providerAdminApiEndpoint, providerID, gatewayIDs) if err != nil { logging.ErrorAndPanic(err.Error()) } @@ -306,33 +263,3 @@ func TestInitProviderAdminNoRetrievalKey(t *testing.T) { logging.Info("/* End TestInitProviderAdminNoRetrievalKey */") logging.Info("/*******************************************************/") } - -// Helper function to generate set of keys -func generateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { - rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) - } - if rootKey == nil { - return "", "", nil, errors.New("error generating blockchain key") - } - - rootPubKey, err = rootKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) - } - - retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() - if err != nil { - return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) - } - if retrievalPrivateKey == nil { - return "", "", nil, errors.New("error generating retrieval key") - } - - retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() - if err != nil { - return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) - } - return -} diff --git a/itest/pkg/util/crypto-facade/keys.go b/itest/pkg/util/crypto-facade/keys.go new file mode 100644 index 00000000..a58ce23e --- /dev/null +++ b/itest/pkg/util/crypto-facade/keys.go @@ -0,0 +1,38 @@ +package crypto_facade + +import ( + "errors" + "fmt" + + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" +) + +// GenerateKeys - helper function to generate set of keys, relying on file coin retrieval crypto package +func GenerateKeys() (rootPubKey string, retrievalPubKey string, retrievalPrivateKey *fcrcrypto.KeyPair, err error) { + rootKey, err := fcrcrypto.GenerateBlockchainKeyPair() + if err != nil { + return "", "", nil, fmt.Errorf("error generating blockchain key: %s", err.Error()) + } + if rootKey == nil { + return "", "", nil, errors.New("error generating blockchain key") + } + + rootPubKey, err = rootKey.EncodePublicKey() + if err != nil { + return "", "", nil, fmt.Errorf("error encoding public key: %s", err.Error()) + } + + retrievalPrivateKey, err = fcrcrypto.GenerateRetrievalV1KeyPair() + if err != nil { + return "", "", nil, fmt.Errorf("error generating retrieval key: %s", err.Error()) + } + if retrievalPrivateKey == nil { + return "", "", nil, errors.New("error generating retrieval key") + } + + retrievalPubKey, err = retrievalPrivateKey.EncodePublicKey() + if err != nil { + return "", "", nil, fmt.Errorf("error encoding retrieval pub key: %s", err.Error()) + } + return +} diff --git a/itest/pkg/util/filecoin-facade/filecoin.go b/itest/pkg/util/filecoin-facade/filecoin.go new file mode 100644 index 00000000..b47a5b8c --- /dev/null +++ b/itest/pkg/util/filecoin-facade/filecoin.go @@ -0,0 +1,184 @@ +package filecoin_facade + +import ( + "context" + "encoding/hex" + "errors" + "net/http" + "os/exec" + "time" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-jsonrpc" + "github.com/filecoin-project/lotus/api/apistruct" + "github.com/filecoin-project/lotus/chain/types" + "github.com/ipfs/go-cid" + + "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" +) + +// GenerateAccount - helper method, used to generate a new filecoin account with 10 filecoins of balance +func GenerateAccount(ctx context.Context, lotusAP string, token string, superAcct string, num int) ([]string, []string, error) { + // Get API + var api apistruct.FullNodeStruct + headers := http.Header{"Authorization": []string{"Bearer " + token}} + closer, err := jsonrpc.NewMergeClient(ctx, lotusAP, "Filecoin", []interface{}{&api.Internal, &api.CommonStruct.Internal}, headers) + if err != nil { + return nil, nil, err + } + defer closer() + + mainAddress, err := address.NewFromString(superAcct) + if err != nil { + return nil, nil, err + } + + privateKeys := make([]string, 0) + addresses := make([]string, 0) + cids := make([]cid.Cid, 0) + + // Send messages + for i := 0; i < num; i++ { + privKey, pubKey, err := generateKeyPair() + if err != nil { + return nil, nil, err + } + privKeyStr := hex.EncodeToString(privKey) + + address1, err := address.NewSecp256k1Address(pubKey) + if err != nil { + return nil, nil, err + } + + // Get amount + amt, err := types.ParseFIL("100") + if err != nil { + return nil, nil, err + } + + msg := &types.Message{ + To: address1, + From: mainAddress, + Value: types.BigInt(amt), + Method: 0, + } + signedMsg, err := fillMsg(ctx, mainAddress, &api, msg) + if err != nil { + return nil, nil, err + } + + // Send request to lotus + contentID, err := api.MpoolPush(ctx, signedMsg) + if err != nil { + return nil, nil, err + } + cids = append(cids, contentID) + + // Add to result + privateKeys = append(privateKeys, privKeyStr) + addresses = append(addresses, address1.String()) + } + + // Finally check receipts + for _, contentID := range cids { + receipt := waitReceipt(&contentID, &api) + if receipt.ExitCode != 0 { + return nil, nil, errors.New("transaction fail to execute") + } + } + + return privateKeys, addresses, nil +} + +// GetLotusToken gets the lotus token and the super account from the lotus container +func GetLotusToken() (string, string) { + cmd := exec.Command("docker", "ps", "--filter", "ancestor=consensys/lotus-full-node:latest", "--format", "{{.ID}}") + stdout, err := cmd.Output() + if err != nil { + panic(err) + } + id := string(stdout[:len(stdout)-1]) + cmd = exec.Command("docker", "exec", id, "bash", "-c", "cd ~/.lotus; cat token") + stdout, err = cmd.Output() + if err != nil { + panic(err) + } + token := string(stdout) + + cmd = exec.Command("docker", "exec", id, "bash", "-c", "./lotus wallet default") + stdout, err = cmd.Output() + if err != nil { + panic(err) + } + acct := string(stdout[:len(stdout)-1]) + return token, acct +} + +func generateKeyPair() ([]byte, []byte, error) { + // Generate Private-Public pairs. Public key will be used as address + var signer fcrpaymentmgr.SecpSigner + privateKey, err := signer.GenPrivate() + if err != nil { + logging.Error("Error generating private key, while creating address %s", err.Error()) + return nil, nil, err + } + + publicKey, err := signer.ToPublic(privateKey) + if err != nil { + logging.Error("Error generating public key, while creating address %s", err.Error()) + return nil, nil, err + } + return privateKey, publicKey, err +} + +// fillMsg will fill the gas and sign a given message +func fillMsg(ctx context.Context, fromAddress address.Address, api *apistruct.FullNodeStruct, msg *types.Message) (*types.SignedMessage, error) { + // Get nonce + nonce, err := api.MpoolGetNonce(ctx, msg.From) + if err != nil { + return nil, err + } + msg.Nonce = nonce + + // Calculate gas + limit, err := api.GasEstimateGasLimit(ctx, msg, types.EmptyTSK) + if err != nil { + return nil, err + } + msg.GasLimit = int64(float64(limit) * 1.25) + + premium, err := api.GasEstimateGasPremium(ctx, 10, msg.From, msg.GasLimit, types.EmptyTSK) + if err != nil { + return nil, err + } + msg.GasPremium = premium + + feeCap, err := api.GasEstimateFeeCap(ctx, msg, 20, types.EmptyTSK) + if err != nil { + return nil, err + } + msg.GasFeeCap = feeCap + + // Sign message + return api.WalletSignMessage(ctx, fromAddress, msg) +} + +// wait receipt will wait until receipt is received for a given cid +func waitReceipt(cid *cid.Cid, api *apistruct.FullNodeStruct) *types.MessageReceipt { + // Return until recipient is returned (transaction is processed) + var receipt *types.MessageReceipt + var err error + for { + receipt, err = api.StateGetReceipt(context.Background(), *cid, types.EmptyTSK) + if err != nil { + logging.Warn("Payment manager has error getting recipient of cid: %s", cid.String()) + } + if receipt != nil { + break + } + // TODO, Make the interval configurable + time.Sleep(1 * time.Second) + } + return receipt +} diff --git a/itest/pkg/util/jsclient-facade/jsclient.go b/itest/pkg/util/jsclient-facade/jsclient.go new file mode 100644 index 00000000..acab298f --- /dev/null +++ b/itest/pkg/util/jsclient-facade/jsclient.go @@ -0,0 +1,36 @@ +package jsclient_facade + +import ( + "fmt" + "os" + "os/exec" +) + +func CallClientJsInstall() error { + cmd := exec.Command("npm", "install") + cmd.Dir = "../../../client-js/" + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + return err +} + +func CallClientJsE2E(key string, walletKey string, registerAPI string, lotusAP string, lotusToken string) error { + cmd := exec.Command("npm", "run", "test-e2e") + if os.Getenv("RELOAD_JS_TESTS") == "yes" { + cmd = exec.Command("npm", "run", "test-e2e-watch") + } + cmd.Dir = "../../../client-js" + + cmd.Env = append(os.Environ(), + "ESTABLISHMENT_TTL=101", + fmt.Sprintf("FCR_BLOCKCHAIN_PUBLIC_KEY=%s", key), + fmt.Sprintf("FCR_REGISTER_API_URL=%s", registerAPI), + fmt.Sprintf("FCR_WALLET_PRIVATE_KEY=%s", walletKey), + fmt.Sprintf("FCR_LOTUS_AP=%s", lotusAP), + fmt.Sprintf("FCR_LOTUS_AUTH_TOKEN=%s", lotusToken), + ) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/itest/pkg/util/test-containers/all_containers.go b/itest/pkg/util/test-containers/all_containers.go new file mode 100644 index 00000000..73bf1c5e --- /dev/null +++ b/itest/pkg/util/test-containers/all_containers.go @@ -0,0 +1,9 @@ +package test_containers + +type AllContainers struct { + Gateways map[string]GatewayPortsResolver + Providers map[string]ProviderPortsResolver + Register RegisterPortsResolver + Redis Container + Lotus LotusPortsResolver +} diff --git a/itest/pkg/util/test-containers/color/color.go b/itest/pkg/util/test-containers/color/color.go new file mode 100644 index 00000000..c839fc91 --- /dev/null +++ b/itest/pkg/util/test-containers/color/color.go @@ -0,0 +1,18 @@ +package color + +const Reset = "\033[0m" +const Red = "\033[31m" +const Green = "\033[32m" +const Yellow = "\033[33m" +const Blue = "\033[34m" +const Purple = "\033[35m" +const Cyan = "\033[36m" +const Gray = "\033[37m" // Used by lotus +const White = "\033[97m" // Used by redis +const RedBright = "\033[91m" +const GreenBright = "\033[91m" +const YellowBright = "\033[91m" +const BlueBright = "\033[91m" +const PurpleBright = "\033[91m" +const CyanBright = "\033[91m" +const WhiteBright = "\033[91m" diff --git a/itest/pkg/util/test-containers/container_port.go b/itest/pkg/util/test-containers/container_port.go new file mode 100644 index 00000000..11edb633 --- /dev/null +++ b/itest/pkg/util/test-containers/container_port.go @@ -0,0 +1,175 @@ +package test_containers + +import ( + "context" + "fmt" + + "github.com/docker/go-connections/nat" + "github.com/spf13/viper" + tc "github.com/testcontainers/testcontainers-go" + + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" +) + +type ContainerPorts struct { + ctx context.Context + container tc.Container + guestToHostPorts map[int]nat.Port + gatewayConfig *viper.Viper + providerConfig *viper.Viper + registerConfig *viper.Viper +} + +type Container interface { + TerminateContainer() + SerialisePortSetup() string +} + +type RegisterPortsResolver interface { + Container + GetRegisterHostApiEndpoint() (registerApiEndpoint string) +} + +type GatewayPortsResolver interface { + Container + GetGatewayHostApiEndpoints() (gatewayApiEndpoint, providerApiEndpoint, clientApiEndpoint, adminApiEndpoint string) +} + +type ProviderPortsResolver interface { + Container + GetProviderHostApiEndpoints() (gatewayApiEndpoint, clientApiEndpoint, adminApiEndpoint string) +} + +type LotusPortsResolver interface { + Container + GetLostHostApiEndpoints() (lotusDaemonApiEndpoint string, lotusMinerApiEndpoint string) +} + +func NewGenericPortsResolver(ctx context.Context, container tc.Container, guestToHostPorts map[int]nat.Port) Container { + return ContainerPorts{ + ctx, + container, + guestToHostPorts, + nil, + nil, + nil, + } +} + +func NewGatewayPortsResolver(ctx context.Context, container tc.Container, guestToHostPorts map[int]nat.Port, config *viper.Viper) GatewayPortsResolver { + return ContainerPorts{ + ctx, + container, + guestToHostPorts, + config, + nil, + nil, + } +} + +func NewProviderPortsResolver(ctx context.Context, container tc.Container, guestToHostPorts map[int]nat.Port, config *viper.Viper) ProviderPortsResolver { + return ContainerPorts{ + ctx, + container, + guestToHostPorts, + nil, + config, + nil, + } +} + +func NewRegisterPortsResolver(ctx context.Context, container tc.Container, guestToHostPorts map[int]nat.Port, config *viper.Viper) RegisterPortsResolver { + return ContainerPorts{ + ctx, + container, + guestToHostPorts, + nil, + nil, + config, + } +} + +func NewLotusPortsResolver(ctx context.Context, container tc.Container, guestToHostPorts map[int]nat.Port) LotusPortsResolver { + return ContainerPorts{ + ctx, + container, + guestToHostPorts, + nil, + nil, + nil, + } +} + +func (c ContainerPorts) SerialisePortSetup() string { + containerName, err := c.container.Name(c.ctx) + if err != nil { + logging.Fatal("GetGatewayHostApiEndpoints - container name resolution error: %s", err) + } + result := fmt.Sprintf(">> container: %-11s ports: ", containerName) + for guestPort, hostPort := range c.guestToHostPorts { + result += fmt.Sprintf("%s->%d ", hostPort.Port(), guestPort) + } + result += "\n" + return result +} + +func (c ContainerPorts) GetRegisterHostApiEndpoint() (registerApiEndpoint string) { + // expecting containerName with extra slash char in the beginning, like: "/bla" + containerName, err := c.container.Name(c.ctx) + if err != nil { + logging.Fatal("GetGatewayHostApiEndpoints - container name resolution error: %s", err) + } + registerApiEndpoint = containerName[1:] + ":" + c.guestToHostPorts[c.registerConfig.GetInt("BIND_API")].Port() + return +} + +func (c ContainerPorts) GetGatewayHostApiEndpoints() (gatewayApiEndpoint, providerApiEndpoint, clientApiEndpoint, adminApiEndpoint string) { + containerName, err := c.container.Name(c.ctx) + if err != nil { + logging.Fatal("GetGatewayHostApiEndpoints - container name resolution error: %s", err) + } + // expecting containerName with extra slash char in the beginning, like: "/bla" + containerName = containerName[1:] + gatewayApiEndpoint = containerName + ":" + c.guestToHostPorts[c.gatewayConfig.GetInt("BIND_GATEWAY_API")].Port() + providerApiEndpoint = containerName + ":" + c.guestToHostPorts[c.gatewayConfig.GetInt("BIND_PROVIDER_API")].Port() + clientApiEndpoint = containerName + ":" + c.guestToHostPorts[c.gatewayConfig.GetInt("BIND_REST_API")].Port() + adminApiEndpoint = containerName + ":" + c.guestToHostPorts[c.gatewayConfig.GetInt("BIND_ADMIN_API")].Port() + return +} + +func (c ContainerPorts) GetProviderHostApiEndpoints() (gatewayApiEndpoint, clientApiEndpoint, adminApiEndpoint string) { + containerName, err := c.container.Name(c.ctx) + if err != nil { + logging.Fatal("GetProviderHostApiEndpoints - container name resolution error: %s", err) + } + containerName = containerName[1:] + gatewayApiEndpoint = containerName + ":" + c.guestToHostPorts[c.providerConfig.GetInt("BIND_GATEWAY_API")].Port() + clientApiEndpoint = containerName + ":" + c.guestToHostPorts[c.providerConfig.GetInt("BIND_REST_API")].Port() + adminApiEndpoint = containerName + ":" + c.guestToHostPorts[c.providerConfig.GetInt("BIND_ADMIN_API")].Port() + return +} + +func (c ContainerPorts) GetLostHostApiEndpoints() (lotusDaemonApiEndpoint string, lotusMinerApiEndpoint string) { + containerName, err := c.container.Name(c.ctx) + if err != nil { + logging.Fatal("GetProviderHostApiEndpoints - container name resolution error: %s", err) + } + containerName = containerName[1:] + lotusDaemonApiEndpoint = containerName + ":" + c.guestToHostPorts[1234].Port() + lotusMinerApiEndpoint = containerName + ":" + c.guestToHostPorts[2345].Port() + return +} + +func (c ContainerPorts) TerminateContainer() { + if c.container == nil { + // already terminated, do nothing + return + } + + if err := c.container.StopLogProducer(); err != nil { + logging.Error("error stopping logs for a test container: %s", err.Error()) + } + if err := c.container.Terminate(c.ctx); err != nil { + logging.Fatal("error terminating test container: %s", err.Error()) + } +} diff --git a/itest/pkg/util/test-containers/test_log_consumer.go b/itest/pkg/util/test-containers/test_log_consumer.go new file mode 100644 index 00000000..fd059e9e --- /dev/null +++ b/itest/pkg/util/test-containers/test_log_consumer.go @@ -0,0 +1,44 @@ +package test_containers + +import ( + "fmt" + "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers/color" + "strings" + + tc "github.com/testcontainers/testcontainers-go" +) + +type TestLogConsumer struct { + Messages []string + ContainerName string + Color string +} + +func (g *TestLogConsumer) Accept(l tc.Log) { + newLogPart := string(l.Content) + g.Messages = append(g.Messages, newLogPart) + fmt.Printf("%s%s:%s %s", g.Color, strings.ToUpper(g.ContainerName), color.Reset, newLogPart) +} + +/* +type logConsumer struct { + name string + color string + // The following are used by itest only + done chan bool +} + +func (g *logConsumer) Accept(l tc.Log) { + log := string(l.Content) + fmt.Print(g.color, "[", strings.ToUpper(g.name), "]", "\033[0m:", log) + if g.done != nil { + if strings.Contains(log, "--- FAIL:") { + // Tests have falied. + g.done <- false + } else if strings.Contains(log, "ok") && strings.Contains(log, "github.com/ConsenSys/fc-retrieval/itest/pkg/") { + // Tests have all passed. + g.done <- true + } + } +} +*/ diff --git a/itest/pkg/util/test-containers/testcontainers.go b/itest/pkg/util/test-containers/testcontainers.go new file mode 100644 index 00000000..52efa576 --- /dev/null +++ b/itest/pkg/util/test-containers/testcontainers.go @@ -0,0 +1,430 @@ +package test_containers + +import ( + "bufio" + "context" + "errors" + "fmt" + "os" + "strings" + "time" + + "github.com/docker/docker/api/types/container" + "github.com/google/uuid" + "github.com/spf13/viper" + tc "github.com/testcontainers/testcontainers-go" + + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/itest/pkg/util/test-containers/color" + + "github.com/docker/go-connections/nat" + "github.com/testcontainers/testcontainers-go/wait" +) + +const lotusFullNodeWaitFor = "starting winning PoSt warmup" +const networkMode = "default" + +func StartContainers(ctx context.Context, gatewaysCount int, providersCount int, testCaseName string, verbose bool, gatewayConfig *viper.Viper, providerConfig *viper.Viper, registerConfig *viper.Viper) ( + containers AllContainers, + network *tc.Network, + err error) { + + if gatewaysCount < 1 || providersCount < 1 { + return containers, nil, errors.New("number of gateways or providers in a test setup can't be less then one") + } + network, networkName := createNetwork(ctx) + containers.Redis, err = startRedis(ctx, networkName, verbose) + if err != nil { + return containers, nil, fmt.Errorf("can't start Redis container, error: %s", err.Error()) + } + containers.Lotus, err = startLotusFullNode(ctx, networkName, verbose) + if err != nil { + return containers, nil, fmt.Errorf("can't start Lotus container, error: %s", err.Error()) + } + containers.Register, err = startRegister(ctx, networkName, verbose, registerConfig) + if err != nil { + return containers, nil, fmt.Errorf("can't start Register container, error: %s", err.Error()) + } + + // Start all providers + containers.Providers = make(map[string]ProviderPortsResolver) + for i := 0; i < providersCount; i++ { + providerName := fmt.Sprintf("provider-%v", i) + containers.Providers[providerName], err = startProvider(ctx, providerName, networkName, verbose, providerConfig) + if err != nil { + return containers, nil, fmt.Errorf("can't start Provider container, error: %s", err.Error()) + } + } + // Start all gateways + containers.Gateways = make(map[string]GatewayPortsResolver) + for i := 0; i < gatewaysCount; i++ { + gatewayName := fmt.Sprintf("gateway-%v", i) + containers.Gateways[gatewayName], err = startGateway(ctx, gatewayName, networkName, verbose, gatewayConfig) + if err != nil { + return containers, nil, fmt.Errorf("can't start Gateway container, error: %s", err.Error()) + } + } + printPortsSetup(testCaseName, networkName, containers) + return +} + +func StopContainers(ctx context.Context, testPackage string, containers AllContainers, network *tc.Network) { + fmt.Printf("\n>> terminating containers for test package: %s\n\n", testPackage) + for _, g := range containers.Gateways { + g.TerminateContainer() + } + for _, p := range containers.Providers { + p.TerminateContainer() + } + containers.Register.TerminateContainer() + containers.Lotus.TerminateContainer() + containers.Redis.TerminateContainer() + if err := (*network).Remove(ctx); err != nil { + logging.Error("error terminating test container network: %s", err.Error()) + } +} + +// getEnvMap gets the env map from a given env file +func getEnvMap(envFile string) map[string]string { + env := make(map[string]string) + file, err := os.Open(envFile) + if err != nil { + panic(err) + } + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + res := strings.Split(scanner.Text(), "=") + if len(res) >= 2 { + env[res[0]] = res[1] + } + } + if err := scanner.Err(); err != nil { + panic(err) + } + if err := file.Close(); err != nil { + logging.Error("error closing file: %s", err.Error()) + } + return env +} + +// CreateNetwork creates a network +func createNetwork(ctx context.Context) (*tc.Network, string) { + randomUuid, _ := uuid.NewRandom() + networkName := randomUuid.String() + net, err := tc.GenericNetwork(ctx, tc.GenericNetworkRequest{ + NetworkRequest: tc.NetworkRequest{ + Name: networkName, + CheckDuplicate: true, + }, + }) + if err != nil { + panic(err) + } + return &net, networkName +} + +// startRedis - starts redis; used by register +func startRedis(ctx context.Context, network string, verbose bool) (Container, error) { + guestPort, _ := nat.NewPort("tcp", "6379") + containerName := "redis" + logConsumer := TestLogConsumer{ + Messages: []string{}, + ContainerName: containerName, + Color: color.White, + } + req := tc.ContainerRequest{ + Name: containerName, + Image: "redis:alpine", + Cmd: []string{"redis-server", "--requirepass", "xxxx"}, + ExposedPorts: []string{guestPort.Port()}, + //Env: map[string]string{"ALLOW_EMPTY_PASSWORD": "yes"}, + WaitingFor: wait.ForLog("Ready to accept connections"), + AutoRemove: true, + Networks: []string{network}, + NetworkMode: container.NetworkMode(networkMode), + NetworkAliases: map[string][]string{network: {"redis"}}, + } + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + + if verbose { + container.StartLogProducer(ctx) + container.FollowOutput(&logConsumer) + } + + hostPort, err := getMappedHostPort(ctx, guestPort, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + return NewGenericPortsResolver(ctx, container, map[int]nat.Port{guestPort.Int(): hostPort}), nil +} + +func startLotusFullNode(ctx context.Context, network string, verbose bool) (LotusPortsResolver, error) { + guestPort1, _ := nat.NewPort("tcp", "1234") + guestPort2, _ := nat.NewPort("tcp", "2345") + containerName := "lotus" + logConsumer := TestLogConsumer{ + Messages: []string{}, + ContainerName: containerName, + Color: color.Gray, + } + req := tc.ContainerRequest{ + Name: containerName, + Image: "consensys/lotus-full-node:latest", + ExposedPorts: []string{guestPort1.Port(), guestPort2.Port()}, + Networks: []string{network}, + NetworkMode: container.NetworkMode(networkMode), + NetworkAliases: map[string][]string{network: {containerName}}, + WaitingFor: wait.ForLog(lotusFullNodeWaitFor).WithStartupTimeout(10 * time.Minute), + AutoRemove: true, + } + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + + if verbose { + container.StartLogProducer(ctx) + container.FollowOutput(&logConsumer) + } + + hostPort1, err := getMappedHostPort(ctx, guestPort1, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort2, err := getMappedHostPort(ctx, guestPort2, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + return NewLotusPortsResolver(ctx, container, map[int]nat.Port{guestPort1.Int(): hostPort1, guestPort2.Int(): hostPort2}), nil +} + +func startRegister(ctx context.Context, network string, verbose bool, config *viper.Viper) (RegisterPortsResolver, error) { + env := getEnvMap(config.ConfigFileUsed()) + guestPort, _ := nat.NewPort("tcp", "9020") + containerName := "register" + logConsumer := TestLogConsumer{ + Messages: []string{}, + ContainerName: containerName, + Color: color.Yellow, + } + req := tc.ContainerRequest{ + Name: containerName, + Image: "consensys/fc-retrieval/register:latest", + ExposedPorts: []string{guestPort.Port()}, + Networks: []string{network}, + Env: env, + NetworkMode: container.NetworkMode(networkMode), + NetworkAliases: map[string][]string{network: {containerName}}, + WaitingFor: wait.ForLog("Serving register at"), + AutoRemove: true, + } + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + + if verbose { + container.StartLogProducer(ctx) + container.FollowOutput(&logConsumer) + } + + hostPort, err := getMappedHostPort(ctx, guestPort, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + return NewRegisterPortsResolver(ctx, container, map[int]nat.Port{guestPort.Int(): hostPort}, config), nil +} + +func startGateway(ctx context.Context, gatewayName string, network string, verbose bool, config *viper.Viper) (GatewayPortsResolver, error) { + env := getEnvMap(config.ConfigFileUsed()) + guestPort1, _ := nat.NewPort("tcp", "9010") + guestPort2, _ := nat.NewPort("tcp", "9011") + guestPort3, _ := nat.NewPort("tcp", "9012") + guestPort4, _ := nat.NewPort("tcp", "9013") + logConsumer := TestLogConsumer{ + Messages: []string{}, + ContainerName: gatewayName, + Color: color.Green, + } + req := tc.ContainerRequest{ + Name: gatewayName, + Image: "consensys/fc-retrieval/gateway:latest", + ExposedPorts: []string{guestPort1.Port(), guestPort2.Port(), guestPort3.Port(), guestPort4.Port()}, + Networks: []string{network}, + Env: env, + NetworkMode: container.NetworkMode(networkMode), + NetworkAliases: map[string][]string{network: {gatewayName}}, + WaitingFor: wait.ForLog("Filecoin Gateway Start-up Complete"), + AutoRemove: true, + } + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + + if verbose { + container.StartLogProducer(ctx) + container.FollowOutput(&logConsumer) + } + + hostPort1, err := getMappedHostPort(ctx, guestPort1, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort2, err := getMappedHostPort(ctx, guestPort2, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort3, err := getMappedHostPort(ctx, guestPort3, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort4, err := getMappedHostPort(ctx, guestPort4, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + return NewGatewayPortsResolver(ctx, container, map[int]nat.Port{guestPort1.Int(): hostPort1, guestPort2.Int(): hostPort2, guestPort3.Int(): hostPort3, guestPort4.Int(): hostPort4}, config), nil +} + +func startProvider(ctx context.Context, providerName string, network string, verbose bool, config *viper.Viper) (ProviderPortsResolver, error) { + env := getEnvMap(config.ConfigFileUsed()) + guestPort1, _ := nat.NewPort("tcp", "9030") + guestPort2, _ := nat.NewPort("tcp", "9032") + guestPort3, _ := nat.NewPort("tcp", "9033") + logConsumer := TestLogConsumer{ + Messages: []string{}, + ContainerName: providerName, + Color: color.Purple, + } + + req := tc.ContainerRequest{ + Name: providerName, + Image: "consensys/fc-retrieval/provider:latest", + ExposedPorts: []string{guestPort1.Port(), guestPort2.Port(), guestPort3.Port()}, + Networks: []string{network}, + Env: env, + NetworkMode: container.NetworkMode(networkMode), + NetworkAliases: map[string][]string{network: {providerName}}, + WaitingFor: wait.ForLog("Filecoin Provider Start-up Complete"), + AutoRemove: true, + } + container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + + if verbose { + container.StartLogProducer(ctx) + container.FollowOutput(&logConsumer) + } + + hostPort1, err := getMappedHostPort(ctx, guestPort1, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort2, err := getMappedHostPort(ctx, guestPort2, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + hostPort3, err := getMappedHostPort(ctx, guestPort3, container) + if err != nil { + if container != nil { + container.Terminate(ctx) + } + return nil, err + } + return NewProviderPortsResolver(ctx, container, map[int]nat.Port{guestPort1.Int(): hostPort1, guestPort2.Int(): hostPort2, guestPort3.Int(): hostPort3}, config), nil +} + +// getMappedHostPort returns the actual host port. The result port is dynamically allocated by TestContainers +func getMappedHostPort(ctx context.Context, guestPort nat.Port, container tc.Container) (nat.Port, error) { + logging.Debug("getMappedHostPort - context is alive: %+v; guestPort: %s; container ID: %s\n", ctx != nil, guestPort.Port(), container.GetContainerID()[:12]) + _, err := container.Host(ctx) + if err != nil { + return "", fmt.Errorf("can't resolve host for the given container ID: %s; error: %s", container.GetContainerID()[:12], err.Error()) + } + + hostPort, err := container.MappedPort(ctx, guestPort) + if err != nil { + return "", fmt.Errorf("can't resolve host port for the given container ID: %s and guest port: %s; error: %s", container.GetContainerID()[:12], guestPort.Port(), err.Error()) + } + return hostPort, nil +} + +func printPortsSetup(testPackageName string, networkName string, containers AllContainers) { + hostPortsMessage := fmt.Sprintf("\n>> running test package: '%s' in Docker network name %s; with host port configuration:\n", testPackageName, networkName) + hostPortsMessage += containers.Redis.SerialisePortSetup() + hostPortsMessage += containers.Lotus.SerialisePortSetup() + hostPortsMessage += containers.Register.SerialisePortSetup() + for _, container := range containers.Gateways { + hostPortsMessage += container.SerialisePortSetup() + } + for _, container := range containers.Providers { + hostPortsMessage += container.SerialisePortSetup() + } + fmt.Println(hostPortsMessage) +} diff --git a/itest/pkg/util/util.go b/itest/pkg/util/util.go deleted file mode 100644 index 82f72446..00000000 --- a/itest/pkg/util/util.go +++ /dev/null @@ -1,560 +0,0 @@ -/* -Package util - common functions used in end-to-end and integration testing. Allowing to start different types of -Retrieval network nodes for testing. -*/ -package util - -import ( - "bufio" - "context" - "encoding/hex" - "errors" - "fmt" - "net/http" - "os" - "os/exec" - "path/filepath" - "strings" - "time" - - "github.com/docker/docker/api/types/container" - "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/lotus/api/apistruct" - "github.com/filecoin-project/lotus/chain/types" - "github.com/google/uuid" - "github.com/ipfs/go-cid" - "github.com/testcontainers/testcontainers-go/wait" - tc "github.com/wcgcyx/testcontainers-go" - - "github.com/ConsenSys/fc-retrieval/common/pkg/fcrpaymentmgr" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" -) - -const ColorRed = "\033[31m" -const ColorGreen = "\033[32m" -const ColorYellow = "\033[33m" -const ColorBlue = "\033[34m" -const ColorPurple = "\033[35m" -const ColorCyan = "\033[36m" -const ColorWhite = "\033[37m" // Used by redis -const ColorGray = "\033[90m" // Used by lotus -const ColorBrightRed = "\033[91m" -const ColorBrightGreen = "\033[91m" -const ColorBrightYellow = "\033[91m" -const ColorBrightBlue = "\033[91m" -const ColorBrightPurple = "\033[91m" -const ColorBrightCyan = "\033[91m" -const ColorBrightWhite = "\033[91m" - -const lotusDaemonWaitFor = "retrieval client" -const lotusFullNodeWaitFor = "starting winning PoSt warmup" -const networkMode = "default" - -// GetLotusToken gets the lotus token and the super account from the lotus container -func GetLotusToken() (string, string) { - cmd := exec.Command("docker", "ps", "--filter", "ancestor=consensys/lotus-full-node:latest", "--format", "{{.ID}}") - stdout, err := cmd.Output() - if err != nil { - panic(err) - } - id := string(stdout[:len(stdout)-1]) - cmd = exec.Command("docker", "exec", id, "bash", "-c", "cd ~/.lotus; cat token") - stdout, err = cmd.Output() - if err != nil { - panic(err) - } - token := string(stdout) - - cmd = exec.Command("docker", "exec", id, "bash", "-c", "./lotus wallet default") - stdout, err = cmd.Output() - if err != nil { - panic(err) - } - acct := string(stdout[:len(stdout)-1]) - return token, acct -} - -// CreateNetwork creates a network -func CreateNetwork(ctx context.Context) (*tc.Network, string) { - randomUuid, _ := uuid.NewRandom() - networkName := randomUuid.String() - net, err := tc.GenericNetwork(ctx, tc.GenericNetworkRequest{ - NetworkRequest: tc.NetworkRequest{ - Name: networkName, - CheckDuplicate: true, - }, - }) - if err != nil { - panic(err) - } - return &net, networkName -} - -// GetEnvMap gets the env map from a given env file -func GetEnvMap(envFile string) map[string]string { - env := make(map[string]string) - file, err := os.Open(envFile) - if err != nil { - panic(err) - } - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - res := strings.Split(scanner.Text(), "=") - if len(res) >= 2 { - env[res[0]] = res[1] - } - } - if err := scanner.Err(); err != nil { - panic(err) - } - if err := file.Close(); err != nil { - logging.Error("error closing file: %s", err.Error()) - } - return env -} - -// StartLotusDaemon starts Lotus local development network, daemon only (miner is missing) -func StartLotusDaemon(ctx context.Context, network string, verbose bool) tc.Container { - // Start lotus - req := tc.ContainerRequest{ - Image: "consensys/lotus-daemon:latest", - Networks: []string{network}, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {"lotus-daemon"}}, - WaitingFor: wait.ForLog(lotusDaemonWaitFor), - ExposedPorts: []string{"1234"}, - AutoRemove: true, - } - lotusC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: fmt.Sprintf("lotus-daemon"), color: ColorGray} - err = lotusC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - lotusC.FollowOutput(g) - } - return lotusC -} - -// StartLotusFullNode starts Lotus local development network, two services: miner and daemon in one container -func StartLotusFullNode(ctx context.Context, network string, verbose bool) tc.Container { - // Start lotus - req := tc.ContainerRequest{ - Image: "consensys/lotus-full-node:latest", - Networks: []string{network}, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {"lotus-full-node"}}, - WaitingFor: wait.ForLog(lotusFullNodeWaitFor).WithStartupTimeout(10 * time.Minute), - ExposedPorts: []string{"1234", "2345"}, - AutoRemove: true, - // --cpus= - } - lotusC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: fmt.Sprintf("lotus-full-node"), color: ColorGray} - err = lotusC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - lotusC.FollowOutput(g) - } - return lotusC -} - -// StartRedis - starts redis; used by register -func StartRedis(ctx context.Context, network string, verbose bool) tc.Container { - // Start redis - req := tc.ContainerRequest{ - Image: "redis:alpine", - Cmd: []string{"redis-server", "--requirepass", "xxxx"}, - Networks: []string{network}, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {"redis"}}, - WaitingFor: wait.ForLog("Ready to accept connections"), - AutoRemove: true, - } - redisC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: fmt.Sprintf("redis"), color: ColorWhite} - err = redisC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - redisC.FollowOutput(g) - } - return redisC -} - -// StartRegister - starts the register -func StartRegister(ctx context.Context, network string, color string, env map[string]string, verbose bool) tc.Container { - // Start a register container - req := tc.ContainerRequest{ - Image: "consensys/fc-retrieval/register:latest", - Networks: []string{network}, - Env: env, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {"register"}}, - WaitingFor: wait.ForLog("Serving register at"), - AutoRemove: true, - } - registerC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: fmt.Sprintf("register"), color: color} - err = registerC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - registerC.FollowOutput(g) - } - return registerC -} - -// StartGateway - start a gateway of specific id, network, log color and env -func StartGateway(ctx context.Context, id string, network string, color string, env map[string]string, verbose bool) tc.Container { - // Start a gateway container - req := tc.ContainerRequest{ - Image: "consensys/fc-retrieval/gateway:latest", - Networks: []string{network}, - Env: env, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {id}}, - WaitingFor: wait.ForLog("Filecoin Gateway Start-up Complete"), - AutoRemove: true, - } - gatewayC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: id, color: color} - err = gatewayC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - gatewayC.FollowOutput(g) - } - return gatewayC -} - -// StartProvider - start a provider of specific id, network, log color and env -func StartProvider(ctx context.Context, id string, network string, color string, env map[string]string, verbose bool) tc.Container { - // Start a provider container - req := tc.ContainerRequest{ - Image: "consensys/fc-retrieval/provider:latest", - Networks: []string{network}, - Env: env, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {id}}, - WaitingFor: wait.ForLog("Filecoin Provider Start-up Complete"), - AutoRemove: true, - } - providerC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: id, color: color} - err = providerC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - providerC.FollowOutput(g) - } - return providerC -} - -// StartItest - start the itest, must only be called in host -func StartItest(ctx context.Context, network string, color string, lotusToken string, superAcct string, done chan bool, verbose bool, reloadJsTests string) tc.Container { - // Start a itest container - // Mount testdir - absPath, err := filepath.Abs(".") - if err != nil { - panic(err) - } - // Mount common, client, gw-admin, pvd-admin - commonPath, err := filepath.Abs("../../../common/pkg") - if err != nil { - panic(err) - } - clientPath, err := filepath.Abs("../../../client/pkg") - if err != nil { - panic(err) - } - gwAdminPath, err := filepath.Abs("../../../gateway-admin/pkg") - if err != nil { - panic(err) - } - pvdAdminPath, err := filepath.Abs("../../../provider-admin/pkg") - if err != nil { - panic(err) - } - clientJsPath, err := filepath.Abs("../../../client-js") - if err != nil { - panic(err) - } - - req := tc.ContainerRequest{ - Image: "consensys/fc-retrieval/itest", - Name: "itest", - Networks: []string{network}, - Env: map[string]string{"ITEST_CALLING_FROM_CONTAINER": "yes", "LOTUS_TOKEN": lotusToken, "SUPER_ACCT": superAcct, "RELOAD_JS_TESTS": reloadJsTests}, - NetworkMode: container.NetworkMode(networkMode), - NetworkAliases: map[string][]string{network: {"itest"}}, - BindMounts: map[string]string{ - clientJsPath: "/go/src/github.com/ConsenSys/fc-retrieval/client-js/", - absPath: "/go/src/github.com/ConsenSys/fc-retrieval/itest/pkg/temp/", - commonPath: "/go/src/github.com/ConsenSys/fc-retrieval/common/pkg/", - clientPath: "/go/src/github.com/ConsenSys/fc-retrieval/client/pkg/", - gwAdminPath: "/go/src/github.com/ConsenSys/fc-retrieval/gateway-admin/pkg/", - pvdAdminPath: "/go/src/github.com/ConsenSys/fc-retrieval/provider-admin/pkg/"}, - Cmd: []string{"go", "test", "-v", "--count=1", "/go/src/github.com/ConsenSys/fc-retrieval/itest/pkg/temp/"}, - AutoRemove: true, - } - - if reloadJsTests == "yes" { - req.Cmd = append(req.Cmd, "-timeout=0") - } - - itestC, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - panic(err) - } - if verbose { - g := &logConsumer{name: fmt.Sprintf("itest"), color: color, done: done} - err = itestC.StartLogProducer(ctx) - if err != nil { - panic(err) - } - itestC.FollowOutput(g) - } - return itestC -} - -type logConsumer struct { - name string - color string - // The following are used by itest only - done chan bool -} - -func (g *logConsumer) Accept(l tc.Log) { - log := string(l.Content) - fmt.Print(g.color, "[", strings.ToUpper(g.name), "]", "\033[0m:", log) - if g.done != nil { - if strings.Contains(log, "--- FAIL:") { - // Tests have falied. - g.done <- false - } else if strings.Contains(log, "ok") && strings.Contains(log, "github.com/ConsenSys/fc-retrieval/itest/pkg/") { - // Tests have all passed. - g.done <- true - } - } -} - -// GenerateAccount - helper method, used to generate a new filecoin account with 10 filecoins of balance -func GenerateAccount(lotusAP string, token string, superAcct string, num int) ([]string, []string, error) { - // Get API - var api apistruct.FullNodeStruct - headers := http.Header{"Authorization": []string{"Bearer " + token}} - closer, err := jsonrpc.NewMergeClient(context.Background(), lotusAP, "Filecoin", []interface{}{&api.Internal, &api.CommonStruct.Internal}, headers) - if err != nil { - return nil, nil, err - } - defer closer() - - mainAddress, err := address.NewFromString(superAcct) - if err != nil { - return nil, nil, err - } - - privateKeys := make([]string, 0) - addresses := make([]string, 0) - cids := make([]cid.Cid, 0) - - // Send messages - for i := 0; i < num; i++ { - privKey, pubKey, err := generateKeyPair() - if err != nil { - return nil, nil, err - } - privKeyStr := hex.EncodeToString(privKey) - - address1, err := address.NewSecp256k1Address(pubKey) - if err != nil { - return nil, nil, err - } - - // Get amount - amt, err := types.ParseFIL("100") - if err != nil { - return nil, nil, err - } - - msg := &types.Message{ - To: address1, - From: mainAddress, - Value: types.BigInt(amt), - Method: 0, - } - signedMsg, err := fillMsg(mainAddress, &api, msg) - if err != nil { - return nil, nil, err - } - - // Send request to lotus - contentID, err := api.MpoolPush(context.Background(), signedMsg) - if err != nil { - return nil, nil, err - } - cids = append(cids, contentID) - - // Add to result - privateKeys = append(privateKeys, privKeyStr) - addresses = append(addresses, address1.String()) - } - - // Finally check receipts - for _, contentID := range cids { - receipt := waitReceipt(&contentID, &api) - if receipt.ExitCode != 0 { - return nil, nil, errors.New("transaction fail to execute") - } - } - - return privateKeys, addresses, nil -} - -func generateKeyPair() ([]byte, []byte, error) { - // Generate Private-Public pairs. Public key will be used as address - var signer fcrpaymentmgr.SecpSigner - privateKey, err := signer.GenPrivate() - if err != nil { - logging.Error("Error generating private key, while creating address %s", err.Error()) - return nil, nil, err - } - - publicKey, err := signer.ToPublic(privateKey) - if err != nil { - logging.Error("Error generating public key, while creating address %s", err.Error()) - return nil, nil, err - } - return privateKey, publicKey, err -} - -// fillMsg will fill the gas and sign a given message -func fillMsg(fromAddress address.Address, api *apistruct.FullNodeStruct, msg *types.Message) (*types.SignedMessage, error) { - // Get nonce - nonce, err := api.MpoolGetNonce(context.Background(), msg.From) - if err != nil { - return nil, err - } - msg.Nonce = nonce - - // Calculate gas - limit, err := api.GasEstimateGasLimit(context.Background(), msg, types.EmptyTSK) - if err != nil { - return nil, err - } - msg.GasLimit = int64(float64(limit) * 1.25) - - premium, err := api.GasEstimateGasPremium(context.Background(), 10, msg.From, msg.GasLimit, types.EmptyTSK) - if err != nil { - return nil, err - } - msg.GasPremium = premium - - feeCap, err := api.GasEstimateFeeCap(context.Background(), msg, 20, types.EmptyTSK) - if err != nil { - return nil, err - } - msg.GasFeeCap = feeCap - - // Sign message - return api.WalletSignMessage(context.Background(), fromAddress, msg) -} - -// wait receipt will wait until receipt is received for a given cid -func waitReceipt(cid *cid.Cid, api *apistruct.FullNodeStruct) *types.MessageReceipt { - // Return until recipient is returned (transaction is processed) - var receipt *types.MessageReceipt - var err error - for { - receipt, err = api.StateGetReceipt(context.Background(), *cid, types.EmptyTSK) - if err != nil { - logging.Warn("Payment manager has error getting recipient of cid: %s", cid.String()) - } - if receipt != nil { - break - } - // TODO, Make the interval configurable - time.Sleep(1 * time.Second) - } - return receipt -} - -func CallClientJsInstall() error { - cmd := exec.Command("npm", "install") - cmd.Dir = "../../../client-js/" - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - return err -} - -func CallClientJsE2E(key string, walletKey string, registerAPI string, lotusAP string, lotusToken string, gtws string, prvs string) error { - cmd := exec.Command("npm", "run", "test-e2e") - if os.Getenv("RELOAD_JS_TESTS") == "yes" { - cmd = exec.Command("npm", "run", "test-e2e-watch") - } - cmd.Dir = "../../../client-js" - - cmd.Env = append(os.Environ(), - "ESTABLISHMENT_TTL=101", - fmt.Sprintf("FCR_BLOCKCHAIN_PUBLIC_KEY=%s", key), - fmt.Sprintf("FCR_REGISTER_API_URL=%s", registerAPI), - fmt.Sprintf("FCR_WALLET_PRIVATE_KEY=%s", walletKey), - fmt.Sprintf("FCR_LOTUS_AP=%s", lotusAP), - fmt.Sprintf("FCR_LOTUS_AUTH_TOKEN=%s", lotusToken), - fmt.Sprintf("FCR_GATEWAYS=%s", gtws), - fmt.Sprintf("FCR_PROVIDERS=%s", prvs), - ) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} diff --git a/provider-admin/pkg/api/adminapi/admin_api.go b/provider-admin/pkg/api/adminapi/admin_api.go index fae9d114..c9482aca 100644 --- a/provider-admin/pkg/api/adminapi/admin_api.go +++ b/provider-admin/pkg/api/adminapi/admin_api.go @@ -15,23 +15,26 @@ type Admin struct { type AdminApi interface { RequestForceRefresh( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion, ) error RequestGetPublishedOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, gatewayIDs []nodeid.NodeID, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion, ) ( - bool, // found + bool, // found []cidoffer.CIDOffer, // offers - error, // error + error, // error ) RequestInitialiseKey( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion, @@ -40,6 +43,7 @@ type AdminApi interface { ) error RequestInitialiseKeyV2( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion, @@ -51,6 +55,7 @@ type AdminApi interface { ) error RequestPublishDHTOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, cids []cid.ContentID, price []uint64, @@ -61,6 +66,7 @@ type AdminApi interface { ) error RequestPublishGroupOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, cids []cid.ContentID, price uint64, @@ -81,4 +87,4 @@ func NewAdminApiWithDep(httpCommunicator request.HttpCommunications) AdminApi { return &Admin{ httpCommunicator: httpCommunicator, } -} \ No newline at end of file +} diff --git a/provider-admin/pkg/api/adminapi/force_refresh_requester.go b/provider-admin/pkg/api/adminapi/force_refresh_requester.go index faac6fb1..5b1c642f 100644 --- a/provider-admin/pkg/api/adminapi/force_refresh_requester.go +++ b/provider-admin/pkg/api/adminapi/force_refresh_requester.go @@ -28,7 +28,8 @@ import ( ) // RequestForceRefresh forces a given provider to refresh its internal register -func(a *Admin) RequestForceRefresh( +func (a *Admin) RequestForceRefresh( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, signingPrivkey *fcrcrypto.KeyPair, signingPrivKeyVer *fcrcrypto.KeyVersion) error { @@ -50,7 +51,7 @@ func(a *Admin) RequestForceRefresh( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/provider-admin/pkg/api/adminapi/force_refresh_requester_test.go b/provider-admin/pkg/api/adminapi/force_refresh_requester_test.go index 269954d4..fb3f87fd 100644 --- a/provider-admin/pkg/api/adminapi/force_refresh_requester_test.go +++ b/provider-admin/pkg/api/adminapi/force_refresh_requester_test.go @@ -39,7 +39,8 @@ func Test_RequestForceRefresh_Calls_RequestForceRefresh(t *testing.T) { keyPair, _ := fcrcrypto.GenerateRetrievalV1KeyPair() pubKeyStr, _ := keyPair.EncodePublicKey() - fakeProviderInfo := ®ister.ProviderRegister{ + fakeAdminApiEndpoint := "fakeAdminApiEndpoint" + fakeProviderInfo := ®ister.ProviderRegister{ NetworkInfoAdmin: "fakeNetworkInfoAdmin", SigningKey: pubKeyStr, } @@ -48,6 +49,6 @@ func Test_RequestForceRefresh_Calls_RequestForceRefresh(t *testing.T) { mockHttpCommunicator.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Return(&fcrmessages.FCRMessage{}, nil).Times(1) // act - _ = a.RequestForceRefresh(fakeProviderInfo, keyPair, fcrcrypto.DecodeKeyVersion(1)) + _ = a.RequestForceRefresh(fakeAdminApiEndpoint, fakeProviderInfo, keyPair, fcrcrypto.DecodeKeyVersion(1)) } diff --git a/provider-admin/pkg/api/adminapi/get_published_offer_requester.go b/provider-admin/pkg/api/adminapi/get_published_offer_requester.go index 9456f949..b5de6b76 100644 --- a/provider-admin/pkg/api/adminapi/get_published_offer_requester.go +++ b/provider-admin/pkg/api/adminapi/get_published_offer_requester.go @@ -28,6 +28,7 @@ import ( // RequestGetPublishedOffer checks the group offer stored in the provider for a given list of gateways. func (a *Admin) RequestGetPublishedOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, gatewayIDs []nodeid.NodeID, signingPrivkey *fcrcrypto.KeyPair, @@ -53,7 +54,7 @@ func (a *Admin) RequestGetPublishedOffer( return false, nil, errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return false, nil, err diff --git a/provider-admin/pkg/api/adminapi/initialise_key_requester.go b/provider-admin/pkg/api/adminapi/initialise_key_requester.go index 4c8ea058..dcf3d999 100644 --- a/provider-admin/pkg/api/adminapi/initialise_key_requester.go +++ b/provider-admin/pkg/api/adminapi/initialise_key_requester.go @@ -27,6 +27,7 @@ import ( // RequestInitialiseKey initialise a given provider func (a *Admin) RequestInitialiseKey( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion, @@ -54,7 +55,7 @@ func (a *Admin) RequestInitialiseKey( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/provider-admin/pkg/api/adminapi/initialise_key_requester_v2.go b/provider-admin/pkg/api/adminapi/initialise_key_requester_v2.go index 34f5e62b..fd3fba46 100644 --- a/provider-admin/pkg/api/adminapi/initialise_key_requester_v2.go +++ b/provider-admin/pkg/api/adminapi/initialise_key_requester_v2.go @@ -27,6 +27,7 @@ import ( // RequestInitialiseKeyV2 initialise a given provider func (a *Admin) RequestInitialiseKeyV2( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion, @@ -65,7 +66,7 @@ func (a *Admin) RequestInitialiseKeyV2( return errors.New("error in signing the request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/provider-admin/pkg/api/adminapi/publish_dht_offer_requester.go b/provider-admin/pkg/api/adminapi/publish_dht_offer_requester.go index 74ee5bf8..2eb0c22b 100644 --- a/provider-admin/pkg/api/adminapi/publish_dht_offer_requester.go +++ b/provider-admin/pkg/api/adminapi/publish_dht_offer_requester.go @@ -27,6 +27,7 @@ import ( // RequestPublishDHTOffer publish a dht offer to a given provider func (a *Admin) RequestPublishDHTOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, cids []cid.ContentID, price []uint64, @@ -52,7 +53,7 @@ func (a *Admin) RequestPublishDHTOffer( return errors.New("can't sign PublishDHTOffer request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/provider-admin/pkg/api/adminapi/publish_group_offer_requester.go b/provider-admin/pkg/api/adminapi/publish_group_offer_requester.go index 31101f06..117732c1 100644 --- a/provider-admin/pkg/api/adminapi/publish_group_offer_requester.go +++ b/provider-admin/pkg/api/adminapi/publish_group_offer_requester.go @@ -27,6 +27,7 @@ import ( // RequestPublishGroupOffer publish a group cid offer to a given provider func (a *Admin) RequestPublishGroupOffer( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, cids []cid.ContentID, price uint64, @@ -51,7 +52,7 @@ func (a *Admin) RequestPublishGroupOffer( return errors.New("error signing PublishGroupOffer request") } - response, err := a.httpCommunicator.SendMessage(providerRegistrar.GetNetworkInfoAdmin(), request) + response, err := a.httpCommunicator.SendMessage(adminApiEndpoint, request) if err != nil { logging.Error("Error in sending the message.") return err diff --git a/provider-admin/pkg/fcrprovideradmin/filecoin_provider_admin.go b/provider-admin/pkg/fcrprovideradmin/filecoin_provider_admin.go index 5597b88c..9bce4ddc 100644 --- a/provider-admin/pkg/fcrprovideradmin/filecoin_provider_admin.go +++ b/provider-admin/pkg/fcrprovideradmin/filecoin_provider_admin.go @@ -50,8 +50,8 @@ func NewFilecoinRetrievalProviderAdmin(settings ProviderAdminSettings) *Filecoin } // InitialiseProvider initialise a given provider -func (c *FilecoinRetrievalProviderAdmin) InitialiseProvider(providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion) error { - err := c.AdminApiCaller.RequestInitialiseKey(providerRegistrar, providerPrivKey, providerPrivKeyVer, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) +func (c *FilecoinRetrievalProviderAdmin) InitialiseProvider(adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion) error { + err := c.AdminApiCaller.RequestInitialiseKey(adminApiEndpoint, providerRegistrar, providerPrivKey, providerPrivKeyVer, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) if err != nil { return err } @@ -65,6 +65,7 @@ func (c *FilecoinRetrievalProviderAdmin) InitialiseProvider(providerRegistrar re // InitialiseProviderV2 initialise a given v2 provider func (c *FilecoinRetrievalProviderAdmin) InitialiseProviderV2( + adminApiEndpoint string, providerRegistrar register.ProviderRegistrar, providerPrivKey *fcrcrypto.KeyPair, providerPrivKeyVer *fcrcrypto.KeyVersion, @@ -73,6 +74,7 @@ func (c *FilecoinRetrievalProviderAdmin) InitialiseProviderV2( lotusAuthToken string, ) error { err := c.AdminApiCaller.RequestInitialiseKeyV2( + adminApiEndpoint, providerRegistrar, providerPrivKey, providerPrivKeyVer, @@ -94,45 +96,45 @@ func (c *FilecoinRetrievalProviderAdmin) InitialiseProviderV2( } // PublishGroupCID publish a group cid offer to a given provider -func (c *FilecoinRetrievalProviderAdmin) PublishGroupCID(providerID *nodeid.NodeID, cids []cid.ContentID, price uint64, expiry int64, qos uint64) error { +func (c *FilecoinRetrievalProviderAdmin) PublishGroupCID(adminApiEndpoint string, providerID *nodeid.NodeID, cids []cid.ContentID, price uint64, expiry int64, qos uint64) error { c.ActiveProvidersLock.RLock() defer c.ActiveProvidersLock.RUnlock() providerRegistrar, exists := c.ActiveProviders[providerID.ToString()] if !exists { return errors.New("unable to find the provider in admin storage") } - return c.AdminApiCaller.RequestPublishGroupOffer(providerRegistrar, cids, price, expiry, qos, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) + return c.AdminApiCaller.RequestPublishGroupOffer(adminApiEndpoint, providerRegistrar, cids, price, expiry, qos, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) } // PublishDHTCID publish a dht cid offer to a given provider -func (c *FilecoinRetrievalProviderAdmin) PublishDHTCID(providerID *nodeid.NodeID, cids []cid.ContentID, price []uint64, expiry []int64, qos []uint64) error { +func (c *FilecoinRetrievalProviderAdmin) PublishDHTCID(adminApiEndpoint string, providerID *nodeid.NodeID, cids []cid.ContentID, price []uint64, expiry []int64, qos []uint64) error { c.ActiveProvidersLock.RLock() defer c.ActiveProvidersLock.RUnlock() providerRegistrar, exists := c.ActiveProviders[providerID.ToString()] if !exists { return errors.New("unable to find the provider in admin storage") } - return c.AdminApiCaller.RequestPublishDHTOffer(providerRegistrar, cids, price, expiry, qos, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) + return c.AdminApiCaller.RequestPublishDHTOffer(adminApiEndpoint, providerRegistrar, cids, price, expiry, qos, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) } // GetGroupCIDOffer checks the group offer stored in the provider -func (c *FilecoinRetrievalProviderAdmin) GetGroupCIDOffer(providerID *nodeid.NodeID, gatewayIDs []nodeid.NodeID) (bool, []cidoffer.CIDOffer, error) { +func (c *FilecoinRetrievalProviderAdmin) GetGroupCIDOffer(adminApiEndpoint string, providerID *nodeid.NodeID, gatewayIDs []nodeid.NodeID) (bool, []cidoffer.CIDOffer, error) { c.ActiveProvidersLock.RLock() defer c.ActiveProvidersLock.RUnlock() providerRegistrar, exists := c.ActiveProviders[providerID.ToString()] if !exists { return false, nil, errors.New("unable to find the provider in admin storage") } - return c.AdminApiCaller.RequestGetPublishedOffer(providerRegistrar, gatewayIDs, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) + return c.AdminApiCaller.RequestGetPublishedOffer(adminApiEndpoint, providerRegistrar, gatewayIDs, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) } // ForceUpdate forces the provider to update its internal register -func (c *FilecoinRetrievalProviderAdmin) ForceUpdate(providerID *nodeid.NodeID) error { +func (c *FilecoinRetrievalProviderAdmin) ForceUpdate(adminApiEndpoint string, providerID *nodeid.NodeID) error { c.ActiveProvidersLock.RLock() defer c.ActiveProvidersLock.RUnlock() providerRegistrar, exists := c.ActiveProviders[providerID.ToString()] if !exists { return errors.New("unable to find the provider in admin storage") } - return c.AdminApiCaller.RequestForceRefresh(providerRegistrar, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) + return c.AdminApiCaller.RequestForceRefresh(adminApiEndpoint, providerRegistrar, c.Settings.providerAdminPrivateKey, c.Settings.providerAdminPrivateKeyVer) } diff --git a/provider/Dockerfile b/provider/Dockerfile index f8559fac..3b81ddec 100644 --- a/provider/Dockerfile +++ b/provider/Dockerfile @@ -14,9 +14,11 @@ RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o /go/bin/provider # Pull build provider into a second stage deploy alpine container FROM alpine:latest -COPY --from=builder /go/bin/provider /provider +COPY --from=builder /go/bin/provider /main +COPY provider/docker-entrypoint.sh /docker-entrypoint.sh # Run the binary when the container starts. WORKDIR / CMD ["/provider"] +CMD ["./docker-entrypoint.sh"] EXPOSE 9030 diff --git a/provider/config/config.go b/provider/config/config.go index 4535063a..07a48b42 100644 --- a/provider/config/config.go +++ b/provider/config/config.go @@ -14,8 +14,8 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" - "github.com/ConsenSys/fc-retrieval/common/pkg/logging" - "github.com/ConsenSys/fc-retrieval/provider/internal/util/settings" + "github.com/ConsenSys/fc-retrieval/common/pkg/logging" + "github.com/ConsenSys/fc-retrieval/provider/internal/util/settings" ) // NewConfig creates a new configuration @@ -93,12 +93,13 @@ func Map(conf *viper.Viper) settings.AppSettings { RegisterRefreshDuration: registerRefreshDuration, ProviderAddress: conf.GetString("PROVIDER_ADDRESS"), + ProviderRegionCode: conf.GetString("PROVIDER_REGION_CODE"), ProviderRootSigningKey: conf.GetString("PROVIDER_ROOT_SIGNING_KEY"), ProviderSigningKey: conf.GetString("PROVIDER_SIGNING_KEY"), - ProviderRegionCode: conf.GetString("PROVIDER_REGION_CODE"), - NetworkInfoClient: conf.GetString("IP") + ":" + conf.GetString("NETWORK_CLIENT_INFO"), - NetworkInfoGateway: conf.GetString("IP") + ":" + conf.GetString("NETWORK_GATEWAY_INFO"), - NetworkInfoAdmin: conf.GetString("IP") + ":" + conf.GetString("NETWORK_ADMIN_INFO"), + + NetworkInfoClient: conf.GetString("IP") + ":" + conf.GetString("BIND_REST_API"), + NetworkInfoGateway: conf.GetString("IP") + ":" + conf.GetString("BIND_GATEWAY_API"), + NetworkInfoAdmin: conf.GetString("IP") + ":" + conf.GetString("BIND_ADMIN_API"), TCPInactivityTimeout: tcpInactivityTimeout, TCPLongInactivityTimeout: tcpLongInactivityTimeout, @@ -117,9 +118,9 @@ func defineFlags(_ *viper.Viper) { func bindFlags(conf *viper.Viper) { pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - if err := conf.BindPFlags(pflag.CommandLine); err != nil { - logging.Error("can't bind a command line flag") - } + if err := conf.BindPFlags(pflag.CommandLine); err != nil { + logging.Error("can't bind a command line flag") + } } func setValues(conf *viper.Viper) { diff --git a/provider/internal/api/gatewayapi/list_dht_offer_handler.go b/provider/internal/api/gatewayapi/list_dht_offer_handler.go index fdf3bc24..122212f4 100644 --- a/provider/internal/api/gatewayapi/list_dht_offer_handler.go +++ b/provider/internal/api/gatewayapi/list_dht_offer_handler.go @@ -21,6 +21,7 @@ package gatewayapi import ( "errors" + "fmt" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrmessages" @@ -134,11 +135,11 @@ func HandleGatewayListDHTOfferRequest(reader *fcrp2pserver.FCRServerReader, writ // TODO: Check nonce. _, signature, err := fcrmessages.DecodeProviderPublishDHTOfferResponse(&acknowledgement) if err != nil { - return err + return fmt.Errorf("error decoding provider publish DHT offer response: %s", err.Error()) } ok, err := fcrcrypto.VerifyMessage(pubKey, signature, msgs[i].GetMessageBody()) if err != nil { - return err + return fmt.Errorf("verification failed with error: %s", err.Error()) } if !ok { return errors.New("verification failed") diff --git a/provider/internal/api/providerapi/publish_dht_offer_requester.go b/provider/internal/api/providerapi/publish_dht_offer_requester.go index 1eec7151..646bd8b2 100644 --- a/provider/internal/api/providerapi/publish_dht_offer_requester.go +++ b/provider/internal/api/providerapi/publish_dht_offer_requester.go @@ -21,6 +21,7 @@ package providerapi import ( "errors" + "fmt" "github.com/ConsenSys/fc-retrieval/common/pkg/cidoffer" "github.com/ConsenSys/fc-retrieval/common/pkg/fcrcrypto" @@ -88,11 +89,11 @@ func RequestProviderPublishDHTOffer(reader *fcrp2pserver.FCRServerReader, writer // TODO: Check nonce _, sig, err := fcrmessages.DecodeProviderPublishDHTOfferResponse(response) if err != nil { - return nil, err + return nil, fmt.Errorf("error decoding provider publish DHT offer response: %s", err.Error()) } ok, err = fcrcrypto.VerifyMessage(pubKey, sig, request.GetMessageBody()) if err != nil { - return nil, errors.New("internal error in verifying ack") + return nil, fmt.Errorf("internal error in verifying ack: %s", err.Error()) } if !ok { return nil, errors.New("fail to verify the ack") diff --git a/provider/internal/util/settings/settings.go b/provider/internal/util/settings/settings.go index a17b99d7..8092a548 100644 --- a/provider/internal/util/settings/settings.go +++ b/provider/internal/util/settings/settings.go @@ -40,7 +40,7 @@ type AppSettings struct { LogLevel string `mapstructure:"LOG_LEVEL"` // Log Level: NONE, ERROR, WARN, INFO, TRACE LogTarget string `mapstructure:"LOG_TARGET"` // Log Level: STDOUT - LogDir string `mapstructure:"LOG_DIR"` // Log Dir: /var/log/fc-retrieval/fc-retrieval/provider + LogDir string `mapstructure:"LOG_DIR"` // Log Dir: /var/log/fc-retrieval/provider LogFile string `mapstructure:"LOG_FILE"` // Log File: provider.log LogMaxBackups int `mapstructure:"LOG_MAX_BACKUPS"` // Log max backups: 3 LogMaxAge int `mapstructure:"LOG_MAX_AGE"` // Log max age (days): 28 diff --git a/register/restapi/operations/gateway/get_gateway_registers_by_id_responses.go b/register/restapi/operations/gateway/get_gateway_registers_by_id_responses.go index de61bf37..237bf35e 100644 --- a/register/restapi/operations/gateway/get_gateway_registers_by_id_responses.go +++ b/register/restapi/operations/gateway/get_gateway_registers_by_id_responses.go @@ -6,6 +6,7 @@ package gateway // Editing this file might prove futile when you re-run the swagger generate command import ( + "log" "net/http" "github.com/go-openapi/runtime" @@ -110,7 +111,7 @@ func (o *GetGatewayRegistersByIDDefault) WriteResponse(rw http.ResponseWriter, p if o.Payload != nil { payload := o.Payload if err := producer.Produce(rw, payload); err != nil { - panic(err) // let the recovery middleware deal with this + log.Printf("error writing GetGatewayRegistersByIDDefault response: %s", err.Error()) } } } diff --git a/register/restapi/operations/gateway/get_gateway_registers_responses.go b/register/restapi/operations/gateway/get_gateway_registers_responses.go index fe4ba4bc..e226c88a 100644 --- a/register/restapi/operations/gateway/get_gateway_registers_responses.go +++ b/register/restapi/operations/gateway/get_gateway_registers_responses.go @@ -6,6 +6,7 @@ package gateway // Editing this file might prove futile when you re-run the swagger generate command import ( + "log" "net/http" "github.com/go-openapi/runtime" @@ -113,7 +114,7 @@ func (o *GetGatewayRegistersDefault) WriteResponse(rw http.ResponseWriter, produ if o.Payload != nil { payload := o.Payload if err := producer.Produce(rw, payload); err != nil { - panic(err) // let the recovery middleware deal with this + log.Printf("error writing GetGatewayRegistersDefault response: %s", err.Error()) } } }