Skip to content

Commit

Permalink
fix: docker build missing bigint - reuse op-service wei conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed May 23, 2024
1 parent 1da9f66 commit 3702d48
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
10 changes: 5 additions & 5 deletions op-challenger/cmd/list_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"strconv"
"time"

"github.com/ethereum-optimism/optimism/indexer/bigint"
"github.com/ethereum-optimism/optimism/op-challenger/flags"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts/metrics"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/eth"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
Expand Down Expand Up @@ -106,8 +106,8 @@ func listClaims(ctx context.Context, game contracts.FaultDisputeGameContract, ve
valueFormat = "%-66v"
}
now := time.Now()
lineFormat := "%3v %-7v %6v %5v %14v " + valueFormat + " %-42v %10v %-19v %10v %v\n"
info := fmt.Sprintf(lineFormat, "Idx", "Move", "Parent", "Depth", "Index", "Value", "Claimant", "Bond", "Time", "Clock Used", "Resolution")
lineFormat := "%3v %-7v %6v %5v %14v " + valueFormat + " %-42v %12v %-19v %10v %v\n"
info := fmt.Sprintf(lineFormat, "Idx", "Move", "Parent", "Depth", "Index", "Value", "Claimant", "Bond (ETH)", "Time", "Clock Used", "Resolution")
for i, claim := range claims {
pos := claim.Position
parent := strconv.Itoa(claim.ParentContractIndex)
Expand Down Expand Up @@ -155,9 +155,9 @@ func listClaims(ctx context.Context, game contracts.FaultDisputeGameContract, ve
value = claim.Value.Hex()
}
timestamp := claim.Clock.Timestamp.Format(time.DateTime)
bond := fmt.Sprintf("%10.6f", bigint.WeiToETH(claim.Bond))
bond := fmt.Sprintf("%12.8f", eth.WeiToEther(claim.Bond))
if verbose {
bond = bigint.WeiToETH(claim.Bond).String()
bond = fmt.Sprintf("%f", eth.WeiToEther(claim.Bond))
}
info = info + fmt.Sprintf(lineFormat,
i, move, parent, pos.Depth(), traceIdx, value, claim.Claimant, bond, timestamp, elapsed, countered)
Expand Down
2 changes: 1 addition & 1 deletion op-service/client/mocks/HTTP.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions op-service/eth/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eth

import (
"math/big"

"github.com/ethereum/go-ethereum/params"
)

// WeiToEther divides the wei value by 10^18 to get a number in ether as a float64
func WeiToEther(wei *big.Int) float64 {
num := new(big.Rat).SetInt(wei)
denom := big.NewRat(params.Ether, 1)
num = num.Quo(num, denom)
f, _ := num.Float64()
return f
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package eth

import (
"math/big"
Expand Down Expand Up @@ -31,10 +31,9 @@ func TestWeiToEther(t *testing.T) {
}

for i, tc := range tests {
out := weiToEther(tc.input)
out := WeiToEther(tc.input)
if out != tc.output {
t.Fatalf("test %v: expected %v but got %v", i, tc.output, out)
}
}

}
14 changes: 2 additions & 12 deletions op-service/metrics/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"context"
"math/big"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -11,20 +10,11 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"

"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum-optimism/optimism/op-service/eth"
)

// weiToEther divides the wei value by 10^18 to get a number in ether as a float64
func weiToEther(wei *big.Int) float64 {
num := new(big.Rat).SetInt(wei)
denom := big.NewRat(params.Ether, 1)
num = num.Quo(num, denom)
f, _ := num.Float64()
return f
}

// LaunchBalanceMetrics starts a periodic query of the balance of the supplied account and records it
// to the "balance" metric of the namespace. The balance of the account is recorded in Ether (not Wei).
// Cancel the supplied context to shut down the go routine
Expand All @@ -42,7 +32,7 @@ func LaunchBalanceMetrics(log log.Logger, r *prometheus.Registry, ns string, cli
log.Warn("failed to get balance of account", "err", err, "address", account)
return
}
bal := weiToEther(bigBal)
bal := eth.WeiToEther(bigBal)
balanceGuage.Set(bal)
}, func() error {
log.Info("balance metrics shutting down")
Expand Down

0 comments on commit 3702d48

Please sign in to comment.