Skip to content

Commit

Permalink
fix converting baseFeePerGas
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Mar 12, 2024
1 parent 7564127 commit b43f139
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions ethstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ func GetValidators(ctx context.Context, client *http.Service, stateID string) (m
}

type BlockData struct {
Version spec.DataVersion
ProposerIndex phase0.ValidatorIndex
Transactions []bellatrix.Transaction
BaseFeePerGas [32]byte
BaseFeePerGas *big.Int
Deposits []*phase0.Deposit
GasUsed uint64
GasLimit uint64
Expand All @@ -199,6 +200,7 @@ type BlockData struct {

func GetBlockData(block *spec.VersionedSignedBeaconBlock) (*BlockData, error) {
d := &BlockData{}
d.Version = block.Version
switch block.Version {
case spec.DataVersionPhase0:
d.Deposits = block.Phase0.Message.Body.Deposits
Expand All @@ -211,15 +213,23 @@ func GetBlockData(block *spec.VersionedSignedBeaconBlock) (*BlockData, error) {
d.ProposerIndex = block.Bellatrix.Message.ProposerIndex
d.GasUsed = block.Bellatrix.Message.Body.ExecutionPayload.GasUsed
d.GasLimit = block.Bellatrix.Message.Body.ExecutionPayload.GasLimit
d.BaseFeePerGas = block.Bellatrix.Message.Body.ExecutionPayload.BaseFeePerGas
baseFeePerGasBEBytes := make([]byte, len(block.Bellatrix.Message.Body.ExecutionPayload.BaseFeePerGas))
for i := 0; i < 32; i++ {
baseFeePerGasBEBytes[i] = block.Bellatrix.Message.Body.ExecutionPayload.BaseFeePerGas[32-1-i]
}
d.BaseFeePerGas = new(big.Int).SetBytes(baseFeePerGasBEBytes)
d.BlockNumber = block.Bellatrix.Message.Body.ExecutionPayload.BlockNumber
d.Transactions = block.Bellatrix.Message.Body.ExecutionPayload.Transactions
case spec.DataVersionCapella:
d.Deposits = block.Capella.Message.Body.Deposits
d.ProposerIndex = block.Capella.Message.ProposerIndex
d.GasUsed = block.Capella.Message.Body.ExecutionPayload.GasUsed
d.GasLimit = block.Capella.Message.Body.ExecutionPayload.GasLimit
d.BaseFeePerGas = block.Capella.Message.Body.ExecutionPayload.BaseFeePerGas
baseFeePerGasBEBytes := make([]byte, len(block.Capella.Message.Body.ExecutionPayload.BaseFeePerGas))
for i := 0; i < 32; i++ {
baseFeePerGasBEBytes[i] = block.Capella.Message.Body.ExecutionPayload.BaseFeePerGas[32-1-i]
}
d.BaseFeePerGas = new(big.Int).SetBytes(baseFeePerGasBEBytes)
d.Withdrawals = block.Capella.Message.Body.ExecutionPayload.Withdrawals
d.BlockNumber = block.Capella.Message.Body.ExecutionPayload.BlockNumber
d.Transactions = block.Capella.Message.Body.ExecutionPayload.Transactions
Expand All @@ -228,7 +238,7 @@ func GetBlockData(block *spec.VersionedSignedBeaconBlock) (*BlockData, error) {
d.ProposerIndex = block.Deneb.Message.ProposerIndex
d.GasUsed = block.Deneb.Message.Body.ExecutionPayload.GasUsed
d.GasLimit = block.Deneb.Message.Body.ExecutionPayload.GasLimit
d.BaseFeePerGas = block.Deneb.Message.Body.ExecutionPayload.BaseFeePerGas.Bytes32()
d.BaseFeePerGas = block.Deneb.Message.Body.ExecutionPayload.BaseFeePerGas.ToBig()
d.Withdrawals = block.Deneb.Message.Body.ExecutionPayload.Withdrawals
d.BlockNumber = block.Deneb.Message.Body.ExecutionPayload.BlockNumber
d.Transactions = block.Deneb.Message.Body.ExecutionPayload.Transactions
Expand Down Expand Up @@ -488,13 +498,7 @@ func Calculate(ctx context.Context, bnAddress, elAddress, dayStr string, concurr
totalTxFee.Add(totalTxFee, txFee)
}

// base fee per gas is stored little-endian but we need it
// big-endian for big.Int.
var baseFeePerGasBEBytes [32]byte
for i := 0; i < 32; i++ {
baseFeePerGasBEBytes[i] = blockData.BaseFeePerGas[32-1-i]
}
baseFeePerGas := new(big.Int).SetBytes(baseFeePerGasBEBytes[:])
baseFeePerGas := blockData.BaseFeePerGas
burntFee := new(big.Int).Mul(baseFeePerGas, new(big.Int).SetUint64(blockData.GasUsed))

totalTxFee.Sub(totalTxFee, burntFee)
Expand Down

0 comments on commit b43f139

Please sign in to comment.