Skip to content

Commit ffe758c

Browse files
lightclientMariusVanDerWijdengballetrjl493456442
authored
internal/ethapi,params: add eth_config (#32239)
~Will probably be mostly supplanted by #32224, but this should do for now for devnet 3.~ Seems like #32224 is going to take some more time, so I have completed the implementation of eth_config here. It is quite a bit simpler to implement now that the config hashing was removed. --------- Co-authored-by: MariusVanDerWijden <[email protected]> Co-authored-by: Guillaume Ballet <[email protected]> Co-authored-by: rjl493456442 <[email protected]>
1 parent bd4b179 commit ffe758c

File tree

8 files changed

+418
-4
lines changed

8 files changed

+418
-4
lines changed

core/vm/contracts.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
type PrecompiledContract interface {
4848
RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
4949
Run(input []byte) ([]byte, error) // Run runs the precompiled contract
50+
Name() string
5051
}
5152

5253
// PrecompiledContracts contains the precompiled contracts supported at the given fork.
@@ -309,6 +310,10 @@ func (c *ecrecover) Run(input []byte) ([]byte, error) {
309310
return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil
310311
}
311312

313+
func (c *ecrecover) Name() string {
314+
return "ECREC"
315+
}
316+
312317
// SHA256 implemented as a native contract.
313318
type sha256hash struct{}
314319

@@ -324,6 +329,10 @@ func (c *sha256hash) Run(input []byte) ([]byte, error) {
324329
return h[:], nil
325330
}
326331

332+
func (c *sha256hash) Name() string {
333+
return "SHA256"
334+
}
335+
327336
// RIPEMD160 implemented as a native contract.
328337
type ripemd160hash struct{}
329338

@@ -340,6 +349,10 @@ func (c *ripemd160hash) Run(input []byte) ([]byte, error) {
340349
return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
341350
}
342351

352+
func (c *ripemd160hash) Name() string {
353+
return "RIPEMD160"
354+
}
355+
343356
// data copy implemented as a native contract.
344357
type dataCopy struct{}
345358

@@ -354,6 +367,10 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
354367
return common.CopyBytes(in), nil
355368
}
356369

370+
func (c *dataCopy) Name() string {
371+
return "ID"
372+
}
373+
357374
// bigModExp implements a native big integer exponential modular operation.
358375
type bigModExp struct {
359376
eip2565 bool
@@ -543,6 +560,10 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
543560
return common.LeftPadBytes(v, int(modLen)), nil
544561
}
545562

