github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/ledger_test.go (about)

     1  package flow_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"testing"
     7  	"unicode/utf8"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/onflow/atree"
    13  
    14  	"github.com/onflow/flow-go/model/flow"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  // this benchmark can run with this command:
    19  //  go test -run=String -bench=.
    20  
    21  // this is to prevent lint errors
    22  var length int
    23  
    24  func BenchmarkString(b *testing.B) {
    25  
    26  	r := flow.NewRegisterID(unittest.RandomAddressFixture(), "123412341234")
    27  
    28  	ownerLen := len(r.Owner)
    29  
    30  	requiredLen := ((ownerLen + len(r.Key)) * 2) + 1
    31  
    32  	arr := make([]byte, requiredLen)
    33  
    34  	hex.Encode(arr, []byte(r.Owner))
    35  
    36  	arr[2*ownerLen] = byte('/')
    37  
    38  	hex.Encode(arr[(2*ownerLen)+1:], []byte(r.Key))
    39  
    40  	s := string(arr)
    41  	length = len(s)
    42  }
    43  
    44  func BenchmarkOriginalString(b *testing.B) {
    45  
    46  	r := flow.NewRegisterID(unittest.RandomAddressFixture(), "123412341234")
    47  
    48  	ret := fmt.Sprintf("%x/%x", r.Owner, r.Key)
    49  
    50  	length = len(ret)
    51  }
    52  
    53  func TestRegisterID_IsInternalState(t *testing.T) {
    54  	requireTrue := func(owner flow.Address, key string) {
    55  		id := flow.NewRegisterID(owner, key)
    56  		require.True(t, id.IsInternalState())
    57  	}
    58  
    59  	requireFalse := func(owner flow.Address, key string) {
    60  		id := flow.NewRegisterID(owner, key)
    61  		require.False(t, id.IsInternalState())
    62  	}
    63  
    64  	for i := 0; i < 256; i++ {
    65  		uuid := flow.UUIDRegisterID(byte(i))
    66  		if i == 0 {
    67  			require.Equal(t, uuid.Key, flow.UUIDKeyPrefix)
    68  			requireTrue(flow.EmptyAddress, flow.UUIDKeyPrefix)
    69  		} else {
    70  			require.Equal(t, uuid.Key, fmt.Sprintf("%s_%d", flow.UUIDKeyPrefix, i))
    71  			requireTrue(flow.EmptyAddress, fmt.Sprintf("%s_%d", flow.UUIDKeyPrefix, i))
    72  		}
    73  		require.True(t, uuid.IsInternalState())
    74  	}
    75  	require.True(t, flow.AddressStateRegisterID.IsInternalState())
    76  	requireTrue(flow.EmptyAddress, flow.AddressStateKey)
    77  	requireFalse(flow.EmptyAddress, "other")
    78  	requireFalse(unittest.RandomAddressFixture(), flow.UUIDKeyPrefix)
    79  	requireFalse(unittest.RandomAddressFixture(), flow.AddressStateKey)
    80  	requireTrue(unittest.RandomAddressFixture(), "public_key_12")
    81  	requireTrue(unittest.RandomAddressFixture(), flow.ContractNamesKey)
    82  	requireTrue(unittest.RandomAddressFixture(), "code.MYCODE")
    83  	requireTrue(unittest.RandomAddressFixture(), flow.AccountStatusKey)
    84  	requireFalse(unittest.RandomAddressFixture(), "anything else")
    85  }
    86  
    87  func TestRegisterID_String(t *testing.T) {
    88  	t.Run("atree slab", func(t *testing.T) {
    89  		// slab with 189 should result in \\xbd
    90  		slabIndex := atree.SlabIndex([8]byte{0, 0, 0, 0, 0, 0, 0, 189})
    91  
    92  		id := flow.NewRegisterID(
    93  			flow.BytesToAddress([]byte{1, 2, 3, 10}),
    94  			string(atree.SlabIndexToLedgerKey(slabIndex)))
    95  		require.False(t, utf8.ValidString(id.Key))
    96  		printable := id.String()
    97  		require.True(t, utf8.ValidString(printable))
    98  		require.Equal(t, "000000000102030a/$189", printable)
    99  	})
   100  
   101  	t.Run("non slab invalid utf-8", func(t *testing.T) {
   102  		id := flow.NewRegisterID(flow.BytesToAddress([]byte("b\xc5y")), "a\xc5z")
   103  		require.False(t, utf8.ValidString(id.Owner))
   104  		require.False(t, utf8.ValidString(id.Key))
   105  		printable := id.String()
   106  		require.True(t, utf8.ValidString(printable))
   107  		require.Equal(t, "000000000062c579/#61c57a", printable)
   108  	})
   109  
   110  	t.Run("global register", func(t *testing.T) {
   111  		uuidRegisterID := flow.UUIDRegisterID(0)
   112  		id := flow.NewRegisterID(flow.EmptyAddress, uuidRegisterID.Key)
   113  		require.Equal(t, uuidRegisterID.Owner, id.Owner)
   114  		require.Equal(t, uuidRegisterID.Key, id.Key)
   115  		printable := id.String()
   116  		assert.True(t, utf8.ValidString(printable))
   117  		assert.Equal(t, "/#75756964", printable)
   118  	})
   119  }