github.com/braveheart12/insolar-09-08-19@v0.8.7/network/merkle/calculator.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package merkle
    19  
    20  import (
    21  	"context"
    22  	"crypto"
    23  
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  type calculator struct {
    29  	ArtifactManager            core.ArtifactManager            `inject:""`
    30  	NodeNetwork                core.NodeNetwork                `inject:""`
    31  	PlatformCryptographyScheme core.PlatformCryptographyScheme `inject:""`
    32  	CryptographyService        core.CryptographyService        `inject:""`
    33  
    34  	merkleHelper *merkleHelper
    35  }
    36  
    37  func NewCalculator() Calculator {
    38  	return &calculator{}
    39  }
    40  
    41  func (c *calculator) Init(ctx context.Context) error {
    42  	c.merkleHelper = newMerkleHelper(c.PlatformCryptographyScheme)
    43  	return nil
    44  }
    45  
    46  func (c *calculator) getStateHash(role core.StaticRole) (OriginHash, error) {
    47  	// TODO: do something with role
    48  	return c.ArtifactManager.State()
    49  }
    50  
    51  func (c *calculator) GetPulseProof(entry *PulseEntry) (OriginHash, *PulseProof, error) {
    52  	role := c.NodeNetwork.GetOrigin().Role()
    53  	stateHash, err := c.getStateHash(role)
    54  	if err != nil {
    55  		return nil, nil, errors.Wrap(err, "[ GetPulseProof ] Failed to get node stateHash")
    56  	}
    57  
    58  	pulseHash := entry.hash(c.merkleHelper)
    59  	nodeInfoHash := c.merkleHelper.nodeInfoHash(pulseHash, stateHash)
    60  
    61  	signature, err := c.CryptographyService.Sign(nodeInfoHash)
    62  	if err != nil {
    63  		return nil, nil, errors.Wrap(err, "[ GetPulseProof ] Failed to sign node info hash")
    64  	}
    65  
    66  	return pulseHash, &PulseProof{
    67  		BaseProof: BaseProof{
    68  			Signature: *signature,
    69  		},
    70  		StateHash: stateHash,
    71  	}, nil
    72  }
    73  
    74  func (c *calculator) GetGlobuleProof(entry *GlobuleEntry) (OriginHash, *GlobuleProof, error) {
    75  	nodeRoot, err := entry.hash(c.merkleHelper)
    76  	if err != nil {
    77  		return nil, nil, errors.Wrap(err, "[ GetGlobuleProof ] Failed to calculate node root")
    78  	}
    79  
    80  	nodeCount := uint32(len(entry.ProofSet))
    81  	globuleInfoHash := c.merkleHelper.globuleInfoHash(entry.PrevCloudHash, uint32(entry.GlobuleID), nodeCount)
    82  	globuleHash := c.merkleHelper.globuleHash(globuleInfoHash, nodeRoot)
    83  
    84  	signature, err := c.CryptographyService.Sign(globuleHash)
    85  	if err != nil {
    86  		return nil, nil, errors.Wrap(err, "[ GetGlobuleProof ] Failed to sign globule hash")
    87  	}
    88  
    89  	return globuleHash, &GlobuleProof{
    90  		BaseProof: BaseProof{
    91  			Signature: *signature,
    92  		},
    93  		PrevCloudHash: entry.PrevCloudHash,
    94  		GlobuleID:     entry.GlobuleID,
    95  		NodeCount:     nodeCount,
    96  		NodeRoot:      nodeRoot,
    97  	}, nil
    98  }
    99  
   100  func (c *calculator) GetCloudProof(entry *CloudEntry) (OriginHash, *CloudProof, error) {
   101  	cloudHash, err := entry.hash(c.merkleHelper)
   102  	if err != nil {
   103  		return nil, nil, errors.Wrap(err, "[ GetCloudProof ] Failed to calculate cloud hash")
   104  	}
   105  
   106  	signature, err := c.CryptographyService.Sign(cloudHash)
   107  	if err != nil {
   108  		return nil, nil, errors.Wrap(err, "[ GetCloudProof ] Failed to sign cloud hash")
   109  	}
   110  
   111  	return cloudHash, &CloudProof{
   112  		BaseProof: BaseProof{
   113  			Signature: *signature,
   114  		},
   115  	}, nil
   116  }
   117  
   118  func (c *calculator) IsValid(proof Proof, hash OriginHash, publicKey crypto.PublicKey) bool {
   119  	return c.CryptographyService.Verify(publicKey, proof.signature(), proof.hash(hash, c.merkleHelper))
   120  }