563+
func (c *bigModExp) Name() string {
564+
return "MODEXP"
565+
}
566+
546567
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
547568
// returning it, or an error if the point is invalid.
548569
func newCurvePoint(blob []byte) (*bn256.G1, error) {
@@ -592,6 +613,10 @@ func (c *bn256AddIstanbul) Run(input []byte) ([]byte, error) {
592613
return runBn256Add(input)
593614
}
594615

616+
func (c *bn256AddIstanbul) Name() string {
617+
return "BN254_ADD"
618+
}
619+
595620
// bn256AddByzantium implements a native elliptic curve point addition
596621
// conforming to Byzantium consensus rules.
597622
type bn256AddByzantium struct{}
@@ -605,6 +630,10 @@ func (c *bn256AddByzantium) Run(input []byte) ([]byte, error) {
605630
return runBn256Add(input)
606631
}
607632

633+
func (c *bn256AddByzantium) Name() string {
634+
return "BN254_ADD"
635+
}
636+
608637
// runBn256ScalarMul implements the Bn256ScalarMul precompile, referenced by
609638
// both Byzantium and Istanbul operations.
610639
func runBn256ScalarMul(input []byte) ([]byte, error) {
@@ -630,6 +659,10 @@ func (c *bn256ScalarMulIstanbul) Run(input []byte) ([]byte, error) {
630659
return runBn256ScalarMul(input)
631660
}
632661

662+
func (c *bn256ScalarMulIstanbul) Name() string {
663+
return "BN254_MUL"
664+
}
665+
633666
// bn256ScalarMulByzantium implements a native elliptic curve scalar
634667
// multiplication conforming to Byzantium consensus rules.
635668
type bn256ScalarMulByzantium struct{}
@@ -643,6 +676,10 @@ func (c *bn256ScalarMulByzantium) Run(input []byte) ([]byte, error) {
643676
return runBn256ScalarMul(input)
644677
}
645678

679+
func (c *bn256ScalarMulByzantium) Name() string {
680+
return "BN254_MUL"
681+
}
682+
646683
var (
647684
// true32Byte is returned if the bn256 pairing check succeeds.
648685
true32Byte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
@@ -698,6 +735,10 @@ func (c *bn256PairingIstanbul) Run(input []byte) ([]byte, error) {
698735
return runBn256Pairing(input)
699736
}
700737

738+
func (c *bn256PairingIstanbul) Name() string {
739+
return "BN254_PAIRING"
740+
}
741+
701742
// bn256PairingByzantium implements a pairing pre-compile for the bn256 curve
702743
// conforming to Byzantium consensus rules.
703744
type bn256PairingByzantium struct{}
@@ -711,6 +752,10 @@ func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
711752
return runBn256Pairing(input)
712753
}
713754

755+
func (c *bn256PairingByzantium) Name() string {
756+
return "BN254_PAIRING"
757+
}
758+
714759
type blake2F struct{}
715760

716761
func (c *blake2F) RequiredGas(input []byte) uint64 {
@@ -772,6 +817,10 @@ func (c *blake2F) Run(input []byte) ([]byte, error) {
772817
return output, nil
773818
}
774819

820+
func (c *blake2F) Name() string {
821+
return "BLAKE2F"
822+
}
823+
775824
var (
776825
errBLS12381InvalidInputLength = errors.New("invalid input length")
777826
errBLS12381InvalidFieldElementTopBytes = errors.New("invalid field element top bytes")
@@ -815,6 +864,10 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
815864
return encodePointG1(p0), nil
816865
}
817866

867+
func (c *bls12381G1Add) Name() string {
868+
return "BLS12_G1ADD"
869+
}
870+
818871
// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile.
819872
type bls12381G1MultiExp struct{}
820873

@@ -875,6 +928,10 @@ func (c *bls12381G1MultiExp) Run(input []byte) ([]byte, error) {
875928
return encodePointG1(r), nil
876929
}
877930

931+
func (c *bls12381G1MultiExp) Name() string {
932+
return "BLS12_G1MSM"
933+
}
934+
878935
// bls12381G2Add implements EIP-2537 G2Add precompile.
879936
type bls12381G2Add struct{}
880937

@@ -912,6 +969,10 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
912969
return encodePointG2(r), nil
913970
}
914971

972+
func (c *bls12381G2Add) Name() string {
973+
return "BLS12_G2ADD"
974+
}
975+
915976
// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
916977
type bls12381G2MultiExp struct{}
917978

@@ -972,6 +1033,10 @@ func (c *bls12381G2MultiExp) Run(input []byte) ([]byte, error) {
9721033
return encodePointG2(r), nil
9731034
}
9741035

1036+
func (c *bls12381G2MultiExp) Name() string {
1037+
return "BLS12_G2MSM"
1038+
}
1039+
9751040
// bls12381Pairing implements EIP-2537 Pairing precompile.
9761041
type bls12381Pairing struct{}
9771042

@@ -1035,6 +1100,10 @@ func (c *bls12381Pairing) Run(input []byte) ([]byte, error) {
10351100
return out, nil
10361101
}
10371102

1103+
func (c *bls12381Pairing) Name() string {
1104+
return "BLS12_PAIRING_CHECK"
1105+
}
1106+
10381107
func decodePointG1(in []byte) (*bls12381.G1Affine, error) {
10391108
if len(in) != 128 {
10401109
return nil, errors.New("invalid g1 point length")
@@ -1153,6 +1222,10 @@ func (c *bls12381MapG1) Run(input []byte) ([]byte, error) {
11531222
return encodePointG1(&r), nil
11541223
}
11551224

1225+
func (c *bls12381MapG1) Name() string {
1226+
return "BLS12_MAP_FP_TO_G1"
1227+
}
1228+
11561229
// bls12381MapG2 implements EIP-2537 MapG2 precompile.
11571230
type bls12381MapG2 struct{}
11581231

@@ -1186,6 +1259,10 @@ func (c *bls12381MapG2) Run(input []byte) ([]byte, error) {
11861259
return encodePointG2(&r), nil
11871260
}
11881261

1262+
func (c *bls12381MapG2) Name() string {
1263+
return "BLS12_MAP_FP2_TO_G2"
1264+
}
1265+
11891266
// kzgPointEvaluation implements the EIP-4844 point evaluation precompile.
11901267
type kzgPointEvaluation struct{}
11911268

@@ -1242,6 +1319,10 @@ func (b *kzgPointEvaluation) Run(input []byte) ([]byte, error) {
12421319
return common.Hex2Bytes(blobPrecompileReturnValue), nil
12431320
}
12441321

1322+
func (b *kzgPointEvaluation) Name() string {
1323+
return "KZG_POINT_EVALUATION"
1324+
}
1325+
12451326
// kZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
12461327
func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
12471328
h := sha256.Sum256(kzg[:])
@@ -1277,3 +1358,7 @@ func (c *p256Verify) Run(input []byte) ([]byte, error) {
12771358
}
12781359
return nil, nil
12791360
}
1361+
1362+
func (c *p256Verify) Name() string {
1363+
return "P256VERIFY"
1364+
}

internal/ethapi/api.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/consensus"
3535
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3636
"github.com/ethereum/go-ethereum/core"
37+
"github.com/ethereum/go-ethereum/core/forkid"
3738
"github.com/ethereum/go-ethereum/core/state"
3839
"github.com/ethereum/go-ethereum/core/types"
3940
"github.com/ethereum/go-ethereum/core/vm"
@@ -1153,6 +1154,71 @@ func (api *BlockChainAPI) CreateAccessList(ctx context.Context, args Transaction
11531154
return result, nil
11541155
}
11551156

1157+
type config struct {
1158+
ActivationTime uint64 `json:"activationTime"`
1159+
BlobSchedule *params.BlobConfig `json:"blobSchedule"`
1160+
ChainId *hexutil.Big `json:"chainId"`
1161+
ForkId hexutil.Bytes `json:"forkId"`
1162+
Precompiles map[string]common.Address `json:"precompiles"`
1163+
SystemContracts map[string]common.Address `json:"systemContracts"`
1164+
}
1165+
1166+
type configResponse struct {
1167+
Current *config `json:"current"`
1168+
Next *config `json:"next"`
1169+
Last *config `json:"last"`
1170+
}
1171+
1172+
// Config implements the EIP-7910 eth_config method.
1173+
func (api *BlockChainAPI) Config(ctx context.Context) (*configResponse, error) {
1174+
genesis, err := api.b.HeaderByNumber(ctx, 0)
1175+
if err != nil {
1176+
return nil, fmt.Errorf("unable to load genesis: %w", err)
1177+
}
1178+
assemble := func(c *params.ChainConfig, ts *uint64) *config {
1179+
if ts == nil {
1180+
return nil
1181+
}
1182+
t := *ts
1183+
1184+
var (
1185+
rules = c.Rules(c.LondonBlock, true, t)
1186+
precompiles = make(map[string]common.Address)
1187+
)
1188+
for addr, c := range vm.ActivePrecompiledContracts(rules) {
1189+
precompiles[c.Name()] = addr
1190+
}
1191+
// Activation time is required. If a fork is activated at genesis the value 0 is used
1192+
activationTime := t
1193+
if genesis.Time >= t {
1194+
activationTime = 0
1195+
}
1196+
forkid := forkid.NewID(c, types.NewBlockWithHeader(genesis), ^uint64(0), t).Hash
1197+
return &config{
1198+
ActivationTime: activationTime,
1199+
BlobSchedule: c.BlobConfig(c.LatestFork(t)),
1200+
ChainId: (*hexutil.Big)(c.ChainID),
1201+
ForkId: forkid[:],
1202+
Precompiles: precompiles,
1203+
SystemContracts: c.ActiveSystemContracts(t),
1204+
}
1205+
}
1206+
var (
1207+
c = api.b.ChainConfig()
1208+
t = api.b.CurrentHeader().Time
1209+
)
1210+
resp := configResponse{
1211+
Next: assemble(c, c.Timestamp(c.LatestFork(t)+1)),
1212+
Current: assemble(c, c.Timestamp(c.LatestFork(t))),
1213+
Last: assemble(c, c.Timestamp(c.LatestFork(^uint64(0)))),
1214+
}
1215+
// Nil out last if no future-fork is configured.
1216+
if resp.Next == nil {
1217+
resp.Last = nil
1218+
}
1219+
return &resp, nil
1220+
}
1221+
11561222
// AccessList creates an access list for the given transaction.
11571223
// If the accesslist creation fails an error is returned.
11581224
// If the transaction itself fails, an vmErr is returned.

0 commit comments

Comments
 (0)