From d78b7e57c72ed6108fe6a5a2e79fafb91f10b418 Mon Sep 17 00:00:00 2001 From: frrist Date: Tue, 7 Jul 2020 17:44:55 -0700 Subject: [PATCH] polish: include key in AdtArrayDiff interface - some values do not contain their keys --- chain/events/state/diff_adt.go | 12 ++--- chain/events/state/diff_adt_test.go | 72 +++++++++++++++++++---------- chain/events/state/predicates.go | 6 +-- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/chain/events/state/diff_adt.go b/chain/events/state/diff_adt.go index 0d281b515..a3603be42 100644 --- a/chain/events/state/diff_adt.go +++ b/chain/events/state/diff_adt.go @@ -11,9 +11,9 @@ import ( // Modify should be called when a value is modified in the array // Remove should be called when a value is removed from the array type AdtArrayDiff interface { - Add(val *typegen.Deferred) error - Modify(from, to *typegen.Deferred) error - Remove(val *typegen.Deferred) error + Add(key uint64, val *typegen.Deferred) error + Modify(key uint64, from, to *typegen.Deferred) error + Remove(key uint64, val *typegen.Deferred) error } // TODO Performance can be improved by diffing the underlying IPLD graph, e.g. https://github.com/ipfs/go-merkledag/blob/749fd8717d46b4f34c9ce08253070079c89bc56d/dagutils/diff.go#L104 @@ -33,13 +33,13 @@ func DiffAdtArray(preArr, curArr *adt.Array, out AdtArrayDiff) error { return err } if !found { - if err := out.Remove(prevVal); err != nil { + if err := out.Remove(uint64(i), prevVal); err != nil { return err } return nil } - if err := out.Modify(prevVal, curVal); err != nil { + if err := out.Modify(uint64(i), prevVal, curVal); err != nil { return err } @@ -50,6 +50,6 @@ func DiffAdtArray(preArr, curArr *adt.Array, out AdtArrayDiff) error { curVal := new(typegen.Deferred) return curArr.ForEach(curVal, func(i int64) error { - return out.Add(curVal) + return out.Add(uint64(i), curVal) }) } diff --git a/chain/events/state/diff_adt_test.go b/chain/events/state/diff_adt_test.go index 6f1c36ccb..c57105179 100644 --- a/chain/events/state/diff_adt_test.go +++ b/chain/events/state/diff_adt_test.go @@ -41,54 +41,69 @@ func TestDiffAdtArray(t *testing.T) { require.NoError(t, arrB.Set(5, runtime.CBORBytes{8})) // add require.NoError(t, arrB.Set(6, runtime.CBORBytes{9})) // add - changes := &TestAdtDiff{ - Added: []runtime.CBORBytes{}, - Modified: []TestAdtDiffModified{}, - Removed: []runtime.CBORBytes{}, - } + changes := new(TestAdtDiff) assert.NoError(t, DiffAdtArray(arrA, arrB, changes)) assert.NotNil(t, changes) assert.Equal(t, 2, len(changes.Added)) - assert.EqualValues(t, []byte{8}, changes.Added[0]) - assert.EqualValues(t, []byte{9}, changes.Added[1]) + // keys 5 and 6 were added + assert.EqualValues(t, uint64(5), changes.Added[0].key) + assert.EqualValues(t, []byte{8}, changes.Added[0].val) + assert.EqualValues(t, uint64(6), changes.Added[1].key) + assert.EqualValues(t, []byte{9}, changes.Added[1].val) assert.Equal(t, 2, len(changes.Modified)) - assert.EqualValues(t, []byte{0}, changes.Modified[0].From) - assert.EqualValues(t, []byte{1}, changes.Modified[0].To) - assert.EqualValues(t, []byte{0}, changes.Modified[1].From) - assert.EqualValues(t, []byte{6}, changes.Modified[1].To) + // keys 1 and 4 were modified + assert.EqualValues(t, uint64(1), changes.Modified[0].From.key) + assert.EqualValues(t, []byte{0}, changes.Modified[0].From.val) + assert.EqualValues(t, uint64(1), changes.Modified[0].To.key) + assert.EqualValues(t, []byte{1}, changes.Modified[0].To.val) + assert.EqualValues(t, uint64(4), changes.Modified[1].From.key) + assert.EqualValues(t, []byte{0}, changes.Modified[1].From.val) + assert.EqualValues(t, uint64(4), changes.Modified[1].To.key) + assert.EqualValues(t, []byte{6}, changes.Modified[1].To.val) assert.Equal(t, 2, len(changes.Removed)) - assert.EqualValues(t, []byte{0}, changes.Removed[0]) - assert.EqualValues(t, []byte{1}, changes.Removed[1]) + // keys 0 and 2 were deleted + assert.EqualValues(t, uint64(0), changes.Removed[0].key) + assert.EqualValues(t, []byte{0}, changes.Removed[0].val) + assert.EqualValues(t, uint64(2), changes.Removed[1].key) + assert.EqualValues(t, []byte{1}, changes.Removed[1].val) +} + +type adtDiffResult struct { + key uint64 + val runtime.CBORBytes } type TestAdtDiff struct { - Added []runtime.CBORBytes + Added []adtDiffResult Modified []TestAdtDiffModified - Removed []runtime.CBORBytes + Removed []adtDiffResult } var _ AdtArrayDiff = &TestAdtDiff{} type TestAdtDiffModified struct { - From runtime.CBORBytes - To runtime.CBORBytes + From adtDiffResult + To adtDiffResult } -func (t *TestAdtDiff) Add(val *typegen.Deferred) error { +func (t *TestAdtDiff) Add(key uint64, val *typegen.Deferred) error { v := new(runtime.CBORBytes) err := v.UnmarshalCBOR(bytes.NewReader(val.Raw)) if err != nil { return err } - t.Added = append(t.Added, *v) + t.Added = append(t.Added, adtDiffResult{ + key: key, + val: *v, + }) return nil } -func (t *TestAdtDiff) Modify(from, to *typegen.Deferred) error { +func (t *TestAdtDiff) Modify(key uint64, from, to *typegen.Deferred) error { vFrom := new(runtime.CBORBytes) err := vFrom.UnmarshalCBOR(bytes.NewReader(from.Raw)) if err != nil { @@ -103,20 +118,29 @@ func (t *TestAdtDiff) Modify(from, to *typegen.Deferred) error { if !bytes.Equal(*vFrom, *vTo) { t.Modified = append(t.Modified, TestAdtDiffModified{ - From: *vFrom, - To: *vTo, + From: adtDiffResult{ + key: key, + val: *vFrom, + }, + To: adtDiffResult{ + key: key, + val: *vTo, + }, }) } return nil } -func (t *TestAdtDiff) Remove(val *typegen.Deferred) error { +func (t *TestAdtDiff) Remove(key uint64, val *typegen.Deferred) error { v := new(runtime.CBORBytes) err := v.UnmarshalCBOR(bytes.NewReader(val.Raw)) if err != nil { return err } - t.Removed = append(t.Removed, *v) + t.Removed = append(t.Removed, adtDiffResult{ + key: key, + val: *v, + }) return nil } diff --git a/chain/events/state/predicates.go b/chain/events/state/predicates.go index 959227adc..793a00806 100644 --- a/chain/events/state/predicates.go +++ b/chain/events/state/predicates.go @@ -172,7 +172,7 @@ type SectorExtensions struct { To miner.SectorOnChainInfo } -func (m *MinerSectorChanges) Add(val *typegen.Deferred) error { +func (m *MinerSectorChanges) Add(key uint64, val *typegen.Deferred) error { si := new(miner.SectorOnChainInfo) err := si.UnmarshalCBOR(bytes.NewReader(val.Raw)) if err != nil { @@ -182,7 +182,7 @@ func (m *MinerSectorChanges) Add(val *typegen.Deferred) error { return nil } -func (m *MinerSectorChanges) Modify(from, to *typegen.Deferred) error { +func (m *MinerSectorChanges) Modify(key uint64, from, to *typegen.Deferred) error { siFrom := new(miner.SectorOnChainInfo) err := siFrom.UnmarshalCBOR(bytes.NewReader(from.Raw)) if err != nil { @@ -204,7 +204,7 @@ func (m *MinerSectorChanges) Modify(from, to *typegen.Deferred) error { return nil } -func (m *MinerSectorChanges) Remove(val *typegen.Deferred) error { +func (m *MinerSectorChanges) Remove(key uint64, val *typegen.Deferred) error { si := new(miner.SectorOnChainInfo) err := si.UnmarshalCBOR(bytes.NewReader(val.Raw)) if err != nil {