Commit c4b37bf5 authored by Roberto Bayardo's avatar Roberto Bayardo Committed by GitHub

add ability to decode ecotone scalar values into components (#10547)

parent 27d2f163
...@@ -12,6 +12,8 @@ import ( ...@@ -12,6 +12,8 @@ import (
func main() { func main() {
var scalar, blobScalar uint var scalar, blobScalar uint
var decode string
flag.StringVar(&decode, "decode", "", "uint256 post-ecotone scalar value to decode into its components")
flag.UintVar(&scalar, "scalar", 0, "base fee scalar value for the gas config (uint32)") flag.UintVar(&scalar, "scalar", 0, "base fee scalar value for the gas config (uint32)")
flag.UintVar(&blobScalar, "blob-scalar", 0, "blob base fee scalar value for the gas config (uint32)") flag.UintVar(&blobScalar, "blob-scalar", 0, "blob base fee scalar value for the gas config (uint32)")
flag.Parse() flag.Parse()
...@@ -27,10 +29,41 @@ func main() { ...@@ -27,10 +29,41 @@ func main() {
os.Exit(2) os.Exit(2)
} }
encoded := eth.EncodeScalar(eth.EcostoneScalars{ var encoded [32]byte
BlobBaseFeeScalar: uint32(blobScalar), if len(decode) > 0 {
BaseFeeScalar: uint32(scalar), if scalar != 0 || blobScalar != 0 {
}) fmt.Fprintln(flag.CommandLine.Output(), "decode parameter should not be used with scalar and blob-scalar")
flag.Usage()
os.Exit(2)
}
uint256 := new(big.Int)
_, ok := uint256.SetString(decode, 10)
if !ok {
fmt.Fprintln(flag.CommandLine.Output(), "failed to parse int from post-ecotone scalar")
flag.Usage()
os.Exit(2)
}
encodedSlice := uint256.Bytes()
if len(encodedSlice) > 32 {
fmt.Fprintln(flag.CommandLine.Output(), "post-ecotone scalar out of uint256 range")
flag.Usage()
os.Exit(2)
}
copy(encoded[:], encodedSlice)
decoded, err := eth.DecodeScalar(encoded)
if err != nil {
fmt.Fprintln(flag.CommandLine.Output(), "post-ecotone scalar could not be decoded:", err)
flag.Usage()
os.Exit(2)
}
scalar = uint(decoded.BaseFeeScalar)
blobScalar = uint(decoded.BlobBaseFeeScalar)
} else {
encoded = eth.EncodeScalar(eth.EcostoneScalars{
BlobBaseFeeScalar: uint32(blobScalar),
BaseFeeScalar: uint32(scalar),
})
}
i := new(big.Int).SetBytes(encoded[:]) i := new(big.Int).SetBytes(encoded[:])
fmt.Println("# base fee scalar :", scalar) fmt.Println("# base fee scalar :", scalar)
......
...@@ -425,7 +425,7 @@ func DecodeScalar(scalar [32]byte) (EcostoneScalars, error) { ...@@ -425,7 +425,7 @@ func DecodeScalar(scalar [32]byte) (EcostoneScalars, error) {
BaseFeeScalar: binary.BigEndian.Uint32(scalar[28:32]), BaseFeeScalar: binary.BigEndian.Uint32(scalar[28:32]),
}, nil }, nil
default: default:
return EcostoneScalars{}, fmt.Errorf("unexpected system config scalar: %s", scalar) return EcostoneScalars{}, fmt.Errorf("unexpected system config scalar: %x", scalar)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment