github.com/Finschia/finschia-sdk@v0.48.1/store/internal/proofs/convert_test.go (about)

     1  package proofs
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func TestLeafOp(t *testing.T) {
    10  	proof := GenerateRangeProof(20, Middle)
    11  
    12  	converted, err := ConvertExistenceProof(proof.Proof, proof.Key, proof.Value)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	leaf := converted.GetLeaf()
    18  	if leaf == nil {
    19  		t.Fatalf("Missing leaf node")
    20  	}
    21  
    22  	hash, err := leaf.Apply(converted.Key, converted.Value)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	if !bytes.Equal(hash, proof.Proof.LeafHash) {
    28  		t.Errorf("Calculated: %X\nExpected:   %X", hash, proof.Proof.LeafHash)
    29  	}
    30  }
    31  
    32  func TestBuildPath(t *testing.T) {
    33  	cases := map[string]struct {
    34  		idx      int64
    35  		total    int64
    36  		expected []bool
    37  	}{
    38  		"pair left": {
    39  			idx:      0,
    40  			total:    2,
    41  			expected: []bool{true},
    42  		},
    43  		"pair right": {
    44  			idx:      1,
    45  			total:    2,
    46  			expected: []bool{false},
    47  		},
    48  		"power of 2": {
    49  			idx:      3,
    50  			total:    8,
    51  			expected: []bool{false, false, true},
    52  		},
    53  		"size of 7 right most": {
    54  			idx:      6,
    55  			total:    7,
    56  			expected: []bool{false, false},
    57  		},
    58  		"size of 6 right-left (from top)": {
    59  			idx:      4,
    60  			total:    6,
    61  			expected: []bool{true, false},
    62  		},
    63  		"size of 6 left-right-left (from top)": {
    64  			idx:      2,
    65  			total:    7,
    66  			expected: []bool{true, false, true},
    67  		},
    68  	}
    69  
    70  	for name, tc := range cases {
    71  		t.Run(name, func(t *testing.T) {
    72  			path := buildPath(tc.idx, tc.total)
    73  			if len(path) != len(tc.expected) {
    74  				t.Fatalf("Got %v\nExpected %v", path, tc.expected)
    75  			}
    76  			for i := range path {
    77  				if path[i] != tc.expected[i] {
    78  					t.Fatalf("Differ at %d\nGot %v\nExpected %v", i, path, tc.expected)
    79  				}
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestConvertProof(t *testing.T) {
    86  	for i := 0; i < 100; i++ {
    87  		t.Run(fmt.Sprintf("Run %d", i), func(t *testing.T) {
    88  			proof := GenerateRangeProof(57, Left)
    89  
    90  			converted, err := ConvertExistenceProof(proof.Proof, proof.Key, proof.Value)
    91  			if err != nil {
    92  				t.Fatal(err)
    93  			}
    94  
    95  			calc, err := converted.Calculate()
    96  			if err != nil {
    97  				t.Fatal(err)
    98  			}
    99  
   100  			if !bytes.Equal(calc, proof.RootHash) {
   101  				t.Errorf("Calculated: %X\nExpected:   %X", calc, proof.RootHash)
   102  			}
   103  		})
   104  	}
   105  }