forked from cerc-io/plugeth
graphql: implement withdrawals (EIP-4895) (#27072)
implements withdrawals in graphql as per https://github.com/ethereum/execution-apis/pull/400
This commit is contained in:
+76
-5
@@ -28,6 +28,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/consensus"
|
||||
"github.com/ethereum/go-ethereum/consensus/beacon"
|
||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
@@ -67,7 +69,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
|
||||
GasLimit: 11500000,
|
||||
Difficulty: big.NewInt(1048576),
|
||||
}
|
||||
newGQLService(t, stack, genesis, 10, func(i int, gen *core.BlockGen) {})
|
||||
newGQLService(t, stack, false, genesis, 10, func(i int, gen *core.BlockGen) {})
|
||||
// start node
|
||||
if err := stack.Start(); err != nil {
|
||||
t.Fatalf("could not start node: %v", err)
|
||||
@@ -191,7 +193,7 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
|
||||
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||
}
|
||||
signer := types.LatestSigner(genesis.Config)
|
||||
newGQLService(t, stack, genesis, 1, func(i int, gen *core.BlockGen) {
|
||||
newGQLService(t, stack, false, genesis, 1, func(i int, gen *core.BlockGen) {
|
||||
gen.SetCoinbase(common.Address{1})
|
||||
tx, _ := types.SignNewTx(key, signer, &types.LegacyTx{
|
||||
Nonce: uint64(0),
|
||||
@@ -292,7 +294,7 @@ func TestGraphQLConcurrentResolvers(t *testing.T) {
|
||||
defer stack.Close()
|
||||
|
||||
var tx *types.Transaction
|
||||
handler, chain := newGQLService(t, stack, genesis, 1, func(i int, gen *core.BlockGen) {
|
||||
handler, chain := newGQLService(t, stack, false, genesis, 1, func(i int, gen *core.BlockGen) {
|
||||
tx, _ = types.SignNewTx(key, signer, &types.LegacyTx{To: &dad, Gas: 100000, GasPrice: big.NewInt(params.InitialBaseFee)})
|
||||
gen.AddTx(tx)
|
||||
tx, _ = types.SignNewTx(key, signer, &types.LegacyTx{To: &dad, Nonce: 1, Gas: 100000, GasPrice: big.NewInt(params.InitialBaseFee)})
|
||||
@@ -360,6 +362,66 @@ func TestGraphQLConcurrentResolvers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithdrawals(t *testing.T) {
|
||||
var (
|
||||
key, _ = crypto.GenerateKey()
|
||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
genesis = &core.Genesis{
|
||||
Config: params.AllEthashProtocolChanges,
|
||||
GasLimit: 11500000,
|
||||
Difficulty: common.Big1,
|
||||
Alloc: core.GenesisAlloc{
|
||||
addr: {Balance: big.NewInt(params.Ether)},
|
||||
},
|
||||
}
|
||||
signer = types.LatestSigner(genesis.Config)
|
||||
stack = createNode(t)
|
||||
)
|
||||
defer stack.Close()
|
||||
|
||||
handler, _ := newGQLService(t, stack, true, genesis, 1, func(i int, gen *core.BlockGen) {
|
||||
tx, _ := types.SignNewTx(key, signer, &types.LegacyTx{To: &common.Address{}, Gas: 100000, GasPrice: big.NewInt(params.InitialBaseFee)})
|
||||
gen.AddTx(tx)
|
||||
gen.AddWithdrawal(&types.Withdrawal{
|
||||
Validator: 5,
|
||||
Address: common.Address{},
|
||||
Amount: 10,
|
||||
})
|
||||
})
|
||||
// start node
|
||||
if err := stack.Start(); err != nil {
|
||||
t.Fatalf("could not start node: %v", err)
|
||||
}
|
||||
|
||||
for i, tt := range []struct {
|
||||
body string
|
||||
want string
|
||||
}{
|
||||
// Genesis block has no withdrawals.
|
||||
{
|
||||
body: "{block(number: 0) { withdrawalsRoot withdrawals { index } } }",
|
||||
want: `{"block":{"withdrawalsRoot":null,"withdrawals":null}}`,
|
||||
},
|
||||
{
|
||||
body: "{block(number: 1) { withdrawalsRoot withdrawals { validator amount } } }",
|
||||
want: `{"block":{"withdrawalsRoot":"0x8418fc1a48818928f6692f148e9b10e99a88edc093b095cb8ca97950284b553d","withdrawals":[{"validator":"0x5","amount":"0xa"}]}}`,
|
||||
},
|
||||
} {
|
||||
res := handler.Schema.Exec(context.Background(), tt.body, "", map[string]interface{}{})
|
||||
if res.Errors != nil {
|
||||
t.Fatalf("failed to execute query for testcase #%d: %v", i, res.Errors)
|
||||
}
|
||||
have, err := json.Marshal(res.Data)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to encode graphql response for testcase #%d: %s", i, err)
|
||||
}
|
||||
if string(have) != tt.want {
|
||||
t.Errorf("response unmatch for testcase #%d.\nhave:\n%s\nwant:\n%s", i, have, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func createNode(t *testing.T) *node.Node {
|
||||
stack, err := node.New(&node.Config{
|
||||
HTTPHost: "127.0.0.1",
|
||||
@@ -374,7 +436,7 @@ func createNode(t *testing.T) *node.Node {
|
||||
return stack
|
||||
}
|
||||
|
||||
func newGQLService(t *testing.T, stack *node.Node, gspec *core.Genesis, genBlocks int, genfunc func(i int, gen *core.BlockGen)) (*handler, []*types.Block) {
|
||||
func newGQLService(t *testing.T, stack *node.Node, shanghai bool, gspec *core.Genesis, genBlocks int, genfunc func(i int, gen *core.BlockGen)) (*handler, []*types.Block) {
|
||||
ethConf := ðconfig.Config{
|
||||
Genesis: gspec,
|
||||
NetworkId: 1337,
|
||||
@@ -389,9 +451,18 @@ func newGQLService(t *testing.T, stack *node.Node, gspec *core.Genesis, genBlock
|
||||
if err != nil {
|
||||
t.Fatalf("could not create eth backend: %v", err)
|
||||
}
|
||||
var engine consensus.Engine = ethash.NewFaker()
|
||||
if shanghai {
|
||||
engine = beacon.NewFaker()
|
||||
chainCfg := gspec.Config
|
||||
chainCfg.TerminalTotalDifficultyPassed = true
|
||||
chainCfg.TerminalTotalDifficulty = common.Big0
|
||||
shanghaiTime := uint64(0)
|
||||
chainCfg.ShanghaiTime = &shanghaiTime
|
||||
}
|
||||
// Create some blocks and import them
|
||||
chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
|
||||
ethash.NewFaker(), ethBackend.ChainDb(), genBlocks, genfunc)
|
||||
engine, ethBackend.ChainDb(), genBlocks, genfunc)
|
||||
_, err = ethBackend.BlockChain().InsertChain(chain)
|
||||
if err != nil {
|
||||
t.Fatalf("could not create import blocks: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user