From 611ea02ceabec3a6a160cba5d716e63847911fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 5 Sep 2019 09:57:12 +0200 Subject: [PATCH] chain events: More logical apply order --- chain/events/events_called.go | 4 ++- chain/events/events_height.go | 5 ++- chain/events/events_test.go | 67 +++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/chain/events/events_called.go b/chain/events/events_called.go index cb7d96236..2eadf6836 100644 --- a/chain/events/events_called.go +++ b/chain/events/events_called.go @@ -86,7 +86,9 @@ func (e *calledEvents) headChangeCalled(rev, app []*types.TipSet) error { e.handleReverts(ts) } - for _, ts := range app { + tail := len(app) - 1 + for i := range app { + ts := app[tail-i] // called triggers e.checkNewCalls(ts) diff --git a/chain/events/events_height.go b/chain/events/events_height.go index 8bfb144a8..41ee8099f 100644 --- a/chain/events/events_height.go +++ b/chain/events/events_height.go @@ -47,7 +47,10 @@ func (e *heightEvents) headChangeAt(rev, app []*types.TipSet) error { } } - for _, ts := range app { + tail := len(app) - 1 + for i := range app { + ts := app[tail-i] + if err := e.tsc.add(ts); err != nil { return err } diff --git a/chain/events/events_test.go b/chain/events/events_test.go index 7a3abe582..37a1d653a 100644 --- a/chain/events/events_test.go +++ b/chain/events/events_test.go @@ -98,7 +98,7 @@ func (fcs *fakeCS) advance(rev, app int, msgs map[int]cid.Cid) { // todo: allow require.NoError(fcs.t, fcs.tsc.revert(ts)) } - var apps []*types.TipSet + apps := make([]*types.TipSet, app) for i := 0; i < app; i++ { fcs.h++ @@ -110,7 +110,7 @@ func (fcs *fakeCS) advance(rev, app int, msgs map[int]cid.Cid) { // todo: allow ts := makeTs(fcs.t, fcs.h, mc) require.NoError(fcs.t, fcs.tsc.add(ts)) - apps = append(apps, ts) + apps[app-i-1] = ts } err := fcs.sub(revs, apps) @@ -446,3 +446,66 @@ func TestCalledTimeout(t *testing.T) { fcs.advance(0, 5, nil) require.False(t, called) } + +func TestCalledOrder(t *testing.T) { + fcs := &fakeCS{ + t: t, + h: 1, + + msgs: map[cid.Cid]fakeMsg{}, + tsc: newTSCache(2 * build.ForkLengthThreshold), + } + require.NoError(t, fcs.tsc.add(makeTs(t, 1, dummyCid))) + + events := NewEvents(fcs) + + t0123, err := address.NewFromString("t0123") + require.NoError(t, err) + + at := 0 + + err = events.Called(func(ts *types.TipSet) (d bool, m bool, e error) { + return false, true, nil + }, func(msg *types.Message, ts *types.TipSet, curH uint64) (bool, error) { + switch at { + case 0: + require.Equal(t, uint64(1), msg.Nonce) + require.Equal(t, uint64(3), ts.Height()) + case 1: + require.Equal(t, uint64(2), msg.Nonce) + require.Equal(t, uint64(4), ts.Height()) + default: + t.Fatal("apply should only get called twice, at: ", at) + } + at++ + return true, nil + }, func(ts *types.TipSet) error { + switch at { + case 2: + require.Equal(t, uint64(4), ts.Height()) + case 3: + require.Equal(t, uint64(3), ts.Height()) + default: + t.Fatal("revert should only get called twice, at: ", at) + } + at++ + return nil + }, 3, 20, t0123, 5) + require.NoError(t, err) + + fcs.advance(0, 10, map[int]cid.Cid{ + 1: fcs.fakeMsgs(fakeMsg{ + bmsgs: []*types.Message{ + {To: t0123, Method: 5, Nonce: 1}, + }, + }), + 2: fcs.fakeMsgs(fakeMsg{ + bmsgs: []*types.Message{ + {To: t0123, Method: 5, Nonce: 2}, + }, + }), + }) + + fcs.advance(9, 1, nil) + +}