Skip to content

Commit

Permalink
Merge pull request #69 from bloXroute-Labs/v2.129.54
Browse files Browse the repository at this point in the history
Publish release v2.129.54
  • Loading branch information
bogdanprodanj authored Dec 9, 2024
2 parents 58aa7d4 + 3bbe116 commit 133ba19
Show file tree
Hide file tree
Showing 20 changed files with 1,359 additions and 1,010 deletions.
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion bxmessage/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ const EmptyProtocol = 0
const MinProtocol = NextValidatorMultipleProtocol

// CurrentProtocol tracks the most recent version of the bloxroute wire protocol
const CurrentProtocol = BundlesUpdatedProtocol
const CurrentProtocol = BundleBlocksCountAndDroppingTxs

// BundleBlocksCountAndDroppingTxs is the minimum protocol version that supports bundle blocks count and dropping txs
const BundleBlocksCountAndDroppingTxs = 52

// BundlesUpdatedProtocol is the minimum protocol version that stops supporting legacy bundle messages
const BundlesUpdatedProtocol = 51
Expand Down
79 changes: 71 additions & 8 deletions bxmessage/mev_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type MEVBundle struct {

// From protocol version 48
IncomingRefundRecipient string `json:"refund_recipient"`

// From protocol version 52
BlocksCount int `json:"blocks_count,omitempty"`
DroppingTxHashes []string `json:"dropping_tx_hashes,omitempty"`
}

// NewMEVBundle creates a new MEVBundle
Expand All @@ -84,6 +88,8 @@ func NewMEVBundle(
avoidMixedBundles bool,
priorityFeeRefund bool,
incomingRefundRecipient string,
blocksCount int,
droppingTxHashes []string,
) (MEVBundle, error) {
if len(uuid) != 0 && len(uuid) != 36 {
return MEVBundle{}, errors.New("invalid uuid len")
Expand All @@ -100,13 +106,15 @@ func NewMEVBundle(
AvoidMixedBundles: avoidMixedBundles,
PriorityFeeRefund: priorityFeeRefund,
IncomingRefundRecipient: incomingRefundRecipient,
BlocksCount: blocksCount,
DroppingTxHashes: droppingTxHashes,
}, nil
}

// String returns a string representation of the MEVBundle
func (m MEVBundle) String() string {
return fmt.Sprintf("mev bundle(sender account ID: %s, hash: %s, blockNumber: %s, builders: %v, txs: %d, sent from cloud api: %v, tier: %v, allowMixedBundles: %v, priorityFeeRefund: %v, incomingRefundRecipient: %v, UUID: %s, MinTimestamp %v , MaxTimestamp %v, RevertingHashes %v)",
m.OriginalSenderAccountID, m.BundleHash, m.BlockNumber, m.MEVBuilders, len(m.Transactions), m.SentFromCloudAPI, m.OriginalSenderAccountTier, m.AvoidMixedBundles, m.PriorityFeeRefund, m.IncomingRefundRecipient, m.UUID, m.MinTimestamp, m.MaxTimestamp, len(m.RevertingHashes))
return fmt.Sprintf("mev bundle(sender account ID: %s, hash: %s, blockNumber: %s, builders: %v, txs: %d, sent from cloud api: %v, tier: %v, allowMixedBundles: %v, priorityFeeRefund: %v, incomingRefundRecipient: %v, UUID: %s, MinTimestamp %v , MaxTimestamp %v, RevertingHashes %v, blocksCount %v, dropingTxs %v)",
m.OriginalSenderAccountID, m.BundleHash, m.BlockNumber, m.MEVBuilders, len(m.Transactions), m.SentFromCloudAPI, m.OriginalSenderAccountTier, m.AvoidMixedBundles, m.PriorityFeeRefund, m.IncomingRefundRecipient, m.UUID, m.MinTimestamp, m.MaxTimestamp, len(m.RevertingHashes), m.BlocksCount, len(m.DroppingTxHashes))
}

// SetHash sets the hash based on the fields in BundleSubmission
Expand Down Expand Up @@ -147,6 +155,14 @@ func (m *MEVBundle) SetHash() {
buf = append(buf, []byte(name+auth)...)
}

blocksCount := make([]byte, 4)
binary.BigEndian.PutUint32(blocksCount, uint32(m.BlocksCount))
buf = append(buf, blocksCount...)

for _, hash := range m.DroppingTxHashes {
buf = append(buf, []byte(hash)...)
}

m.hash = utils.DoubleSHA256(buf[:])
}

Expand Down Expand Up @@ -220,6 +236,12 @@ func (m MEVBundle) size(protocol Protocol, txs [][]byte) uint32 {
size += common.AddressLength // IncomingRefundRecipient (address)
}

if protocol >= BundleBlocksCountAndDroppingTxs {
size += types.UInt32Len // BlocksCount
size += types.UInt16Len // DroppingTxHashes count
size += uint32(types.SHA256HashLen * len(m.DroppingTxHashes)) // DroppingTxHashes
}

return size
}

