Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: venus-shared: Change the type of HeadChange.Type #4755

Merged
merged 3 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/submodule/chain/chaininfo_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ func (cia *chainInfoAPI) ChainGetPath(ctx context.Context, from types.TipSetKey,

path := make([]*types.HeadChange, len(revert)+len(apply))
for i, r := range revert {
path[i] = &types.HeadChange{Type: chain.HCRevert, Val: r}
path[i] = &types.HeadChange{Type: types.HCRevert, Val: r}
}
for j, i := 0, len(apply)-1; i >= 0; j, i = j+1, i-1 {
path[j+len(revert)] = &types.HeadChange{Type: chain.HCApply, Val: apply[i]}
path[j+len(revert)] = &types.HeadChange{Type: types.HCApply, Val: apply[i]}
}
return path, nil
}
7 changes: 3 additions & 4 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
logging "github.com/ipfs/go-log/v2"

"github.com/filecoin-project/venus/app/node"
chainpkg "github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/venus-shared/actors"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
Expand Down Expand Up @@ -179,7 +178,7 @@ var disputerStartCmd = &cmds.Command{
return xerrors.Errorf("Notify first entry should have been one item")
}

if head[0].Type != chainpkg.HCCurrent {
if head[0].Type != types.HCCurrent {
return xerrors.Errorf("expected current head on Notify stream (got %s)", head[0].Type)
}

Expand Down Expand Up @@ -280,14 +279,14 @@ var disputerStartCmd = &cmds.Command{

for _, val := range notif {
switch val.Type {
case chainpkg.HCApply:
case types.HCApply:
for ; lastEpoch <= val.Val.Height(); lastEpoch++ {
err := applyTsk(val.Val.Key())
if err != nil {
return err
}
}
case chainpkg.HCRevert:
case types.HCRevert:
// do nothing
default:
return xerrors.Errorf("unexpected head change type %s", val.Type)
Expand Down
18 changes: 5 additions & 13 deletions pkg/chain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ import (
"github.com/filecoin-project/venus/venus-shared/types"
)

// HeadChangeTopic is the topic used to publish new heads.
const (
HeadChangeTopic = "headchange"
HCRevert = "revert"
HCApply = "apply"
HCCurrent = "current"
)

// ErrNoMethod is returned by Get when there is no method signature (eg, transfer).
var ErrNoMethod = errors.New("no method")

Expand Down Expand Up @@ -616,20 +608,20 @@ func (store *Store) reorgWorker(ctx context.Context) chan reorg {
notif := make([]*types.HeadChange, len(rev)+len(app))
for i, revert := range rev {
notif[i] = &types.HeadChange{
Type: HCRevert,
Type: types.HCRevert,
Val: revert,
}
}

for i, apply := range app {
notif[i+len(rev)] = &types.HeadChange{
Type: HCApply,
Type: types.HCApply,
Val: apply,
}
}

// Publish an event that we have a new head.
store.headEvents.Pub(notif, HeadChangeTopic)
store.headEvents.Pub(notif, types.HeadChangeTopic)
return nil
}

Expand Down Expand Up @@ -687,13 +679,13 @@ func (store *Store) reorgWorker(ctx context.Context) chan reorg {
// Then event in the message may be HCApply and HCRevert.
func (store *Store) SubHeadChanges(ctx context.Context) chan []*types.HeadChange {
store.mu.RLock()
subCh := store.headEvents.Sub(HeadChangeTopic)
subCh := store.headEvents.Sub(types.HeadChangeTopic)
head := store.head
store.mu.RUnlock()

out := make(chan []*types.HeadChange, 16)
out <- []*types.HeadChange{{
Type: HCCurrent,
Type: types.HCCurrent,
Val: head,
}}

Expand Down
21 changes: 11 additions & 10 deletions pkg/chain/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestRevertChange(t *testing.T) {

ch := cs.SubHeadChanges(ctx)
currentA := <-ch
test.Equal(t, currentA[0].Type, chain.HCCurrent)
test.Equal(t, currentA[0].Type, types.HCCurrent)
test.Equal(t, currentA[0].Val, link3)

err = cs.SetHead(ctx, link6)
Expand All @@ -171,18 +171,18 @@ func TestRevertChange(t *testing.T) {
//maybe link3, if link3 fetch next
headChanges = <-ch
}
test.Equal(t, headChanges[0].Type, chain.HCRevert)
test.Equal(t, headChanges[0].Type, types.HCRevert)
test.Equal(t, headChanges[0].Val, link3)
test.Equal(t, headChanges[1].Type, chain.HCRevert)
test.Equal(t, headChanges[1].Type, types.HCRevert)
test.Equal(t, headChanges[1].Val, link2)
test.Equal(t, headChanges[2].Type, chain.HCRevert)
test.Equal(t, headChanges[2].Type, types.HCRevert)
test.Equal(t, headChanges[2].Val, link1)

test.Equal(t, headChanges[3].Type, chain.HCApply)
test.Equal(t, headChanges[3].Type, types.HCApply)
test.Equal(t, headChanges[3].Val, link4)
test.Equal(t, headChanges[4].Type, chain.HCApply)
test.Equal(t, headChanges[4].Type, types.HCApply)
test.Equal(t, headChanges[4].Val, link5)
test.Equal(t, headChanges[5].Type, chain.HCApply)
test.Equal(t, headChanges[5].Type, types.HCApply)
test.Equal(t, headChanges[5].Val, link6)
}

Expand Down Expand Up @@ -269,11 +269,11 @@ func TestHeadEvents(t *testing.T) {
chB := chainStore.SubHeadChanges(ctx)
// HCurrent
currentA := <-chA
test.Equal(t, currentA[0].Type, chain.HCCurrent)
test.Equal(t, currentA[0].Type, types.HCCurrent)
test.Equal(t, currentA[0].Val, genTS)

currentB := <-chB
test.Equal(t, currentB[0].Type, chain.HCCurrent)
test.Equal(t, currentB[0].Type, types.HCCurrent)
test.Equal(t, currentB[0].Val, genTS)

defer ctx.Done()
Expand All @@ -286,7 +286,8 @@ func TestHeadEvents(t *testing.T) {
assertSetHead(t, chainStore, link1)
assertSetHead(t, chainStore, genTS)
heads := []*types.TipSet{genTS, link1, link2, link3, link4, link4, link3, link2, link1, genTS}
types := []string{"apply", "apply", "apply", "apply", "apply", "revert", "revert", "revert", "revert"}
types := []types.HeadChangeType{types.HCApply, types.HCApply, types.HCApply, types.HCApply, types.HCApply, types.HCRevert,
types.HCRevert, types.HCRevert, types.HCRevert}
// Heads arrive in the expected order
for i := 0; i < 9; i++ {
headA := <-chA
Expand Down
6 changes: 3 additions & 3 deletions pkg/chain/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (w *Waiter) waitForMessage(ctx context.Context, ch <-chan []*types.HeadChan
return nil, false, fmt.Errorf("SubHeadChanges first entry should have been one item")
}

if current[0].Type != HCCurrent {
if current[0].Type != types.HCCurrent {
return nil, false, fmt.Errorf("expected current head on SHC stream (got %s)", current[0].Type)
}

Expand Down Expand Up @@ -220,15 +220,15 @@ func (w *Waiter) waitForMessage(ctx context.Context, ch <-chan []*types.HeadChan
}
for _, val := range notif {
switch val.Type {
case HCRevert:
case types.HCRevert:
if val.Val.Equals(candidateTS) {
candidateTS = nil
candidateRcp = nil
}
if backSearchWait != nil {
reverts[val.Val.Key().String()] = true
}
case HCApply:
case types.HCApply:
if candidateTS != nil && val.Val.Height() >= candidateTS.Height()+abi.ChainEpoch(confidence) {
return candidateRcp, true, nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func (fcs *fakeCS) ChainGetPath(ctx context.Context, from, to types.TipSetKey) (

path := make([]*types.HeadChange, len(revert)+len(apply))
for i, r := range revert {
path[i] = &types.HeadChange{Type: chain.HCRevert, Val: r}
path[i] = &types.HeadChange{Type: types.HCRevert, Val: r}
}
for j, i := 0, len(apply)-1; i >= 0; j, i = j+1, i-1 {
path[j+len(revert)] = &types.HeadChange{Type: chain.HCApply, Val: apply[i]}
path[j+len(revert)] = &types.HeadChange{Type: types.HCApply, Val: apply[i]}
}
return path, nil
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (fcs *fakeCS) ChainNotify(ctx context.Context) (<-chan []*types.HeadChange,
panic(err)
}

out <- []*types.HeadChange{{Type: chain.HCCurrent, Val: best}}
out <- []*types.HeadChange{{Type: types.HCCurrent, Val: best}}
fcs.subCh = out
close(fcs.waitSub)

Expand Down Expand Up @@ -310,13 +310,13 @@ func (fcs *fakeCS) sub(rev, app []*types.TipSet) {

for i, r := range rev {
notif[i] = &types.HeadChange{
Type: chain.HCRevert,
Type: types.HCRevert,
Val: r,
}
}
for i, r := range app {
notif[i+len(rev)] = &types.HeadChange{
Type: chain.HCApply,
Type: types.HCApply,
Val: r,
}
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/events/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go.opencensus.io/trace"
"golang.org/x/xerrors"

"github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/pkg/constants"
)

Expand Down Expand Up @@ -113,9 +112,9 @@ func (o *observer) applyChanges(ctx context.Context, changes []*types.HeadChange
var rev, app []*types.TipSet
for _, changes := range changes {
switch changes.Type {
case chain.HCRevert:
case types.HCRevert:
rev = append(rev, changes.Val)
case chain.HCApply:
case types.HCApply:
app = append(app, changes.Val)
default:
log.Errorf("unexpected head change notification type: '%s'", changes.Type)
Expand Down
1 change: 1 addition & 0 deletions venus-devtool/api-gen/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func init() {
addExample([]abi.SectorNumber{123, 124})
addExample(types.CheckStatusCode(0))
addExample(map[string]interface{}{"abc": 123})
addExample(types.HCApply)

// messager
uuid, err := types.ParseUUID("e26f1e5c-47f7-4561-a11d-18fab6e748af")
Expand Down
4 changes: 2 additions & 2 deletions venus-shared/api/chain/v0/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ Response:
```json
[
{
"Type": "string value",
"Type": "apply",
"Val": {
"Cids": null,
"Blocks": null,
Expand Down Expand Up @@ -889,7 +889,7 @@ Response:
```json
[
{
"Type": "string value",
"Type": "apply",
"Val": {
"Cids": null,
"Blocks": null,
Expand Down
4 changes: 2 additions & 2 deletions venus-shared/api/chain/v1/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ Response:
```json
[
{
"Type": "string value",
"Type": "apply",
"Val": {
"Cids": null,
"Blocks": null,
Expand Down Expand Up @@ -921,7 +921,7 @@ Response:
```json
[
{
"Type": "string value",
"Type": "apply",
"Val": {
"Cids": null,
"Blocks": null,
Expand Down
20 changes: 15 additions & 5 deletions venus-shared/types/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ import (
"github.com/filecoin-project/venus/venus-shared/api"
)

type ObjStat struct {
Size uint64
Links uint64
}
type HeadChangeType string

// HeadChangeTopic is the topic used to publish new heads.
const HeadChangeTopic = "headchange"
const (
HCRevert HeadChangeType = "revert"
HCApply HeadChangeType = "apply"
HCCurrent HeadChangeType = "current"
)

type HeadChange struct {
Type string
Type HeadChangeType
Val *TipSet
}

type ObjStat struct {
Size uint64
Links uint64
}

// ChainMessage is an on-chain message with its block and receipt.
type ChainMessage struct { //nolint
TS *TipSet
Expand Down