|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "math/big" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/ethereum/go-ethereum/log" |
| 11 | + boostTypes "github.com/flashbots/go-boost-utils/types" |
| 12 | + "github.com/jmoiron/sqlx" |
| 13 | + _ "github.com/lib/pq" |
| 14 | +) |
| 15 | + |
| 16 | +type IDatabaseService interface { |
| 17 | + ConsumeBuiltBlock(block *types.Block, bundles []types.SimulatedBundle, bidTrace *boostTypes.BidTrace) |
| 18 | +} |
| 19 | + |
| 20 | +type NilDbService struct{} |
| 21 | + |
| 22 | +func (NilDbService) ConsumeBuiltBlock(*types.Block, []types.SimulatedBundle, *boostTypes.BidTrace) {} |
| 23 | + |
| 24 | +type DatabaseService struct { |
| 25 | + db *sqlx.DB |
| 26 | + |
| 27 | + insertBuiltBlockStmt *sqlx.NamedStmt |
| 28 | + insertBlockBuiltBundleNoIdStmt *sqlx.NamedStmt |
| 29 | + insertBlockBuiltBundleWithIdStmt *sqlx.NamedStmt |
| 30 | + insertMissingBundleStmt *sqlx.NamedStmt |
| 31 | +} |
| 32 | + |
| 33 | +func NewDatabaseService(postgresDSN string) (*DatabaseService, error) { |
| 34 | + db, err := sqlx.Connect("postgres", postgresDSN) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + insertBuiltBlockStmt, err := db.PrepareNamed("insert into built_blocks (block_number, profit, slot, hash, gas_limit, gas_used, base_fee, parent_hash, proposer_pubkey, proposer_fee_recipient, builder_pubkey, timestamp, timestamp_datetime) values (:block_number, :profit, :slot, :hash, :gas_limit, :gas_used, :base_fee, :parent_hash, :proposer_pubkey, :proposer_fee_recipient, :builder_pubkey, :timestamp, to_timestamp(:timestamp)) returning block_id") |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + insertBlockBuiltBundleNoIdStmt, err := db.PrepareNamed("insert into built_blocks_bundles (block_id, bundle_id) select :block_id, id from bundles where bundle_hash = :bundle_hash and param_block_number = :block_number returning bundle_id") |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + insertBlockBuiltBundleWithIdStmt, err := db.PrepareNamed("insert into built_blocks_bundles (block_id, bundle_id) select :block_id, :bundle_id returning bundle_id") |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + insertMissingBundleStmt, err := db.PrepareNamed("insert into bundles (bundle_hash, param_signed_txs, param_block_number, param_timestamp, received_timestamp, param_reverting_tx_hashes, coinbase_diff, total_gas_used, state_block_number, gas_fees, eth_sent_to_coinbase) values (:bundle_hash, :param_signed_txs, :param_block_number, :param_timestamp, :received_timestamp, :param_reverting_tx_hashes, :coinbase_diff, :total_gas_used, :state_block_number, :gas_fees, :eth_sent_to_coinbase) on conflict (bundle_hash, param_block_number) do nothing returning id") |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + return &DatabaseService{ |
| 60 | + db: db, |
| 61 | + insertBuiltBlockStmt: insertBuiltBlockStmt, |
| 62 | + insertBlockBuiltBundleNoIdStmt: insertBlockBuiltBundleNoIdStmt, |
| 63 | + insertBlockBuiltBundleWithIdStmt: insertBlockBuiltBundleWithIdStmt, |
| 64 | + insertMissingBundleStmt: insertMissingBundleStmt, |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (ds *DatabaseService) ConsumeBuiltBlock(block *types.Block, bundles []types.SimulatedBundle, bidTrace *boostTypes.BidTrace) { |
| 69 | + tx, err := ds.db.Beginx() |
| 70 | + |
| 71 | + blockData := BuiltBlock{ |
| 72 | + BlockNumber: block.NumberU64(), |
| 73 | + Profit: new(big.Rat).SetFrac(block.Profit, big.NewInt(1e18)).FloatString(18), |
| 74 | + Slot: bidTrace.Slot, |
| 75 | + Hash: block.Hash().String(), |
| 76 | + GasLimit: block.GasLimit(), |
| 77 | + GasUsed: block.GasUsed(), |
| 78 | + BaseFee: block.BaseFee().Uint64(), |
| 79 | + ParentHash: block.ParentHash().String(), |
| 80 | + ProposerPubkey: bidTrace.ProposerPubkey.String(), |
| 81 | + ProposerFeeRecipient: bidTrace.ProposerFeeRecipient.String(), |
| 82 | + BuilderPubkey: bidTrace.BuilderPubkey.String(), |
| 83 | + Timestamp: block.Time(), |
| 84 | + } |
| 85 | + |
| 86 | + ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) |
| 87 | + defer cancel() |
| 88 | + var blockId uint64 |
| 89 | + if err = tx.NamedStmtContext(ctx, ds.insertBuiltBlockStmt).GetContext(ctx, &blockId, blockData); err != nil { |
| 90 | + log.Error("could not insert built block", "err", err) |
| 91 | + tx.Rollback() |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + for _, bundle := range bundles { |
| 96 | + bundleData := BuiltBlockBundle{ |
| 97 | + BlockId: blockId, |
| 98 | + BundleId: nil, |
| 99 | + BlockNumber: blockData.BlockNumber, |
| 100 | + BundleHash: bundle.OriginalBundle.Hash.String(), |
| 101 | + } |
| 102 | + |
| 103 | + var bundleId uint64 |
| 104 | + err := tx.NamedStmtContext(ctx, ds.insertBlockBuiltBundleNoIdStmt).GetContext(ctx, &bundleId, bundleData) |
| 105 | + if err == nil { |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + if err != sql.ErrNoRows { |
| 110 | + log.Error("could not insert bundle", "err", err) |
| 111 | + // Try anyway |
| 112 | + } |
| 113 | + |
| 114 | + missingBundleData := SimulatedBundleToDbBundle(&bundle) |
| 115 | + err = ds.insertMissingBundleStmt.GetContext(ctx, &bundleId, missingBundleData) // not using the tx as it relies on the unique constraint! |
| 116 | + if err == nil { |
| 117 | + bundleData.BundleId = &bundleId |
| 118 | + _, err = tx.NamedStmtContext(ctx, ds.insertBlockBuiltBundleWithIdStmt).ExecContext(ctx, bundleData) |
| 119 | + if err != nil { |
| 120 | + log.Error("could not insert built block bundle after inserting missing bundle", "err", err) |
| 121 | + } |
| 122 | + } else if err == sql.ErrNoRows /* conflict, someone else inserted the bundle before we could */ { |
| 123 | + if err := tx.NamedStmtContext(ctx, ds.insertBlockBuiltBundleNoIdStmt).GetContext(ctx, &bundleId, bundleData); err != nil { |
| 124 | + log.Error("could not insert bundle on retry", "err", err) |
| 125 | + continue |
| 126 | + } |
| 127 | + } else { |
| 128 | + log.Error("could not insert missing bundle", "err", err) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + err = tx.Commit() |
| 133 | + if err != nil { |
| 134 | + log.Error("could not commit DB trasnaction", "err", err) |
| 135 | + } |
| 136 | +} |
0 commit comments