Expand All @@ -244,7 +266,7 @@ func (m MEVBundle) Pack(protocol Protocol) ([]byte, error) {
return nil, err
}

decodedRevertingHashes, err := m.decodeRevertingHashes()
decodedRevertingHashes, err := m.decodeHashes(m.RevertingHashes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -414,6 +436,22 @@ func (m MEVBundle) Pack(protocol Protocol) ([]byte, error) {
offset += common.AddressLength //nolint:ineffassign
}

if protocol >= BundleBlocksCountAndDroppingTxs {
binary.LittleEndian.PutUint32(buf[offset:], uint32(m.BlocksCount))
offset += types.UInt32Len

decodedDroppingTxs, err := m.decodeHashes(m.DroppingTxHashes)
if err != nil {
return nil, err
}
binary.LittleEndian.PutUint16(buf[offset:], uint16(len(decodedDroppingTxs)))
offset += types.UInt16Len
for _, tx := range decodedDroppingTxs {
copy(buf[offset:], tx)
offset += types.SHA256HashLen
}
}

if err := checkBuffEnd(&buf, offset); err != nil {
return nil, err
}
Expand Down Expand Up @@ -679,6 +717,31 @@ func (m *MEVBundle) Unpack(data []byte, protocol Protocol) error {
offset += common.AddressLength //nolint:ineffassign
}

if protocol >= BundleBlocksCountAndDroppingTxs {
if err = checkBufSize(&data, offset, types.UInt32Len); err != nil {
return err
}
m.BlocksCount = int(binary.LittleEndian.Uint32(data[offset : offset+4]))
offset += types.UInt32Len

if err = checkBufSize(&data, offset, types.UInt16Len); err != nil {
return err
}
numOfDroppedTxs := int(binary.LittleEndian.Uint16(data[offset:]))
offset += types.UInt16Len
m.DroppingTxHashes = make([]string, numOfDroppedTxs)
for i := 0; i < numOfDroppedTxs; i++ {
if err = checkBufSize(&data, offset, types.SHA256HashLen); err != nil {
return err
}
decodedHash := data[offset : offset+types.SHA256HashLen]
offset += types.SHA256HashLen
m.DroppingTxHashes[i] = "0x" + hex.EncodeToString(decodedHash)
}
} else {
m.DroppingTxHashes = []string{}
}

return nil
}

Expand All @@ -705,18 +768,18 @@ func (m *MEVBundle) decodeTransactions() ([][]byte, error) {
return decoded, nil
}

