github.com/braveheart12/just@v0.8.7/genesisdataprovider/genesisdataprovider.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package genesisdataprovider
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"github.com/insolar/insolar/application/extractor"
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/core/reply"
    26  	"github.com/pkg/errors"
    27  )
    28  
    29  // GenesisDataProvider gives access to basic information about genesis objects
    30  type GenesisDataProvider struct {
    31  	CertificateManager core.CertificateManager `inject:""`
    32  	ContractRequester  core.ContractRequester  `inject:""`
    33  
    34  	rootMemberRef *core.RecordRef
    35  	nodeDomainRef *core.RecordRef
    36  	lock          sync.RWMutex
    37  }
    38  
    39  // New creates new GenesisDataProvider
    40  func New() (*GenesisDataProvider, error) {
    41  	return &GenesisDataProvider{}, nil
    42  }
    43  
    44  func (gdp *GenesisDataProvider) setInfo(ctx context.Context) error {
    45  	routResult, err := gdp.ContractRequester.SendRequest(ctx, gdp.GetRootDomain(ctx), "Info", []interface{}{})
    46  	if err != nil {
    47  		return errors.Wrap(err, "[ setInfo ] Can't send request")
    48  	}
    49  
    50  	info, err := extractor.InfoResponse(routResult.(*reply.CallMethod).Result)
    51  	if err != nil {
    52  		return errors.Wrap(err, "[ setInfo ] Can't extract response")
    53  	}
    54  	rootMemberRef, err := core.NewRefFromBase58(info.RootMember)
    55  	if err != nil {
    56  		return errors.Wrap(err, "[ setInfo ] Failed to parse info.RootMember")
    57  	}
    58  	gdp.rootMemberRef = rootMemberRef
    59  	nodeDomainRef, err := core.NewRefFromBase58(info.NodeDomain)
    60  	if err != nil {
    61  		return errors.Wrap(err, "[ setInfo ] Failed to parse info.NodeDomain")
    62  	}
    63  	gdp.nodeDomainRef = nodeDomainRef
    64  
    65  	return nil
    66  }
    67  
    68  // GetRootDomain returns reference to RootDomain
    69  func (gdp *GenesisDataProvider) GetRootDomain(ctx context.Context) *core.RecordRef {
    70  	return gdp.CertificateManager.GetCertificate().GetRootDomainReference()
    71  }
    72  
    73  // GetNodeDomain returns reference to NodeDomain
    74  func (gdp *GenesisDataProvider) GetNodeDomain(ctx context.Context) (*core.RecordRef, error) {
    75  	gdp.lock.Lock()
    76  	defer gdp.lock.Unlock()
    77  	if gdp.nodeDomainRef == nil {
    78  		err := gdp.setInfo(ctx)
    79  		if err != nil {
    80  			return nil, errors.Wrap(err, "[ GenesisDataProvider::GetNodeDomain ] Can't get info")
    81  		}
    82  	}
    83  	return gdp.nodeDomainRef, nil
    84  }
    85  
    86  // GetRootMember returns reference to RootMember
    87  func (gdp *GenesisDataProvider) GetRootMember(ctx context.Context) (*core.RecordRef, error) {
    88  	gdp.lock.Lock()
    89  	defer gdp.lock.Unlock()
    90  	if gdp.rootMemberRef == nil {
    91  		err := gdp.setInfo(ctx)
    92  		if err != nil {
    93  			return nil, errors.Wrap(err, "[ GenesisDataProvider::GetRootMember ] Can't get info")
    94  		}
    95  	}
    96  	return gdp.rootMemberRef, nil
    97  }