-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] Introduce action radio to broadcast (#4497)
Co-authored-by: CoderZhi <[email protected]>
- Loading branch information
Showing
8 changed files
with
175 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
|
||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"go.uber.org/zap" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/iotexproject/iotex-core/v2/action" | ||
"github.com/iotexproject/iotex-core/v2/pkg/log" | ||
batch "github.com/iotexproject/iotex-core/v2/pkg/messagebatcher" | ||
) | ||
|
||
// ActionRadioOption is the option to create ActionRadio | ||
type ActionRadioOption func(*ActionRadio) | ||
|
||
// WithMessageBatch enables message batching | ||
func WithMessageBatch() ActionRadioOption { | ||
return func(ar *ActionRadio) { | ||
ar.messageBatcher = batch.NewManager(func(msg *batch.Message) error { | ||
return ar.broadcastHandler(context.Background(), ar.chainID, msg.Data) | ||
}) | ||
} | ||
} | ||
|
||
// ActionRadio broadcasts actions to the network | ||
type ActionRadio struct { | ||
broadcastHandler BroadcastOutbound | ||
messageBatcher *batch.Manager | ||
chainID uint32 | ||
} | ||
|
||
// NewActionRadio creates a new ActionRadio | ||
func NewActionRadio(broadcastHandler BroadcastOutbound, chainID uint32, opts ...ActionRadioOption) *ActionRadio { | ||
ar := &ActionRadio{ | ||
broadcastHandler: broadcastHandler, | ||
chainID: chainID, | ||
} | ||
for _, opt := range opts { | ||
opt(ar) | ||
} | ||
return ar | ||
} | ||
|
||
// Start starts the action radio | ||
func (ar *ActionRadio) Start() error { | ||
if ar.messageBatcher != nil { | ||
return ar.messageBatcher.Start() | ||
} | ||
return nil | ||
} | ||
|
||
// Stop stops the action radio | ||
func (ar *ActionRadio) Stop() error { | ||
if ar.messageBatcher != nil { | ||
return ar.messageBatcher.Stop() | ||
} | ||
return nil | ||
} | ||
|
||
// OnAdded broadcasts the action to the network | ||
func (ar *ActionRadio) OnAdded(selp *action.SealedEnvelope) { | ||
var ( | ||
hasSidecar = selp.BlobTxSidecar() != nil | ||
hash, _ = selp.Hash() | ||
out proto.Message | ||
err error | ||
) | ||
if hasSidecar { | ||
out = &iotextypes.ActionHash{ | ||
Hash: hash[:], | ||
} | ||
} else { | ||
out = selp.Proto() | ||
} | ||
if ar.messageBatcher != nil && !hasSidecar { // TODO: batch blobTx | ||
err = ar.messageBatcher.Put(&batch.Message{ | ||
ChainID: ar.chainID, | ||
Target: nil, | ||
Data: out, | ||
}) | ||
} else { | ||
err = ar.broadcastHandler(context.Background(), ar.chainID, out) | ||
} | ||
if err != nil { | ||
log.L().Warn("Failed to broadcast SendAction request.", zap.Error(err), zap.String("actionHash", hex.EncodeToString(hash[:]))) | ||
} | ||
} | ||
|
||
// OnRemoved does nothing | ||
func (ar *ActionRadio) OnRemoved(act *action.SealedEnvelope) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/iotexproject/iotex-core/v2/action" | ||
"github.com/iotexproject/iotex-core/v2/test/identityset" | ||
) | ||
|
||
func TestActionRadio(t *testing.T) { | ||
r := require.New(t) | ||
broadcastCount := uint64(0) | ||
radio := NewActionRadio( | ||
func(_ context.Context, _ uint32, _ proto.Message) error { | ||
atomic.AddUint64(&broadcastCount, 1) | ||
return nil | ||
}, | ||
0) | ||
r.NoError(radio.Start()) | ||
defer func() { | ||
r.NoError(radio.Stop()) | ||
}() | ||
|
||
gas := uint64(100000) | ||
gasPrice := big.NewInt(10) | ||
selp, err := action.SignedTransfer(identityset.Address(1).String(), identityset.PrivateKey(1), 1, big.NewInt(1), nil, gas, gasPrice) | ||
r.NoError(err) | ||
|
||
radio.OnAdded(selp) | ||
r.Equal(uint64(1), atomic.LoadUint64(&broadcastCount)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.