github.com/braveheart12/just@v0.8.7/network/merkle/helpers_test.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  	"encoding/hex"
    22  	"testing"
    23  
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/platformpolicy"
    26  	"github.com/insolar/insolar/pulsar/pulsartestutils"
    27  	"github.com/stretchr/testify/suite"
    28  )
    29  
    30  func (t *merkleHelperSuite) TestMerkleHelperPulseHash() {
    31  	pulse := &core.Pulse{
    32  		PulseNumber:     core.PulseNumber(1337),
    33  		NextPulseNumber: core.PulseNumber(1347),
    34  		Entropy:         pulsartestutils.MockEntropyGenerator{}.GenerateEntropy(),
    35  	}
    36  
    37  	expectedHash, _ := hex.DecodeString(
    38  		"bd18c009950389026c5c6f85c838b899d188ec0d667f77948aa72a49747c3ed31835b1bdbb8bd1d1de62846b5f308ae3eac5127c7d36d7d5464985004122cc90",
    39  	)
    40  
    41  	actualHash := t.mh.pulseHash(pulse)
    42  
    43  	t.Assert().Equal(expectedHash, actualHash)
    44  }
    45  
    46  func (t *merkleHelperSuite) TestMerkleHelperNodeInfoHash() {
    47  	pulseHash, _ := hex.DecodeString(
    48  		"bd18c009950389026c5c6f85c838b899d188ec0d667f77948aa72a49747c3ed31835b1bdbb8bd1d1de62846b5f308ae3eac5127c7d36d7d5464985004122cc90",
    49  	)
    50  
    51  	stateHash, _ := hex.DecodeString(
    52  		"11b2b580757fc04fccbbd88880c8f7e0cba5b1f87e61bcf5c18a20eca9bba83a8607119124bd54f6794f24104b9f16844ec26faa58782dec08829874edef8e75",
    53  	)
    54  
    55  	expectedHash, _ := hex.DecodeString(
    56  		"a03ff8f1a5c845b0fb563b76866d5aa73aa81dfc25398382af3b087d8be061d6c686b1f890b547350057daa746d374ff92b7e03c42e2d0d5ae0aaa4e6b6d5ce8",
    57  	)
    58  
    59  	actualHash := t.mh.nodeInfoHash(pulseHash, stateHash)
    60  
    61  	t.Assert().Equal(expectedHash, actualHash)
    62  }
    63  
    64  func (t *merkleHelperSuite) TestMerkleHelperNodeHash() {
    65  	nodeInfoHash, _ := hex.DecodeString(
    66  		"a03ff8f1a5c845b0fb563b76866d5aa73aa81dfc25398382af3b087d8be061d6c686b1f890b547350057daa746d374ff92b7e03c42e2d0d5ae0aaa4e6b6d5ce8",
    67  	)
    68  
    69  	signature, _ := hex.DecodeString("00000000")
    70  
    71  	expectedHash, _ := hex.DecodeString(
    72  		"09bbd5069e005d7a700838c54c07a828a90c0142087f00d0b779ad03645e7d7239a98f4cfa3f83c38646524b5c71bf5d0fb8a3ec303e4609aee9006ef7e27a9d",
    73  	)
    74  
    75  	actualHash := t.mh.nodeHash(signature, nodeInfoHash)
    76  
    77  	t.Assert().Equal(expectedHash, actualHash)
    78  }
    79  
    80  func (t *merkleHelperSuite) TestMerkleHelperBucketEntryHash() {
    81  	nodeHash, _ := hex.DecodeString(
    82  		"09bbd5069e005d7a700838c54c07a828a90c0142087f00d0b779ad03645e7d7239a98f4cfa3f83c38646524b5c71bf5d0fb8a3ec303e4609aee9006ef7e27a9d",
    83  	)
    84  
    85  	entryIndex := 0
    86  
    87  	expectedHash, _ := hex.DecodeString(
    88  		"de4f82d2fa34ca006f7e7c644dd8a7d71662094b4057d1ecf03d32bf9f5bb3e9a3ab03d6f91e14f0c01c3ca00e67911402e5cc308dc1ff549e17e1d520b9c291",
    89  	)
    90  
    91  	actualHash := t.mh.bucketEntryHash(uint32(entryIndex), nodeHash)
    92  
    93  	t.Assert().Equal(expectedHash, actualHash)
    94  }
    95  
    96  func (t *merkleHelperSuite) TestMerkleHelperBucketInfoHash() {
    97  	nodeCount := 1337
    98  	role := core.StaticRoleVirtual
    99  
   100  	expectedHash, _ := hex.DecodeString(
   101  		"eeb9dd175bb0d139083eadae8020f5b8623cb694263e8aec199c97213c383daf6ba0a58e734429b914cad1e401db1619526b1dabb57c5a020cd2fffed1f0cdeb",
   102  	)
   103  
   104  	actualHash := t.mh.bucketInfoHash(role, uint32(nodeCount))
   105  
   106  	t.Assert().Equal(expectedHash, actualHash)
   107  }
   108  
   109  func (t *merkleHelperSuite) TestMerkleHelperBucketHash() {
   110  	bucketInfoHash, _ := hex.DecodeString(
   111  		"de4f82d2fa34ca006f7e7c644dd8a7d71662094b4057d1ecf03d32bf9f5bb3e9a3ab03d6f91e14f0c01c3ca00e67911402e5cc308dc1ff549e17e1d520b9c291",
   112  	)
   113  
   114  	bucketEntryHash, _ := hex.DecodeString(
   115  		"eeb9dd175bb0d139083eadae8020f5b8623cb694263e8aec199c97213c383daf6ba0a58e734429b914cad1e401db1619526b1dabb57c5a020cd2fffed1f0cdeb",
   116  	)
   117  
   118  	expectedHash, _ := hex.DecodeString(
   119  		"6fe03f36a4dd1599bcf671b81b33bd9dae3c6ddcbf8616fec26d9dff4ab56d31cbb272c6de14b590b010327f6c76a745b3b99df9512607a97c7508f183ec5183",
   120  	)
   121  
   122  	actualHash := t.mh.bucketHash(bucketInfoHash, bucketEntryHash)
   123  
   124  	t.Assert().Equal(expectedHash, actualHash)
   125  }
   126  
   127  func (t *merkleHelperSuite) TestMerkleHelperGlobuleInfoHash() {
   128  	prevCloudHash, _ := hex.DecodeString(
   129  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   130  	)
   131  
   132  	globuleID := 1
   133  	nodeCount := 1337
   134  
   135  	expectedHash, _ := hex.DecodeString(
   136  		"c6e8c3ad6446f235c308425825f130a1840c625618b0bb04682a683fe42a72bd03d5de800a5f0ff0080503e925efd18dd27242a35882acdb7feab7631dd33ba6",
   137  	)
   138  
   139  	actualHash := t.mh.globuleInfoHash(prevCloudHash, uint32(globuleID), uint32(nodeCount))
   140  
   141  	t.Assert().Equal(expectedHash, actualHash)
   142  }
   143  
   144  func (t *merkleHelperSuite) TestMerkleHelperGlobuleHash() {
   145  	globuleInfoHash, _ := hex.DecodeString(
   146  		"c6e8c3ad6446f235c308425825f130a1840c625618b0bb04682a683fe42a72bd03d5de800a5f0ff0080503e925efd18dd27242a35882acdb7feab7631dd33ba6",
   147  	)
   148  
   149  	globuleNodeRoot, _ := hex.DecodeString(
   150  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   151  	)
   152  
   153  	expectedHash, _ := hex.DecodeString(
   154  		"e1ee3ff9d599cd946629ac14e052e1537abaeadc633ef8295313651ab25c432686a56c45c3f3fb2a59ac2a47ed947d5337281fa949f81ceeb87231d52f1f8eae",
   155  	)
   156  
   157  	actualHash := t.mh.globuleHash(globuleInfoHash, globuleNodeRoot)
   158  
   159  	t.Assert().Equal(expectedHash, actualHash)
   160  }
   161  
   162  type merkleHelperSuite struct {
   163  	suite.Suite
   164  
   165  	mh *merkleHelper
   166  }
   167  
   168  func TestMerkleHelper(t *testing.T) {
   169  	mh := newMerkleHelper(platformpolicy.NewPlatformCryptographyScheme())
   170  
   171  	s := &merkleHelperSuite{
   172  		Suite: suite.Suite{},
   173  		mh:    mh,
   174  	}
   175  	suite.Run(t, s)
   176  }