github.com/aergoio/aergo@v1.3.1/contract/ethstorageproof_test.go (about)

     1  package contract
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  )
     8  
     9  func TestVerify(t *testing.T) {
    10  	tests := []struct {
    11  		key    []byte
    12  		value  []byte
    13  		proof  [][]byte
    14  		verify bool
    15  	}{
    16  		{
    17  			toBytes("0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49"),
    18  			toBytes("0x2710"),
    19  			proofToBytes([]string{
    20  				"0xf871a0379a71a6fb36a75e085aff02beec9f5934b9648d24e2901da307492219608b3780a006a684f73e33f5c18739fd1339977f6fe328eb5cbe64239244b0cec88744355180808080a023866491ea0336f72e659c2a7daf61285de093b04fa353c48069a807c2ba845f808080808080808080",
    21  				"0xe5a03eb5be412f275a18f6e4d622aee4ff40b21467c926224771b782d4c095d1444b83822710",
    22  			}),
    23  			true,
    24  		},
    25  		{
    26  			toBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
    27  			toBytes("0x6b746c656500000000000000000000000000000000000000000000000000000a"),
    28  			proofToBytes([]string{
    29  				"0xf844a120290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563a1a06b746c656500000000000000000000000000000000000000000000000000000a",
    30  			}),
    31  			true,
    32  		},
    33  		{
    34  			toBytes("0x0000000000000000000000000000000000000000000000000000000000000001"),
    35  			toBytes("0x6b736c656500000000000000000000000000000000000000000000000000000a"),
    36  			proofToBytes([]string{
    37  				"0xf8518080a05d2423f2a53dd794285a39f2c7ba5c0c24cba719d05e3bdd1f5eefae445b34358080808080808080a01ec473dfa012cb440907fa4f2c34be3e733c92430d98a48831700bc8ab159f5d8080808080",
    38  				"0xf843a0310e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6a1a06b736c656500000000000000000000000000000000000000000000000000000a",
    39  			}),
    40  			true,
    41  		},
    42  		{
    43  			toBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
    44  			toBytes("0x9"),
    45  			proofToBytes([]string{
    46  				"0xf8518080a05d2423f2a53dd794285a39f2c7ba5c0c24cba719d05e3bdd1f5eefae445b34358080808080808080a01ec473dfa012cb440907fa4f2c34be3e733c92430d98a48831700bc8ab159f5d8080808080",
    47  
    48  				"0xe2a0390decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56309",
    49  			}),
    50  			true,
    51  		},
    52  		{
    53  			toBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
    54  			toBytes("0x6b736c656500000000000000000000000000000000000000000000000000000a"),
    55  			proofToBytes([]string{
    56  				"0xf871a0379a71a6fb36a75e085aff02beec9f5934b9648d24e2901da307492219608b3780a006a684f73e33f5c18739fd1339977f6fe328eb5cbe64239244b0cec88744355180808080a023866491ea0336f72e659c2a7daf61285de093b04fa353c48069a807c2ba845f808080808080808080",
    57  				"0xf843a0390decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563a1a06b736c656500000000000000000000000000000000000000000000000000000a",
    58  			}),
    59  			true,
    60  		},
    61  		{
    62  			toBytes("0xac33ff75c19e70fe83507db0d683fd3465c996598dc972688b7ace676c89077b"),
    63  			toBytes("0x6b746c656500000000000000000000000000000000000000000000000000000a"),
    64  			proofToBytes([]string{
    65  				"0xf871a0379a71a6fb36a75e085aff02beec9f5934b9648d24e2901da307492219608b3780a006a684f73e33f5c18739fd1339977f6fe328eb5cbe64239244b0cec88744355180808080a023866491ea0336f72e659c2a7daf61285de093b04fa353c48069a807c2ba845f808080808080808080",
    66  				"0xf843a03d2944a272ac5bae96b5bd2f67b6c13276d541dc09eb1cf414d96b19a09e1c2fa1a06b746c656500000000000000000000000000000000000000000000000000000a",
    67  			}),
    68  			true,
    69  		},
    70  		{
    71  			toBytes("ac33ff75c19e70fe83507db0d683fd3465c996598dc972688b7ace676c89077b"),
    72  			toBytes("6b746c656500000000000000000000000000000000000000000000000000000a"),
    73  			proofToBytes([]string{
    74  				"0xf871a0379a71a6fb36a75e085aff02beec9f5934b9648d24e2901da307492219608b3780a006a684f73e33f5c18739fd1339977f6fe328eb5cbe64239244b0cec88744355180808080a023866491ea0336f72e659c2a7daf61285de093b04fa353c48069a807c2ba845f808080808080808080",
    75  				"0xf843a03d2944a272ac5bae96b5bd2f67b6c13276d541dc09eb1cf414d96b19a09e1c2fa1a06b746c656500000000000000000000000000000000000000000000000000000a",
    76  			}),
    77  			true,
    78  		},
    79  		{
    80  			toBytes("0xac33ff75c19e70fe83507db0d683fd3465c996598dc972688b7ace676c89077b"),
    81  			toBytes(""),
    82  			proofToBytes([]string{
    83  				"0xf871a0379a71a6fb36a75e085aff02beec9f5934b9648d24e2901da307492219608b3780a006a684f73e33f5c18739fd1339977f6fe328eb5cbe64239244b0cec88744355180808080a023866491ea0336f72e659c2a7daf61285de093b04fa353c48069a807c2ba845f808080808080808080",
    84  				"0xf843a03d2944a272ac5bae96b5bd2f67b6c13276d541dc09eb1cf414d96b19a09e1c2fa1a06b746c656500000000000000000000000000000000000000000000000000000a",
    85  			}),
    86  			false,
    87  		},
    88  	}
    89  
    90  	for i, tt := range tests {
    91  		if verifyEthStorageProof(tt.key, tt.value, keccak256(tt.proof[0]), tt.proof) != tt.verify {
    92  			t.Errorf("testcase %d: want %v, got %v\n", i, tt.verify, !tt.verify)
    93  		}
    94  	}
    95  }
    96  
    97  func TestDecodeRlpTrieNode(t *testing.T) {
    98  	tests := []struct {
    99  		data   []byte
   100  		length int
   101  		index  int
   102  		expect []byte
   103  	}{
   104  		{
   105  			[]byte{0xc5, 0x83, 'c', 'a', 't', 0x80},
   106  			2,
   107  			-1,
   108  			[]byte{},
   109  		},
   110  		{
   111  			[]byte{0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g'},
   112  			2,
   113  			-1,
   114  			[]byte{},
   115  		},
   116  		{
   117  			[]byte{0xC0},
   118  			0,
   119  			-1,
   120  			[]byte{},
   121  		},
   122  		{
   123  			[]byte{0xd1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q'},
   124  			17,
   125  			-1,
   126  			[]byte{},
   127  		},
   128  		{
   129  			[]byte{0xd1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'},
   130  			0,
   131  			-1,
   132  			[]byte{},
   133  		},
   134  		{
   135  			[]byte{0xd1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 0x80},
   136  			17,
   137  			-1,
   138  			[]byte{},
   139  		},
   140  		{
   141  			[]byte{0xd1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 0x80},
   142  			17,
   143  			0,
   144  			[]byte{'a'},
   145  		},
   146  		{
   147  			[]byte{0xd1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 0x80},
   148  			17,
   149  			16,
   150  			[]byte{},
   151  		},
   152  		{
   153  			[]byte{0xd3, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 0x82, 'm', 'n', 'o', 'p', 0x80, 'x'},
   154  			17,
   155  			12,
   156  			[]byte{'m', 'n'},
   157  		},
   158  	}
   159  	for i, tt := range tests {
   160  		n := decodeRlpTrieNode(tt.data)
   161  		if len(n) != tt.length {
   162  			t.Errorf("testcase %d: want %d, got %d\n", i, tt.length, len(n))
   163  			continue
   164  		}
   165  		if tt.index != -1 {
   166  			if !bytes.Equal(tt.expect, n[tt.index]) {
   167  				t.Errorf("testcase %d: want %v, got %v\n", i, tt.expect, n[tt.index])
   168  			}
   169  		}
   170  	}
   171  }
   172  
   173  func TestKeccak256(t *testing.T) {
   174  	tests := []struct {
   175  		msg string
   176  		h   string
   177  	}{
   178  		{
   179  			"abc",
   180  			"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
   181  		},
   182  		{
   183  			"aergo",
   184  			"e98bb03ab37161f8bbfe131f711dcccf3002a9cd9ec31bbd52edf181f7ab09a0",
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		h := keccak256Hex([]byte(tt.msg))
   189  		if tt.h != h {
   190  			t.Errorf("want %s, got %s\n", tt.h, h)
   191  		}
   192  	}
   193  }
   194  
   195  func removeHexPrefix(s string) string {
   196  	if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
   197  		s = s[2:]
   198  	}
   199  	if len(s)&1 == 1 {
   200  		s = "0" + s
   201  	}
   202  	return s
   203  }
   204  
   205  func toBytes(s string) []byte {
   206  	n, _ := hex.DecodeString(removeHexPrefix(s))
   207  	return n
   208  }
   209  
   210  func proofToBytes(proof []string) [][]byte {
   211  	var r [][]byte
   212  	for _, n := range proof {
   213  		d, err := hex.DecodeString(removeHexPrefix(n))
   214  		if err != nil {
   215  			return [][]byte{}
   216  		}
   217  		r = append(r, d)
   218  	}
   219  	return r
   220  }