Skip to content

Commit

Permalink
Request through API time provider details and add ForkAfter param
Browse files Browse the repository at this point in the history
  • Loading branch information
daria305 committed Mar 9, 2023
1 parent ec965bf commit ea844d5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 11 deletions.
35 changes: 27 additions & 8 deletions client/evilspammer/commitmentmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ import (
)

type CommitmentManagerParams struct {
CommitmentType string
ParentRefsCount int
GenesisTime time.Time
SlotDuration int64 // in seconds
CommitmentType string
ParentRefsCount int
GenesisTime time.Time
SlotDuration time.Duration
OptionalForkAfter int
}
type CommitmentManager struct {
Params *CommitmentManagerParams
commitmentByIndex map[slot.Index]*commitment.Commitment
clockSync *ClockSync
// todo get slot duration, genesis time, and request latest committed slot LCS
// todo need the genesis time to calculate the separate commitment chain
// todo need to specify the forking, "in 20 slots" from now

connector evilwallet.Connector
forkIndex slot.Index
}

func NewCommitmentManager() *CommitmentManager {
// todo get timeProvider (genesisTime, SlotDuration) from node config
return &CommitmentManager{
Params: &CommitmentManagerParams{
ParentRefsCount: 2,
GenesisTime: time.Now(),
SlotDuration: 5 * time.Second,
},
}
}
Expand All @@ -46,6 +46,25 @@ func (c *CommitmentManager) SetCommitmentType(commitmentType string) {
c.Params.CommitmentType = commitmentType
}

func (c *CommitmentManager) SetForkAfter(forkAfter int) {
c.Params.OptionalForkAfter = forkAfter
}

// SetupForkingPoint sets the forking point for the commitment manager. It uses ForkAfter parameter so need to be called after params are read.
func (c *CommitmentManager) SetupForkingPoint() {
c.forkIndex = c.clockSync.LatestCommittedSlotClock.Get() + slot.Index(c.Params.OptionalForkAfter)
}

// SetupTimeParams requests through API and sets the genesis time and slot duration for the commitment manager.
func (c *CommitmentManager) SetupTimeParams(clt evilwallet.Client) {
genesisTime, slotDuration, err := clt.GetTimeProvider()
if err != nil {
panic(errors.Wrapf(err, "failed to get time provider for the committment manager setup"))
}
c.Params.GenesisTime = genesisTime
c.Params.SlotDuration = slotDuration
}

