-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 📝 Summary Allows sending out collected txs. `--tx-receivers-allowed-sources` is a list of tx sources to resend `--tx-receivers` urls where to send txs (as a octet-stream POST request) ## ⛱ Motivation and Context <!--- Why is this change required? What problem does it solve? --> ## 📚 References <!-- Any interesting external links to documentation, articles, tweets which add value to the PR --> --- ## ✅ I have run these commands * [x] `make lint` * [x] `make test` * [x] `go mod tidy` --------- Co-authored-by: Chris Hager <[email protected]>
- Loading branch information
Showing
5 changed files
with
188 additions
and
24 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
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,43 @@ | ||
package collector | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type TxReceiver interface { | ||
SendTx(ctx context.Context, tx *TxIn) error | ||
} | ||
|
||
type HTTPReceiver struct { | ||
url string | ||
} | ||
|
||
func NewHTTPReceiver(url string) *HTTPReceiver { | ||
return &HTTPReceiver{ | ||
url: url, | ||
} | ||
} | ||
|
||
func (r *HTTPReceiver) SendTx(ctx context.Context, tx *TxIn) error { | ||
rawTx, err := tx.Tx.MarshalBinary() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.url, bytes.NewReader(rawTx)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/octet-stream") | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
_, err = io.Copy(io.Discard, res.Body) | ||
return err | ||
} |
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 |
---|---|---|
@@ -1,9 +1,57 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/flashbots/mempool-dumpster/common" | ||
) | ||
|
||
// var testLog = common.GetLogger(true, false) | ||
|
||
// func TestBuilderAliases(t *testing.T) { | ||
// tempDir := t.TempDir() | ||
// txp := NewTxProcessor(testLog, tempDir, "test1") | ||
// require.Equal(t, "collector", "collector") | ||
// } | ||
|
||
type MockTxReceiver struct { | ||
ReceivedTx *TxIn | ||
} | ||
|
||
func (r *MockTxReceiver) SendTx(ctx context.Context, tx *TxIn) error { | ||
r.ReceivedTx = tx | ||
return nil | ||
} | ||
|
||
func TestTxProcessor_sendTxToReceivers(t *testing.T) { | ||
receiver := MockTxReceiver{ReceivedTx: nil} | ||
|
||
processor := NewTxProcessor(TxProcessorOpts{ | ||
Log: common.GetLogger(true, false), | ||
OutDir: "", | ||
UID: "", | ||
CheckNodeURI: "", | ||
HTTPReceivers: nil, | ||
ReceiversAllowedSources: []string{"allowed"}, | ||
}) | ||
processor.receivers = append(processor.receivers, &receiver) | ||
|
||
tx := TxIn{ | ||
T: time.Now(), | ||
Tx: nil, | ||
Source: "not-allowed", | ||
} | ||
processor.sendTxToReceivers(tx) | ||
|
||
if receiver.ReceivedTx != nil { | ||
t.Errorf("expected nil, got %v", receiver.ReceivedTx) | ||
} | ||
|
||
tx.Source = "allowed" | ||
processor.sendTxToReceivers(tx) | ||
if receiver.ReceivedTx == nil { | ||
t.Errorf("expected tx, got nil") | ||
} | ||
} |