-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cody Littley <[email protected]>
- Loading branch information
1 parent
fff7c76
commit 3089bb0
Showing
4 changed files
with
135 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package traffic |
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,124 @@ | ||
package traffic | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"github.com/Layr-Labs/eigenda/encoding/utils/codec" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// TODO document | ||
|
||
// BlobWriter sends blobs to a disperser at a configured rate. | ||
type BlobWriter struct { | ||
ctx *context.Context | ||
waitGroup *sync.WaitGroup | ||
generator *TrafficGenerator | ||
verifier *StatusVerifier | ||
|
||
// fixedRandomData contains random data for blobs if RandomizeBlobs is false, and nil otherwise. | ||
fixedRandomData *[]byte | ||
} | ||
|
||
// NewBlobWriter creates a new BlobWriter instance. | ||
func NewBlobWriter( | ||
ctx *context.Context, | ||
waitGroup *sync.WaitGroup, | ||
generator *TrafficGenerator, | ||
verifier *StatusVerifier) BlobWriter { | ||
|
||
var fixedRandomData []byte | ||
if generator.Config.RandomizeBlobs { | ||
// New random data will be generated for each blob. | ||
fixedRandomData = nil | ||
} else { | ||
// Use this random data for each blob. | ||
fixedRandomData := make([]byte, generator.Config.DataSize) | ||
_, err := rand.Read(fixedRandomData) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fixedRandomData = codec.ConvertByPaddingEmptyByte(fixedRandomData) | ||
} | ||
|
||
return BlobWriter{ | ||
ctx: ctx, | ||
waitGroup: waitGroup, | ||
generator: generator, | ||
verifier: verifier, | ||
fixedRandomData: &fixedRandomData, | ||
} | ||
} | ||
|
||
// Start begins the blob writer goroutine. | ||
func (writer *BlobWriter) Start() { | ||
writer.waitGroup.Add(1) | ||
go func() { | ||
writer.run() | ||
writer.waitGroup.Done() | ||
}() | ||
} | ||
|
||
// run sends blobs to a disperser at a configured rate. | ||
// Continues and dues not return until the context is cancelled. | ||
func (writer *BlobWriter) run() { | ||
ticker := time.NewTicker(writer.generator.Config.WriteRequestInterval) | ||
for { | ||
select { | ||
case <-(*writer.ctx).Done(): | ||
return | ||
case <-ticker.C: | ||
key, err := writer.sendRequest(*writer.getRandomData()) | ||
|
||
if err != nil { | ||
writer.generator.Logger.Error("failed to send blob request", "err:", err) | ||
continue | ||
} | ||
|
||
writer.verifier.AddUnconfirmedKey(&key) | ||
} | ||
} | ||
} | ||
|
||
// getRandomData returns a slice of random data to be used for a blob. | ||
func (writer *BlobWriter) getRandomData() *[]byte { | ||
if *writer.fixedRandomData != nil { | ||
return writer.fixedRandomData | ||
} | ||
|
||
data := make([]byte, writer.generator.Config.DataSize) | ||
_, err := rand.Read(data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
data = codec.ConvertByPaddingEmptyByte(data) | ||
|
||
return &data | ||
} | ||
|
||
// sendRequest sends a blob to a disperser. | ||
func (writer *BlobWriter) sendRequest(data []byte) ([]byte /* key */, error) { | ||
ctxTimeout, cancel := context.WithTimeout(*writer.ctx, writer.generator.Config.Timeout) | ||
defer cancel() | ||
|
||
if writer.generator.Config.SignerPrivateKey != "" { | ||
blobStatus, key, err := | ||
writer.generator.DisperserClient.DisperseBlobAuthenticated(ctxTimeout, data, writer.generator.Config.CustomQuorums) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
writer.generator.Logger.Info("successfully dispersed new blob", "authenticated", true, "key", hex.EncodeToString(key), "status", blobStatus.String()) | ||
return key, nil | ||
} else { | ||
blobStatus, key, err := writer.generator.DisperserClient.DisperseBlob(ctxTimeout, data, writer.generator.Config.CustomQuorums) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
writer.generator.Logger.Info("successfully dispersed new blob", "authenticated", false, "key", hex.EncodeToString(key), "status", blobStatus.String()) | ||
return key, nil | ||
} | ||
} |
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