Skip to content

Commit 3ce82fd

Browse files
envestccCoderZhi
andauthored
Only broadcast actions from api (#4643)
--------- Co-authored-by: CoderZhi <[email protected]>
1 parent d38a3ca commit 3ce82fd

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

actpool/actpool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type ActPool interface {
8585

8686
// Subscriber is the interface for actpool subscriber
8787
type Subscriber interface {
88-
OnAdded(*action.SealedEnvelope)
88+
OnAdded(context.Context, *action.SealedEnvelope)
8989
OnRemoved(*action.SealedEnvelope)
9090
}
9191

@@ -547,9 +547,9 @@ func (ap *actPool) AddSubscriber(sub Subscriber) {
547547
ap.subs = append(ap.subs, sub)
548548
}
549549

550-
func (ap *actPool) onAdded(act *action.SealedEnvelope) {
550+
func (ap *actPool) onAdded(ctx context.Context, act *action.SealedEnvelope) {
551551
for _, sub := range ap.subs {
552-
sub.OnAdded(act)
552+
sub.OnAdded(ctx, act)
553553
}
554554
}
555555

actpool/queueworker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (worker *queueWorker) Handle(job workerJob) error {
105105
}
106106

107107
worker.ap.allActions.Set(actHash, act)
108-
worker.ap.onAdded(act)
108+
worker.ap.onAdded(ctx, act)
109109
isBlobTx := len(act.BlobHashes()) > 0 // only store blob tx
110110
if worker.ap.store != nil && isBlobTx {
111111
if err := worker.ap.store.Put(act); err != nil {

actpool/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (v *blobValidator) Validate(ctx context.Context, act *action.SealedEnvelope
3838
return nil
3939
}
4040

41-
func (v *blobValidator) OnAdded(act *action.SealedEnvelope) {
41+
func (v *blobValidator) OnAdded(ctx context.Context, act *action.SealedEnvelope) {
4242
if len(act.BlobHashes()) == 0 {
4343
return
4444
}

api/action_radio.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ func (ar *ActionRadio) Stop() error {
6161
}
6262

6363
// OnAdded broadcasts the action to the network
64-
func (ar *ActionRadio) OnAdded(selp *action.SealedEnvelope) {
64+
func (ar *ActionRadio) OnAdded(ctx context.Context, selp *action.SealedEnvelope) {
65+
if _, fromAPI := GetAPIContext(ctx); !fromAPI {
66+
// only broadcast actions from API context
67+
return
68+
}
6569
var (
6670
hasSidecar = selp.BlobTxSidecar() != nil
6771
hash, _ = selp.Hash()

api/action_radio_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func TestActionRadio(t *testing.T) {
3232
selp, err := action.SignedTransfer(identityset.Address(1).String(), identityset.PrivateKey(1), 1, big.NewInt(1), nil, gas, gasPrice)
3333
r.NoError(err)
3434

35-
radio.OnAdded(selp)
35+
radio.OnAdded(WithAPIContext(context.Background()), selp)
36+
r.Equal(uint64(1), atomic.LoadUint64(&broadcastCount))
37+
38+
radio.OnAdded(context.Background(), selp)
3639
r.Equal(uint64(1), atomic.LoadUint64(&broadcastCount))
3740
}

api/context.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type (
1212
listenerIDs map[string]struct{}
1313
mutex sync.Mutex
1414
}
15+
16+
apiContextKey struct{}
1517
)
1618

1719
func (sc *StreamContext) AddListener(id string) {
@@ -46,3 +48,15 @@ func StreamFromContext(ctx context.Context) (*StreamContext, bool) {
4648
sc, ok := ctx.Value(streamContextKey{}).(*StreamContext)
4749
return sc, ok
4850
}
51+
52+
func WithAPIContext(ctx context.Context) context.Context {
53+
return context.WithValue(ctx, apiContextKey{}, struct{}{})
54+
}
55+
56+
func GetAPIContext(ctx context.Context) (struct{}, bool) {
57+
c := ctx.Value(apiContextKey{})
58+
if c == nil {
59+
return struct{}{}, false
60+
}
61+
return c.(struct{}), true
62+
}

api/coreservice.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ func (core *coreService) SendAction(ctx context.Context, in *iotextypes.Action)
489489
return "", err
490490
}
491491
l := log.T(ctx).Logger().With(zap.String("actionHash", hex.EncodeToString(hash[:])))
492+
ctx = WithAPIContext(ctx)
492493
if err = core.ap.Add(ctx, selp); err != nil {
493494
txBytes, serErr := proto.Marshal(in)
494495
if serErr != nil {

0 commit comments

Comments
 (0)