github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/types/keys_test.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGetContractByCodeIDSecondaryIndexPrefix(t *testing.T) { 11 specs := map[string]struct { 12 src uint64 13 exp []byte 14 }{ 15 "small number": { 16 src: 1, 17 exp: []byte{6, 0, 0, 0, 0, 0, 0, 0, 1}, 18 }, 19 "big number": { 20 src: 1 << (8 * 7), 21 exp: []byte{6, 1, 0, 0, 0, 0, 0, 0, 0}, 22 }, 23 } 24 for msg, spec := range specs { 25 t.Run(msg, func(t *testing.T) { 26 got := GetContractByCodeIDSecondaryIndexPrefix(spec.src) 27 assert.Equal(t, spec.exp, got) 28 }) 29 } 30 } 31 32 func TestGetContractCodeHistoryElementPrefix(t *testing.T) { 33 // test that contract addresses of 20 length are still supported 34 addr := bytes.Repeat([]byte{4}, 20) 35 got := GetContractCodeHistoryElementPrefix(addr) 36 exp := []byte{ 37 5, // prefix 38 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 20 bytes 39 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 40 } 41 assert.Equal(t, exp, got) 42 43 addr = bytes.Repeat([]byte{4}, ContractAddrLen) 44 got = GetContractCodeHistoryElementPrefix(addr) 45 exp = []byte{ 46 5, // prefix 47 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes 48 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 49 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 50 4, 4, 51 } 52 assert.Equal(t, exp, got) 53 } 54 55 func TestGetContractByCreatedSecondaryIndexKey(t *testing.T) { 56 e := ContractCodeHistoryEntry{ 57 CodeID: 1, 58 Updated: &AbsoluteTxPosition{2 + 1<<(8*7), 3 + 1<<(8*7)}, 59 } 60 61 // test that contract addresses of 20 length are still supported 62 addr := bytes.Repeat([]byte{4}, 20) 63 got := GetContractByCreatedSecondaryIndexKey(addr, e) 64 exp := []byte{ 65 6, // prefix 66 0, 0, 0, 0, 0, 0, 0, 1, // codeID 67 1, 0, 0, 0, 0, 0, 0, 2, // height 68 1, 0, 0, 0, 0, 0, 0, 3, // index 69 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes 70 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 71 } 72 assert.Equal(t, exp, got) 73 74 addr = bytes.Repeat([]byte{4}, ContractAddrLen) 75 got = GetContractByCreatedSecondaryIndexKey(addr, e) 76 exp = []byte{ 77 6, // prefix 78 0, 0, 0, 0, 0, 0, 0, 1, // codeID 79 1, 0, 0, 0, 0, 0, 0, 2, // height 80 1, 0, 0, 0, 0, 0, 0, 3, // index 81 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes 82 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 83 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 84 4, 4, 85 } 86 assert.Equal(t, exp, got) 87 }