github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/ledger/common/convert/convert_test.go (about)

     1  package convert_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/onflow/flow-go/ledger"
     9  	"github.com/onflow/flow-go/ledger/common/convert"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  func TestLedgerKeyToRegisterID(t *testing.T) {
    15  	expectedRegisterID := unittest.RegisterIDFixture()
    16  
    17  	key := ledger.Key{
    18  		KeyParts: []ledger.KeyPart{
    19  			{
    20  				Type:  ledger.KeyPartOwner,
    21  				Value: []byte(expectedRegisterID.Owner),
    22  			},
    23  			{
    24  				Type:  ledger.KeyPartKey,
    25  				Value: []byte("key"),
    26  			},
    27  		},
    28  	}
    29  
    30  	registerID, err := convert.LedgerKeyToRegisterID(key)
    31  	require.NoError(t, err)
    32  	require.Equal(t, expectedRegisterID, registerID)
    33  
    34  	p := ledger.NewPayload(key, ledger.Value("value"))
    35  
    36  	address, err := p.Address()
    37  
    38  	require.NoError(t, err)
    39  	require.Equal(t, registerID.Owner, flow.AddressToRegisterOwner(address))
    40  	require.Equal(t, registerID.Owner, string(address[:]))
    41  }
    42  
    43  func TestLedgerKeyToRegisterID_Global(t *testing.T) {
    44  	key := ledger.Key{
    45  		KeyParts: []ledger.KeyPart{
    46  			{
    47  				Type:  ledger.KeyPartOwner,
    48  				Value: []byte(""),
    49  			},
    50  			{
    51  				Type:  ledger.KeyPartKey,
    52  				Value: []byte("uuid"),
    53  			},
    54  		},
    55  	}
    56  
    57  	expectedRegisterID := flow.UUIDRegisterID(0)
    58  	registerID, err := convert.LedgerKeyToRegisterID(key)
    59  	require.NoError(t, err)
    60  	require.Equal(t, expectedRegisterID, registerID)
    61  
    62  	p := ledger.NewPayload(key, ledger.Value("value"))
    63  
    64  	address, err := p.Address()
    65  
    66  	require.NoError(t, err)
    67  	require.Equal(t, registerID.Owner, flow.AddressToRegisterOwner(address))
    68  	require.NotEqual(t, registerID.Owner, string(address[:]))
    69  }
    70  
    71  func TestLedgerKeyToRegisterID_Error(t *testing.T) {
    72  	key := ledger.Key{
    73  		KeyParts: []ledger.KeyPart{
    74  			{
    75  				Type:  999, // Invalid type
    76  				Value: []byte("owner"),
    77  			},
    78  			{
    79  				Type:  ledger.KeyPartKey,
    80  				Value: []byte("key"),
    81  			},
    82  		},
    83  	}
    84  
    85  	_, err := convert.LedgerKeyToRegisterID(key)
    86  	require.Error(t, err)
    87  	require.ErrorIs(t, err, convert.UnexpectedLedgerKeyFormat)
    88  }
    89  
    90  func TestRegisterIDToLedgerKey(t *testing.T) {
    91  	registerID := unittest.RegisterIDFixture()
    92  	expectedKey := ledger.Key{
    93  		KeyParts: []ledger.KeyPart{
    94  			{
    95  				Type: ledger.KeyPartOwner,
    96  				// Note: the owner field is extended to address length during NewRegisterID
    97  				// so we have to do the same here
    98  				Value: []byte(registerID.Owner),
    99  			},
   100  			{
   101  				Type:  ledger.KeyPartKey,
   102  				Value: []byte("key"),
   103  			},
   104  		},
   105  	}
   106  
   107  	key := convert.RegisterIDToLedgerKey(registerID)
   108  	require.Equal(t, expectedKey, key)
   109  }
   110  
   111  func TestRegisterIDToLedgerKey_Global(t *testing.T) {
   112  	registerID := flow.UUIDRegisterID(0)
   113  	expectedKey := ledger.Key{
   114  		KeyParts: []ledger.KeyPart{
   115  			{
   116  				Type:  ledger.KeyPartOwner,
   117  				Value: []byte(""),
   118  			},
   119  			{
   120  				Type:  ledger.KeyPartKey,
   121  				Value: []byte("uuid"),
   122  			},
   123  		},
   124  	}
   125  
   126  	key := convert.RegisterIDToLedgerKey(registerID)
   127  	require.Equal(t, expectedKey, key)
   128  }
   129  
   130  func TestPayloadToRegister(t *testing.T) {
   131  	expected := unittest.RegisterIDFixture()
   132  	t.Run("can convert", func(t *testing.T) {
   133  		value := []byte("value")
   134  		p := ledger.NewPayload(
   135  			ledger.NewKey(
   136  				[]ledger.KeyPart{
   137  					ledger.NewKeyPart(ledger.KeyPartOwner, []byte(expected.Owner)),
   138  					ledger.NewKeyPart(ledger.KeyPartKey, []byte(expected.Key)),
   139  				},
   140  			),
   141  			value,
   142  		)
   143  		regID, regValue, err := convert.PayloadToRegister(p)
   144  		require.NoError(t, err)
   145  		require.Equal(t, expected, regID)
   146  		require.Equal(t, value, regValue)
   147  	})
   148  
   149  	t.Run("global key", func(t *testing.T) {
   150  		value := []byte("1")
   151  		p := ledger.NewPayload(
   152  			ledger.NewKey(
   153  				[]ledger.KeyPart{
   154  					ledger.NewKeyPart(ledger.KeyPartOwner, []byte("")),
   155  					ledger.NewKeyPart(ledger.KeyPartKey, []byte("uuid")),
   156  				},
   157  			),
   158  			value,
   159  		)
   160  		regID, regValue, err := convert.PayloadToRegister(p)
   161  		require.NoError(t, err)
   162  		require.Equal(t, flow.NewRegisterID(flow.EmptyAddress, "uuid"), regID)
   163  		require.Equal(t, "", regID.Owner)
   164  		require.Equal(t, "uuid", regID.Key)
   165  		require.True(t, regID.IsInternalState())
   166  		require.Equal(t, value, regValue)
   167  	})
   168  
   169  	t.Run("empty payload", func(t *testing.T) {
   170  		p := ledger.EmptyPayload()
   171  		_, _, err := convert.PayloadToRegister(p)
   172  		require.Error(t, err)
   173  	})
   174  }