-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: nicolas <[email protected]>
- Loading branch information
1 parent
4408178
commit 16863c7
Showing
5 changed files
with
350 additions
and
6 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,56 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/flashbots/go-utils/jsonrpc" | ||
) | ||
|
||
// boltSidecar is a thin http client that communicates with the | ||
// bolt sidecar server to fetch preconfirmed transactions. | ||
type boltSidecar struct { | ||
endpoint string | ||
} | ||
|
||
// newBoltSidecar creates a new boltSidecar instance. | ||
func newBoltSidecar(endpoint string) *boltSidecar { | ||
return &boltSidecar{ | ||
endpoint: endpoint, | ||
} | ||
} | ||
|
||
type rawPreconfirmation struct { | ||
Slot uint64 `json:"slot"` | ||
TxHash string `json:"txHash"` | ||
RawTx string `json:"rawTx"` | ||
} | ||
|
||
func (b *boltSidecar) GetPreconfirmations(slot uint64) ([]*rawPreconfirmation, error) { | ||
var preconfirms = new([]*rawPreconfirmation) | ||
|
||
params := map[string]interface{}{ | ||
"slot": slot, | ||
} | ||
|
||
// Request preconfirmations directly from the next proposer in line. | ||
// In a real version, this would be done through a mempool / DA service. | ||
req := jsonrpc.NewJSONRPCRequest("1", "eth_getPreconfirmations", params) | ||
res, err := jsonrpc.SendJSONRPCRequest(*req, b.endpoint) | ||
if err != nil { | ||
log.Error("Error getting preconfs via RPC: ", err) | ||
return nil, err | ||
} | ||
|
||
// Unmarshal the JSON data | ||
err = json.Unmarshal(res.Result, &preconfirms) | ||
if err != nil { | ||
log.Error("Error unmarshaling data: ", err) | ||
return nil, err | ||
} | ||
|
||
log.Info(fmt.Sprintf("Preconf Response Body: %s", string(res.Result))) | ||
|
||
return *preconfirms, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
fastSsz "github.com/ferranbt/fastssz" | ||
|
||
builderSpec "github.com/attestantio/go-builder-client/spec" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
) | ||
|
||
type BidWithPreconfirmationsProofs struct { | ||
// The block bid | ||
Bid *builderSpec.VersionedSignedBuilderBid `json:"bid"` | ||
// The preconfirmations with proofs | ||
Proofs []*PreconfirmationWithProof `json:"proofs"` | ||
} | ||
|
||
func (b *BidWithPreconfirmationsProofs) String() string { | ||
out, err := json.Marshal(b) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return string(out) | ||
} | ||
|
||
func (p *PreconfirmationWithProof) String() string { | ||
proofs, err := json.Marshal(p) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return string(proofs) | ||
} | ||
|
||
type HexBytes []byte | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
func (h HexBytes) MarshalJSON() ([]byte, error) { | ||
return []byte(fmt.Sprintf(`"%#x"`, h)), nil | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler. | ||
func (h *HexBytes) UnmarshalJSON(input []byte) error { | ||
if len(input) == 0 { | ||
return errors.New("input missing") | ||
} | ||
|
||
if !bytes.HasPrefix(input, []byte{'"', '0', 'x'}) { | ||
return errors.New("invalid prefix") | ||
} | ||
|
||
if !bytes.HasSuffix(input, []byte{'"'}) { | ||
return errors.New("invalid suffix") | ||
} | ||
|
||
var data string | ||
json.Unmarshal(input, &data) | ||
|
||
res, _ := hex.DecodeString(strings.TrimPrefix(data, "0x")) | ||
|
||
*h = res | ||
|
||
return nil | ||
} | ||
|
||
// SerializedMerkleProof contains a serialized Merkle proof of transaction inclusion. | ||
// - `Index“ is the generalized index of the included transaction from the SSZ tree | ||
// created from the list of transactions. | ||
// - `Hashes` are the other branch hashes needed to reconstruct the Merkle proof. | ||
// | ||
// For reference, see https://github.com/ethereum/consensus-specs/blob/dev/ssz/simple-serialize.md | ||
type SerializedMerkleProof struct { | ||
Index int `json:"index"` | ||
Hashes []HexBytes `ssz-size:"dynamic" json:"hashes"` | ||
} | ||
|
||
func (s *SerializedMerkleProof) FromFastSszProof(p *fastSsz.Proof) { | ||
s.Index = p.Index | ||
s.Hashes = make([]HexBytes, len(p.Hashes)) | ||
for i, h := range p.Hashes { | ||
s.Hashes[i] = h | ||
} | ||
} | ||
|
||
// ToFastSszProof converts a SerializedMerkleProof to a fastssz.Proof. | ||
func (s *SerializedMerkleProof) ToFastSszProof(leaf []byte) *fastSsz.Proof { | ||
p := &fastSsz.Proof{ | ||
Index: s.Index, | ||
Leaf: leaf, | ||
Hashes: make([][]byte, len(s.Hashes)), | ||
} | ||
for i, h := range s.Hashes { | ||
p.Hashes[i] = h | ||
} | ||
return p | ||
} | ||
|
||
// PreconfirmationWithProof is a preconfirmed transaction in the block with | ||
// proof of inclusion, using Merkle Trees. | ||
type PreconfirmationWithProof struct { | ||
// The transaction hash of the preconfirmation | ||
TxHash phase0.Hash32 `ssz-size:"32" json:"txHash"` | ||
// The Merkle proof of the preconfirmation | ||
MerkleProof *SerializedMerkleProof `json:"merkleProof"` | ||
} |
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.