github.com/koko1123/flow-go-1@v0.29.6/engine/execution/state/delta/view_test.go (about)

     1  package delta_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/engine/execution/state/delta"
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/onflow/flow-go/crypto/hash"
    12  )
    13  
    14  func TestViewGet(t *testing.T) {
    15  	registerID := "fruit"
    16  
    17  	t.Run("ValueNotSet", func(t *testing.T) {
    18  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    19  			return nil, nil
    20  		})
    21  
    22  		b, err := v.Get(registerID, "")
    23  		assert.NoError(t, err)
    24  		assert.Nil(t, b)
    25  	})
    26  
    27  	t.Run("ValueNotInCache", func(t *testing.T) {
    28  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    29  			if owner == registerID {
    30  				return flow.RegisterValue("orange"), nil
    31  			}
    32  
    33  			return nil, nil
    34  		})
    35  		b, err := v.Get(registerID, "")
    36  		assert.NoError(t, err)
    37  		assert.Equal(t, flow.RegisterValue("orange"), b)
    38  	})
    39  
    40  	t.Run("ValueInCache", func(t *testing.T) {
    41  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    42  			if owner == registerID {
    43  				return flow.RegisterValue("orange"), nil
    44  			}
    45  
    46  			return nil, nil
    47  		})
    48  
    49  		err := v.Set(registerID, "", flow.RegisterValue("apple"))
    50  		assert.NoError(t, err)
    51  
    52  		b, err := v.Get(registerID, "")
    53  		assert.NoError(t, err)
    54  		assert.Equal(t, flow.RegisterValue("apple"), b)
    55  	})
    56  }
    57  
    58  func TestViewSet(t *testing.T) {
    59  	registerID := "fruit"
    60  
    61  	v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    62  		return nil, nil
    63  	})
    64  
    65  	err := v.Set(registerID, "", flow.RegisterValue("apple"))
    66  	assert.NoError(t, err)
    67  
    68  	b1, err := v.Get(registerID, "")
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, flow.RegisterValue("apple"), b1)
    71  
    72  	err = v.Set(registerID, "", flow.RegisterValue("orange"))
    73  	assert.NoError(t, err)
    74  
    75  	b2, err := v.Get(registerID, "")
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, flow.RegisterValue("orange"), b2)
    78  
    79  	t.Run("AfterDelete", func(t *testing.T) {
    80  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    81  			return nil, nil
    82  		})
    83  
    84  		err := v.Set(registerID, "", flow.RegisterValue("apple"))
    85  		assert.NoError(t, err)
    86  		err = v.Delete(registerID, "")
    87  		assert.NoError(t, err)
    88  		err = v.Set(registerID, "", flow.RegisterValue("orange"))
    89  		assert.NoError(t, err)
    90  
    91  		b, err := v.Get(registerID, "")
    92  		assert.NoError(t, err)
    93  		assert.Equal(t, flow.RegisterValue("orange"), b)
    94  	})
    95  
    96  	t.Run("SpockSecret", func(t *testing.T) {
    97  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
    98  			return nil, nil
    99  		})
   100  
   101  		t.Run("reflects in the snapshot", func(t *testing.T) {
   102  			assert.Equal(t, v.SpockSecret(), v.Interactions().SpockSecret)
   103  		})
   104  
   105  		v = delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   106  			return nil, nil
   107  		})
   108  
   109  		registerID1 := "reg1"
   110  		registerID2 := "reg2"
   111  		registerID3 := "reg3"
   112  
   113  		// prepare the registerID bytes
   114  		register := flow.NewRegisterID("", "")
   115  		register.Owner = registerID1
   116  		registerID1Bytes := register.Bytes()
   117  		register.Owner = registerID2
   118  		registerID2Bytes := register.Bytes()
   119  		register.Owner = registerID3
   120  		registerID3Bytes := register.Bytes()
   121  
   122  		// this part checks that spocks ordering be based
   123  		// on update orders and not registerIDs
   124  		expSpock := hash.NewSHA3_256()
   125  		err = v.Set(registerID2, "", flow.RegisterValue("1"))
   126  		require.NoError(t, err)
   127  		hashIt(t, expSpock, registerID2Bytes)
   128  		hashIt(t, expSpock, []byte("1"))
   129  
   130  		err = v.Set(registerID3, "", flow.RegisterValue("2"))
   131  		require.NoError(t, err)
   132  		hashIt(t, expSpock, registerID3Bytes)
   133  		hashIt(t, expSpock, []byte("2"))
   134  
   135  		err = v.Set(registerID1, "", flow.RegisterValue("3"))
   136  		require.NoError(t, err)
   137  		hashIt(t, expSpock, registerID1Bytes)
   138  		hashIt(t, expSpock, []byte("3"))
   139  
   140  		_, err := v.Get(registerID1, "")
   141  		require.NoError(t, err)
   142  		hashIt(t, expSpock, registerID1Bytes)
   143  
   144  		// this part uses the delete functionality
   145  		// to check that only the register ID is written to the spock secret
   146  		err = v.Delete(registerID1, "")
   147  		require.NoError(t, err)
   148  		hashIt(t, expSpock, registerID1Bytes)
   149  
   150  		// this part checks that it always update the
   151  		// intermediate values and not just the final values
   152  		err = v.Set(registerID1, "", flow.RegisterValue("4"))
   153  		require.NoError(t, err)
   154  		hashIt(t, expSpock, registerID1Bytes)
   155  		hashIt(t, expSpock, []byte("4"))
   156  
   157  		err = v.Set(registerID1, "", flow.RegisterValue("5"))
   158  		require.NoError(t, err)
   159  		hashIt(t, expSpock, registerID1Bytes)
   160  		hashIt(t, expSpock, []byte("5"))
   161  
   162  		err = v.Set(registerID3, "", flow.RegisterValue("6"))
   163  		require.NoError(t, err)
   164  		hashIt(t, expSpock, registerID3Bytes)
   165  		hashIt(t, expSpock, []byte("6"))
   166  
   167  		s := v.SpockSecret()
   168  		assert.Equal(t, hash.Hash(s), expSpock.SumHash())
   169  
   170  		t.Run("reflects in the snapshot", func(t *testing.T) {
   171  			assert.Equal(t, v.SpockSecret(), v.Interactions().SpockSecret)
   172  		})
   173  	})
   174  }
   175  
   176  func TestView_Delete(t *testing.T) {
   177  	registerID := "fruit"
   178  
   179  	t.Run("ValueNotSet", func(t *testing.T) {
   180  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   181  			return nil, nil
   182  		})
   183  
   184  		b1, err := v.Get(registerID, "")
   185  		assert.NoError(t, err)
   186  		assert.Nil(t, b1)
   187  
   188  		err = v.Delete(registerID, "")
   189  		assert.NoError(t, err)
   190  
   191  		b2, err := v.Get(registerID, "")
   192  		assert.NoError(t, err)
   193  		assert.Nil(t, b2)
   194  	})
   195  
   196  	t.Run("ValueInCache", func(t *testing.T) {
   197  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   198  			if owner == registerID {
   199  				return flow.RegisterValue("orange"), nil
   200  			}
   201  
   202  			return nil, nil
   203  		})
   204  
   205  		err := v.Set(registerID, "", flow.RegisterValue("apple"))
   206  		assert.NoError(t, err)
   207  
   208  		b1, err := v.Get(registerID, "")
   209  		assert.NoError(t, err)
   210  		assert.Equal(t, flow.RegisterValue("apple"), b1)
   211  
   212  		err = v.Delete(registerID, "")
   213  		assert.NoError(t, err)
   214  
   215  		b2, err := v.Get(registerID, "")
   216  		assert.NoError(t, err)
   217  		assert.Nil(t, b2)
   218  	})
   219  }
   220  
   221  func TestViewMergeView(t *testing.T) {
   222  	registerID1 := "fruit"
   223  
   224  	registerID2 := "vegetable"
   225  
   226  	registerID3 := "diary"
   227  
   228  	t.Run("EmptyView", func(t *testing.T) {
   229  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   230  			return nil, nil
   231  		})
   232  
   233  		chView := v.NewChild()
   234  		err := chView.Set(registerID1, "", flow.RegisterValue("apple"))
   235  		assert.NoError(t, err)
   236  		err = chView.Set(registerID2, "", flow.RegisterValue("carrot"))
   237  		assert.NoError(t, err)
   238  
   239  		err = v.MergeView(chView)
   240  		assert.NoError(t, err)
   241  
   242  		b1, err := v.Get(registerID1, "")
   243  		assert.NoError(t, err)
   244  		assert.Equal(t, flow.RegisterValue("apple"), b1)
   245  
   246  		b2, err := v.Get(registerID2, "")
   247  		assert.NoError(t, err)
   248  		assert.Equal(t, flow.RegisterValue("carrot"), b2)
   249  	})
   250  
   251  	t.Run("EmptyDelta", func(t *testing.T) {
   252  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   253  			return nil, nil
   254  		})
   255  
   256  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   257  		assert.NoError(t, err)
   258  		err = v.Set(registerID2, "", flow.RegisterValue("carrot"))
   259  		assert.NoError(t, err)
   260  
   261  		chView := v.NewChild()
   262  		err = v.MergeView(chView)
   263  		assert.NoError(t, err)
   264  
   265  		b1, err := v.Get(registerID1, "")
   266  		assert.NoError(t, err)
   267  		assert.Equal(t, flow.RegisterValue("apple"), b1)
   268  
   269  		b2, err := v.Get(registerID2, "")
   270  		assert.NoError(t, err)
   271  		assert.Equal(t, flow.RegisterValue("carrot"), b2)
   272  	})
   273  
   274  	t.Run("NoCollisions", func(t *testing.T) {
   275  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   276  			return nil, nil
   277  		})
   278  
   279  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   280  		assert.NoError(t, err)
   281  
   282  		chView := v.NewChild()
   283  		err = chView.Set(registerID2, "", flow.RegisterValue("carrot"))
   284  		assert.NoError(t, err)
   285  
   286  		err = v.MergeView(chView)
   287  		assert.NoError(t, err)
   288  
   289  		b1, err := v.Get(registerID1, "")
   290  		assert.NoError(t, err)
   291  		assert.Equal(t, flow.RegisterValue("apple"), b1)
   292  
   293  		b2, err := v.Get(registerID2, "")
   294  		assert.NoError(t, err)
   295  		assert.Equal(t, flow.RegisterValue("carrot"), b2)
   296  	})
   297  
   298  	t.Run("OverwriteSetValue", func(t *testing.T) {
   299  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   300  			return nil, nil
   301  		})
   302  
   303  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   304  		assert.NoError(t, err)
   305  
   306  		chView := v.NewChild()
   307  		err = chView.Set(registerID1, "", flow.RegisterValue("orange"))
   308  		assert.NoError(t, err)
   309  		err = v.MergeView(chView)
   310  		assert.NoError(t, err)
   311  
   312  		b, err := v.Get(registerID1, "")
   313  		assert.NoError(t, err)
   314  		assert.Equal(t, flow.RegisterValue("orange"), b)
   315  	})
   316  
   317  	t.Run("OverwriteDeletedValue", func(t *testing.T) {
   318  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   319  			return nil, nil
   320  		})
   321  
   322  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   323  		assert.NoError(t, err)
   324  		err = v.Delete(registerID1, "")
   325  		assert.NoError(t, err)
   326  
   327  		chView := v.NewChild()
   328  		err = chView.Set(registerID1, "", flow.RegisterValue("orange"))
   329  		assert.NoError(t, err)
   330  		err = v.MergeView(chView)
   331  		assert.NoError(t, err)
   332  
   333  		b, err := v.Get(registerID1, "")
   334  		assert.NoError(t, err)
   335  		assert.Equal(t, flow.RegisterValue("orange"), b)
   336  	})
   337  
   338  	t.Run("DeleteSetValue", func(t *testing.T) {
   339  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   340  			return nil, nil
   341  		})
   342  
   343  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   344  		assert.NoError(t, err)
   345  
   346  		chView := v.NewChild()
   347  		err = chView.Delete(registerID1, "")
   348  		assert.NoError(t, err)
   349  		err = v.MergeView(chView)
   350  		assert.NoError(t, err)
   351  
   352  		b, err := v.Get(registerID1, "")
   353  		assert.NoError(t, err)
   354  		assert.Nil(t, b)
   355  	})
   356  	t.Run("SpockDataMerge", func(t *testing.T) {
   357  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   358  			return nil, nil
   359  		})
   360  
   361  		register := flow.NewRegisterID("", "")
   362  		register.Owner = registerID1
   363  		registerID1Bytes := register.Bytes()
   364  		register.Owner = registerID2
   365  		registerID2Bytes := register.Bytes()
   366  
   367  		expSpock1 := hash.NewSHA3_256()
   368  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   369  		assert.NoError(t, err)
   370  		hashIt(t, expSpock1, registerID1Bytes)
   371  		hashIt(t, expSpock1, []byte("apple"))
   372  		assert.NoError(t, err)
   373  
   374  		expSpock2 := hash.NewSHA3_256()
   375  		chView := v.NewChild()
   376  		err = chView.Set(registerID2, "", flow.RegisterValue("carrot"))
   377  		require.NoError(t, err)
   378  		hashIt(t, expSpock2, registerID2Bytes)
   379  		hashIt(t, expSpock2, []byte("carrot"))
   380  
   381  		hash2 := expSpock2.SumHash()
   382  		assert.Equal(t, chView.(*delta.View).SpockSecret(), []uint8(hash2))
   383  
   384  		err = v.MergeView(chView)
   385  		assert.NoError(t, err)
   386  
   387  		hashIt(t, expSpock1, hash2)
   388  		assert.Equal(t, v.SpockSecret(), []uint8(expSpock1.SumHash()))
   389  	})
   390  
   391  	t.Run("RegisterTouchesDataMerge", func(t *testing.T) {
   392  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   393  			return nil, nil
   394  		})
   395  
   396  		err := v.Set(registerID1, "", flow.RegisterValue("apple"))
   397  		assert.NoError(t, err)
   398  
   399  		chView := v.NewChild()
   400  		err = chView.Set(registerID2, "", flow.RegisterValue("carrot"))
   401  		assert.NoError(t, err)
   402  		err = chView.Set(registerID3, "", flow.RegisterValue("milk"))
   403  		assert.NoError(t, err)
   404  
   405  		err = v.MergeView(chView)
   406  		assert.NoError(t, err)
   407  
   408  		reads := v.Interactions().Reads
   409  
   410  		require.Len(t, reads, 3)
   411  
   412  		r1 := flow.NewRegisterID(registerID1, "")
   413  		r2 := flow.NewRegisterID(registerID2, "")
   414  		r3 := flow.NewRegisterID(registerID3, "")
   415  
   416  		assert.Equal(t, map[flow.RegisterID]struct{}{
   417  			r1: struct{}{},
   418  			r2: struct{}{},
   419  			r3: struct{}{},
   420  		}, reads)
   421  	})
   422  
   423  }
   424  
   425  func TestView_RegisterTouches(t *testing.T) {
   426  	registerID1 := "fruit"
   427  	registerID2 := "vegetable"
   428  
   429  	v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   430  		return nil, nil
   431  	})
   432  
   433  	t.Run("Empty", func(t *testing.T) {
   434  		touches := v.Interactions().RegisterTouches()
   435  		assert.Empty(t, touches)
   436  	})
   437  
   438  	t.Run("Set and Get", func(t *testing.T) {
   439  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   440  			if owner == registerID1 {
   441  				return flow.RegisterValue("orange"), nil
   442  			}
   443  
   444  			if owner == registerID2 {
   445  				return flow.RegisterValue("carrot"), nil
   446  			}
   447  
   448  			return nil, nil
   449  		})
   450  
   451  		_, err := v.Get(registerID1, "")
   452  		assert.NoError(t, err)
   453  
   454  		err = v.Set(registerID2, "", flow.RegisterValue("apple"))
   455  		assert.NoError(t, err)
   456  
   457  		touches := v.Interactions().RegisterTouches()
   458  		assert.Len(t, touches, 2)
   459  	})
   460  }
   461  
   462  func TestView_AllRegisters(t *testing.T) {
   463  	v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   464  		return nil, nil
   465  	})
   466  
   467  	t.Run("Empty", func(t *testing.T) {
   468  		regs := v.Interactions().AllRegisters()
   469  		assert.Empty(t, regs)
   470  	})
   471  
   472  	t.Run("Set and Get", func(t *testing.T) {
   473  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   474  			if owner == "a" {
   475  				return flow.RegisterValue("a_value"), nil
   476  			}
   477  
   478  			if owner == "b" {
   479  				return flow.RegisterValue("b_value"), nil
   480  			}
   481  			return nil, nil
   482  		})
   483  
   484  		_, err := v.Get("a", "")
   485  		assert.NoError(t, err)
   486  
   487  		_, err = v.Get("b", "")
   488  		assert.NoError(t, err)
   489  
   490  		err = v.Set("c", "", flow.RegisterValue("c_value"))
   491  		assert.NoError(t, err)
   492  
   493  		err = v.Set("d", "", flow.RegisterValue("d_value"))
   494  		assert.NoError(t, err)
   495  
   496  		err = v.Touch("e", "")
   497  		assert.NoError(t, err)
   498  		err = v.Touch("f", "")
   499  		assert.NoError(t, err)
   500  
   501  		allRegs := v.Interactions().AllRegisters()
   502  		assert.Len(t, allRegs, 6)
   503  	})
   504  	t.Run("With Merge", func(t *testing.T) {
   505  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   506  			if owner == "a" {
   507  				return flow.RegisterValue("a_value"), nil
   508  			}
   509  
   510  			if owner == "b" {
   511  				return flow.RegisterValue("b_value"), nil
   512  			}
   513  			return nil, nil
   514  		})
   515  
   516  		vv := v.NewChild()
   517  		_, err := vv.Get("a", "")
   518  		assert.NoError(t, err)
   519  
   520  		_, err = vv.Get("b", "")
   521  		assert.NoError(t, err)
   522  
   523  		err = vv.Set("c", "", flow.RegisterValue("c_value"))
   524  		assert.NoError(t, err)
   525  		err = vv.Set("d", "", flow.RegisterValue("d_value"))
   526  		assert.NoError(t, err)
   527  
   528  		err = vv.Touch("e", "")
   529  		assert.NoError(t, err)
   530  		err = vv.Touch("f", "")
   531  		assert.NoError(t, err)
   532  
   533  		err = v.MergeView(vv)
   534  		assert.NoError(t, err)
   535  		allRegs := v.Interactions().AllRegisters()
   536  		assert.Len(t, allRegs, 6)
   537  	})
   538  }
   539  
   540  func TestView_Reads(t *testing.T) {
   541  	registerID1 := "fruit"
   542  	registerID2 := "vegetable"
   543  
   544  	v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   545  		return nil, nil
   546  	})
   547  
   548  	t.Run("Empty", func(t *testing.T) {
   549  		reads := v.Interactions().Reads
   550  		assert.Empty(t, reads)
   551  	})
   552  
   553  	t.Run("Set and Get", func(t *testing.T) {
   554  		v := delta.NewView(func(owner, key string) (flow.RegisterValue, error) {
   555  			return nil, nil
   556  		})
   557  
   558  		_, err := v.Get(registerID2, "")
   559  		assert.NoError(t, err)
   560  
   561  		_, err = v.Get(registerID1, "")
   562  		assert.NoError(t, err)
   563  
   564  		_, err = v.Get(registerID2, "")
   565  		assert.NoError(t, err)
   566  
   567  		touches := v.Interactions().Reads
   568  		require.Len(t, touches, 2)
   569  
   570  		r1 := flow.NewRegisterID(registerID1, "")
   571  		r2 := flow.NewRegisterID(registerID2, "")
   572  
   573  		assert.Equal(t, map[flow.RegisterID]struct{}{
   574  			r1: struct{}{},
   575  			r2: struct{}{},
   576  		}, touches)
   577  	})
   578  }
   579  
   580  func hashIt(t *testing.T, spock hash.Hasher, value []byte) {
   581  	_, err := spock.Write(value)
   582  	assert.NoError(t, err, "spock write is not supposed to error")
   583  }