// GenerateCommitment generates a commitment based on the commitment type provided in spam details.
func (c *CommitmentManager) GenerateCommitment(clt evilwallet.Client) (*commitment.Commitment, slot.Index, error) {
switch c.Params.CommitmentType {
Expand Down
7 changes: 7 additions & 0 deletions client/evilspammer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func WithCommitmentType(commitmentType string) Options {
}
}

// WithForkAfter provides after how many slots from the spammer setup should fork bee created, this option can be used with CommitmentType: fork.
func WithForkAfter(forkingAfter int) Options {
return func(s *Spammer) {
s.CommitmentManager.SetForkAfter(forkingAfter)
}
}

type SpamDetails struct {
Rate int
TimeUnit time.Duration
Expand Down
1 change: 1 addition & 0 deletions client/evilspammer/spammer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (s *Spammer) BatchesPrepared() uint64 {
func (s *Spammer) setup() {
s.Clients = s.EvilWallet.Connector()
s.CommitmentManager.SetConnector(s.Clients)
s.CommitmentManager.SetupTimeParams(s.Clients.GetClient())
s.setupSpamDetails()

s.State.spamTicker = s.initSpamTicker()
Expand Down
2 changes: 1 addition & 1 deletion client/evilspammer/spamming_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func CommitmentsSpammingFunction(s *Spammer) {
return
}
block.SetSignature(signature)
timeProvider := slot.NewTimeProvider(s.CommitmentManager.Params.GenesisTime.Unix(), s.CommitmentManager.Params.SlotDuration)
timeProvider := slot.NewTimeProvider(s.CommitmentManager.Params.GenesisTime.Unix(), int64(s.CommitmentManager.Params.SlotDuration.Seconds()))
if err = block.DetermineID(timeProvider); err != nil {
s.ErrCounter.CountError(ErrFailPrepareBlock)
}
Expand Down
12 changes: 12 additions & 0 deletions client/evilwallet/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ type Client interface {
GetReferences(payload []byte, parentsCount int) (refs models.ParentBlockIDs, err error)
// GetLatestCommittedSlot returns the latest committed slot.
GetLatestCommittedSlot() (slot.Index, error)
// GetTimeProvider returns the time provider info such as genesis time and slot duration.
GetTimeProvider() (time.Time, time.Duration, error)
}

// WebClient contains a GoShimmer web API to interact with a node.
Expand Down Expand Up @@ -428,4 +430,14 @@ func (c *WebClient) GetLatestCommittedSlot() (slotIndex slot.Index, err error) {
return slot.Index(resp.Index), nil
}

func (c *WebClient) GetTimeProvider() (genesisTime time.Time, slotDuration time.Duration, err error) {
resp, err := c.api.Info()
if err != nil {
return
}
genesisTime = resp.TimeProvider.GenesisTime
slotDuration = resp.TimeProvider.SlotDuration
return
}

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
8 changes: 8 additions & 0 deletions packages/app/jsonmodels/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type InfoResponse struct {
NetworkVersion uint32 `json:"networkVersion,omitempty"`
// TangleTime sync status
TangleTime TangleTime `json:"tangleTime,omitempty"`
// TimeProvider contain the details for the time provider.
TimeProvider TimeProvider `json:"timeProvider,omitempty"`
// identity ID of the node encoded in base58
IdentityID string `json:"identityID,omitempty"`
// identity ID of the node encoded in base58 and truncated to its first 8 bytes
Expand Down Expand Up @@ -53,6 +55,12 @@ type TangleTime struct {
Bootstrapped bool `json:"bootstrapped"`
}

// TimeProvider contains the details for the time provider.
type TimeProvider struct {
GenesisTime time.Time `json:"genesisTime"`
SlotDuration time.Duration `json:"slotDuration"`
}

// Mana contains the different mana values of the node.
type Mana struct {
Access int64 `json:"access"`
Expand Down
6 changes: 6 additions & 0 deletions plugins/webapi/info/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func getInfo(c echo.Context) error {
RCTT: tm.RelativeConfirmedTime().UnixNano(),
}

timeProvider := jsonmodels.TimeProvider{
GenesisTime: deps.Protocol.Engine().SlotTimeProvider.GenesisTime(),
SlotDuration: time.Second * time.Duration(deps.Protocol.Engine().SlotTimeProvider.Duration()),
}

accessMana, _ := deps.Protocol.Engine().ThroughputQuota.Balance(deps.Local.ID())
consensusMana := lo.Return1(deps.Protocol.Engine().SybilProtection.Weights().Get(deps.Local.ID())).Value
nodeMana := jsonmodels.Mana{
Expand All @@ -159,6 +164,7 @@ func getInfo(c echo.Context) error {
Version: banner.AppVersion,
NetworkVersion: discovery.Parameters.NetworkVersion,
TangleTime: tangleTime,
TimeProvider: timeProvider,
IdentityID: base58.Encode(lo.PanicOnErr(deps.Local.Identity.ID().Bytes())),
IdentityIDShort: deps.Local.Identity.ID().String(),
PublicKey: deps.Local.PublicKey().String(),
Expand Down
6 changes: 4 additions & 2 deletions tools/evil-spammer/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ type CommitmentsSpamParams struct {
TimeUnit time.Duration
NetworkAlias string
IdentityAlias string
ForkAfter int // optional, will be used only with CommitmentType = "Fork"
}

func CommitmentsSpam(params *CommitmentsSpamParams) {
identity.LoadConfig()
SpamCommitments(params.Rate, params.TimeUnit, params.Duration, params.NetworkAlias, params.IdentityAlias, params.CommitmentType)
SpamCommitments(params.Rate, params.ForkAfter, params.TimeUnit, params.Duration, params.NetworkAlias, params.IdentityAlias, params.CommitmentType)
}

func SpamCommitments(rate int, timeUnit, duration time.Duration, networkAlias, identityAlias, commitmentType string) {
func SpamCommitments(rate, optionalForkAfter int, timeUnit, duration time.Duration, networkAlias, identityAlias, commitmentType string) {
privateKey := identity.LoadIdentity(networkAlias, identityAlias)
options := []evilspammer.Options{
evilspammer.WithSpamRate(rate, timeUnit),
evilspammer.WithSpamDuration(duration),
evilspammer.WithSpammingFunc(evilspammer.CommitmentsSpammingFunction),
evilspammer.WithIdentity(identityAlias, privateKey),
evilspammer.WithCommitmentType(commitmentType),
evilspammer.WithForkAfter(optionalForkAfter),
}
spammer := evilspammer.NewSpammer(options...)
spammer.Spam()
Expand Down
2 changes: 2 additions & 0 deletions tools/evil-spammer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func parseCommitmentsSpamFlags() {
timeUnit := optionFlagSet.Duration("tu", commitmentsSpamParams.TimeUnit, "Time unit for the spamming rate. Format: decimal numbers, each with optional fraction and a unit suffix, such as '300ms', '-1.5h' or '2h45m'.\n Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h'.")
networkAlias := optionFlagSet.String("network", commitmentsSpamParams.NetworkAlias, "Network alias for the test. Check your keys-config.json file for possible values.")
identityAlias := optionFlagSet.String("identity", commitmentsSpamParams.IdentityAlias, "Identity alias for the node identity and its private keys. Check your keys-config.json file for possible values.")
forkAfter := optionFlagSet.Int("forkAfter", commitmentsSpamParams.Rate, "Indicates how many slots after spammer startup should fork be placed in the created commitment chain. Works only for 'fork' commitment spam type.")

parseOptionFlagSet(optionFlagSet)

Expand All @@ -173,6 +174,7 @@ func parseCommitmentsSpamFlags() {
commitmentsSpamParams.TimeUnit = *timeUnit
commitmentsSpamParams.NetworkAlias = *networkAlias
commitmentsSpamParams.IdentityAlias = *identityAlias
commitmentsSpamParams.ForkAfter = *forkAfter
}

func parseCommaSepString(urls string) []string {
Expand Down

0 comments on commit ea844d5

Please sign in to comment.