|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + _ "os" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 8 | + "github.com/ethereum/go-ethereum/core/beacon" |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/ethereum/go-ethereum/log" |
| 11 | + |
| 12 | + "github.com/flashbots/go-boost-utils/bls" |
| 13 | + boostTypes "github.com/flashbots/go-boost-utils/types" |
| 14 | +) |
| 15 | + |
| 16 | +type PubkeyHex string |
| 17 | + |
| 18 | +type ValidatorData struct { |
| 19 | + Pubkey PubkeyHex |
| 20 | + FeeRecipient boostTypes.Address `json:"feeRecipient"` |
| 21 | + GasLimit uint64 `json:"gasLimit"` |
| 22 | + Timestamp uint64 `json:"timestamp"` |
| 23 | +} |
| 24 | + |
| 25 | +type IBeaconClient interface { |
| 26 | + isValidator(pubkey PubkeyHex) bool |
| 27 | + getProposerForNextSlot(requestedSlot uint64) (PubkeyHex, error) |
| 28 | + onForkchoiceUpdate() (uint64, error) |
| 29 | +} |
| 30 | + |
| 31 | +type IRelay interface { |
| 32 | + SubmitBlock(msg *boostTypes.BuilderSubmitBlockRequest) error |
| 33 | + GetValidatorForSlot(nextSlot uint64) (ValidatorData, error) |
| 34 | +} |
| 35 | + |
| 36 | +type Builder struct { |
| 37 | + beaconClient IBeaconClient |
| 38 | + relay IRelay |
| 39 | + |
| 40 | + builderSecretKey *bls.SecretKey |
| 41 | + builderPublicKey boostTypes.PublicKey |
| 42 | + builderSigningDomain boostTypes.Domain |
| 43 | +} |
| 44 | + |
| 45 | +func NewBuilder(sk *bls.SecretKey, bc IBeaconClient, relay IRelay, builderSigningDomain boostTypes.Domain) *Builder { |
| 46 | + pkBytes := bls.PublicKeyFromSecretKey(sk).Compress() |
| 47 | + pk := boostTypes.PublicKey{} |
| 48 | + pk.FromSlice(pkBytes) |
| 49 | + |
| 50 | + _, err := bc.onForkchoiceUpdate() |
| 51 | + if err != nil { |
| 52 | + log.Error("could not initialize beacon client", "err", err) |
| 53 | + } |
| 54 | + |
| 55 | + return &Builder{ |
| 56 | + beaconClient: bc, |
| 57 | + relay: relay, |
| 58 | + builderSecretKey: sk, |
| 59 | + builderPublicKey: pk, |
| 60 | + |
| 61 | + builderSigningDomain: builderSigningDomain, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (b *Builder) onForkchoice(payloadAttributes *beacon.PayloadAttributesV1) { |
| 66 | + dataJson, err := json.Marshal(payloadAttributes) |
| 67 | + if err == nil { |
| 68 | + log.Info("FCU", "data", string(dataJson)) |
| 69 | + } else { |
| 70 | + log.Info("FCU", "data", payloadAttributes, "parsingError", err) |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + nextSlot, err := b.beaconClient.onForkchoiceUpdate() |
| 75 | + if err != nil { |
| 76 | + log.Error("FCU hook failed", "err", err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + if payloadAttributes != nil { |
| 81 | + payloadAttributes.Slot = nextSlot |
| 82 | + if vd, err := b.relay.GetValidatorForSlot(nextSlot); err == nil { |
| 83 | + payloadAttributes.SuggestedFeeRecipient = [20]byte(vd.FeeRecipient) |
| 84 | + payloadAttributes.GasLimit = vd.GasLimit |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (b *Builder) newSealedBlock(data *beacon.ExecutableDataV1, block *types.Block, payloadAttributes *beacon.PayloadAttributesV1) { |
| 90 | + dataJson, err := json.Marshal(data) |
| 91 | + if err == nil { |
| 92 | + log.Info("newSealedBlock", "data", string(dataJson)) |
| 93 | + } else { |
| 94 | + log.Info("newSealedBlock", "data", data, "parsingError", err) |
| 95 | + } |
| 96 | + payload, err := executableDataToExecutionPayload(data) |
| 97 | + if err != nil { |
| 98 | + log.Error("could not format execution payload", "err", err) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + vd, err := b.relay.GetValidatorForSlot(payloadAttributes.Slot) |
| 103 | + if err != nil { |
| 104 | + log.Error("could not get validator while submitting block", "err", err, "slot", payloadAttributes.Slot) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + pubkey, err := boostTypes.HexToPubkey(string(vd.Pubkey)) |
| 109 | + if err != nil { |
| 110 | + log.Error("could not parse pubkey", "err", err, "pubkey", vd.Pubkey) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + value := new(boostTypes.U256Str) |
| 115 | + err = value.FromBig(block.Profit) |
| 116 | + if err != nil { |
| 117 | + log.Error("could not set block value", "err", err) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + blockBidMsg := boostTypes.BidTraceMessage{ |
| 122 | + Slot: payloadAttributes.Slot, |
| 123 | + ParentHash: payload.ParentHash, |
| 124 | + BlockHash: payload.BlockHash, |
| 125 | + BuilderPubkey: b.builderPublicKey, |
| 126 | + ProposerPubkey: pubkey, |
| 127 | + ProposerFeeRecipient: boostTypes.Address(payloadAttributes.SuggestedFeeRecipient), |
| 128 | + Value: *value, |
| 129 | + } |
| 130 | + |
| 131 | + signature, err := boostTypes.SignMessage(&blockBidMsg, b.builderSigningDomain, b.builderSecretKey) |
| 132 | + if err != nil { |
| 133 | + log.Error("could not sign builder bid", "err", err) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + blockSubmitReq := boostTypes.BuilderSubmitBlockRequest{ |
| 138 | + Signature: signature, |
| 139 | + Message: &blockBidMsg, |
| 140 | + ExecutionPayload: payload, |
| 141 | + } |
| 142 | + |
| 143 | + err = b.relay.SubmitBlock(&blockSubmitReq) |
| 144 | + if err != nil { |
| 145 | + log.Error("could not submit block", "err", err) |
| 146 | + return |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func executableDataToExecutionPayload(data *beacon.ExecutableDataV1) (*boostTypes.ExecutionPayload, error) { |
| 151 | + transactionData := make([]hexutil.Bytes, len(data.Transactions)) |
| 152 | + for i, tx := range data.Transactions { |
| 153 | + transactionData[i] = hexutil.Bytes(tx) |
| 154 | + } |
| 155 | + |
| 156 | + baseFeePerGas := new(boostTypes.U256Str) |
| 157 | + err := baseFeePerGas.FromBig(data.BaseFeePerGas) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + return &boostTypes.ExecutionPayload{ |
| 163 | + ParentHash: [32]byte(data.ParentHash), |
| 164 | + FeeRecipient: [20]byte(data.FeeRecipient), |
| 165 | + StateRoot: [32]byte(data.StateRoot), |
| 166 | + ReceiptsRoot: [32]byte(data.ReceiptsRoot), |
| 167 | + LogsBloom: boostTypes.Bloom(types.BytesToBloom(data.LogsBloom)), |
| 168 | + Random: [32]byte(data.Random), |
| 169 | + BlockNumber: data.Number, |
| 170 | + GasLimit: data.GasLimit, |
| 171 | + GasUsed: data.GasUsed, |
| 172 | + Timestamp: data.Timestamp, |
| 173 | + ExtraData: data.ExtraData, |
| 174 | + BaseFeePerGas: *baseFeePerGas, |
| 175 | + BlockHash: [32]byte(data.BlockHash), |
| 176 | + Transactions: transactionData, |
| 177 | + }, nil |
| 178 | +} |
0 commit comments