func (m *MEVBundle) decodeRevertingHashes() ([][]byte, error) {
decoded := make([][]byte, 0, len(m.RevertingHashes))
func (m *MEVBundle) decodeHashes(txs []string) ([][]byte, error) {
decoded := make([][]byte, 0, len(txs))

for _, hash := range m.RevertingHashes {
for _, hash := range txs {
// Remove the "0x" prefix and decode the hex string
decodedHash, err := hex.DecodeString(strings.TrimPrefix(hash, "0x"))
if err != nil {
return nil, fmt.Errorf("decode reverting hash hex %v for bundle %v: %s", hash, m.BundleHash, err)
return nil, fmt.Errorf("decode hash hex %v for bundle %v: %s", hash, m.BundleHash, err)
}

if ln := len(decodedHash); ln != types.SHA256HashLen {
return nil, fmt.Errorf("illegal reverting hash size %v for bundle %v: expected size: %dbytes, got %dbytes", hash, m.BundleHash, types.SHA256HashLen, ln)
return nil, fmt.Errorf("illegal hash size %v for bundle %v: expected size: %dbytes, got %dbytes", hash, m.BundleHash, types.SHA256HashLen, ln)
}

decoded = append(decoded, decodedHash)
Expand Down
60 changes: 58 additions & 2 deletions bxmessage/mev_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ func TestMEVBundle(t *testing.T) {
SentFromCloudAPI: true,
AvoidMixedBundles: true,
PriorityFeeRefund: true,
BlocksCount: 2,
DroppingTxHashes: []string{
"0xa74b582bd1505262d2b9154d87b181600f5a6fe7b49bd7f0b0192407773f0143",
"0x2d283f3b4bfc4a7fe37ec935566b92e9255d1413a2d0c814125e43750d952ecd",
},
}

b, err := m.Pack(BundlePriorityFeeRefundProtocol)
b, err := m.Pack(CurrentProtocol)
require.NoError(t, err)

var m2 MEVBundle
err = m2.Unpack(b, BundlePriorityFeeRefundProtocol)
err = m2.Unpack(b, CurrentProtocol)
require.NoError(t, err)

m.msgType = MEVBundleType
Expand Down Expand Up @@ -70,6 +75,7 @@ func TestMEVBundle_EmptyBuilders(t *testing.T) {
OriginalSenderAccountID: "bloXroute LABS",
OriginalSenderAccountTier: sdnmessage.ATierUltra,
SentFromCloudAPI: true,
DroppingTxHashes: []string{},
}

b, err := m.Pack(BundlesOverBDNOriginalSenderTierProtocol)
Expand Down Expand Up @@ -106,6 +112,7 @@ func TestMEVBundle_NoOriginalSenderAccountTier(t *testing.T) {
PerformanceTimestamp: time.Now().UTC(),
OriginalSenderAccountID: "bloXroute LABS",
SentFromCloudAPI: true,
DroppingTxHashes: []string{},
}

b, err := m.Pack(BundlesOverBDNOriginalSenderTierProtocol)
Expand Down Expand Up @@ -141,6 +148,7 @@ func TestMEVBundlePayoutBackCompatibility(t *testing.T) {
"builder2": "0x456",
},
PerformanceTimestamp: time.Now(),
DroppingTxHashes: []string{},
}

b, err := m.Pack(BundlesOverBDNPayoutProtocol - 1)
Expand Down Expand Up @@ -181,6 +189,7 @@ func TestMEVBundleOriginalSenderAccountBackCompatibility(t *testing.T) {
},
PerformanceTimestamp: time.Now().UTC(),
OriginalSenderAccountID: "bloXroute LABS",
DroppingTxHashes: []string{},
}

b, err := m.Pack(BundlesOverBDNOriginalSenderAccountProtocol - 1)
Expand Down Expand Up @@ -221,6 +230,7 @@ func TestMEVBundleAvoidMixedBundleBackCompatibility(t *testing.T) {
PerformanceTimestamp: time.Now().UTC(),
OriginalSenderAccountID: "bloXroute LABS",
AvoidMixedBundles: true,
DroppingTxHashes: []string{},
}

b, err := m.Pack(AvoidMixedBundleProtocol - 1)
Expand Down Expand Up @@ -261,6 +271,7 @@ func TestMevBundlePriorityFeeRefundBackCompatibility(t *testing.T) {
PerformanceTimestamp: time.Now().UTC(),
OriginalSenderAccountID: "bloXroute LABS",
PriorityFeeRefund: true,
DroppingTxHashes: []string{},
}

t.Run("BundlePriorityFeeRefundProtocol - 1 protocol", func(t *testing.T) {
Expand Down Expand Up @@ -292,3 +303,48 @@ func TestMevBundlePriorityFeeRefundBackCompatibility(t *testing.T) {
assert.Equal(t, m, m2)
})
}

func TestMEVBundleBlocksCountAndDroppingTxsBackCompatibility(t *testing.T) {
m := MEVBundle{
UUID: "123e4567-e89b-12d3-a456-426614174000",
BundleHash: "0x5ac23d9a014dfbfc3ec5d06435f74c3dbb616a5a7d5dc77b152b1db2c524083d",
Transactions: []string{
"0xf85d808080945ac6ba4e9b9a4bb23be58af43f15351f70b71769808025a05a35c20b14e4bae033357c7ff5772dbb84a831b290e98ff26fb4073c7483afdba0492ac5720a1c153ca1a35a6214b9811fd04c7ba434c2d0cdf93f8d23080458cb",
"0xf85d8080809419503078a85ceb93e3d7b12721bedcdfc0978ddc808026a05f3cff439aa83fcbde58c2011b5577e98148e7408a170d08acad4e3eb1d64e39a07e4295280fbfff13fbd2e6605bd20b7de0816c25aab8106fdd1ee280cc96a027",
"0xf85d8080809415817f1d896737cbdf4b3dc1cc175be63a9d347f808025a07e809211aff74a5c0af15ee389f53dd0cab085d373f6cef7897948c8518c574fa0024a201974175b06e28bf4ba63709188a30961417394576b5b2ba008dead802f",
},
BlockNumber: "0x8",
MinTimestamp: 1633036800,
MaxTimestamp: 1633123200,
RevertingHashes: []string{
"0xa74b582bd1505262d2b9154d87b181600f5a6fe7b49bd7f0b0192407773f0143",
"0x2d283f3b4bfc4a7fe37ec935566b92e9255d1413a2d0c814125e43750d952ecd",
},
MEVBuilders: map[string]string{
"builder1": "0x123",
"builder2": "0x456",
},
PerformanceTimestamp: time.Now().UTC(),
OriginalSenderAccountID: "bloXroute LABS",
AvoidMixedBundles: true,
BlocksCount: 2,
DroppingTxHashes: []string{
"0xf85d808080945ac6ba4e9b9a4bb23be58af43f15351f70b71769808025a05a35c20b14e4bae033357c7ff5772dbb84a831b290e98ff26fb4073c7483afdba0492ac5720a1c153ca1a35a6214b9811fd04c7ba434c2d0cdf93f8d23080458cb",
},
}

b, err := m.Pack(BundleBlocksCountAndDroppingTxs - 1)
assert.NoError(t, err)

var m2 MEVBundle
err = m2.Unpack(b, BundleBlocksCountAndDroppingTxs-1)
assert.NoError(t, err)

m.msgType = MEVBundleType

// new fields
m.BlocksCount = 0
m.DroppingTxHashes = []string{}

assert.Equal(t, m, m2)
}
Loading

0 comments on commit 133ba19

Please sign in to comment.