From 204bedcf1da021fbe5fd4eccbe62543cf873362a Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 15:51:43 +0800
Subject: [PATCH 01/14] cmd/devp2p: fix eth protocol tests
---
cmd/devp2p/internal/ethtest/chain_test.go | 154 ++++++++++------------
cmd/devp2p/internal/ethtest/conn.go | 99 ++++++++++----
cmd/devp2p/internal/ethtest/protocol.go | 30 ++++-
cmd/devp2p/internal/ethtest/snap2.go | 13 +-
cmd/devp2p/internal/ethtest/suite.go | 31 +++--
cmd/devp2p/internal/ethtest/suite_test.go | 83 +++++++-----
6 files changed, 253 insertions(+), 157 deletions(-)
diff --git a/cmd/devp2p/internal/ethtest/chain_test.go b/cmd/devp2p/internal/ethtest/chain_test.go
index 74a181eede..ec4ea4c020 100644
--- a/cmd/devp2p/internal/ethtest/chain_test.go
+++ b/cmd/devp2p/internal/ethtest/chain_test.go
@@ -27,108 +27,100 @@ import (
"github.com/stretchr/testify/assert"
)
-// TestEthProtocolNegotiation tests whether the test suite
-// can negotiate the highest eth protocol in a status message exchange
+// TestEthProtocolNegotiation checks that version gaps do not create a common capability.
func TestEthProtocolNegotiation(t *testing.T) {
t.Parallel()
- var tests = []struct {
- conn *Conn
- caps []p2p.Cap
- expected uint32
+ for _, test := range []struct {
+ name string
+ local, remote []p2p.Cap
+ ethVersion, snapVersion uint
}{
{
- conn: &Conn{
- ourHighestProtoVersion: 65,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "eth", Version: 65},
- },
- expected: uint32(65),
+ name: "prefer71",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "eth", Version: 68}},
+ remote: []p2p.Cap{{Name: "eth", Version: 68}, {Name: "eth", Version: 71}},
+ ethVersion: 71,
},
{
- conn: &Conn{
- ourHighestProtoVersion: 65,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "eth", Version: 65},
- },
- expected: uint32(65),
+ name: "fallback68",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "eth", Version: 68}},
+ remote: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 68}},
+ ethVersion: 68,
},
{
- conn: &Conn{
- ourHighestProtoVersion: 65,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "eth", Version: 65},
- },
- expected: uint32(65),
+ name: "only71",
+ local: []p2p.Cap{{Name: "eth", Version: 71}},
+ remote: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 71}},
+ ethVersion: 71,
},
{
- conn: &Conn{
- ourHighestProtoVersion: 64,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "eth", Version: 65},
- },
- expected: 64,
+ name: "noCommonVersion",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "eth", Version: 68}},
+ remote: []p2p.Cap{{Name: "eth", Version: 70}},
},
{
- conn: &Conn{
- ourHighestProtoVersion: 65,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 0},
- {Name: "eth", Version: 89},
- {Name: "eth", Version: 65},
- },
- expected: uint32(65),
+ name: "only71Rejects68",
+ local: []p2p.Cap{{Name: "eth", Version: 71}},
+ remote: []p2p.Cap{{Name: "eth", Version: 68}},
},
{
- conn: &Conn{
- ourHighestProtoVersion: 64,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "wrongProto", Version: 65},
- },
- expected: uint32(64),
+ name: "ignore72",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "eth", Version: 68}},
+ remote: []p2p.Cap{{Name: "eth", Version: 72}, {Name: "eth", Version: 68}},
+ ethVersion: 68,
},
{
- conn: &Conn{
- ourHighestProtoVersion: 65,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: 63},
- {Name: "eth", Version: 64},
- {Name: "wrongProto", Version: 65},
- },
- expected: uint32(64),
+ name: "current70",
+ local: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 68}},
+ remote: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 68}},
+ ethVersion: 70,
},
{
- conn: &Conn{
- ourHighestProtoVersion: eth.ETH70,
- },
- caps: []p2p.Cap{
- {Name: "eth", Version: eth.ETH68},
- {Name: "eth", Version: eth.ETH70},
- },
- expected: eth.ETH70,
+ name: "snap1",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "snap", Version: 1}},
+ remote: []p2p.Cap{{Name: "snap", Version: 2}, {Name: "eth", Version: 71}, {Name: "snap", Version: 1}},
+ ethVersion: 71,
+ snapVersion: 1,
},
+ {
+ name: "snap2",
+ local: []p2p.Cap{{Name: "eth", Version: 71}, {Name: "snap", Version: 1}, {Name: "snap", Version: 2}},
+ remote: []p2p.Cap{{Name: "snap", Version: 2}, {Name: "eth", Version: 71}, {Name: "snap", Version: 1}},
+ ethVersion: 71,
+ snapVersion: 2,
+ },
+ {
+ name: "noCommonSnap",
+ local: []p2p.Cap{{Name: "eth", Version: 68}, {Name: "snap", Version: 2}},
+ remote: []p2p.Cap{{Name: "eth", Version: 68}, {Name: "snap", Version: 1}},
+ ethVersion: 68,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ conn := &Conn{caps: test.local}
+ conn.negotiateEthProtocol(test.remote)
+ assert.Equal(t, test.ethVersion, conn.negotiatedProtoVersion)
+ assert.Equal(t, test.snapVersion, conn.negotiatedSnapProtoVersion)
+ })
}
+}
- for i, tt := range tests {
- t.Run(strconv.Itoa(i), func(t *testing.T) {
- tt.conn.negotiateEthProtocol(tt.caps)
- assert.Equal(t, tt.expected, uint32(tt.conn.negotiatedProtoVersion))
+func TestProtocolOffsets(t *testing.T) {
+ for _, test := range []struct {
+ version uint
+ length uint64
+ }{
+ {eth.ETH68, 17}, {eth.ETH70, 18}, {eth.ETH71, 20}, {eth.ETH72, 22},
+ } {
+ t.Run(strconv.Itoa(int(test.version)), func(t *testing.T) {
+ conn := &Conn{negotiatedProtoVersion: test.version}
+ assert.Equal(t, uint64(16), conn.protoOffset(ethProto))
+ assert.Equal(t, 16+test.length, conn.protoOffset(snapProto))
+ assert.Equal(t, baseProto, conn.getProto(15))
+ assert.Equal(t, ethProto, conn.getProto(16))
+ assert.Equal(t, ethProto, conn.getProto(15+test.length))
+ assert.Equal(t, snapProto, conn.getProto(16+test.length))
+ assert.Equal(t, snapProto, conn.getProto(25+test.length))
})
}
}
diff --git a/cmd/devp2p/internal/ethtest/conn.go b/cmd/devp2p/internal/ethtest/conn.go
index 5995ff4464..c9fb00e4d7 100644
--- a/cmd/devp2p/internal/ethtest/conn.go
+++ b/cmd/devp2p/internal/ethtest/conn.go
@@ -22,6 +22,7 @@ import (
"fmt"
"net"
"reflect"
+ "slices"
"time"
"github.com/davecgh/go-spew/spew"
@@ -66,15 +67,13 @@ func (s *Suite) dialAs(key *ecdsa.PrivateKey) (*Conn, error) {
conn.Close()
return nil, err
}
- // TODO(BSC): Enable ETH/71 after adapting the handshake and block propagation
- // handlers, and testing sync, v0 blob relay, unavailable BAL responses and
- // ETH/70 and ETH/68 fallback. Update the node's ProtocolVersions, these caps
- // and ourHighestProtoVersion together when enabling it.
conn.caps = []p2p.Cap{
- {Name: "eth", Version: eth.ETH70},
+ {Name: "eth", Version: eth.ETH71},
{Name: "eth", Version: eth.ETH68},
}
- conn.ourHighestProtoVersion = eth.ETH70
+ if s.ethVersion != 0 {
+ conn.caps = []p2p.Cap{{Name: "eth", Version: s.ethVersion}}
+ }
return &conn, nil
}
@@ -111,7 +110,6 @@ func (s *Suite) dialEth71() (*Conn, error) {
return nil, fmt.Errorf("dial failed: %v", err)
}
conn.caps = []p2p.Cap{{Name: "eth", Version: eth.ETH71}}
- conn.ourHighestProtoVersion = eth.ETH71
return conn, nil
}
@@ -121,7 +119,6 @@ type Conn struct {
ourKey *ecdsa.PrivateKey
negotiatedProtoVersion uint
negotiatedSnapProtoVersion uint
- ourHighestProtoVersion uint
ourHighestSnapProtoVersion uint
caps []p2p.Cap
@@ -148,7 +145,7 @@ func (c *Conn) ReadMsg(proto Proto, code uint64, msg any) error {
if err != nil {
return err
}
- if protoOffset(proto)+code == got {
+ if c.protoOffset(proto)+code == got {
return rlp.DecodeBytes(data, msg)
}
}
@@ -161,7 +158,7 @@ func (c *Conn) Write(proto Proto, code uint64, msg any) error {
if err != nil {
return err
}
- _, err = c.Conn.Write(protoOffset(proto)+code, payload)
+ _, err = c.Conn.Write(c.protoOffset(proto)+code, payload)
return err
}
@@ -192,7 +189,7 @@ func (c *Conn) ReadEth() (any, error) {
c.Write(baseProto, pongMsg, []byte{})
continue
}
- if getProto(code) != ethProto {
+ if c.getProto(code) != ethProto {
// Read until eth message.
continue
}
@@ -253,11 +250,20 @@ func (c *Conn) ReadSnap() (any, error) {
if err != nil {
return nil, err
}
- if getProto(code) != snapProto {
+ if code == discMsg {
+ return nil, errDisc
+ }
+ if code == pingMsg {
+ if err := c.Write(baseProto, pongMsg, []byte{}); err != nil {
+ return nil, err
+ }
+ continue
+ }
+ if c.getProto(code) != snapProto {
// Read until snap message.
continue
}
- code -= protoOffset(snapProto)
+ code -= c.protoOffset(snapProto)
var msg any
switch int(code) {
@@ -364,7 +370,7 @@ func (c *Conn) handshake() error {
}
c.negotiateEthProtocol(msg.Caps)
if c.negotiatedProtoVersion == 0 {
- return fmt.Errorf("could not negotiate eth protocol (remote caps: %v, local eth version: %v)", msg.Caps, c.ourHighestProtoVersion)
+ return fmt.Errorf("could not negotiate eth protocol (remote caps: %v, local caps: %v)", msg.Caps, c.caps)
}
// If we require snap, verify that it was negotiated.
if c.ourHighestSnapProtoVersion != c.negotiatedSnapProtoVersion {
@@ -376,25 +382,25 @@ func (c *Conn) handshake() error {
}
}
-// negotiateEthProtocol sets the Conn's eth protocol version to highest
-// advertised capability from peer.
+// negotiateEthProtocol selects the highest capability advertised by both peers.
func (c *Conn) negotiateEthProtocol(caps []p2p.Cap) {
- var highestEthVersion uint
- var highestSnapVersion uint
+ c.negotiatedProtoVersion = 0
+ c.negotiatedSnapProtoVersion = 0
for _, capability := range caps {
+ if !slices.Contains(c.caps, capability) {
+ continue
+ }
switch capability.Name {
case "eth":
- if capability.Version > highestEthVersion && capability.Version <= c.ourHighestProtoVersion {
- highestEthVersion = capability.Version
+ if capability.Version > c.negotiatedProtoVersion {
+ c.negotiatedProtoVersion = capability.Version
}
case "snap":
- if capability.Version > highestSnapVersion && capability.Version <= c.ourHighestSnapProtoVersion {
- highestSnapVersion = capability.Version
+ if capability.Version > c.negotiatedSnapProtoVersion {
+ c.negotiatedSnapProtoVersion = capability.Version
}
}
}
- c.negotiatedProtoVersion = highestEthVersion
- c.negotiatedSnapProtoVersion = highestSnapVersion
}
// statusExchange performs a `Status` message exchange with the given node.
@@ -405,13 +411,16 @@ func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket) error {
return fmt.Errorf("failed to read from connection: %w", err)
}
switch code {
- case eth.StatusMsg + protoOffset(ethProto):
+ case eth.StatusMsg + c.protoOffset(ethProto):
if c.negotiatedProtoVersion == 0 {
return errors.New("eth protocol version must be set in Conn")
}
+ if c.negotiatedProtoVersion == eth.ETH68 {
+ return c.statusExchange68(chain, data, status)
+ }
msg := new(eth.StatusPacket)
if err := rlp.DecodeBytes(data, msg); err != nil {
- return fmt.Errorf("error decoding eth/70 status packet: %w", err)
+ return fmt.Errorf("error decoding status packet: %w", err)
}
if uint(msg.ProtocolVersion) != c.negotiatedProtoVersion {
return fmt.Errorf("mismatched eth protocol version: node %d, negotiated %d", msg.ProtocolVersion, c.negotiatedProtoVersion)
@@ -460,3 +469,41 @@ func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket) error {
}
}
}
+
+// statusExchange68 retains the BSC UpgradeStatus exchange used by legacy peers.
+func (c *Conn) statusExchange68(chain *Chain, data []byte, status *eth.StatusPacket) error {
+ var remote eth.StatusPacket68
+ if err := rlp.DecodeBytes(data, &remote); err != nil {
+ return fmt.Errorf("error decoding eth/68 status packet: %w", err)
+ }
+ if remote.ProtocolVersion != eth.ETH68 || remote.NetworkID != chain.config.ChainID.Uint64() ||
+ remote.Genesis != chain.blocks[0].Hash() || remote.Head != chain.Head().Hash() ||
+ remote.TD == nil || remote.TD.Cmp(chain.TD()) != 0 || remote.ForkID != chain.ForkID() {
+ return fmt.Errorf("unexpected eth/68 status: %+v", remote)
+ }
+ local := eth.StatusPacket68{
+ ProtocolVersion: eth.ETH68,
+ NetworkID: chain.config.ChainID.Uint64(),
+ TD: chain.TD(),
+ Head: chain.Head().Hash(),
+ Genesis: chain.blocks[0].Hash(),
+ ForkID: chain.ForkID(),
+ }
+ if status != nil {
+ local = eth.StatusPacket68{
+ ProtocolVersion: status.ProtocolVersion, NetworkID: status.NetworkID,
+ TD: status.TD, Head: status.LatestBlockHash, Genesis: status.Genesis, ForkID: status.ForkID,
+ }
+ }
+ if err := c.Write(ethProto, eth.StatusMsg, &local); err != nil {
+ return err
+ }
+ var upgrade eth.UpgradeStatusPacket
+ if err := c.ReadMsg(ethProto, eth.UpgradeStatusMsg, &upgrade); err != nil {
+ return fmt.Errorf("read upgrade status: %w", err)
+ }
+ if _, err := upgrade.GetExtension(); err != nil {
+ return fmt.Errorf("decode upgrade status: %w", err)
+ }
+ return c.Write(ethProto, eth.UpgradeStatusMsg, ð.UpgradeStatusPacket{})
+}
diff --git a/cmd/devp2p/internal/ethtest/protocol.go b/cmd/devp2p/internal/ethtest/protocol.go
index 28c7767726..e93b958fbc 100644
--- a/cmd/devp2p/internal/ethtest/protocol.go
+++ b/cmd/devp2p/internal/ethtest/protocol.go
@@ -17,6 +17,7 @@
package ethtest
import (
+ "github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
)
@@ -32,7 +33,6 @@ const (
// Unexported devp2p protocol lengths from p2p package.
const (
baseProtoLen = 16
- ethProtoLen = 22
// snapProtoLen accommodates snap/2 (EIP-8189) which extends snap/1 with two
// additional message codes (GetBlockAccessLists=0x08, BlockAccessLists=0x09).
// Using 10 is safe for snap/1 connections because the extra codes are simply
@@ -63,10 +63,12 @@ const (
// getProto returns the protocol a certain message code is associated with
// (assuming the negotiated capabilities are exactly {eth,snap})
-func getProto(code uint64) Proto {
- switch {
- case code < baseProtoLen:
+func (c *Conn) getProto(code uint64) Proto {
+ if code < baseProtoLen {
return baseProto
+ }
+ ethProtoLen := c.ethProtoLen()
+ switch {
case code < baseProtoLen+ethProtoLen:
return ethProto
case code < baseProtoLen+ethProtoLen+snapProtoLen:
@@ -78,15 +80,31 @@ func getProto(code uint64) Proto {
// protoOffset will return the offset at which the specified protocol's messages
// begin.
-func protoOffset(proto Proto) uint64 {
+func (c *Conn) protoOffset(proto Proto) uint64 {
switch proto {
case baseProto:
return 0
case ethProto:
return baseProtoLen
case snapProto:
- return baseProtoLen + ethProtoLen
+ return baseProtoLen + c.ethProtoLen()
default:
panic("unhandled protocol")
}
}
+
+// ethProtoLen determines where snap messages begin for the negotiated eth version.
+func (c *Conn) ethProtoLen() uint64 {
+ switch c.negotiatedProtoVersion {
+ case eth.ETH68:
+ return 17
+ case eth.ETH70:
+ return 18
+ case eth.ETH71:
+ return 20
+ case eth.ETH72:
+ return 22
+ default:
+ panic("unsupported eth protocol version")
+ }
+}
diff --git a/cmd/devp2p/internal/ethtest/snap2.go b/cmd/devp2p/internal/ethtest/snap2.go
index 38e09024ee..f79989f6f4 100644
--- a/cmd/devp2p/internal/ethtest/snap2.go
+++ b/cmd/devp2p/internal/ethtest/snap2.go
@@ -17,7 +17,9 @@
package ethtest
import (
+ "errors"
"fmt"
+ "io"
"math/rand"
"github.com/ethereum/go-ethereum/common"
@@ -239,16 +241,15 @@ is removed in snap/2.`)
t.Fatalf("failed to write GetTrieNodes: %v", err)
}
- // We expect either a disconnect or a read error/timeout. We must NOT
- // receive a valid TrieNodes response. Loop a few times to consume any
- // incidental messages the peer might send (e.g. block updates) before
- // deciding.
+ // A timeout is not evidence that the peer rejected the removed message.
for i := 0; i < 5; i++ {
msg, err := conn.ReadSnap()
- if err != nil {
- // Disconnect or read error — the peer rejected the request.
+ if errors.Is(err, errDisc) || errors.Is(err, io.EOF) {
return
}
+ if err != nil {
+ t.Fatalf("expected disconnect after GetTrieNodes, got: %v", err)
+ }
if _, ok := msg.(*snap.TrieNodesPacket); ok {
t.Fatal("peer responded with TrieNodes over snap/2; GetTrieNodes must be unsupported")
}
diff --git a/cmd/devp2p/internal/ethtest/suite.go b/cmd/devp2p/internal/ethtest/suite.go
index c54d9d2c99..a021cbe2dd 100644
--- a/cmd/devp2p/internal/ethtest/suite.go
+++ b/cmd/devp2p/internal/ethtest/suite.go
@@ -21,6 +21,7 @@ import (
"crypto/rand"
"errors"
"fmt"
+ "io"
"os"
"reflect"
"slices"
@@ -48,6 +49,9 @@ type Suite struct {
chain *Chain
engine *EngineClient
+ // ethVersion forces a single eth capability for in-process protocol tests.
+ ethVersion uint
+
// requireAvailableBALs is enabled only by the in-process geth tests.
// External conformance runs must continue accepting unavailable BALs.
requireAvailableBALs bool
@@ -593,8 +597,11 @@ func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
var (
key, _ = crypto.GenerateKey()
pub0 = crypto.FromECDSAPub(&key.PublicKey)[1:]
- version = eth.ProtocolVersions[0]
+ version = s.ethVersion
)
+ if version == 0 {
+ version = eth.ETH71
+ }
handshakes := []*protoHandshake{
{
Version: 5,
@@ -639,24 +646,32 @@ func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
}
defer conn.Close()
- if err := conn.Write(ethProto, handshakeMsg, handshake); err != nil {
+ if err := conn.Write(baseProto, handshakeMsg, handshake); err != nil {
t.Fatalf("could not write to connection: %v", err)
}
- // Check that the peer disconnected
+ // Allow the concurrently sent Hello before the disconnect.
+ disconnected := false
for i := 0; i < 2; i++ {
code, _, err := conn.Read()
+ if errors.Is(err, io.EOF) {
+ disconnected = true
+ break
+ }
if err != nil {
- // Client may have disconnected without sending disconnect msg.
- continue
+ t.Fatalf("expected disconnect after malicious handshake: %v", err)
}
switch code {
case discMsg:
+ disconnected = true
case handshakeMsg:
- // Discard one hello as Hello's are sent concurrently
continue
default:
t.Fatalf("unexpected msg: code %d", code)
}
+ break
+ }
+ if !disconnected {
+ t.Fatal("peer did not reject malicious handshake")
}
}
}
@@ -1145,8 +1160,8 @@ func (s *Suite) TestBlobViolations(t *utesting.T) {
break
}
switch code {
- case protoOffset(ethProto) + eth.NewPooledTransactionHashesMsg,
- protoOffset(ethProto) + eth.GetCellsMsg:
+ case conn.protoOffset(ethProto) + eth.NewPooledTransactionHashesMsg,
+ conn.protoOffset(ethProto) + eth.GetCellsMsg:
continue
default:
t.Fatalf("expected disconnect on blob violation, got msg code: %d", code)
diff --git a/cmd/devp2p/internal/ethtest/suite_test.go b/cmd/devp2p/internal/ethtest/suite_test.go
index 393da50e8f..aa2b647947 100644
--- a/cmd/devp2p/internal/ethtest/suite_test.go
+++ b/cmd/devp2p/internal/ethtest/suite_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see .
-package ethtest // TOFIX
+package ethtest
import (
crand "crypto/rand"
@@ -48,15 +48,21 @@ func makeJWTSecret(t *testing.T) (string, [32]byte, error) {
return jwtPath, secret, nil
}
-func TestEthSuite(t *testing.T) {
- testEthSuite(t, ethproto.ETH72)
-}
+// Keep eth/70 coverage until the production protocol list drops it.
+var suiteEthVersions = []uint{ethproto.ETH71, ethproto.ETH68, ethproto.ETH70}
-func TestEthSuiteEth71(t *testing.T) {
- testEthSuite(t, ethproto.ETH71)
+func TestEthSuite(t *testing.T) {
+ for _, version := range suiteEthVersions {
+ t.Run(fmt.Sprintf("eth%d", version), func(t *testing.T) {
+ testEthSuite(t, version)
+ })
+ }
}
func testEthSuite(t *testing.T, ethVersion uint) {
+ if !slices.Contains(ethproto.ProtocolVersions, ethVersion) {
+ t.Skipf("eth/%d is not enabled in the node", ethVersion)
+ }
jwtPath, secret, err := makeJWTSecret(t)
if err != nil {
t.Fatalf("could not make jwt secret: %v", err)
@@ -71,6 +77,10 @@ func testEthSuite(t *testing.T, ethVersion uint) {
if err != nil {
t.Fatalf("could not create new test suite: %v", err)
}
+ // The default 71+68 capabilities must negotiate either target version.
+ if ethVersion == ethproto.ETH70 {
+ suite.ethVersion = ethVersion
+ }
conn, err := suite.dialAndPeer(nil)
if err != nil {
t.Fatalf("could not peer with test node: %v", err)
@@ -79,9 +89,24 @@ func testEthSuite(t *testing.T, ethVersion uint) {
if conn.negotiatedProtoVersion != ethVersion {
t.Fatalf("negotiated eth/%d, want eth/%d", conn.negotiatedProtoVersion, ethVersion)
}
+ suite.ethVersion = ethVersion
suite.requireAvailableBALs = true
for _, test := range suite.EthTests() {
t.Run(test.Name, func(t *testing.T) {
+ switch test.Name {
+ case "GetBlockAccessLists":
+ if ethVersion < ethproto.ETH71 {
+ t.Skip("BAL exchange requires eth/71")
+ }
+ case "GetLargeReceipts":
+ if ethVersion < ethproto.ETH70 {
+ t.Skip("receipt pagination requires eth/70")
+ }
+ case "BlobTxAvailabilityFailure", "GetCells", "BlobTxWithInvalidCells":
+ if ethVersion < ethproto.ETH72 {
+ t.Skip("cell exchange requires eth/72")
+ }
+ }
if test.Slow && testing.Short() {
t.Skipf("%s: skipping in -short mode", test.Name)
}
@@ -94,47 +119,45 @@ func testEthSuite(t *testing.T, ethVersion uint) {
}
func TestSnapSuite(t *testing.T) {
- jwtPath, secret, err := makeJWTSecret(t)
- if err != nil {
- t.Fatalf("could not make jwt secret: %v", err)
- }
- geth, err := runGeth("./testdata", jwtPath, ethproto.ETH72)
- if err != nil {
- t.Fatalf("could not run geth: %v", err)
+ for _, version := range suiteEthVersions {
+ t.Run(fmt.Sprintf("eth%d", version), func(t *testing.T) {
+ testSnapSuite(t, version, false)
+ })
}
- defer geth.Close()
+}
- suite, err := NewSuite(geth.Server().Self(), "./testdata", geth.HTTPAuthEndpoint(), common.Bytes2Hex(secret[:]))
- if err != nil {
- t.Fatalf("could not create new test suite: %v", err)
- }
- for _, test := range suite.SnapTests() {
- t.Run(test.Name, func(t *testing.T) {
- result := utesting.RunTests([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
- if result[0].Failed {
- t.Fatal()
- }
+func TestSnap2Suite(t *testing.T) {
+ for _, version := range suiteEthVersions {
+ t.Run(fmt.Sprintf("eth%d", version), func(t *testing.T) {
+ testSnapSuite(t, version, true)
})
}
}
-func TestSnap2Suite(t *testing.T) {
+func testSnapSuite(t *testing.T, ethVersion uint, snap2 bool) {
+ if !slices.Contains(ethproto.ProtocolVersions, ethVersion) {
+ t.Skipf("eth/%d is not enabled in the node", ethVersion)
+ }
jwtPath, secret, err := makeJWTSecret(t)
if err != nil {
t.Fatalf("could not make jwt secret: %v", err)
}
- geth, err := runGeth("./testdata", jwtPath, ethproto.ETH72)
+ geth, err := runGeth("./testdata", jwtPath, ethVersion)
if err != nil {
t.Fatalf("could not run geth: %v", err)
}
defer geth.Close()
-
suite, err := NewSuite(geth.Server().Self(), "./testdata", geth.HTTPAuthEndpoint(), common.Bytes2Hex(secret[:]))
if err != nil {
t.Fatalf("could not create new test suite: %v", err)
}
- suite.requireAvailableBALs = true
- for _, test := range suite.Snap2Tests() {
+ suite.ethVersion = ethVersion
+ tests := suite.SnapTests()
+ if snap2 {
+ suite.requireAvailableBALs = true
+ tests = suite.Snap2Tests()
+ }
+ for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
result := utesting.RunTests([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
if result[0].Failed {
@@ -168,7 +191,7 @@ func runGeth(dir string, jwtPath string, ethVersion uint) (*node.Node, error) {
return nil, err
}
stack.Server().Protocols = slices.DeleteFunc(stack.Server().Protocols, func(protocol p2p.Protocol) bool {
- return protocol.Name == "eth" && protocol.Version > ethVersion
+ return protocol.Name == "eth" && protocol.Version != ethVersion
})
if err = stack.Start(); err != nil {
stack.Close()
From 13e8eff0100457bbb15178d4e6750f44515fc058 Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 16:36:49 +0800
Subject: [PATCH 02/14] eth: enable eth/71 with TD
---
cmd/devp2p/internal/ethtest/chain_test.go | 2 +-
cmd/devp2p/internal/ethtest/suite_test.go | 12 +++----
eth/downloader/downloader_test.go | 13 ++++---
eth/handler_eth_test.go | 17 ++++++---
eth/protocols/eth/handler.go | 3 ++
eth/protocols/eth/handler_test.go | 43 ++++++++++++++---------
eth/protocols/eth/handshake.go | 2 +-
eth/protocols/eth/handshake_test.go | 35 ++++++++++++++++--
eth/protocols/eth/protocol.go | 7 ++--
eth/sync_test.go | 15 +++++---
10 files changed, 103 insertions(+), 46 deletions(-)
diff --git a/cmd/devp2p/internal/ethtest/chain_test.go b/cmd/devp2p/internal/ethtest/chain_test.go
index ec4ea4c020..7f5740e4bc 100644
--- a/cmd/devp2p/internal/ethtest/chain_test.go
+++ b/cmd/devp2p/internal/ethtest/chain_test.go
@@ -70,7 +70,7 @@ func TestEthProtocolNegotiation(t *testing.T) {
ethVersion: 68,
},
{
- name: "current70",
+ name: "eth70",
local: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 68}},
remote: []p2p.Cap{{Name: "eth", Version: 70}, {Name: "eth", Version: 68}},
ethVersion: 70,
diff --git a/cmd/devp2p/internal/ethtest/suite_test.go b/cmd/devp2p/internal/ethtest/suite_test.go
index aa2b647947..aa4a83a6be 100644
--- a/cmd/devp2p/internal/ethtest/suite_test.go
+++ b/cmd/devp2p/internal/ethtest/suite_test.go
@@ -48,8 +48,7 @@ func makeJWTSecret(t *testing.T) (string, [32]byte, error) {
return jwtPath, secret, nil
}
-// Keep eth/70 coverage until the production protocol list drops it.
-var suiteEthVersions = []uint{ethproto.ETH71, ethproto.ETH68, ethproto.ETH70}
+var suiteEthVersions = []uint{ethproto.ETH71, ethproto.ETH68}
func TestEthSuite(t *testing.T) {
for _, version := range suiteEthVersions {
@@ -61,7 +60,7 @@ func TestEthSuite(t *testing.T) {
func testEthSuite(t *testing.T, ethVersion uint) {
if !slices.Contains(ethproto.ProtocolVersions, ethVersion) {
- t.Skipf("eth/%d is not enabled in the node", ethVersion)
+ t.Fatalf("required protocol eth/%d is not enabled in the node", ethVersion)
}
jwtPath, secret, err := makeJWTSecret(t)
if err != nil {
@@ -77,10 +76,7 @@ func testEthSuite(t *testing.T, ethVersion uint) {
if err != nil {
t.Fatalf("could not create new test suite: %v", err)
}
- // The default 71+68 capabilities must negotiate either target version.
- if ethVersion == ethproto.ETH70 {
- suite.ethVersion = ethVersion
- }
+ // The default capabilities must negotiate the requested version.
conn, err := suite.dialAndPeer(nil)
if err != nil {
t.Fatalf("could not peer with test node: %v", err)
@@ -136,7 +132,7 @@ func TestSnap2Suite(t *testing.T) {
func testSnapSuite(t *testing.T, ethVersion uint, snap2 bool) {
if !slices.Contains(ethproto.ProtocolVersions, ethVersion) {
- t.Skipf("eth/%d is not enabled in the node", ethVersion)
+ t.Fatalf("required protocol eth/%d is not enabled in the node", ethVersion)
}
jwtPath, secret, err := makeJWTSecret(t)
if err != nil {
diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go
index 51b036e0c7..7a52a2d2e7 100644
--- a/eth/downloader/downloader_test.go
+++ b/eth/downloader/downloader_test.go
@@ -624,8 +624,10 @@ func assertOwnChain(t *testing.T, tester *downloadTester, length int) {
}
}
-func TestCanonicalSynchronisationFull(t *testing.T) { testCanonSync(t, eth.ETH70, FullSync, false) }
-func TestCanonicalSynchronisationSnap(t *testing.T) { testCanonSync(t, eth.ETH70, SnapSync, false) }
+func TestCanonicalSynchronisationFull(t *testing.T) { testCanonSync(t, eth.ETH70, FullSync, false) }
+func TestCanonicalSynchronisationSnap(t *testing.T) { testCanonSync(t, eth.ETH70, SnapSync, false) }
+func TestCanonicalSynchronisationFull71(t *testing.T) { testCanonSync(t, eth.ETH71, FullSync, false) }
+func TestCanonicalSynchronisationSnap71(t *testing.T) { testCanonSync(t, eth.ETH71, SnapSync, false) }
func testCanonSync(t *testing.T, protocol uint, mode SyncMode, snapV2 bool) {
success := make(chan struct{})
@@ -756,9 +758,10 @@ func makeBALChain(n int) (*core.Genesis, []*types.Block) {
// and end up attached to the imported blocks; and that syncing against peers
// which cannot (or do not) serve access lists still completes, importing the
// blocks without them.
-func TestBALSynchronisationFull(t *testing.T) { testBALSync(t, FullSync, eth.ETH71) }
-func TestBALSynchronisationSnap(t *testing.T) { testBALSync(t, SnapSync, eth.ETH71) }
-func TestBALSynchronisationLegacyPeer(t *testing.T) { testBALSync(t, FullSync, eth.ETH70) }
+func TestBALSynchronisationFull(t *testing.T) { testBALSync(t, FullSync, eth.ETH71) }
+func TestBALSynchronisationSnap(t *testing.T) { testBALSync(t, SnapSync, eth.ETH71) }
+func TestBALSynchronisationLegacyPeer(t *testing.T) { testBALSync(t, FullSync, eth.ETH70) }
+func TestBALSynchronisationLegacyPeer68(t *testing.T) { testBALSync(t, FullSync, eth.ETH68) }
func testBALSync(t *testing.T, mode SyncMode, protocol uint) {
gspec, blocks := makeBALChain(96) // long enough for a snap sync pivot below the head
diff --git a/eth/handler_eth_test.go b/eth/handler_eth_test.go
index cdf8cd60de..a58d94e84b 100644
--- a/eth/handler_eth_test.go
+++ b/eth/handler_eth_test.go
@@ -89,6 +89,7 @@ func (h *testEthHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
// Tests that peers are correctly accepted (or rejected) based on the advertised
// fork IDs in the protocol handshake.
func TestForkIDSplit68(t *testing.T) { testForkIDSplit(t, eth.ETH68) }
+func TestForkIDSplit71(t *testing.T) { testForkIDSplit(t, eth.ETH71) }
func testForkIDSplit(t *testing.T, protocol uint) {
t.Parallel()
@@ -468,6 +469,7 @@ func testSendTransactions(t *testing.T, protocol uint) {
// Tests that transactions get propagated to all attached peers, either via direct
// broadcasts or via announcements/retrievals.
func TestTransactionPropagation68(t *testing.T) { testTransactionPropagation(t, eth.ETH68) }
+func TestTransactionPropagation71(t *testing.T) { testTransactionPropagation(t, eth.ETH71) }
func testTransactionPropagation(t *testing.T, protocol uint) {
t.Parallel()
@@ -546,7 +548,13 @@ func TestBroadcastBlock16Peers(t *testing.T) { testBroadcastBlock(t, 16, 4) }
func TestBroadcastBloc26Peers(t *testing.T) { testBroadcastBlock(t, 26, 5) }
func TestBroadcastBlock100Peers(t *testing.T) { testBroadcastBlock(t, 100, 10) }
+func TestBroadcastBlock71(t *testing.T) { testBroadcastBlockProtocol(t, eth.ETH71, 1, 1) }
+
func testBroadcastBlock(t *testing.T, peers, bcasts int) {
+ testBroadcastBlockProtocol(t, eth.ETH68, peers, bcasts)
+}
+
+func testBroadcastBlockProtocol(t *testing.T, protocol uint, peers, bcasts int) {
t.Parallel()
// Create a source handler to broadcast blocks from and a number of sinks
@@ -568,8 +576,8 @@ func testBroadcastBlock(t *testing.T, peers, bcasts int) {
defer sourcePipe.Close()
defer sinkPipe.Close()
- sourcePeer := eth.NewPeer(eth.ETH68, p2p.NewPeerPipe(enode.ID{byte(i)}, "", nil, sourcePipe), sourcePipe, nil, nil, nil)
- sinkPeer := eth.NewPeer(eth.ETH68, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, nil, nil, nil)
+ sourcePeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{byte(i)}, "", nil, sourcePipe), sourcePipe, nil, nil, nil)
+ sinkPeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, nil, nil, nil)
defer sourcePeer.Close()
defer sinkPeer.Close()
@@ -579,7 +587,7 @@ func testBroadcastBlock(t *testing.T, peers, bcasts int) {
// Wait a bit for the above handlers to start
time.Sleep(100 * time.Millisecond)
- if err := sinkPeer.Handshake(1, source.chain, eth.BlockRangeUpdatePacket{}, td, nil); err != nil {
+ if err := sinkPeer.Handshake(1, source.chain, eth.BlockRangeUpdatePacket{LatestBlockHash: genesis.Hash()}, td, nil); err != nil {
t.Fatalf("failed to run protocol handshake")
}
go eth.Handle(sink, sinkPeer)
@@ -624,6 +632,7 @@ func testBroadcastBlock(t *testing.T, peers, bcasts int) {
// Tests that a propagated malformed block (uncles or transactions don't match
// with the hashes in the header) gets discarded and not broadcast forward.
func TestBroadcastMalformedBlock68(t *testing.T) { testBroadcastMalformedBlock(t, eth.ETH68) }
+func TestBroadcastMalformedBlock71(t *testing.T) { testBroadcastMalformedBlock(t, eth.ETH71) }
func testBroadcastMalformedBlock(t *testing.T, protocol uint) {
t.Parallel()
@@ -651,7 +660,7 @@ func testBroadcastMalformedBlock(t *testing.T, protocol uint) {
genesis = source.chain.Genesis()
td = source.chain.GetTd(genesis.Hash(), genesis.NumberU64())
)
- if err := sink.Handshake(1, source.chain, eth.BlockRangeUpdatePacket{}, td, nil); err != nil {
+ if err := sink.Handshake(1, source.chain, eth.BlockRangeUpdatePacket{LatestBlockHash: genesis.Hash()}, td, nil); err != nil {
t.Fatalf("failed to run protocol handshake")
}
// After the handshake completes, the source handler should stream the sink
diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go
index 21717b9f60..87e5d5c089 100644
--- a/eth/protocols/eth/handler.go
+++ b/eth/protocols/eth/handler.go
@@ -233,6 +233,9 @@ var eth70 = map[uint64]msgHandler{
}
var eth71 = map[uint64]msgHandler{
+ // Parlia retains block propagation alongside BAL exchange.
+ NewBlockHashesMsg: handleNewBlockhashes,
+ NewBlockMsg: handleNewBlock,
TransactionsMsg: handleTransactions,
NewPooledTransactionHashesMsg: handleNewPooledTransactionHashes,
GetBlockHeadersMsg: handleGetBlockHeaders,
diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go
index e979c2f429..9281acbc58 100644
--- a/eth/protocols/eth/handler_test.go
+++ b/eth/protocols/eth/handler_test.go
@@ -184,6 +184,7 @@ func (b *testBackend) Handle(*Peer, Packet) error {
// Tests that block headers can be retrieved from a remote chain based on user queries.
func TestGetBlockHeaders68(t *testing.T) { testGetBlockHeaders(t, ETH68) }
+func TestGetBlockHeaders71(t *testing.T) { testGetBlockHeaders(t, ETH71) }
func testGetBlockHeaders(t *testing.T, protocol uint) {
t.Parallel()
@@ -397,6 +398,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
// Tests that block contents can be retrieved from a remote chain based on their hashes.
func TestGetBlockBodies68(t *testing.T) { testGetBlockBodies(t, ETH68) }
+func TestGetBlockBodies71(t *testing.T) { testGetBlockBodies(t, ETH71) }
func testGetBlockBodies(t *testing.T, protocol uint) {
t.Parallel()
@@ -697,7 +699,8 @@ func testGetBlockReceipts(t *testing.T, protocol uint) {
}
}
-func TestGetBlockPartialReceipts(t *testing.T) { testGetBlockPartialReceipts(t, ETH70) }
+func TestGetBlockPartialReceipts(t *testing.T) { testGetBlockPartialReceipts(t, ETH70) }
+func TestGetBlockPartialReceipts71(t *testing.T) { testGetBlockPartialReceipts(t, ETH71) }
func testGetBlockPartialReceipts(t *testing.T, protocol int) {
// First, generate the chain and overwrite the receipts.
@@ -964,7 +967,10 @@ func FuzzEthProtocolHandlers(f *testing.F) {
})
}
-func TestHandleNewBlock(t *testing.T) {
+func TestHandleNewBlock(t *testing.T) { testHandleNewBlock(t, ETH68) }
+func TestHandleNewBlock71(t *testing.T) { testHandleNewBlock(t, ETH71) }
+
+func testHandleNewBlock(t *testing.T, protocol uint) {
t.Parallel()
gen := func(n int, g *core.BlockGen) {
@@ -980,11 +986,6 @@ func TestHandleNewBlock(t *testing.T) {
backend := newTestBackendWithGenerator(maxBodiesServe+15, true, false, gen)
defer backend.close()
- peer, _ := newTestPeer("peer", ETH68, backend)
- defer peer.close()
-
- v := new(uint32)
- *v = 1
genBlobs := makeBlkBlobs(1, 2)
tx1 := types.NewTx(&types.BlobTx{
ChainID: new(uint256.Int).SetUint64(1),
@@ -1034,7 +1035,7 @@ func TestHandleNewBlock(t *testing.T) {
{
name: "Valid block",
msg: p2p.Msg{
- Code: 1,
+ Code: NewBlockMsg,
Size: uint32(sizeNonNil),
Payload: rNonNil,
},
@@ -1043,7 +1044,7 @@ func TestHandleNewBlock(t *testing.T) {
{
name: "Nil sidecars",
msg: p2p.Msg{
- Code: 2,
+ Code: NewBlockMsg,
Size: uint32(sizeNil),
Payload: rNil,
},
@@ -1054,7 +1055,7 @@ func TestHandleNewBlock(t *testing.T) {
protos := []p2p.Protocol{
{
Name: "eth",
- Version: ETH68,
+ Version: protocol,
},
{
Name: "bsc",
@@ -1064,7 +1065,7 @@ func TestHandleNewBlock(t *testing.T) {
caps := []p2p.Cap{
{
Name: "eth",
- Version: ETH68,
+ Version: protocol,
},
{
Name: "bsc",
@@ -1076,12 +1077,14 @@ func TestHandleNewBlock(t *testing.T) {
defer p2pEthSrc.Close()
defer p2pEthSink.Close()
- localEth := NewPeer(ETH68, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pEthSrc, nil, nil, backend.chain.Config())
+ localEth := NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pEthSrc, nil, nil, backend.chain.Config())
+ defer localEth.Close()
// Run the tests
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- err := handleNewBlock(backend, tc.msg, localEth)
+ go p2pEthSink.WriteMsg(tc.msg)
+ err := handleMessage(backend, localEth)
if err != tc.err {
t.Errorf("expected error %v, got %v", tc.err, err)
}
@@ -1089,13 +1092,20 @@ func TestHandleNewBlock(t *testing.T) {
}
}
-func TestHandleNewBlockhashes(t *testing.T) {
+func TestHandleNewBlockhashes(t *testing.T) { testHandleNewBlockhashes(t, ETH68) }
+func TestHandleNewBlockhashes71(t *testing.T) { testHandleNewBlockhashes(t, ETH71) }
+
+func testHandleNewBlockhashes(t *testing.T, protocol uint) {
t.Parallel()
backend := newTestBackendWithGenerator(1, false, false, nil)
defer backend.close()
- localEth := NewPeer(ETH68, p2p.NewPeer(enode.ID{1}, "peer", nil), nil, nil, nil, backend.chain.Config())
+ app, net := p2p.MsgPipe()
+ defer app.Close()
+ defer net.Close()
+ localEth := NewPeer(protocol, p2p.NewPeer(enode.ID{1}, "peer", nil), net, nil, nil, backend.chain.Config())
+ defer localEth.Close()
newPacket := func(n int) NewBlockHashesPacket {
ann := make(NewBlockHashesPacket, n)
@@ -1120,7 +1130,8 @@ func TestHandleNewBlockhashes(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- err := handleNewBlockhashes(backend, msgFor(newPacket(tc.count)), localEth)
+ go app.WriteMsg(msgFor(newPacket(tc.count)))
+ err := handleMessage(backend, localEth)
if tc.wantErr && err == nil {
t.Fatalf("expected error for %d announcements, got nil", tc.count)
}
diff --git a/eth/protocols/eth/handshake.go b/eth/protocols/eth/handshake.go
index 1a7f3aebcc..48a8ff419b 100644
--- a/eth/protocols/eth/handshake.go
+++ b/eth/protocols/eth/handshake.go
@@ -39,7 +39,7 @@ const (
// network IDs, difficulties, head and genesis blocks.
func (p *Peer) Handshake(networkID uint64, chain forkid.Blockchain, rangeMsg BlockRangeUpdatePacket, td *big.Int, extension *UpgradeStatusExtension) error {
switch p.version {
- case ETH70:
+ case ETH70, ETH71:
return p.handshake70(networkID, chain, rangeMsg, td)
case ETH68:
return p.handshake68(networkID, chain, td, extension)
diff --git a/eth/protocols/eth/handshake_test.go b/eth/protocols/eth/handshake_test.go
index 27b9ea022b..3d3c981188 100644
--- a/eth/protocols/eth/handshake_test.go
+++ b/eth/protocols/eth/handshake_test.go
@@ -19,6 +19,8 @@ package eth
import (
"errors"
"fmt"
+ "math/big"
+ "strings"
"testing"
"github.com/ethereum/go-ethereum/common"
@@ -30,6 +32,7 @@ import (
// Tests that handshake failures are detected and reported correctly.
func TestHandshake68(t *testing.T) { testHandshake(t, ETH68) }
func TestHandshake70(t *testing.T) { testHandshake(t, ETH70) }
+func TestHandshake71(t *testing.T) { testHandshake(t, ETH71) }
func testHandshake(t *testing.T, protocol uint) {
t.Parallel()
@@ -128,7 +131,7 @@ func testHandshake(t *testing.T, protocol uint) {
}
func TestHandshakeSuccess(t *testing.T) {
- for _, protocol := range []uint{ETH68, ETH70} {
+ for _, protocol := range []uint{ETH68, ETH70, ETH71} {
t.Run(fmt.Sprintf("eth/%d", protocol), func(t *testing.T) {
backend := newTestBackend(3)
defer backend.close()
@@ -163,9 +166,9 @@ func TestHandshakeSuccess(t *testing.T) {
if gotHead != head.Hash() || gotTD.Cmp(td) != 0 {
t.Fatalf("peer head mismatch: have (%s, %s), want (%s, %s)", gotHead, gotTD, head.Hash(), td)
}
- if protocol == ETH70 {
+ if protocol >= ETH70 {
if peer.statusExtension != nil {
- t.Fatal("eth/70 negotiated an upgrade status extension")
+ t.Fatalf("eth/%d negotiated an upgrade status extension", protocol)
}
if got := peer.BlockRange(); got == nil || *got != blockRange {
t.Fatalf("peer block range mismatch: have %+v, want %+v", got, blockRange)
@@ -177,3 +180,29 @@ func TestHandshakeSuccess(t *testing.T) {
})
}
}
+
+func TestHandshake71LargeTD(t *testing.T) {
+ backend := newTestBackend(1)
+ defer backend.close()
+ head := backend.chain.CurrentBlock()
+ td := backend.chain.GetTd(head.Hash(), head.Number.Uint64())
+ app, net := p2p.MsgPipe()
+ defer app.Close()
+ defer net.Close()
+ peer := NewPeer(ETH71, p2p.NewPeer(enode.ID{}, "peer", nil), net, nil, nil, backend.chain.Config())
+ defer peer.Close()
+ status := StatusPacket{
+ ProtocolVersion: ETH71,
+ NetworkID: 1,
+ TD: new(big.Int).Lsh(big.NewInt(1), 100),
+ Genesis: backend.chain.Genesis().Hash(),
+ ForkID: forkid.NewID(backend.chain.Config(), backend.chain.Genesis(), head.Number.Uint64(), head.Time),
+ LatestBlock: head.Number.Uint64(),
+ LatestBlockHash: head.Hash(),
+ }
+ go p2p.Send(app, StatusMsg, &status)
+ err := peer.Handshake(1, backend.chain, BlockRangeUpdatePacket{}, td, nil)
+ if err == nil || !strings.Contains(err.Error(), "too large total difficulty") {
+ t.Fatalf("expected oversized TD rejection, got %v", err)
+ }
+}
diff --git a/eth/protocols/eth/protocol.go b/eth/protocols/eth/protocol.go
index 2cbc031c08..cbb6240d6c 100644
--- a/eth/protocols/eth/protocol.go
+++ b/eth/protocols/eth/protocol.go
@@ -43,7 +43,7 @@ const ProtocolName = "eth"
// ProtocolVersions are the supported versions of the `eth` protocol (first
// is primary).
-var ProtocolVersions = []uint{ETH70, ETH68}
+var ProtocolVersions = []uint{ETH71, ETH68}
// protocolLengths are the number of implemented message corresponding to
// different protocol versions.
@@ -141,9 +141,8 @@ func (p *UpgradeStatusPacket) GetExtension() (*UpgradeStatusExtension, error) {
return extension, nil
}
-// StatusPacket is the network packet for the eth/70 status message. It follows
-// upstream eth/70, except that BSC retains TD for Parlia chain synchronization.
-// Unlike eth/68, eth/70 does not exchange UpgradeStatusMsg.
+// StatusPacket is the eth/70+ status message, retaining TD for Parlia synchronization.
+// Unlike eth/68, these versions do not exchange UpgradeStatusMsg.
type StatusPacket struct {
ProtocolVersion uint32
NetworkID uint64
diff --git a/eth/sync_test.go b/eth/sync_test.go
index 6e2bc59cb0..5739d0b731 100644
--- a/eth/sync_test.go
+++ b/eth/sync_test.go
@@ -106,14 +106,22 @@ func testSnapSyncDisabling(t *testing.T, ethVer uint, snapVer uint) {
}
func TestFullSyncWithBlobs(t *testing.T) {
- testChainSyncWithBlobs(t, ethconfig.FullSync, 128, 128)
+ testChainSyncWithBlobs(t, eth.ETH68, ethconfig.FullSync, 128, 128)
}
func TestSnapSyncWithBlobs(t *testing.T) {
- testChainSyncWithBlobs(t, ethconfig.SnapSync, 128, 128)
+ testChainSyncWithBlobs(t, eth.ETH68, ethconfig.SnapSync, 128, 128)
}
-func testChainSyncWithBlobs(t *testing.T, mode downloader.SyncMode, preCancunBlks, postCancunBlks uint64) {
+func TestFullSyncWithBlobs71(t *testing.T) {
+ testChainSyncWithBlobs(t, eth.ETH71, ethconfig.FullSync, 128, 128)
+}
+
+func TestSnapSyncWithBlobs71(t *testing.T) {
+ testChainSyncWithBlobs(t, eth.ETH71, ethconfig.SnapSync, 128, 128)
+}
+
+func testChainSyncWithBlobs(t *testing.T, ethVer uint, mode downloader.SyncMode, preCancunBlks, postCancunBlks uint64) {
t.Parallel()
config := *params.ParliaTestChainConfig
cancunTime := (preCancunBlks + 1) * 10
@@ -137,7 +145,6 @@ func testChainSyncWithBlobs(t *testing.T, mode downloader.SyncMode, preCancunBlk
checkChainWithBlobs(t, full.chain, preCancunBlks, postCancunBlks)
// Sync up the two handlers via both `eth` and `snap`
- ethVer := uint(eth.ETH68)
snapVer := uint(snap.SNAP1)
// Sync up the two handlers via both `eth` and `snap`
From b9828ac40ecd32265507e9f55cb9a7c049e09937 Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 19:17:28 +0800
Subject: [PATCH 03/14] consensus/parlia, miner, core, internal/ethapi: fix BSC
slot numbers
---
consensus/parlia/parlia.go | 3 ++
consensus/parlia/parlia_test.go | 41 +++++++++++++++
core/chain_makers.go | 4 +-
core/chain_makers_test.go | 32 ++++++++++++
internal/ethapi/override/bsc.go | 21 ++++++--
internal/ethapi/override/bsc_test.go | 59 +++++++++++++++++++++
internal/ethapi/simulate.go | 8 ++-
internal/ethapi/simulate_test.go | 41 +++++++++++++++
miner/worker.go | 14 +++--
miner/worker_test.go | 76 ++++++++++++++++++++++++++++
10 files changed, 287 insertions(+), 12 deletions(-)
diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go
index 212a98c0a7..7858d24a34 100644
--- a/consensus/parlia/parlia.go
+++ b/consensus/parlia/parlia.go
@@ -686,6 +686,9 @@ func (p *Parlia) VerifyUnsealedHeader(chain consensus.ChainHeaderReader, header
if header.SlotNumber == nil {
return errors.New("header has nil SlotNumber after Amsterdam")
}
+ if *header.SlotNumber != number {
+ return fmt.Errorf("invalid SlotNumber: have %d, want %d", *header.SlotNumber, number)
+ }
}
// All basic checks passed, verify cascading fields
diff --git a/consensus/parlia/parlia_test.go b/consensus/parlia/parlia_test.go
index b5f67e6d23..f08f3c183d 100644
--- a/consensus/parlia/parlia_test.go
+++ b/consensus/parlia/parlia_test.go
@@ -1472,3 +1472,44 @@ func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, head
func (c *mockParlia) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
return big.NewInt(1)
}
+
+func TestVerifyHeaderAmsterdamSlotNumber(t *testing.T) {
+ fork := uint64(3)
+ cfg := ¶ms.ChainConfig{ChainID: big.NewInt(56), Parlia: ¶ms.ParliaConfig{}, LondonBlock: big.NewInt(0), AmsterdamTime: &fork}
+ parent := &types.Header{Number: big.NewInt(0), GasLimit: 30_000_000, Time: 1, BaseFee: big.NewInt(0)}
+ chain := &finalizedHeaderChain{cfg: cfg, byHash: map[common.Hash]*types.Header{parent.Hash(): parent}}
+ engine := New(cfg, nil, nil, parent.Hash())
+ engine.recentSnaps.Add(parent.Hash(), newSnapshot(cfg.Parlia, engine.signatures, 0, parent.Hash(), []common.Address{testAddr}, nil, nil))
+ slot := uint64(1)
+ balHash := types.EmptyBlockAccessListHash
+ header := &types.Header{ParentHash: parent.Hash(), Number: big.NewInt(1), Coinbase: testAddr,
+ UncleHash: types.EmptyUncleHash, Difficulty: new(big.Int).Set(diffInTurn), GasLimit: parent.GasLimit,
+ Time: 4, Extra: make([]byte, extraVanity+extraSeal), BaseFee: big.NewInt(0),
+ SlotNumber: &slot, BlockAccessListHash: &balHash}
+ seal := types.SealHash(header, cfg.ChainID)
+ sig, err := crypto.Sign(seal[:], testKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+ copy(header.Extra[len(header.Extra)-extraSeal:], sig)
+ if err := engine.VerifyHeader(chain, header); err != nil {
+ t.Fatalf("valid slot rejected: %v", err)
+ }
+ for _, invalidSlot := range []uint64{0, 2, ^uint64(0)} {
+ invalid := types.CopyHeader(header)
+ invalid.SlotNumber = &invalidSlot
+ if types.SealHash(invalid, cfg.ChainID) != seal {
+ t.Fatal("slot changed seal hash")
+ }
+ if err := engine.VerifyHeader(chain, invalid); err == nil || !strings.HasPrefix(err.Error(), "invalid SlotNumber") {
+ t.Fatalf("slot %d: expected rejection, got %v", invalidSlot, err)
+ }
+ if err := engine.VerifyUnsealedHeader(chain, invalid, nil); err == nil {
+ t.Fatalf("unsealed slot %d accepted", invalidSlot)
+ }
+ }
+ header.SlotNumber = nil
+ if err := engine.VerifyHeader(chain, header); err == nil {
+ t.Fatal("nil slot accepted")
+ }
+}
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 68aa3a0267..1af6e06344 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -603,7 +603,9 @@ func (cm *chainMaker) makeHeader(parent *types.Block, engine consensus.Engine) *
}
if cm.config.IsAmsterdam(header.Number, header.Time) {
var slot uint64
- if parentHeader.SlotNumber != nil {
+ if cm.config.IsInBSC() {
+ slot = header.Number.Uint64()
+ } else if parentHeader.SlotNumber != nil {
slot = *parentHeader.SlotNumber + 1
}
header.SlotNumber = &slot
diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go
index 7f8f4b933e..3fe780a4b8 100644
--- a/core/chain_makers_test.go
+++ b/core/chain_makers_test.go
@@ -251,3 +251,35 @@ func ExampleGenerateChain() {
// balance of addr2: 10000
// balance of addr3: 19687500000000001000
}
+
+func TestChainMakerSlotNumber(t *testing.T) {
+ slot := func(v uint64) *uint64 { return &v }
+ for _, test := range []struct {
+ name string
+ bsc bool
+ parentTime uint64
+ parentSlot *uint64
+ want *uint64
+ }{
+ {name: "bsc-before-fork", bsc: true, parentTime: 1},
+ {name: "bsc-first-fork", bsc: true, parentTime: 10, want: slot(101)},
+ {name: "bsc-next", bsc: true, parentTime: 20, parentSlot: slot(100), want: slot(101)},
+ {name: "eth-first-fork", parentTime: 10, want: slot(0)},
+ {name: "eth-next", parentTime: 20, parentSlot: slot(500), want: slot(501)},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ fork := uint64(20)
+ cfg := ¶ms.ChainConfig{LondonBlock: big.NewInt(0), AmsterdamTime: &fork}
+ if test.bsc {
+ cfg.Parlia = ¶ms.ParliaConfig{}
+ }
+ parent := types.NewBlockWithHeader(&types.Header{Number: big.NewInt(100), Time: test.parentTime,
+ GasLimit: 30_000_000, Difficulty: big.NewInt(2), BaseFee: big.NewInt(0), SlotNumber: test.parentSlot})
+ engine := ethash.NewFaker()
+ header := newChainMaker(parent, cfg, engine).makeHeader(parent, engine)
+ if !reflect.DeepEqual(header.SlotNumber, test.want) {
+ t.Fatalf("slot: have %v, want %v", header.SlotNumber, test.want)
+ }
+ })
+ }
+}
diff --git a/internal/ethapi/override/bsc.go b/internal/ethapi/override/bsc.go
index 396306e100..28b614db95 100644
--- a/internal/ethapi/override/bsc.go
+++ b/internal/ethapi/override/bsc.go
@@ -63,8 +63,15 @@ func (o *BlockOverrides) ApplyBSC(blockCtx *vm.BlockContext) error {
// ApplyFor applies BSC-specific block overrides only on BSC chains.
func (o *BlockOverrides) ApplyFor(cfg *params.ChainConfig, blockCtx *vm.BlockContext) error {
- if cfg != nil && cfg.IsInBSC() {
- return o.ApplyBSC(blockCtx)
+ if cfg != nil && cfg.IsInBSC() && o != nil {
+ if err := o.ApplyBSC(blockCtx); err != nil {
+ return err
+ }
+ blockCtx.SlotNum = 0
+ if cfg.IsAmsterdam(blockCtx.BlockNumber, blockCtx.Time) {
+ blockCtx.SlotNum = blockCtx.BlockNumber.Uint64()
+ }
+ return nil
}
return o.Apply(blockCtx)
}
@@ -80,8 +87,14 @@ func (o *BlockOverrides) MakeHeaderBSC(header *types.Header) *types.Header {
// MakeHeaderFor applies BSC-specific header overrides only on BSC chains.
func (o *BlockOverrides) MakeHeaderFor(cfg *params.ChainConfig, header *types.Header) *types.Header {
- if cfg != nil && cfg.IsInBSC() {
- return o.MakeHeaderBSC(header)
+ if cfg != nil && cfg.IsInBSC() && o != nil {
+ h := o.MakeHeaderBSC(header)
+ h.SlotNumber = nil
+ if cfg.IsAmsterdam(h.Number, h.Time) {
+ slot := h.Number.Uint64()
+ h.SlotNumber = &slot
+ }
+ return h
}
return o.MakeHeader(header)
}
diff --git a/internal/ethapi/override/bsc_test.go b/internal/ethapi/override/bsc_test.go
index 503df61aed..e1ec7609cf 100644
--- a/internal/ethapi/override/bsc_test.go
+++ b/internal/ethapi/override/bsc_test.go
@@ -3,9 +3,14 @@ package override
import (
"math"
"math/big"
+ "reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/params"
)
func TestBSCMilliRemainder(t *testing.T) {
@@ -46,3 +51,57 @@ func TestBSCMilliTimestampBounds(t *testing.T) {
}
}
}
+
+func TestBSCSlotNumberOverrides(t *testing.T) {
+ slot := func(v uint64) *uint64 { return &v }
+ number := hexutil.Big(*big.NewInt(200))
+ before, after := hexutil.Uint64(19), hexutil.Uint64(20)
+ for _, test := range []struct {
+ name string
+ bsc bool
+ timestamp uint64
+ parentSlot *uint64
+ overrides *BlockOverrides
+ want *uint64
+ }{
+ {name: "number", bsc: true, timestamp: 20, parentSlot: slot(100), overrides: &BlockOverrides{Number: &number}, want: slot(200)},
+ {name: "first-fork", bsc: true, timestamp: 19, overrides: &BlockOverrides{Time: &after}, want: slot(100)},
+ {name: "before-fork", bsc: true, timestamp: 20, parentSlot: slot(100), overrides: &BlockOverrides{Time: &before}},
+ {name: "both", bsc: true, timestamp: 19, overrides: &BlockOverrides{Number: &number, Time: &after}, want: slot(200)},
+ {name: "nil", bsc: true, timestamp: 20, parentSlot: slot(100), want: slot(100)},
+ {name: "empty", bsc: true, timestamp: 20, parentSlot: slot(100), overrides: &BlockOverrides{}, want: slot(100)},
+ {name: "eth-number", timestamp: 20, parentSlot: slot(500), overrides: &BlockOverrides{Number: &number}, want: slot(500)},
+ {name: "eth-first-fork", timestamp: 19, overrides: &BlockOverrides{Time: &after}},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ fork := uint64(20)
+ cfg := ¶ms.ChainConfig{LondonBlock: big.NewInt(0), AmsterdamTime: &fork}
+ if test.bsc {
+ cfg.Parlia = ¶ms.ParliaConfig{}
+ }
+ header := &types.Header{Number: big.NewInt(100), Time: test.timestamp, SlotNumber: test.parentSlot}
+ original := types.CopyHeader(header)
+ ctx := vm.BlockContext{BlockNumber: big.NewInt(100), Time: test.timestamp}
+ if header.SlotNumber != nil {
+ ctx.SlotNum = *header.SlotNumber
+ }
+ if err := test.overrides.ApplyFor(cfg, &ctx); err != nil {
+ t.Fatal(err)
+ }
+ got := test.overrides.MakeHeaderFor(cfg, header)
+ want := uint64(0)
+ if test.want != nil {
+ want = *test.want
+ }
+ if ctx.SlotNum != want || !reflect.DeepEqual(got.SlotNumber, test.want) {
+ t.Fatalf("slot context=%d header=%v, want %v", ctx.SlotNum, got.SlotNumber, test.want)
+ }
+ if ctx.BlockNumber.Cmp(got.Number) != 0 || ctx.Time != got.Time {
+ t.Fatal("header/context mismatch")
+ }
+ if !reflect.DeepEqual(types.CopyHeader(header), original) {
+ t.Fatal("override mutated the original header")
+ }
+ })
+ }
+}
diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go
index 41586513d8..bdf3aba5e0 100644
--- a/internal/ethapi/simulate.go
+++ b/internal/ethapi/simulate.go
@@ -734,9 +734,13 @@ func (sim *simulator) makeHeaders(blocks []simBlock) ([]*types.Header, error) {
if overrides.Difficulty != nil && difficulty.Sign() == 0 {
return nil, &invalidParamsError{fmt.Sprintf("block %d: difficulty override is not allowed after the merge", number)}
}
- // The slot number is unknown when the parent has none, so it is omitted then.
var slotNumber *uint64
- if header.SlotNumber != nil {
+ if sim.isBSC() {
+ if sim.chainConfig.IsAmsterdam(number, timestamp) {
+ slot := number.Uint64()
+ slotNumber = &slot
+ }
+ } else if header.SlotNumber != nil {
slot := *header.SlotNumber + 1
slotNumber = &slot
}
diff --git a/internal/ethapi/simulate_test.go b/internal/ethapi/simulate_test.go
index 8b9397b8b0..4871028e8e 100644
--- a/internal/ethapi/simulate_test.go
+++ b/internal/ethapi/simulate_test.go
@@ -19,11 +19,13 @@ package ethapi
import (
"encoding/json"
"math/big"
+ "reflect"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi/override"
+ "github.com/ethereum/go-ethereum/params"
)
func TestSimulateSanitizeBlockOrder(t *testing.T) {
@@ -149,3 +151,42 @@ func TestSimBlockUnmarshalOverrideKeys(t *testing.T) {
}
}
}
+
+func TestSimulateSlotNumber(t *testing.T) {
+ slot := func(v uint64) *uint64 { return &v }
+ for _, test := range []struct {
+ name string
+ bsc bool
+ parentSlot *uint64
+ timestamp uint64
+ want *uint64
+ }{
+ {name: "bsc-before-fork", bsc: true, timestamp: 19},
+ {name: "bsc-first-fork", bsc: true, timestamp: 20, want: slot(101)},
+ {name: "bsc-next", bsc: true, timestamp: 21, parentSlot: slot(100), want: slot(101)},
+ {name: "eth-first-fork", timestamp: 20},
+ {name: "eth-next", timestamp: 21, parentSlot: slot(500), want: slot(501)},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ fork := uint64(20)
+ cfg := ¶ms.ChainConfig{LondonBlock: big.NewInt(0), AmsterdamTime: &fork}
+ if test.bsc {
+ cfg.Parlia = ¶ms.ParliaConfig{}
+ }
+ parentTime := uint64(18)
+ if test.parentSlot != nil {
+ parentTime = 20
+ }
+ sim := &simulator{chainConfig: cfg, base: &types.Header{Number: big.NewInt(100), Time: parentTime,
+ Difficulty: big.NewInt(2), GasLimit: 30_000_000, BaseFee: big.NewInt(0), SlotNumber: test.parentSlot}}
+ number, timestamp := hexutil.Big(*big.NewInt(101)), hexutil.Uint64(test.timestamp)
+ headers, err := sim.makeHeaders([]simBlock{{BlockOverrides: &override.BlockOverrides{Number: &number, Time: ×tamp}}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(headers[0].SlotNumber, test.want) {
+ t.Fatalf("slot: have %v, want %v", headers[0].SlotNumber, test.want)
+ }
+ })
+ }
+}
diff --git a/miner/worker.go b/miner/worker.go
index 1c34b61605..782522cf95 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -1147,13 +1147,17 @@ func (w *worker) prepareWork(genParams *generateParams, witness bool) (*environm
}
}
// Apply EIP-7843.
- // TODO: Define BSC's slot mapping and supply slotNum from commitWork
- // before enabling Amsterdam block production.
if w.chainConfig.IsAmsterdam(header.Number, header.Time) {
- if genParams.slotNum == nil {
- return nil, errors.New("missing slot number")
+ if w.chainConfig.IsInBSC() {
+ // BSC uses block numbers instead of beacon slots.
+ slot := header.Number.Uint64()
+ header.SlotNumber = &slot
+ } else {
+ if genParams.slotNum == nil {
+ return nil, errors.New("missing slot number")
+ }
+ header.SlotNumber = genParams.slotNum
}
- header.SlotNumber = genParams.slotNum
}
// Could potentially happen if starting to mine in an odd state.
// Note genParams.coinbase can be different with header.Coinbase
diff --git a/miner/worker_test.go b/miner/worker_test.go
index 4f4c12b454..19df4be598 100644
--- a/miner/worker_test.go
+++ b/miner/worker_test.go
@@ -30,10 +30,12 @@ import (
"github.com/ethereum/go-ethereum/consensus/parlia"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
+ "github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
"github.com/ethereum/go-ethereum/core/types"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
+ "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethdb"
@@ -488,3 +490,77 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
}
}
}
+
+func TestPrepareWorkSlotNumber(t *testing.T) {
+ for _, bsc := range []bool{true, false} {
+ name := "eth"
+ config := *params.AllDevChainProtocolChanges
+ config.BogotaTime = nil
+ if bsc {
+ name = "bsc"
+ config = *params.BSCChainConfig
+ config.LondonBlock = big.NewInt(0)
+ }
+ t.Run(name, func(t *testing.T) {
+ fork := uint64(2_000_000_000)
+ config.AmsterdamTime = &fork
+ w, backend := newTestWorker(t, &config, ethash.NewFaker(), rawdb.NewMemoryDatabase(), 0)
+ defer backend.chain.Stop()
+ defer backend.txPool.Close()
+ defer w.close()
+ suppliedSlot, zeroSlot := uint64(500), uint64(0)
+ for _, test := range []struct {
+ name string
+ timestamp uint64
+ supplied *uint64
+ }{
+ {name: "before-fork", timestamp: 1},
+ {name: "before-fork-supplied", timestamp: 1, supplied: &suppliedSlot},
+ {name: "first-fork", timestamp: fork},
+ {name: "supplied", timestamp: fork, supplied: &suppliedSlot},
+ {name: "supplied-zero", timestamp: fork, supplied: &zeroSlot},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ env, err := w.prepareWork(&generateParams{timestamp: test.timestamp, forceTime: true, coinbase: testBankAddress, slotNum: test.supplied}, false)
+ if !bsc && test.timestamp == fork && test.supplied == nil {
+ if err == nil || err.Error() != "missing slot number" {
+ t.Fatalf("expected missing slot error, got %v", err)
+ }
+ return
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer env.discard()
+ if test.timestamp < fork {
+ if env.header.SlotNumber != nil {
+ t.Fatal("slot present before fork")
+ }
+ return
+ }
+ want := env.header.Number.Uint64()
+ if !bsc {
+ want = *test.supplied
+ }
+ if env.header.SlotNumber == nil || *env.header.SlotNumber != want || env.evm.Context.SlotNum != want {
+ t.Fatalf("slot header=%v context=%d, want %d", env.header.SlotNumber, env.evm.Context.SlotNum, want)
+ }
+ if bsc {
+ address := common.Address{0x80}
+ code := []byte{byte(vm.SLOTNUM), byte(vm.PUSH1), 0, byte(vm.MSTORE), byte(vm.PUSH1), 32, byte(vm.PUSH1), 0, byte(vm.RETURN)}
+ env.state.SetCode(address, code, tracing.CodeChangeUnspecified)
+ rules := env.evm.GetRules()
+ env.state.Prepare(rules, testBankAddress, testBankAddress, &address, vm.ActivePrecompiles(rules), nil)
+ ret, _, err := env.evm.Call(testBankAddress, address, nil, vm.NewGasBudget(100_000, 0), new(uint256.Int))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(ret) != 32 || new(big.Int).SetBytes(ret).Uint64() != want {
+ t.Fatalf("SLOTNUM returned %x, want %d", ret, want)
+ }
+ }
+ })
+ }
+ })
+ }
+}
From c19341177834e7a1ab6b852a7001f54f8ec13d36 Mon Sep 17 00:00:00 2001
From: qybdyx
Date: Thu, 12 Nov 2026 11:12:24 +0800
Subject: [PATCH 04/14] core: record parlia system transactions and protocol
changes in bal
Collect BALs returned by Parlia system transaction finalization during
serial BSC import. Record block-begin upgrades at index 0 and trailing
protocol mutations at n+1, preserving the next included transaction
scope for preceding protocol mutations.
Keep direct protocol reads and omitted transaction attempts out of the
BAL. Gate collection on BSC Amsterdam without enabling mining BAL
construction or parallel execution.
---
consensus/parlia/bal_test.go | 115 ++++++++++++++++++++++
core/state_processor.go | 18 +++-
core/state_processor_bsc.go | 47 +++++++++
core/state_processor_bsc_test.go | 164 +++++++++++++++++++++++++++++++
4 files changed, 341 insertions(+), 3 deletions(-)
create mode 100644 consensus/parlia/bal_test.go
create mode 100644 core/state_processor_bsc.go
create mode 100644 core/state_processor_bsc_test.go
diff --git a/consensus/parlia/bal_test.go b/consensus/parlia/bal_test.go
new file mode 100644
index 0000000000..9f02ad7b2f
--- /dev/null
+++ b/consensus/parlia/bal_test.go
@@ -0,0 +1,115 @@
+package parlia
+
+import (
+ "context"
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/consensus"
+ "github.com/ethereum/go-ethereum/core"
+ "github.com/ethereum/go-ethereum/core/rawdb"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/systemcontracts"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
+ "github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/params"
+ "github.com/ethereum/go-ethereum/trie"
+ "github.com/ethereum/go-ethereum/triedb"
+ "github.com/holiman/uint256"
+)
+
+func TestParliaSystemTransactionBAL(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ config.Parlia = ¶ms.ParliaConfig{}
+ config.HaberTime, config.HaberFixTime, config.BohrTime, config.PascalTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.PragueTime, config.LorentzTime, config.MaxwellTime, config.FermiTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.OsakaTime, config.MendelTime, config.PasteurTime, config.AmsterdamTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.BlobScheduleConfig = ¶ms.BlobScheduleConfig{Cancun: params.DefaultCancunBlobConfig, Prague: params.DefaultPragueBlobConfigBSC}
+ validatorContract := common.HexToAddress(systemcontracts.ValidatorContract)
+ db := rawdb.NewMemoryDatabase()
+ defer db.Close()
+ triedb := triedb.NewDatabase(db, nil)
+ defer triedb.Close()
+ gspec := &core.Genesis{Config: &config, GasLimit: 30_000_000, Alloc: types.GenesisAlloc{
+ consensus.SystemAddress: {Balance: big.NewInt(100)},
+ validatorContract: {Code: []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.LOG0), byte(vm.STOP)}, Nonce: 1},
+ params.HistoryStorageAddress: {Code: params.HistoryStorageCode, Nonce: 1},
+ }}
+ genesis := gspec.MustCommit(db, triedb)
+ engine := New(&config, db, nil, genesis.Hash())
+ defer engine.Close()
+ engine.recentSnaps.Add(genesis.Hash(), newSnapshot(config.Parlia, engine.signatures, 0, genesis.Hash(), []common.Address{testAddr}, nil, nil))
+ chain, err := core.NewBlockChain(db, gspec, engine, core.DefaultConfig())
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer chain.Stop()
+
+ // Start after block one's contract-initialization transactions. The parent
+ // uses the committed prestate, and its snapshot supplies the validator set.
+ parent := genesis.Header()
+ parent.Number = big.NewInt(1)
+ parent.Coinbase, parent.Difficulty = testAddr, diffInTurn
+ rawdb.WriteHeader(db, parent)
+ engine.recentSnaps.Add(parent.Hash(), newSnapshot(config.Parlia, engine.signatures, 1, parent.Hash(), []common.Address{testAddr}, nil, nil))
+ header := &types.Header{ParentHash: parent.Hash(), Number: big.NewInt(2), Time: 3, Difficulty: diffInTurn, Coinbase: testAddr, GasLimit: 30_000_000}
+ data, err := engine.validatorSetABI.Pack("deposit", testAddr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ msg := engine.getSystemMessage(testAddr, validatorContract, data, big.NewInt(100))
+ tx := types.MustSignNewTx(testKey, engine.signer, &types.LegacyTx{To: &validatorContract, Gas: msg.GasLimit, GasPrice: common.Big0, Value: big.NewInt(100), Data: data})
+ body := types.Body{Transactions: []*types.Transaction{tx}}
+ block := types.NewBlockWithHeader(header).WithBody(body)
+ sdb, err := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ if err != nil {
+ t.Fatal(err)
+ }
+ res, err := core.NewStateProcessor(chain.HeaderChain()).Process(context.Background(), block, sdb, nil, nil, vm.Config{}, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(res.Receipts) != 1 || res.Receipts[0].TransactionIndex != 0 || res.Receipts[0].TxHash != tx.Hash() || res.GasUsed == 0 {
+ t.Fatalf("unexpected system transaction result: %+v", res)
+ }
+ // Compare gas and receipts with Parlia's existing direct finalization path.
+ rules := config.Rules(header.Number, false, header.Time)
+ reference, _ := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ reference.SetState(params.HistoryStorageAddress, common.Hash{31: 1}, parent.Hash())
+ reference.Finalise(rules)
+ var referenceReceipts []*types.Receipt
+ var referenceGas uint64
+ received := []*types.Transaction{tx}
+ if err := engine.Finalize(chain, header, reference, &types.Body{}, &referenceReceipts, &received, &referenceGas, nil); err != nil {
+ t.Fatal(err)
+ }
+ if res.GasUsed != referenceGas || types.DeriveSha(res.Receipts, trie.NewStackTrie(nil)) != types.DeriveSha(types.Receipts(referenceReceipts), trie.NewStackTrie(nil)) {
+ t.Fatal("BAL collection changed Parlia gas or receipts")
+ }
+ want := bal.NewConstructionBlockAccessList()
+ want.StorageWrite(0, params.HistoryStorageAddress, common.Hash{31: 1}, parent.Hash())
+ want.BalanceChange(1, consensus.SystemAddress, common.U2560)
+ want.NonceChange(testAddr, 1, 1)
+ want.BalanceChange(1, validatorContract, uint256.NewInt(100))
+ want.StorageWrite(1, validatorContract, common.Hash{}, common.Hash{31: 1})
+ list := want.ToEncodingObj()
+ replayed, _ := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ if err := replayed.ApplyBlockAccessList(list); err != nil {
+ t.Fatal(err)
+ }
+ header.Root = replayed.IntermediateRoot(rules)
+ if header.Root != reference.IntermediateRoot(rules) {
+ t.Fatal("BAL collection changed Parlia post-state")
+ }
+ header.GasUsed = res.GasUsed
+ header.ReceiptHash = types.DeriveSha(res.Receipts, trie.NewStackTrie(nil))
+ header.Bloom = types.MergeBloom(res.Receipts)
+ hash := list.Hash()
+ header.BlockAccessListHash = &hash
+ block = types.NewBlockWithHeader(header).WithBody(body)
+ if err := core.NewBlockValidator(&config, chain).ValidateState(block, sdb, res, false); err != nil {
+ t.Fatalf("Parlia execution differs from the expected BAL and post-state: %v", err)
+ }
+}
diff --git a/core/state_processor.go b/core/state_processor.go
index d9db3daa09..f6b6d17e0c 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -120,6 +120,12 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
// Handle upgrade built-in system contract code
systemcontracts.TryUpdateBuildInSystemContract(p.chain.Config(), blockNumber, parent.Time, block.Time(), statedb, true)
+ recordSystemBAL := config.IsInBSC() && config.IsAmsterdam(blockNumber, block.Time())
+ var upgradeBAL *bal.ConstructionBlockAccessList
+ if recordSystemBAL {
+ rules := config.Rules(blockNumber, header.Difficulty.Sign() == 0, header.Time)
+ upgradeBAL = finaliseProtocolChanges(statedb, rules, 0)
+ }
var (
context = NewEVMBlockContext(header, p.chain, nil)
@@ -129,6 +135,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
txNum = len(block.Transactions())
)
defer evm.Release()
+ blockAccessList.Merge(upgradeBAL)
if jumpDestCache != nil {
evm.SetJumpDestCache(jumpDestCache)
@@ -194,12 +201,17 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
gasUsed := gp.Used()
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
body := &types.Body{Transactions: commonTxs, Uncles: block.Uncles(), Withdrawals: block.Withdrawals()}
- // TODO: Include Parlia system transactions and system-contract upgrades in
- // the block access list before enabling Amsterdam on BSC.
- err = p.chain.Engine().Finalize(p.chain, header, tracingStateDB, body, &receipts, &systemTxs, &gasUsed, cfg.Tracer)
+ finalizeState := tracingStateDB
+ if recordSystemBAL {
+ finalizeState = &systemTxStateDB{StateDB: tracingStateDB, accessList: blockAccessList}
+ }
+ err = p.chain.Engine().Finalize(p.chain, header, finalizeState, body, &receipts, &systemTxs, &gasUsed, cfg.Tracer)
if err != nil {
return nil, err
}
+ if recordSystemBAL {
+ blockAccessList.Merge(finaliseProtocolChanges(tracingStateDB, evm.GetRules(), uint32(len(block.Transactions())+1)))
+ }
if len(receipts) != len(block.Transactions()) {
return nil, fmt.Errorf("receipt count mismatch: have %d, want %d", len(receipts), len(block.Transactions()))
}
diff --git a/core/state_processor_bsc.go b/core/state_processor_bsc.go
new file mode 100644
index 0000000000..e6bd57aae1
--- /dev/null
+++ b/core/state_processor_bsc.go
@@ -0,0 +1,47 @@
+// Copyright 2026 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package core
+
+import (
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/types/bal"
+ "github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+// systemTxStateDB collects the BALs finalized by Parlia without changing the
+// consensus engine interface. Pending protocol mutations share the next system
+// transaction's scope, so their combined net changes are recorded together.
+type systemTxStateDB struct {
+ vm.StateDB
+ accessList *bal.ConstructionBlockAccessList
+}
+
+func (s *systemTxStateDB) Finalise(rules params.Rules) *bal.ConstructionBlockAccessList {
+ list := s.StateDB.Finalise(rules)
+ s.accessList.Merge(list)
+ return list
+}
+
+// finaliseProtocolChanges records only pending mutations. Prepare runs after
+// the protocol operations so their read-only accesses, including any omitted
+// system transaction attempt, do not enter the BAL.
+func finaliseProtocolChanges(statedb vm.StateDB, rules params.Rules, index uint32) *bal.ConstructionBlockAccessList {
+ statedb.Prepare(rules, common.Address{}, common.Address{}, nil, nil, nil)
+ statedb.SetTxContext(common.Hash{}, max(0, int(index)-1), index)
+ return statedb.Finalise(rules)
+}
diff --git a/core/state_processor_bsc_test.go b/core/state_processor_bsc_test.go
new file mode 100644
index 0000000000..8e2caa0b60
--- /dev/null
+++ b/core/state_processor_bsc_test.go
@@ -0,0 +1,164 @@
+package core
+
+import (
+ "context"
+ "errors"
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/consensus"
+ "github.com/ethereum/go-ethereum/consensus/ethash"
+ "github.com/ethereum/go-ethereum/core/rawdb"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/tracing"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
+ "github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/params"
+ "github.com/ethereum/go-ethereum/triedb"
+ "github.com/holiman/uint256"
+)
+
+func TestSystemTransactionAccessList(t *testing.T) {
+ for _, traced := range []bool{false, true} {
+ t.Run(map[bool]string{false: "plain", true: "traced"}[traced], func(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ config.AmsterdamTime = new(uint64)
+ rules := config.Rules(common.Big1, false, 1)
+ coinbase := common.HexToAddress("0xbeef")
+ contract := common.HexToAddress("0xcafe")
+ upgrade := common.HexToAddress("0xaaaa")
+ omitted := common.HexToAddress("0xbbbb")
+ readOnly := common.HexToAddress("0xcccc")
+ tail := common.HexToAddress("0xdddd")
+ code := []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.STOP)}
+ codeChange := []byte{byte(vm.STOP)}
+ db := state.NewDatabaseForTesting()
+ sdb, _ := state.New(types.EmptyRootHash, db)
+ sdb.SetBalance(consensus.SystemAddress, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
+ sdb.SetCode(contract, code, tracing.CodeChangeUnspecified)
+ sdb.SetCode(omitted, []byte{byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.REVERT)}, tracing.CodeChangeUnspecified)
+ root, err := sdb.Commit(params.Rules{}, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ sdb, _ = state.New(root, db)
+ var inner vm.StateDB = sdb
+ if traced {
+ inner = state.NewHookedState(sdb, &tracing.Hooks{OnBalanceChange: func(common.Address, *big.Int, *big.Int, tracing.BalanceChangeReason) {}})
+ }
+ list := bal.NewConstructionBlockAccessList()
+ systemState := &systemTxStateDB{StateDB: inner, accessList: list}
+ // Finalize's direct reads are excluded; the transfer and upgrade
+ // share the following system transaction's net-change baseline.
+ systemState.GetBalance(readOnly)
+ systemState.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
+ systemState.AddBalance(coinbase, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
+ systemState.SetCode(upgrade, codeChange, tracing.CodeChangeSystemContractUpgrade)
+ systemState.Prepare(rules, coinbase, coinbase, &contract, vm.ActivePrecompiles(rules), nil)
+ systemState.SetTxContext(common.Hash{1}, 0, 1)
+ evm := vm.NewEVM(vm.BlockContext{BlockNumber: common.Big1, Time: 1, CanTransfer: CanTransfer, Transfer: Transfer}, systemState, &config, vm.Config{})
+ defer evm.Release()
+ if _, _, err := evm.Call(coinbase, contract, nil, vm.NewGasBudget(1_000_000, 0), uint256.NewInt(100)); err != nil {
+ t.Fatal(err)
+ }
+ systemState.SetNonce(coinbase, 1, tracing.NonceChangeEoACall)
+ systemState.Finalise(rules)
+
+ // A failed, omitted attempt contributes no accesses. Its preceding
+ // protocol write remains and belongs to n+1 because no tx follows.
+ systemState.SetCode(tail, codeChange, tracing.CodeChangeSystemContractUpgrade)
+ systemState.Prepare(rules, coinbase, coinbase, &omitted, vm.ActivePrecompiles(rules), nil)
+ systemState.SetTxContext(common.Hash{2}, 1, 2)
+ if _, _, err := evm.Call(coinbase, omitted, nil, vm.NewGasBudget(1_000_000, 0), common.U2560); !errors.Is(err, vm.ErrExecutionReverted) {
+ t.Fatalf("omitted transaction: %v", err)
+ }
+ list.Merge(finaliseProtocolChanges(inner, rules, 2))
+
+ want := bal.NewConstructionBlockAccessList()
+ want.BalanceChange(1, consensus.SystemAddress, common.U2560)
+ want.NonceChange(coinbase, 1, 1)
+ want.BalanceChange(1, contract, uint256.NewInt(100))
+ want.StorageWrite(1, contract, common.Hash{}, common.Hash{31: 1})
+ want.CodeChange(upgrade, 1, codeChange)
+ want.CodeChange(tail, 2, codeChange)
+ if got := list.ToEncodingObj(); got.Hash() != want.ToEncodingObj().Hash() {
+ t.Fatalf("unexpected BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
+ }
+ replayed, _ := state.New(root, db)
+ if err := replayed.ApplyBlockAccessList(list.ToEncodingObj()); err != nil {
+ t.Fatal(err)
+ }
+ if got, want := replayed.IntermediateRoot(rules), sdb.IntermediateRoot(rules); got != want {
+ t.Fatalf("BAL post-state %x, executed %x", got, want)
+ }
+ })
+ }
+}
+
+type protocolBALTestEngine struct{ consensus.Engine }
+
+func (e *protocolBALTestEngine) Finalize(_ consensus.ChainHeaderReader, _ *types.Header, s vm.StateDB, _ *types.Body, _ *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64, _ *tracing.Hooks) error {
+ s.GetBalance(common.HexToAddress("0xcccc"))
+ s.SetCode(common.HexToAddress("0xdddd"), []byte{byte(vm.STOP)}, tracing.CodeChangeSystemContractUpgrade)
+ return nil
+}
+
+func TestStateProcessorProtocolAccessList(t *testing.T) {
+ for _, active := range []bool{false, true} {
+ t.Run(map[bool]string{false: "pre-fork", true: "active"}[active], func(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ config.HaberTime, config.HaberFixTime, config.BohrTime, config.PascalTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.PragueTime = u64(1)
+ config.LorentzTime, config.MaxwellTime, config.FermiTime = u64(1), u64(1), u64(1)
+ config.OsakaTime, config.MendelTime, config.PasteurTime = u64(1), u64(1), u64(1)
+ config.BlobScheduleConfig = ¶ms.BlobScheduleConfig{Cancun: params.DefaultCancunBlobConfig, Prague: params.DefaultPragueBlobConfigBSC}
+ if active {
+ config.AmsterdamTime = u64(1)
+ }
+ db := rawdb.NewMemoryDatabase()
+ defer db.Close()
+ triedb := triedb.NewDatabase(db, nil)
+ defer triedb.Close()
+ genesis := (&Genesis{Config: &config}).MustCommit(db, triedb)
+ engine := &protocolBALTestEngine{Engine: ethash.NewFaker()}
+ defer engine.Close()
+ chain, err := NewHeaderChain(db, &config, engine, func() bool { return false })
+ if err != nil {
+ t.Fatal(err)
+ }
+ sdb, _ := state.New(genesis.Root(), state.NewDatabase(triedb, nil))
+ header := &types.Header{ParentHash: genesis.Hash(), Number: common.Big1, Time: 1, Difficulty: common.Big1, GasLimit: 30_000_000}
+ block := types.NewBlockWithHeader(header)
+ res, err := NewStateProcessor(chain).Process(context.Background(), block, sdb, nil, nil, vm.Config{}, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ list, hash := res.encodedAccessList()
+ want := bal.NewConstructionBlockAccessList()
+ if active {
+ want.CodeChange(params.HistoryStorageAddress, 0, params.HistoryStorageCode)
+ want.NonceChange(params.HistoryStorageAddress, 0, 1)
+ want.StorageWrite(0, params.HistoryStorageAddress, common.Hash{}, genesis.Hash())
+ want.CodeChange(common.HexToAddress("0xdddd"), 1, []byte{byte(vm.STOP)})
+ }
+ if hash != want.ToEncodingObj().Hash() {
+ t.Fatalf("unexpected BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
+ }
+ if res.GasUsed != 0 || len(res.Receipts) != 0 {
+ t.Fatalf("protocol operations changed gas or receipts: %+v", res)
+ }
+ if active {
+ replayed, _ := state.New(genesis.Root(), state.NewDatabase(triedb, nil))
+ if err := replayed.ApplyBlockAccessList(list); err != nil {
+ t.Fatal(err)
+ }
+ rules := config.Rules(common.Big1, false, 1)
+ if got, want := replayed.IntermediateRoot(rules), sdb.IntermediateRoot(rules); got != want {
+ t.Fatalf("BAL post-state %x, executed %x", got, want)
+ }
+ }
+ })
+ }
+}
From 471a266ce71849add2fe4fb2874016436f62bb41 Mon Sep 17 00:00:00 2001
From: qybdyx
Date: Thu, 12 Nov 2026 14:09:45 +0800
Subject: [PATCH 05/14] core, consensus/parlia, miner: centralize system
contract upgrades
Move block-begin upgrades and their BAL recording into PreExecution.
Apply post-normal-transaction upgrades in PostExecution before Parlia
executes system transactions, leaving their journal changes pending
for the next included transaction or the final n+1 scope.
Update mining, BidBlock and replay callers for the shared phases, and
cover Jenner upgrade timing and BAL replay before and after Amsterdam.
---
cmd/evm/internal/t8ntool/execution.go | 2 +-
consensus/parlia/bid_block.go | 11 +++++
consensus/parlia/parlia.go | 6 +--
core/chain_makers.go | 2 +-
core/state_processor.go | 22 +++++----
core/state_processor_bsc_test.go | 69 +++++++++++++++++++++++++++
core/state_processor_parallel.go | 2 +-
eth/api_debug_replay.go | 2 +-
eth/state_accessor.go | 2 -
eth/tracers/api.go | 11 -----
internal/ethapi/api.go | 1 -
internal/ethapi/simulate.go | 2 +-
miner/worker.go | 37 +++++++++-----
13 files changed, 124 insertions(+), 45 deletions(-)
diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go
index 7df58c9a5e..a46c681d0c 100644
--- a/cmd/evm/internal/t8ntool/execution.go
+++ b/cmd/evm/internal/t8ntool/execution.go
@@ -340,7 +340,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
for _, receipt := range receipts {
allLogs = append(allLogs, receipt.Logs...)
}
- requests, bal, err := core.PostExecution(context.Background(), chainConfig, vmContext.BlockNumber, vmContext.Time, allLogs, pre.Env.Withdrawals, evm, uint32(len(receipts)+1))
+ requests, bal, err := core.PostExecution(context.Background(), &types.Header{Time: pre.Env.ParentTimestamp}, chainConfig, vmContext.BlockNumber, vmContext.Time, allLogs, pre.Env.Withdrawals, evm, uint32(len(receipts)+1))
if err != nil {
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("failed to process post-execution: %v", err))
}
diff --git a/consensus/parlia/bid_block.go b/consensus/parlia/bid_block.go
index 6fc6a7beca..d0462b4295 100644
--- a/consensus/parlia/bid_block.go
+++ b/consensus/parlia/bid_block.go
@@ -14,11 +14,13 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
+ "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
+ "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -49,6 +51,15 @@ func (p *Parlia) PrepareForBidBlock(chain consensus.ChainHeaderReader, header *t
// FinalizeAndAssembleBidBlock assembles a BidBlock with unsigned system txs.
func (p *Parlia) FinalizeAndAssembleBidBlock(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
+ parent := chain.GetHeaderByHash(header.ParentHash)
+ if parent == nil {
+ return nil, nil, errors.New("parent not found")
+ }
+ evm := vm.NewEVM(core.NewEVMBlockContext(header, chainContext{chain, p}, nil), state, p.chainConfig, vm.Config{Tracer: tracer})
+ defer evm.Release()
+ if _, _, err := core.PostExecution(context.Background(), parent, p.chainConfig, header.Number, header.Time, nil, body.Withdrawals, evm, uint32(len(body.Transactions)+1)); err != nil {
+ return nil, nil, err
+ }
block, receipts, err := p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxPacking)
if err != nil {
return nil, nil, err
diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go
index 7858d24a34..30c7800e5b 100644
--- a/consensus/parlia/parlia.go
+++ b/consensus/parlia/parlia.go
@@ -1433,6 +1433,7 @@ func (p *Parlia) EstimateGasReservedForSystemTxs(chain consensus.ChainHeaderRead
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
+// Finalize executes system transactions after the caller has run core.PostExecution.
func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body,
receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64, tracer *tracing.Hooks) error {
txs := &body.Transactions
@@ -1456,8 +1457,6 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
return errors.New("parent not found")
}
- systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false)
-
if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, txs, receipts, systemTxs, usedGas, systemTxImporting, tracer)
if err != nil {
@@ -1543,6 +1542,7 @@ const (
systemTxPacking
)
+// FinalizeAndAssemble requires core.PostExecution to have run on the state.
func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
return p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxMining)
@@ -1565,8 +1565,6 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
return nil, nil, errors.New("parent not found")
}
- systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false)
-
if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 1af6e06344..f765fba7e4 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -353,7 +353,7 @@ func (b *BlockGen) collectRequests(readonly bool) (requests [][]byte, bal *bal.C
blockContext := NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
evm := vm.NewEVM(blockContext, statedb, b.cm.config, vm.Config{})
- requests, bal, err := PostExecution(context.Background(), b.cm.config, b.header.Number, b.header.Time, blockLogs, b.withdrawals, evm, uint32(len(b.txs)+1))
+ requests, bal, err := PostExecution(context.Background(), b.parent.Header(), b.cm.config, b.header.Number, b.header.Time, blockLogs, b.withdrawals, evm, uint32(len(b.txs)+1))
if err != nil {
panic(fmt.Sprintf("failed to run post-execution: %v", err))
}
diff --git a/core/state_processor.go b/core/state_processor.go
index f6b6d17e0c..1bbfdc1753 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -118,14 +118,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
}
}
- // Handle upgrade built-in system contract code
- systemcontracts.TryUpdateBuildInSystemContract(p.chain.Config(), blockNumber, parent.Time, block.Time(), statedb, true)
recordSystemBAL := config.IsInBSC() && config.IsAmsterdam(blockNumber, block.Time())
- var upgradeBAL *bal.ConstructionBlockAccessList
- if recordSystemBAL {
- rules := config.Rules(blockNumber, header.Difficulty.Sign() == 0, header.Time)
- upgradeBAL = finaliseProtocolChanges(statedb, rules, 0)
- }
var (
context = NewEVMBlockContext(header, p.chain, nil)
@@ -135,7 +128,6 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
txNum = len(block.Transactions())
)
defer evm.Release()
- blockAccessList.Merge(upgradeBAL)
if jumpDestCache != nil {
evm.SetJumpDestCache(jumpDestCache)
@@ -192,7 +184,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
blockAccessList.Merge(bal)
}
numUserReceipts := len(receipts)
- requests, bal, err := PostExecution(ctx, config, block.Number(), block.Time(), allLogs, block.Withdrawals(), evm, uint32(len(block.Transactions())+1))
+ requests, bal, err := PostExecution(ctx, parent, config, block.Number(), block.Time(), allLogs, block.Withdrawals(), evm, uint32(len(block.Transactions())+1))
if err != nil {
return nil, err
}
@@ -254,6 +246,10 @@ func PreExecution(ctx context.Context, beaconRoot *common.Hash, parent *types.He
if config.IsAmsterdam(number, time) {
blockAccessList = bal.NewConstructionBlockAccessList()
}
+ systemcontracts.TryUpdateBuildInSystemContract(config, number, parent.Time, time, evm.StateDB, true)
+ if config.IsInBSC() && config.IsAmsterdam(number, time) {
+ blockAccessList.Merge(finaliseProtocolChanges(evm.StateDB, evm.GetRules(), 0))
+ }
// EIP-4788
if beaconRoot != nil {
ProcessBeaconBlockRoot(*beaconRoot, evm, blockAccessList)
@@ -268,13 +264,19 @@ func PreExecution(ctx context.Context, beaconRoot *common.Hash, parent *types.He
// PostExecution processes the withdrawals and then the post-execution system
// calls when Prague is enabled. If Prague is not activated, it returns null
// requests to differentiate from empty requests.
-func PostExecution(ctx context.Context, config *params.ChainConfig, number *big.Int, time uint64, allLogs []*types.Log, withdrawals types.Withdrawals, evm *vm.EVM, blockAccessIndex uint32) (requests [][]byte, blockAccessList *bal.ConstructionBlockAccessList, err error) {
+// On BSC, it applies upgrades before Parlia executes the system transactions.
+func PostExecution(ctx context.Context, parent *types.Header, config *params.ChainConfig, number *big.Int, time uint64, allLogs []*types.Log, withdrawals types.Withdrawals, evm *vm.EVM, blockAccessIndex uint32) (requests [][]byte, blockAccessList *bal.ConstructionBlockAccessList, err error) {
_, _, spanEnd := telemetry.StartSpan(ctx, "core.postExecution")
defer spanEnd(&err)
if config.IsAmsterdam(number, time) {
blockAccessList = bal.NewConstructionBlockAccessList()
}
+ if config.IsInBSC() {
+ // Leave the mutations pending for the following Parlia system transaction.
+ systemcontracts.TryUpdateBuildInSystemContract(config, number, parent.Time, time, evm.StateDB, false)
+ return nil, blockAccessList, nil
+ }
rules := config.Rules(number, true, time) // IsMerge is always true
// Withdrawals are applied before the system calls
diff --git a/core/state_processor_bsc_test.go b/core/state_processor_bsc_test.go
index 8e2caa0b60..c361f8eefa 100644
--- a/core/state_processor_bsc_test.go
+++ b/core/state_processor_bsc_test.go
@@ -1,6 +1,7 @@
package core
import (
+ "bytes"
"context"
"errors"
"math/big"
@@ -11,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types/bal"
@@ -20,6 +22,73 @@ import (
"github.com/holiman/uint256"
)
+func TestBSCSystemContractUpgradePhases(t *testing.T) {
+ genesisHash := systemcontracts.GenesisHash
+ systemcontracts.GenesisHash = params.BSCGenesisHash
+ t.Cleanup(func() { systemcontracts.GenesisHash = genesisHash })
+ for _, active := range []bool{false, true} {
+ t.Run(map[bool]string{false: "pre-amsterdam", true: "amsterdam"}[active], func(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ config.JennerTime = u64(2)
+ if active {
+ config.AmsterdamTime = u64(2)
+ }
+ parent := &types.Header{Time: 1}
+ number := big.NewInt(2)
+ db := state.NewDatabaseForTesting()
+ sdb, _ := state.New(types.EmptyRootHash, db)
+ evm := vm.NewEVM(vm.BlockContext{BlockNumber: number, Time: 2}, sdb, &config, vm.Config{})
+ defer evm.Release()
+ list := bal.NewConstructionBlockAccessList()
+ list.Merge(PreExecution(context.Background(), nil, parent, &config, evm, number, 2))
+ for _, addr := range []common.Address{vm.CAS20ActivationRegistryAddress, vm.CAS20PolicyRegistryAddress} {
+ if code := sdb.GetCode(addr); !bytes.Equal(code, vm.CAS20MarkerCode) {
+ t.Fatalf("missing pre-execution sentinel at %s", addr)
+ }
+ }
+ paymentLane := common.HexToAddress(systemcontracts.PaymentLaneContract)
+ if len(sdb.GetCode(paymentLane)) != 0 {
+ t.Fatal("payment-lane contract upgraded before normal transactions")
+ }
+ requests, postBAL, err := PostExecution(context.Background(), parent, &config, number, 2, nil, nil, evm, 2)
+ if err != nil || requests != nil {
+ t.Fatalf("post-execution: requests %v, err %v", requests, err)
+ }
+ list.Merge(postBAL)
+ code := sdb.GetCode(paymentLane)
+ if len(code) == 0 {
+ t.Fatal("payment-lane contract not upgraded before system transactions")
+ }
+ coinbase := common.HexToAddress("0xbeef")
+ systemState := &systemTxStateDB{StateDB: sdb, accessList: list}
+ systemState.Prepare(evm.GetRules(), coinbase, coinbase, nil, nil, nil)
+ systemState.SetTxContext(common.Hash{1}, 0, 1)
+ systemState.SetNonce(coinbase, 1, tracing.NonceChangeEoACall)
+ systemState.Finalise(evm.GetRules())
+
+ want := bal.NewConstructionBlockAccessList()
+ if active {
+ want.CodeChange(vm.CAS20ActivationRegistryAddress, 0, vm.CAS20MarkerCode)
+ want.CodeChange(vm.CAS20PolicyRegistryAddress, 0, vm.CAS20MarkerCode)
+ want.CodeChange(paymentLane, 1, code)
+ want.NonceChange(coinbase, 1, 1)
+ }
+ if list.ToEncodingObj().Hash() != want.ToEncodingObj().Hash() {
+ t.Fatalf("unexpected upgrade BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
+ }
+ if active {
+ replayed, _ := state.New(types.EmptyRootHash, db)
+ if err := replayed.ApplyBlockAccessList(list.ToEncodingObj()); err != nil {
+ t.Fatal(err)
+ }
+ if replayed.IntermediateRoot(evm.GetRules()) != sdb.IntermediateRoot(evm.GetRules()) {
+ t.Fatal("BAL replay differs from executed upgrades")
+ }
+ }
+ })
+ }
+}
+
func TestSystemTransactionAccessList(t *testing.T) {
for _, traced := range []bool{false, true} {
t.Run(map[bool]string{false: "plain", true: "traced"}[traced], func(t *testing.T) {
diff --git a/core/state_processor_parallel.go b/core/state_processor_parallel.go
index 17dd1af44e..fd35d31a33 100644
--- a/core/state_processor_parallel.go
+++ b/core/state_processor_parallel.go
@@ -285,7 +285,7 @@ func (p *StateProcessor) processParallel(ctx context.Context, block *types.Block
if precompileCache != nil {
postEVM.SetPrecompileCache(precompileCache)
}
- requests, postBAL, err := PostExecution(ctx, config, header.Number, header.Time, allLogs, block.Withdrawals(), postEVM, postIndex)
+ requests, postBAL, err := PostExecution(ctx, parent, config, header.Number, header.Time, allLogs, block.Withdrawals(), postEVM, postIndex)
postEVM.Release()
if err != nil {
return nil, err
diff --git a/eth/api_debug_replay.go b/eth/api_debug_replay.go
index 44a0de38c5..8ddfa4628b 100644
--- a/eth/api_debug_replay.go
+++ b/eth/api_debug_replay.go
@@ -183,7 +183,7 @@ func (api *DebugAPI) replayBuild(ctx context.Context, block *types.Block, stated
for _, r := range receipts {
allLogs = append(allLogs, r.Logs...)
}
- _, postBal, err := core.PostExecution(ctx, config, header.Number, header.Time, allLogs, block.Withdrawals(), evm, uint32(tcount+1))
+ _, postBal, err := core.PostExecution(ctx, parent, config, header.Number, header.Time, allLogs, block.Withdrawals(), evm, uint32(tcount+1))
if err != nil {
return nil, nil, 0, common.Hash{}, err
}
diff --git a/eth/state_accessor.go b/eth/state_accessor.go
index e4497ef54f..05db81b634 100644
--- a/eth/state_accessor.go
+++ b/eth/state_accessor.go
@@ -252,8 +252,6 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
if err != nil {
return nil, vm.BlockContext{}, nil, nil, err
}
- // upgrade built-in system contract before normal txs if Feynman is not enabled
- systemcontracts.TryUpdateBuildInSystemContract(eth.blockchain.Config(), block.Number(), parent.Time(), block.Time(), statedb, true)
// Insert parent beacon block root in the state as per EIP-4788.
context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil)
evm := vm.NewEVM(context, statedb, eth.blockchain.Config(), vm.Config{})
diff --git a/eth/tracers/api.go b/eth/tracers/api.go
index 9d05533148..29b5146e8b 100644
--- a/eth/tracers/api.go
+++ b/eth/tracers/api.go
@@ -393,9 +393,6 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed
break
}
- // upgrade built-in system contract before normal txs if Feynman is not enabled
- systemcontracts.TryUpdateBuildInSystemContract(api.backend.ChainConfig(), next.Number(), block.Time(), next.Time(), statedb, true)
-
// Insert block's parent beacon block root in the state
// as per EIP-4788.
context := core.NewEVMBlockContext(next.Header(), api.chainContext(ctx), nil)
@@ -541,9 +538,6 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
}
defer release()
- // upgrade built-in system contract before normal txs if Feynman is not enabled
- systemcontracts.TryUpdateBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true)
-
var (
roots []common.Hash
signer = types.MakeSigner(api.backend.ChainConfig(), block.Number(), block.Time())
@@ -627,8 +621,6 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
}
defer release()
- // upgrade built-in system contract before normal txs if Feynman is not enabled
- systemcontracts.TryUpdateBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true)
blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
evm := vm.NewEVM(blockCtx, statedb, api.backend.ChainConfig(), vm.Config{})
defer evm.Release()
@@ -820,9 +812,6 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
}
defer release()
- // upgrade built-in system contract before normal txs if Feynman is not enabled
- systemcontracts.TryUpdateBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true)
-
// Retrieve the tracing configurations, or use default values
var (
logConfig logger.Config
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go
index 186ebb12c3..4926abb50a 100644
--- a/internal/ethapi/api.go
+++ b/internal/ethapi/api.go
@@ -1402,7 +1402,6 @@ func (api *BlockChainAPI) replay(ctx context.Context, block *types.Block, accoun
// Block level pre-execution, in the same order as core.StateProcessor.Process. Without it the
// system contracts touched here keep their parent values and transactions reading them diverge.
config := api.b.ChainConfig()
- systemcontracts.TryUpdateBuildInSystemContract(config, block.Number(), parent.Time(), block.Time(), statedb, true)
evm := vm.NewEVM(core.NewEVMBlockContext(block.Header(), api.b.Chain(), nil), statedb, config, vm.Config{})
defer evm.Release()
core.PreExecution(ctx, block.BeaconRoot(), parent.Header(), config, evm, block.Number(), block.Time())
diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go
index bdf3aba5e0..d7843891b6 100644
--- a/internal/ethapi/simulate.go
+++ b/internal/ethapi/simulate.go
@@ -530,7 +530,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
blockBody.Withdrawals = *block.BlockOverrides.Withdrawals
}
// Process the withdrawals and EIP-7685 requests
- requests, bal, err := core.PostExecution(ctx, sim.chainConfig, header.Number, header.Time, allLogs, blockBody.Withdrawals, evm, uint32(len(block.Calls)+1))
+ requests, bal, err := core.PostExecution(ctx, parent, sim.chainConfig, header.Number, header.Time, allLogs, blockBody.Withdrawals, evm, uint32(len(block.Calls)+1))
if err != nil {
return nil, nil, nil, err
}
diff --git a/miner/worker.go b/miner/worker.go
index 782522cf95..095404ae3a 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -38,7 +38,6 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/stateless"
- "github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/txorder"
"github.com/ethereum/go-ethereum/core/types"
@@ -1168,9 +1167,6 @@ func (w *worker) prepareWork(genParams *generateParams, witness bool) (*environm
return nil, err
}
- // Handle upgrade built-in system contract code
- systemcontracts.TryUpdateBuildInSystemContract(w.chainConfig, header.Number, parent.Time, header.Time, env.state, true)
-
env.bal.Merge(core.PreExecution(context.Background(), header.ParentBeaconRoot, parent, w.chainConfig, env.evm, header.Number, header.Time))
return env, nil
}
@@ -1322,15 +1318,10 @@ func (w *worker) generateWork(genParam *generateParams, witness bool) *newPayloa
}
}
}
- allLogs := make([]*types.Log, 0)
- for _, r := range work.receipts {
- allLogs = append(allLogs, r.Logs...)
- }
- requests, postBAL, err := core.PostExecution(context.Background(), w.chainConfig, work.header.Number, work.header.Time, allLogs, genParam.withdrawals, work.evm, uint32(work.tcount+1))
+ requests, err := w.postExecution(work, genParam.withdrawals)
if err != nil {
return &newPayloadResult{err: err}
}
- work.bal.Merge(postBAL)
if requests != nil {
reqHash := types.CalcRequestsHash(requests)
work.header.RequestsHash = &reqHash
@@ -1363,6 +1354,23 @@ func (w *worker) generateWork(genParam *generateParams, witness bool) *newPayloa
}
}
+func (w *worker) postExecution(work *environment, withdrawals types.Withdrawals) ([][]byte, error) {
+ parent := w.chain.GetHeader(work.header.ParentHash, work.header.Number.Uint64()-1)
+ if parent == nil {
+ return nil, consensus.ErrUnknownAncestor
+ }
+ var allLogs []*types.Log
+ for _, receipt := range work.receipts {
+ allLogs = append(allLogs, receipt.Logs...)
+ }
+ requests, postBAL, err := core.PostExecution(context.Background(), parent, w.chainConfig, work.header.Number, work.header.Time, allLogs, withdrawals, work.evm, uint32(work.tcount+1))
+ if err != nil {
+ return nil, err
+ }
+ work.bal.Merge(postBAL)
+ return requests, nil
+}
+
// delay returns the time budget for local block building.
// For the last block in a validator's turn it caps the budget to BlockInterval/4,
// giving the next validator enough lead time to avoid producing an empty block.
@@ -1707,8 +1715,13 @@ func (w *worker) commit(env *environment, interval func(), start time.Time) erro
if env.header.EmptyWithdrawalsHash() {
body.Withdrawals = make([]*types.Withdrawal, 0)
}
- // TODO: Run post-execution and collect Parlia finalization changes into
- // the BAL, then attach it to the block before enabling Amsterdam on BSC.
+ if w.chainConfig.IsInBSC() {
+ if _, err := w.postExecution(env, body.Withdrawals); err != nil {
+ return err
+ }
+ }
+ // TODO: Collect Parlia finalization changes into the BAL and attach it
+ // to the block before enabling Amsterdam on BSC.
block, receipts, err := core.AssembleBlock(w.engine, w.chain, types.CopyHeader(env.header), env.state, &body, env.receipts, env.bal)
env.committed = true
if err != nil {
From 9823484bc2f16ac39788b3c52f29169d33a98386 Mon Sep 17 00:00:00 2001
From: qybdyx
Date: Thu, 12 Nov 2026 16:17:08 +0800
Subject: [PATCH 06/14] core, consensus/parlia: simplify bsc bal tests
Keep BAL replay in the Parlia integration test and remove duplicate
replay and direct-finalization comparisons. Exercise the helper through
a traced StateDB, with the integration test covering the plain path.
---
consensus/parlia/bal_test.go | 19 +---
core/state_processor_bsc_test.go | 143 ++++++++++++-------------------
2 files changed, 54 insertions(+), 108 deletions(-)
diff --git a/consensus/parlia/bal_test.go b/consensus/parlia/bal_test.go
index 9f02ad7b2f..b12cc55dda 100644
--- a/consensus/parlia/bal_test.go
+++ b/consensus/parlia/bal_test.go
@@ -74,20 +74,6 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
if len(res.Receipts) != 1 || res.Receipts[0].TransactionIndex != 0 || res.Receipts[0].TxHash != tx.Hash() || res.GasUsed == 0 {
t.Fatalf("unexpected system transaction result: %+v", res)
}
- // Compare gas and receipts with Parlia's existing direct finalization path.
- rules := config.Rules(header.Number, false, header.Time)
- reference, _ := state.New(parent.Root, state.NewDatabase(triedb, nil))
- reference.SetState(params.HistoryStorageAddress, common.Hash{31: 1}, parent.Hash())
- reference.Finalise(rules)
- var referenceReceipts []*types.Receipt
- var referenceGas uint64
- received := []*types.Transaction{tx}
- if err := engine.Finalize(chain, header, reference, &types.Body{}, &referenceReceipts, &received, &referenceGas, nil); err != nil {
- t.Fatal(err)
- }
- if res.GasUsed != referenceGas || types.DeriveSha(res.Receipts, trie.NewStackTrie(nil)) != types.DeriveSha(types.Receipts(referenceReceipts), trie.NewStackTrie(nil)) {
- t.Fatal("BAL collection changed Parlia gas or receipts")
- }
want := bal.NewConstructionBlockAccessList()
want.StorageWrite(0, params.HistoryStorageAddress, common.Hash{31: 1}, parent.Hash())
want.BalanceChange(1, consensus.SystemAddress, common.U2560)
@@ -99,10 +85,7 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
if err := replayed.ApplyBlockAccessList(list); err != nil {
t.Fatal(err)
}
- header.Root = replayed.IntermediateRoot(rules)
- if header.Root != reference.IntermediateRoot(rules) {
- t.Fatal("BAL collection changed Parlia post-state")
- }
+ header.Root = replayed.IntermediateRoot(config.Rules(header.Number, false, header.Time))
header.GasUsed = res.GasUsed
header.ReceiptHash = types.DeriveSha(res.Receipts, trie.NewStackTrie(nil))
header.Bloom = types.MergeBloom(res.Receipts)
diff --git a/core/state_processor_bsc_test.go b/core/state_processor_bsc_test.go
index c361f8eefa..00bbb6567f 100644
--- a/core/state_processor_bsc_test.go
+++ b/core/state_processor_bsc_test.go
@@ -76,93 +76,66 @@ func TestBSCSystemContractUpgradePhases(t *testing.T) {
if list.ToEncodingObj().Hash() != want.ToEncodingObj().Hash() {
t.Fatalf("unexpected upgrade BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
}
- if active {
- replayed, _ := state.New(types.EmptyRootHash, db)
- if err := replayed.ApplyBlockAccessList(list.ToEncodingObj()); err != nil {
- t.Fatal(err)
- }
- if replayed.IntermediateRoot(evm.GetRules()) != sdb.IntermediateRoot(evm.GetRules()) {
- t.Fatal("BAL replay differs from executed upgrades")
- }
- }
})
}
}
func TestSystemTransactionAccessList(t *testing.T) {
- for _, traced := range []bool{false, true} {
- t.Run(map[bool]string{false: "plain", true: "traced"}[traced], func(t *testing.T) {
- config := *params.ParliaTestChainConfig
- config.AmsterdamTime = new(uint64)
- rules := config.Rules(common.Big1, false, 1)
- coinbase := common.HexToAddress("0xbeef")
- contract := common.HexToAddress("0xcafe")
- upgrade := common.HexToAddress("0xaaaa")
- omitted := common.HexToAddress("0xbbbb")
- readOnly := common.HexToAddress("0xcccc")
- tail := common.HexToAddress("0xdddd")
- code := []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.STOP)}
- codeChange := []byte{byte(vm.STOP)}
- db := state.NewDatabaseForTesting()
- sdb, _ := state.New(types.EmptyRootHash, db)
- sdb.SetBalance(consensus.SystemAddress, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
- sdb.SetCode(contract, code, tracing.CodeChangeUnspecified)
- sdb.SetCode(omitted, []byte{byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.REVERT)}, tracing.CodeChangeUnspecified)
- root, err := sdb.Commit(params.Rules{}, 0)
- if err != nil {
- t.Fatal(err)
- }
- sdb, _ = state.New(root, db)
- var inner vm.StateDB = sdb
- if traced {
- inner = state.NewHookedState(sdb, &tracing.Hooks{OnBalanceChange: func(common.Address, *big.Int, *big.Int, tracing.BalanceChangeReason) {}})
- }
- list := bal.NewConstructionBlockAccessList()
- systemState := &systemTxStateDB{StateDB: inner, accessList: list}
- // Finalize's direct reads are excluded; the transfer and upgrade
- // share the following system transaction's net-change baseline.
- systemState.GetBalance(readOnly)
- systemState.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
- systemState.AddBalance(coinbase, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
- systemState.SetCode(upgrade, codeChange, tracing.CodeChangeSystemContractUpgrade)
- systemState.Prepare(rules, coinbase, coinbase, &contract, vm.ActivePrecompiles(rules), nil)
- systemState.SetTxContext(common.Hash{1}, 0, 1)
- evm := vm.NewEVM(vm.BlockContext{BlockNumber: common.Big1, Time: 1, CanTransfer: CanTransfer, Transfer: Transfer}, systemState, &config, vm.Config{})
- defer evm.Release()
- if _, _, err := evm.Call(coinbase, contract, nil, vm.NewGasBudget(1_000_000, 0), uint256.NewInt(100)); err != nil {
- t.Fatal(err)
- }
- systemState.SetNonce(coinbase, 1, tracing.NonceChangeEoACall)
- systemState.Finalise(rules)
+ config := *params.ParliaTestChainConfig
+ config.AmsterdamTime = new(uint64)
+ rules := config.Rules(common.Big1, false, 1)
+ coinbase := common.HexToAddress("0xbeef")
+ contract := common.HexToAddress("0xcafe")
+ upgrade := common.HexToAddress("0xaaaa")
+ omitted := common.HexToAddress("0xbbbb")
+ readOnly := common.HexToAddress("0xcccc")
+ tail := common.HexToAddress("0xdddd")
+ code := []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.STOP)}
+ codeChange := []byte{byte(vm.STOP)}
+ db := state.NewDatabaseForTesting()
+ sdb, _ := state.New(types.EmptyRootHash, db)
+ sdb.SetBalance(consensus.SystemAddress, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
+ sdb.SetCode(contract, code, tracing.CodeChangeUnspecified)
+ sdb.SetCode(omitted, []byte{byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.REVERT)}, tracing.CodeChangeUnspecified)
+ sdb.Finalise(params.Rules{})
+ inner := state.NewHookedState(sdb, &tracing.Hooks{OnBalanceChange: func(common.Address, *big.Int, *big.Int, tracing.BalanceChangeReason) {}})
+ list := bal.NewConstructionBlockAccessList()
+ systemState := &systemTxStateDB{StateDB: inner, accessList: list}
+ // Finalize's direct reads are excluded; the transfer and upgrade
+ // share the following system transaction's net-change baseline.
+ systemState.GetBalance(readOnly)
+ systemState.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
+ systemState.AddBalance(coinbase, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
+ systemState.SetCode(upgrade, codeChange, tracing.CodeChangeSystemContractUpgrade)
+ systemState.Prepare(rules, coinbase, coinbase, &contract, vm.ActivePrecompiles(rules), nil)
+ systemState.SetTxContext(common.Hash{1}, 0, 1)
+ evm := vm.NewEVM(vm.BlockContext{BlockNumber: common.Big1, Time: 1, CanTransfer: CanTransfer, Transfer: Transfer}, systemState, &config, vm.Config{})
+ defer evm.Release()
+ if _, _, err := evm.Call(coinbase, contract, nil, vm.NewGasBudget(1_000_000, 0), uint256.NewInt(100)); err != nil {
+ t.Fatal(err)
+ }
+ systemState.SetNonce(coinbase, 1, tracing.NonceChangeEoACall)
+ systemState.Finalise(rules)
- // A failed, omitted attempt contributes no accesses. Its preceding
- // protocol write remains and belongs to n+1 because no tx follows.
- systemState.SetCode(tail, codeChange, tracing.CodeChangeSystemContractUpgrade)
- systemState.Prepare(rules, coinbase, coinbase, &omitted, vm.ActivePrecompiles(rules), nil)
- systemState.SetTxContext(common.Hash{2}, 1, 2)
- if _, _, err := evm.Call(coinbase, omitted, nil, vm.NewGasBudget(1_000_000, 0), common.U2560); !errors.Is(err, vm.ErrExecutionReverted) {
- t.Fatalf("omitted transaction: %v", err)
- }
- list.Merge(finaliseProtocolChanges(inner, rules, 2))
+ // A failed, omitted attempt contributes no accesses. Its preceding
+ // protocol write remains and belongs to n+1 because no tx follows.
+ systemState.SetCode(tail, codeChange, tracing.CodeChangeSystemContractUpgrade)
+ systemState.Prepare(rules, coinbase, coinbase, &omitted, vm.ActivePrecompiles(rules), nil)
+ systemState.SetTxContext(common.Hash{2}, 1, 2)
+ if _, _, err := evm.Call(coinbase, omitted, nil, vm.NewGasBudget(1_000_000, 0), common.U2560); !errors.Is(err, vm.ErrExecutionReverted) {
+ t.Fatalf("omitted transaction: %v", err)
+ }
+ list.Merge(finaliseProtocolChanges(inner, rules, 2))
- want := bal.NewConstructionBlockAccessList()
- want.BalanceChange(1, consensus.SystemAddress, common.U2560)
- want.NonceChange(coinbase, 1, 1)
- want.BalanceChange(1, contract, uint256.NewInt(100))
- want.StorageWrite(1, contract, common.Hash{}, common.Hash{31: 1})
- want.CodeChange(upgrade, 1, codeChange)
- want.CodeChange(tail, 2, codeChange)
- if got := list.ToEncodingObj(); got.Hash() != want.ToEncodingObj().Hash() {
- t.Fatalf("unexpected BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
- }
- replayed, _ := state.New(root, db)
- if err := replayed.ApplyBlockAccessList(list.ToEncodingObj()); err != nil {
- t.Fatal(err)
- }
- if got, want := replayed.IntermediateRoot(rules), sdb.IntermediateRoot(rules); got != want {
- t.Fatalf("BAL post-state %x, executed %x", got, want)
- }
- })
+ want := bal.NewConstructionBlockAccessList()
+ want.BalanceChange(1, consensus.SystemAddress, common.U2560)
+ want.NonceChange(coinbase, 1, 1)
+ want.BalanceChange(1, contract, uint256.NewInt(100))
+ want.StorageWrite(1, contract, common.Hash{}, common.Hash{31: 1})
+ want.CodeChange(upgrade, 1, codeChange)
+ want.CodeChange(tail, 2, codeChange)
+ if got := list.ToEncodingObj(); got.Hash() != want.ToEncodingObj().Hash() {
+ t.Fatalf("unexpected BAL:\n%s\nwant:\n%s", list.PrettyPrint(), want.PrettyPrint())
}
}
@@ -218,16 +191,6 @@ func TestStateProcessorProtocolAccessList(t *testing.T) {
if res.GasUsed != 0 || len(res.Receipts) != 0 {
t.Fatalf("protocol operations changed gas or receipts: %+v", res)
}
- if active {
- replayed, _ := state.New(genesis.Root(), state.NewDatabase(triedb, nil))
- if err := replayed.ApplyBlockAccessList(list); err != nil {
- t.Fatal(err)
- }
- rules := config.Rules(common.Big1, false, 1)
- if got, want := replayed.IntermediateRoot(rules), sdb.IntermediateRoot(rules); got != want {
- t.Fatalf("BAL post-state %x, executed %x", got, want)
- }
- }
})
}
}
From adca93e6acd37de98a68cf7e63217bf17c6cb99a Mon Sep 17 00:00:00 2001
From: qybdyx
Date: Fri, 13 Nov 2026 16:12:08 +0800
Subject: [PATCH 07/14] core, consensus/parlia, miner: defer bsc post execution
Keep Feynman-era contract upgrades at the start of Parlia finalization.
Run BSC post-execution after system transactions and collect remaining
protocol changes at n+1. Share the same ordering for signed mining and
unsigned BidBlock assembly before computing the final state root.
Restore the upstream PostExecution signature and preserve Ethereum
ordering. Update simulation and extend the Parlia integration test
to check post-execution ordering and packed/imported state agreement.
---
cmd/evm/internal/t8ntool/execution.go | 2 +-
consensus/parlia/bal_test.go | 16 ++++++++++
consensus/parlia/bid_block.go | 11 -------
consensus/parlia/parlia.go | 18 +++++++++--
core/chain_makers.go | 5 ++-
core/state_processor.go | 32 ++++++++++++-------
core/state_processor_bsc_test.go | 11 ++++---
core/state_processor_parallel.go | 2 +-
eth/api_debug_replay.go | 2 +-
internal/ethapi/simulate.go | 7 ++++-
miner/worker.go | 45 ++++++++++-----------------
11 files changed, 88 insertions(+), 63 deletions(-)
diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go
index a46c681d0c..7df58c9a5e 100644
--- a/cmd/evm/internal/t8ntool/execution.go
+++ b/cmd/evm/internal/t8ntool/execution.go
@@ -340,7 +340,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
for _, receipt := range receipts {
allLogs = append(allLogs, receipt.Logs...)
}
- requests, bal, err := core.PostExecution(context.Background(), &types.Header{Time: pre.Env.ParentTimestamp}, chainConfig, vmContext.BlockNumber, vmContext.Time, allLogs, pre.Env.Withdrawals, evm, uint32(len(receipts)+1))
+ requests, bal, err := core.PostExecution(context.Background(), chainConfig, vmContext.BlockNumber, vmContext.Time, allLogs, pre.Env.Withdrawals, evm, uint32(len(receipts)+1))
if err != nil {
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("failed to process post-execution: %v", err))
}
diff --git a/consensus/parlia/bal_test.go b/consensus/parlia/bal_test.go
index b12cc55dda..cfa6667fef 100644
--- a/consensus/parlia/bal_test.go
+++ b/consensus/parlia/bal_test.go
@@ -95,4 +95,20 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
if err := core.NewBlockValidator(&config, chain).ValidateState(block, sdb, res, false); err != nil {
t.Fatalf("Parlia execution differs from the expected BAL and post-state: %v", err)
}
+ miningState, _ := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ preEVM := vm.NewEVM(core.NewEVMBlockContext(header, chain, nil), miningState, &config, vm.Config{})
+ core.PreExecution(context.Background(), nil, parent, &config, preEVM, header.Number, header.Time)
+ preEVM.Release()
+ packingHeader := types.CopyHeader(header)
+ packingHeader.GasUsed = 0
+ packed, _, err := engine.FinalizeAndAssembleBidBlock(chain, packingHeader, miningState, &types.Body{}, nil, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if miningState.TxIndex() != len(packed.Transactions()) {
+ t.Fatal("post-execution did not follow the packed system transactions")
+ }
+ if packed.Root() != header.Root {
+ t.Fatal("packed system transactions differ from imported post-state")
+ }
}
diff --git a/consensus/parlia/bid_block.go b/consensus/parlia/bid_block.go
index d0462b4295..6fc6a7beca 100644
--- a/consensus/parlia/bid_block.go
+++ b/consensus/parlia/bid_block.go
@@ -14,13 +14,11 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
- "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
- "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -51,15 +49,6 @@ func (p *Parlia) PrepareForBidBlock(chain consensus.ChainHeaderReader, header *t
// FinalizeAndAssembleBidBlock assembles a BidBlock with unsigned system txs.
func (p *Parlia) FinalizeAndAssembleBidBlock(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
- parent := chain.GetHeaderByHash(header.ParentHash)
- if parent == nil {
- return nil, nil, errors.New("parent not found")
- }
- evm := vm.NewEVM(core.NewEVMBlockContext(header, chainContext{chain, p}, nil), state, p.chainConfig, vm.Config{Tracer: tracer})
- defer evm.Release()
- if _, _, err := core.PostExecution(context.Background(), parent, p.chainConfig, header.Number, header.Time, nil, body.Withdrawals, evm, uint32(len(body.Transactions)+1)); err != nil {
- return nil, nil, err
- }
block, receipts, err := p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxPacking)
if err != nil {
return nil, nil, err
diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go
index 30c7800e5b..9fb919173c 100644
--- a/consensus/parlia/parlia.go
+++ b/consensus/parlia/parlia.go
@@ -1433,7 +1433,6 @@ func (p *Parlia) EstimateGasReservedForSystemTxs(chain consensus.ChainHeaderRead
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
-// Finalize executes system transactions after the caller has run core.PostExecution.
func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body,
receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64, tracer *tracing.Hooks) error {
txs := &body.Transactions
@@ -1457,6 +1456,8 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
return errors.New("parent not found")
}
+ systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false)
+
if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, txs, receipts, systemTxs, usedGas, systemTxImporting, tracer)
if err != nil {
@@ -1542,7 +1543,6 @@ const (
systemTxPacking
)
-// FinalizeAndAssemble requires core.PostExecution to have run on the state.
func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
return p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxMining)
@@ -1565,6 +1565,8 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
return nil, nil, errors.New("parent not found")
}
+ systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false)
+
if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
@@ -1625,6 +1627,18 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
}
}
+ if p.chainConfig.IsAmsterdam(header.Number, header.Time) {
+ var allLogs []*types.Log
+ for _, receipt := range receipts {
+ allLogs = append(allLogs, receipt.Logs...)
+ }
+ evm := vm.NewEVM(core.NewEVMBlockContext(header, cx, nil), state, p.chainConfig, vm.Config{Tracer: tracer})
+ _, _, err := core.PostExecution(context.Background(), p.chainConfig, header.Number, header.Time, allLogs, body.Withdrawals, evm, uint32(len(body.Transactions)+1))
+ evm.Release()
+ if err != nil {
+ return nil, nil, err
+ }
+ }
// should not happen. Once happen, stop the node is better than broadcast the block
if header.GasLimit < header.GasUsed {
return nil, nil, errors.New("gas consumption of system txs exceed the gas limit")
diff --git a/core/chain_makers.go b/core/chain_makers.go
index f765fba7e4..2d1f6efe40 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -338,6 +338,9 @@ func (b *BlockGen) ConsensusLayerRequests() [][]byte {
}
func (b *BlockGen) collectRequests(readonly bool) (requests [][]byte, bal *bal.ConstructionBlockAccessList) {
+ if b.cm.config.IsInBSC() {
+ return nil, nil
+ }
statedb := b.statedb
if readonly {
// The system contracts clear themselves on a system-initiated read.
@@ -353,7 +356,7 @@ func (b *BlockGen) collectRequests(readonly bool) (requests [][]byte, bal *bal.C
blockContext := NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
evm := vm.NewEVM(blockContext, statedb, b.cm.config, vm.Config{})
- requests, bal, err := PostExecution(context.Background(), b.parent.Header(), b.cm.config, b.header.Number, b.header.Time, blockLogs, b.withdrawals, evm, uint32(len(b.txs)+1))
+ requests, bal, err := PostExecution(context.Background(), b.cm.config, b.header.Number, b.header.Time, blockLogs, b.withdrawals, evm, uint32(len(b.txs)+1))
if err != nil {
panic(fmt.Sprintf("failed to run post-execution: %v", err))
}
diff --git a/core/state_processor.go b/core/state_processor.go
index 1bbfdc1753..80e599500a 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -184,11 +184,15 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
blockAccessList.Merge(bal)
}
numUserReceipts := len(receipts)
- requests, bal, err := PostExecution(ctx, parent, config, block.Number(), block.Time(), allLogs, block.Withdrawals(), evm, uint32(len(block.Transactions())+1))
- if err != nil {
- return nil, err
+ var requests [][]byte
+ if config.IsNotInBSC() {
+ var postBAL *bal.ConstructionBlockAccessList
+ requests, postBAL, err = PostExecution(ctx, config, block.Number(), block.Time(), allLogs, block.Withdrawals(), evm, uint32(len(block.Transactions())+1))
+ if err != nil {
+ return nil, err
+ }
+ blockAccessList.Merge(postBAL)
}
- blockAccessList.Merge(bal)
gasUsed := gp.Used()
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
@@ -201,9 +205,6 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
if err != nil {
return nil, err
}
- if recordSystemBAL {
- blockAccessList.Merge(finaliseProtocolChanges(tracingStateDB, evm.GetRules(), uint32(len(block.Transactions())+1)))
- }
if len(receipts) != len(block.Transactions()) {
return nil, fmt.Errorf("receipt count mismatch: have %d, want %d", len(receipts), len(block.Transactions()))
}
@@ -212,6 +213,14 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
pipeline.feedReceipt(receipt)
allLogs = append(allLogs, receipt.Logs...)
}
+ if config.IsInBSC() {
+ var postBAL *bal.ConstructionBlockAccessList
+ requests, postBAL, err = PostExecution(ctx, config, block.Number(), block.Time(), allLogs, block.Withdrawals(), evm, uint32(len(block.Transactions())+1))
+ if err != nil {
+ return nil, err
+ }
+ blockAccessList.Merge(postBAL)
+ }
if err := lane.Verify(gasUsed); err != nil {
return nil, laneReject(err)
@@ -264,8 +273,8 @@ func PreExecution(ctx context.Context, beaconRoot *common.Hash, parent *types.He
// PostExecution processes the withdrawals and then the post-execution system
// calls when Prague is enabled. If Prague is not activated, it returns null
// requests to differentiate from empty requests.
-// On BSC, it applies upgrades before Parlia executes the system transactions.
-func PostExecution(ctx context.Context, parent *types.Header, config *params.ChainConfig, number *big.Int, time uint64, allLogs []*types.Log, withdrawals types.Withdrawals, evm *vm.EVM, blockAccessIndex uint32) (requests [][]byte, blockAccessList *bal.ConstructionBlockAccessList, err error) {
+// On BSC, it collects remaining protocol changes after all system transactions.
+func PostExecution(ctx context.Context, config *params.ChainConfig, number *big.Int, time uint64, allLogs []*types.Log, withdrawals types.Withdrawals, evm *vm.EVM, blockAccessIndex uint32) (requests [][]byte, blockAccessList *bal.ConstructionBlockAccessList, err error) {
_, _, spanEnd := telemetry.StartSpan(ctx, "core.postExecution")
defer spanEnd(&err)
@@ -273,8 +282,9 @@ func PostExecution(ctx context.Context, parent *types.Header, config *params.Cha
blockAccessList = bal.NewConstructionBlockAccessList()
}
if config.IsInBSC() {
- // Leave the mutations pending for the following Parlia system transaction.
- systemcontracts.TryUpdateBuildInSystemContract(config, number, parent.Time, time, evm.StateDB, false)
+ if config.IsAmsterdam(number, time) {
+ blockAccessList.Merge(finaliseProtocolChanges(evm.StateDB, evm.GetRules(), blockAccessIndex))
+ }
return nil, blockAccessList, nil
}
rules := config.Rules(number, true, time) // IsMerge is always true
diff --git a/core/state_processor_bsc_test.go b/core/state_processor_bsc_test.go
index 00bbb6567f..b782c7f781 100644
--- a/core/state_processor_bsc_test.go
+++ b/core/state_processor_bsc_test.go
@@ -50,11 +50,7 @@ func TestBSCSystemContractUpgradePhases(t *testing.T) {
if len(sdb.GetCode(paymentLane)) != 0 {
t.Fatal("payment-lane contract upgraded before normal transactions")
}
- requests, postBAL, err := PostExecution(context.Background(), parent, &config, number, 2, nil, nil, evm, 2)
- if err != nil || requests != nil {
- t.Fatalf("post-execution: requests %v, err %v", requests, err)
- }
- list.Merge(postBAL)
+ systemcontracts.TryUpdateBuildInSystemContract(&config, number, parent.Time, 2, sdb, false)
code := sdb.GetCode(paymentLane)
if len(code) == 0 {
t.Fatal("payment-lane contract not upgraded before system transactions")
@@ -65,6 +61,11 @@ func TestBSCSystemContractUpgradePhases(t *testing.T) {
systemState.SetTxContext(common.Hash{1}, 0, 1)
systemState.SetNonce(coinbase, 1, tracing.NonceChangeEoACall)
systemState.Finalise(evm.GetRules())
+ requests, postBAL, err := PostExecution(context.Background(), &config, number, 2, nil, nil, evm, 2)
+ if err != nil || requests != nil {
+ t.Fatalf("post-execution: requests %v, err %v", requests, err)
+ }
+ list.Merge(postBAL)
want := bal.NewConstructionBlockAccessList()
if active {
diff --git a/core/state_processor_parallel.go b/core/state_processor_parallel.go
index fd35d31a33..17dd1af44e 100644
--- a/core/state_processor_parallel.go
+++ b/core/state_processor_parallel.go
@@ -285,7 +285,7 @@ func (p *StateProcessor) processParallel(ctx context.Context, block *types.Block
if precompileCache != nil {
postEVM.SetPrecompileCache(precompileCache)
}
- requests, postBAL, err := PostExecution(ctx, parent, config, header.Number, header.Time, allLogs, block.Withdrawals(), postEVM, postIndex)
+ requests, postBAL, err := PostExecution(ctx, config, header.Number, header.Time, allLogs, block.Withdrawals(), postEVM, postIndex)
postEVM.Release()
if err != nil {
return nil, err
diff --git a/eth/api_debug_replay.go b/eth/api_debug_replay.go
index 8ddfa4628b..44a0de38c5 100644
--- a/eth/api_debug_replay.go
+++ b/eth/api_debug_replay.go
@@ -183,7 +183,7 @@ func (api *DebugAPI) replayBuild(ctx context.Context, block *types.Block, stated
for _, r := range receipts {
allLogs = append(allLogs, r.Logs...)
}
- _, postBal, err := core.PostExecution(ctx, parent, config, header.Number, header.Time, allLogs, block.Withdrawals(), evm, uint32(tcount+1))
+ _, postBal, err := core.PostExecution(ctx, config, header.Number, header.Time, allLogs, block.Withdrawals(), evm, uint32(tcount+1))
if err != nil {
return nil, nil, 0, common.Hash{}, err
}
diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go
index d7843891b6..95ed0cfb48 100644
--- a/internal/ethapi/simulate.go
+++ b/internal/ethapi/simulate.go
@@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
@@ -529,8 +530,12 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
if sim.chainConfig.IsShanghai(header.Number, header.Time) {
blockBody.Withdrawals = *block.BlockOverrides.Withdrawals
}
+ if sim.isBSC() {
+ // Simulation omits Parlia system transactions but still applies contract upgrades.
+ systemcontracts.TryUpdateBuildInSystemContract(sim.chainConfig, header.Number, parent.Time, header.Time, sim.state, false)
+ }
// Process the withdrawals and EIP-7685 requests
- requests, bal, err := core.PostExecution(ctx, parent, sim.chainConfig, header.Number, header.Time, allLogs, blockBody.Withdrawals, evm, uint32(len(block.Calls)+1))
+ requests, bal, err := core.PostExecution(ctx, sim.chainConfig, header.Number, header.Time, allLogs, blockBody.Withdrawals, evm, uint32(len(block.Calls)+1))
if err != nil {
return nil, nil, nil, err
}
diff --git a/miner/worker.go b/miner/worker.go
index 095404ae3a..dd3e44137f 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -1318,13 +1318,22 @@ func (w *worker) generateWork(genParam *generateParams, witness bool) *newPayloa
}
}
}
- requests, err := w.postExecution(work, genParam.withdrawals)
- if err != nil {
- return &newPayloadResult{err: err}
- }
- if requests != nil {
- reqHash := types.CalcRequestsHash(requests)
- work.header.RequestsHash = &reqHash
+ var requests [][]byte
+ if w.chainConfig.IsNotInBSC() {
+ var allLogs []*types.Log
+ for _, receipt := range work.receipts {
+ allLogs = append(allLogs, receipt.Logs...)
+ }
+ var postBAL *bal.ConstructionBlockAccessList
+ requests, postBAL, err = core.PostExecution(context.Background(), w.chainConfig, work.header.Number, work.header.Time, allLogs, genParam.withdrawals, work.evm, uint32(work.tcount+1))
+ if err != nil {
+ return &newPayloadResult{err: err}
+ }
+ work.bal.Merge(postBAL)
+ if requests != nil {
+ reqHash := types.CalcRequestsHash(requests)
+ work.header.RequestsHash = &reqHash
+ }
}
fees := work.state.GetBalance(consensus.SystemAddress)
@@ -1354,23 +1363,6 @@ func (w *worker) generateWork(genParam *generateParams, witness bool) *newPayloa
}
}
-func (w *worker) postExecution(work *environment, withdrawals types.Withdrawals) ([][]byte, error) {
- parent := w.chain.GetHeader(work.header.ParentHash, work.header.Number.Uint64()-1)
- if parent == nil {
- return nil, consensus.ErrUnknownAncestor
- }
- var allLogs []*types.Log
- for _, receipt := range work.receipts {
- allLogs = append(allLogs, receipt.Logs...)
- }
- requests, postBAL, err := core.PostExecution(context.Background(), parent, w.chainConfig, work.header.Number, work.header.Time, allLogs, withdrawals, work.evm, uint32(work.tcount+1))
- if err != nil {
- return nil, err
- }
- work.bal.Merge(postBAL)
- return requests, nil
-}
-
// delay returns the time budget for local block building.
// For the last block in a validator's turn it caps the budget to BlockInterval/4,
// giving the next validator enough lead time to avoid producing an empty block.
@@ -1715,11 +1707,6 @@ func (w *worker) commit(env *environment, interval func(), start time.Time) erro
if env.header.EmptyWithdrawalsHash() {
body.Withdrawals = make([]*types.Withdrawal, 0)
}
- if w.chainConfig.IsInBSC() {
- if _, err := w.postExecution(env, body.Withdrawals); err != nil {
- return err
- }
- }
// TODO: Collect Parlia finalization changes into the BAL and attach it
// to the block before enabling Amsterdam on BSC.
block, receipts, err := core.AssembleBlock(w.engine, w.chain, types.CopyHeader(env.header), env.state, &body, env.receipts, env.bal)
From 1845e5a9c4eaf636e72022d93c67fe1c931e8086 Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 19:44:39 +0800
Subject: [PATCH 08/14] consensus/parlia, core: complete block access list
assembly
---
consensus/parlia/bal_test.go | 216 +++++++++++++++++++++++++++++---
consensus/parlia/bid_block.go | 9 +-
consensus/parlia/parlia.go | 51 +++++---
consensus/parlia/parlia_test.go | 3 +-
core/blockchain_test.go | 5 +-
core/state_processor.go | 12 +-
core/state_processor_bsc.go | 5 +
eth/handler_test.go | 3 +-
miner/worker.go | 2 -
9 files changed, 254 insertions(+), 52 deletions(-)
diff --git a/consensus/parlia/bal_test.go b/consensus/parlia/bal_test.go
index cfa6667fef..3047d70cfc 100644
--- a/consensus/parlia/bal_test.go
+++ b/consensus/parlia/bal_test.go
@@ -5,12 +5,14 @@ import (
"math/big"
"testing"
+ "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/systemcontracts"
+ "github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
@@ -28,13 +30,17 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
config.OsakaTime, config.MendelTime, config.PasteurTime, config.AmsterdamTime = new(uint64), new(uint64), new(uint64), new(uint64)
config.BlobScheduleConfig = ¶ms.BlobScheduleConfig{Cancun: params.DefaultCancunBlobConfig, Prague: params.DefaultPragueBlobConfigBSC}
validatorContract := common.HexToAddress(systemcontracts.ValidatorContract)
+ slashContract := common.HexToAddress(systemcontracts.SlashContract)
+ code := []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.LOG0), byte(vm.STOP)}
db := rawdb.NewMemoryDatabase()
defer db.Close()
triedb := triedb.NewDatabase(db, nil)
defer triedb.Close()
gspec := &core.Genesis{Config: &config, GasLimit: 30_000_000, Alloc: types.GenesisAlloc{
consensus.SystemAddress: {Balance: big.NewInt(100)},
- validatorContract: {Code: []byte{byte(vm.PUSH1), 1, byte(vm.PUSH1), 0, byte(vm.SSTORE), byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.LOG0), byte(vm.STOP)}, Nonce: 1},
+ testAddr: {Balance: big.NewInt(1_000_000_000)},
+ validatorContract: {Code: code, Nonce: 1},
+ slashContract: {Code: code, Nonce: 1},
params.HistoryStorageAddress: {Code: params.HistoryStorageCode, Nonce: 1},
}}
genesis := gspec.MustCommit(db, triedb)
@@ -54,7 +60,13 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
parent.Coinbase, parent.Difficulty = testAddr, diffInTurn
rawdb.WriteHeader(db, parent)
engine.recentSnaps.Add(parent.Hash(), newSnapshot(config.Parlia, engine.signatures, 1, parent.Hash(), []common.Address{testAddr}, nil, nil))
- header := &types.Header{ParentHash: parent.Hash(), Number: big.NewInt(2), Time: 3, Difficulty: diffInTurn, Coinbase: testAddr, GasLimit: 30_000_000}
+ header := &types.Header{
+ ParentHash: parent.Hash(), Number: big.NewInt(2), Time: 3,
+ Difficulty: diffInTurn, Coinbase: testAddr, GasLimit: 30_000_000,
+ BaseFee: common.Big0, ExcessBlobGas: new(uint64), BlobGasUsed: new(uint64),
+ SlotNumber: new(uint64),
+ }
+ *header.SlotNumber = header.Number.Uint64()
data, err := engine.validatorSetABI.Pack("deposit", testAddr)
if err != nil {
t.Fatal(err)
@@ -95,20 +107,190 @@ func TestParliaSystemTransactionBAL(t *testing.T) {
if err := core.NewBlockValidator(&config, chain).ValidateState(block, sdb, res, false); err != nil {
t.Fatalf("Parlia execution differs from the expected BAL and post-state: %v", err)
}
- miningState, _ := state.New(parent.Root, state.NewDatabase(triedb, nil))
- preEVM := vm.NewEVM(core.NewEVMBlockContext(header, chain, nil), miningState, &config, vm.Config{})
- core.PreExecution(context.Background(), nil, parent, &config, preEVM, header.Number, header.Time)
- preEVM.Release()
- packingHeader := types.CopyHeader(header)
- packingHeader.GasUsed = 0
- packed, _, err := engine.FinalizeAndAssembleBidBlock(chain, packingHeader, miningState, &types.Body{}, nil, nil)
- if err != nil {
- t.Fatal(err)
- }
- if miningState.TxIndex() != len(packed.Transactions()) {
- t.Fatal("post-execution did not follow the packed system transactions")
- }
- if packed.Root() != header.Root {
- t.Fatal("packed system transactions differ from imported post-state")
+ engine.Authorize(testAddr, nil, func(_ accounts.Account, tx *types.Transaction, _ *big.Int) (*types.Transaction, error) {
+ return types.SignTx(tx, engine.signer, testKey)
+ })
+ t.Run("missing-bal", func(t *testing.T) {
+ for _, lists := range [][]*bal.ConstructionBlockAccessList{nil, {nil}} {
+ for _, packing := range []bool{false, true} {
+ sdb, err := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if packing {
+ _, _, err = engine.FinalizeAndAssembleBidBlock(chain, types.CopyHeader(header), sdb, &types.Body{}, nil, nil, lists...)
+ } else {
+ _, _, err = core.AssembleBlock(engine, chain, types.CopyHeader(header), sdb, &types.Body{}, nil, lists...)
+ }
+ if err == nil {
+ t.Fatal("assembled Amsterdam block without input BAL")
+ }
+ if sdb.GetNonce(testAddr) != 0 || sdb.GetBalance(consensus.SystemAddress).Uint64() != 100 {
+ t.Fatal("missing BAL changed finalization state")
+ }
+ }
+ }
+ })
+ for _, tc := range []struct {
+ name string
+ packing bool
+ user bool
+ traced bool
+ empty bool
+ slash bool
+ }{
+ {name: "signed"},
+ {name: "packing", packing: true},
+ {name: "user-and-system", user: true},
+ {name: "traced", traced: true},
+ {name: "no-system-transactions", empty: true},
+ {name: "multiple-system-transactions", slash: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ parent := types.CopyHeader(parent)
+ if tc.empty {
+ sdb, err := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ if err != nil {
+ t.Fatal(err)
+ }
+ sdb.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
+ parent.Root, err = sdb.Commit(config.Rules(parent.Number, false, parent.Time), parent.Number.Uint64())
+ if err != nil {
+ t.Fatal(err)
+ }
+ rawdb.WriteHeader(db, parent)
+ }
+ validators := []common.Address{testAddr}
+ if tc.slash {
+ validators = append(validators, common.HexToAddress("0x1234"))
+ }
+ engine.recentSnaps.Add(parent.Hash(), newSnapshot(config.Parlia, engine.signatures, 1, parent.Hash(), validators, nil, nil))
+ newState := func() *state.StateDB {
+ sdb, err := state.New(parent.Root, state.NewDatabase(triedb, nil))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return sdb
+ }
+ miningState := newState()
+ miningHeader := types.CopyHeader(header)
+ miningHeader.ParentHash = parent.Hash()
+ miningHeader.GasUsed = 0
+ miningHeader.Bloom = types.Bloom{}
+ if tc.slash {
+ miningHeader.Difficulty = diffNoTurn
+ }
+ miningHeader.BlockAccessListHash = nil
+ var hooks *tracing.Hooks
+ var changes int
+ if tc.traced {
+ hooks = &tracing.Hooks{OnBalanceChange: func(common.Address, *big.Int, *big.Int, tracing.BalanceChangeReason) { changes++ }}
+ }
+ preEVM := vm.NewEVM(core.NewEVMBlockContext(miningHeader, chain, nil), miningState, &config, vm.Config{})
+ defer preEVM.Release()
+ list := core.PreExecution(context.Background(), nil, parent, &config, preEVM, header.Number, header.Time)
+ body := &types.Body{}
+ var receipts []*types.Receipt
+ if tc.user {
+ tx := types.MustSignNewTx(testKey, types.MakeSigner(&config, header.Number, header.Time), &types.LegacyTx{
+ To: &validatorContract, Gas: 100_000, GasPrice: common.Big1,
+ })
+ miningState.SetTxContext(tx.Hash(), 0, 1)
+ gp := core.NewGasPool(header.GasLimit)
+ receipt, txBAL, err := core.ApplyTransaction(context.Background(), preEVM, gp, miningState, miningHeader, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ list.Merge(txBAL)
+ miningHeader.GasUsed = gp.Used()
+ body.Transactions = append(body.Transactions, tx)
+ receipts = append(receipts, receipt)
+ }
+ var produced *types.Block
+ var err error
+ if tc.packing {
+ produced, receipts, err = engine.FinalizeAndAssembleBidBlock(chain, miningHeader, miningState, body, receipts, hooks, list)
+ } else if tc.traced {
+ produced, receipts, err = engine.FinalizeAndAssemble(chain, miningHeader, miningState, body, receipts, hooks, list)
+ } else {
+ produced, receipts, err = core.AssembleBlock(engine, chain, miningHeader, miningState, body, receipts, list)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ if produced.AccessList() == nil || produced.BlockAccessListHash() == nil || produced.AccessList().Hash() != *produced.BlockAccessListHash() {
+ t.Fatal("missing or inconsistent block access list")
+ }
+ if err := produced.AccessList().Validate(produced.GasLimit(), len(produced.Transactions())); err != nil {
+ t.Fatal(err)
+ }
+ if !tc.user && !tc.empty && !tc.slash && produced.AccessList().Hash() != hash {
+ t.Fatal("assembled BAL differs from expected system changes")
+ }
+ if tc.empty && len(produced.Transactions()) != 0 {
+ t.Fatal("empty reward balance produced a system transaction")
+ }
+ if tc.slash {
+ if len(produced.Transactions()) != 2 || list.Accounts[testAddr].NonceChanges[1] != 1 || list.Accounts[testAddr].NonceChanges[2] != 2 {
+ t.Fatal("system transaction indices do not follow block order")
+ }
+ }
+ if miningState.TxIndex() != len(produced.Transactions()) {
+ t.Fatal("post-execution did not follow system transactions")
+ }
+ if tc.traced && changes == 0 {
+ t.Fatal("finalization bypassed state tracing")
+ }
+ // Signing the packing template changes transaction hashes, not BAL indices or state.
+ if tc.packing {
+ body := produced.Body()
+ body.Transactions = append([]*types.Transaction(nil), body.Transactions...)
+ for i, tx := range body.Transactions {
+ body.Transactions[i], err = engine.SignSystemTx(tx, config.ChainID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ produced = types.NewBlock(produced.Header(), body, receipts, trie.NewStackTrie(nil)).WithAccessListUnsafe(produced.AccessList())
+ }
+ imported := newState()
+ res, err := core.NewStateProcessor(chain.HeaderChain()).Process(context.Background(), produced, imported, nil, nil, vm.Config{Tracer: hooks}, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := core.NewBlockValidator(&config, chain).ValidateState(produced, imported, res, false); err != nil {
+ t.Fatalf("independent import: %v", err)
+ }
+ for i, receipt := range res.Receipts {
+ if receipt.TxHash != produced.Transactions()[i].Hash() || receipt.TransactionIndex != uint(i) {
+ t.Fatal("imported receipt has an incorrect transaction index or hash")
+ }
+ if receipt.GasUsed != receipts[i].GasUsed || receipt.CumulativeGasUsed != receipts[i].CumulativeGasUsed || len(receipt.Logs) != len(receipts[i].Logs) {
+ t.Fatal("imported receipt differs from assembly")
+ }
+ for j, log := range receipt.Logs {
+ if log.Index != receipts[i].Logs[j].Index || log.TxIndex != uint(i) || log.TxHash != receipt.TxHash || log.BlockHash != produced.Hash() {
+ t.Fatal("imported log metadata does not follow block order")
+ }
+ }
+ }
+ applied := newState()
+ if err := applied.ApplyBlockAccessList(produced.AccessList()); err != nil {
+ t.Fatal(err)
+ }
+ if root := applied.IntermediateRoot(config.Rules(header.Number, false, header.Time)); root != produced.Root() {
+ t.Fatalf("BAL replay root %s differs from produced root %s", root, produced.Root())
+ }
+ for _, sdb := range []*state.StateDB{imported, applied} {
+ for _, addr := range []common.Address{testAddr, validatorContract, consensus.SystemAddress} {
+ if sdb.GetBalance(addr).Cmp(miningState.GetBalance(addr)) != 0 || sdb.GetNonce(addr) != miningState.GetNonce(addr) {
+ t.Fatalf("account state mismatch at %s", addr)
+ }
+ }
+ if sdb.GetState(validatorContract, common.Hash{}) != miningState.GetState(validatorContract, common.Hash{}) {
+ t.Fatal("system contract storage mismatch")
+ }
+ }
+ })
}
}
diff --git a/consensus/parlia/bid_block.go b/consensus/parlia/bid_block.go
index 6fc6a7beca..108be94884 100644
--- a/consensus/parlia/bid_block.go
+++ b/consensus/parlia/bid_block.go
@@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc"
@@ -48,12 +49,8 @@ func (p *Parlia) PrepareForBidBlock(chain consensus.ChainHeaderReader, header *t
// FinalizeAndAssembleBidBlock assembles a BidBlock with unsigned system txs.
func (p *Parlia) FinalizeAndAssembleBidBlock(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
- body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
- block, receipts, err := p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxPacking)
- if err != nil {
- return nil, nil, err
- }
- return block, receipts, nil
+ body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, blockAccessLists ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
+ return p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxPacking, blockAccessLists...)
}
// SignSystemTx signs a BidBlock system tx with the validator key.
diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go
index 9fb919173c..013334de5f 100644
--- a/consensus/parlia/parlia.go
+++ b/consensus/parlia/parlia.go
@@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
@@ -1544,13 +1545,25 @@ const (
)
func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
- body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
- return p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxMining)
+ body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, blockAccessLists ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
+ return p.finalizeAndAssemble(chain, header, state, body, receipts, tracer, systemTxMining, blockAccessLists...)
}
-func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
- body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, mode systemTxMode) (*types.Block, []*types.Receipt, error) {
- // No block rewards in PoA, so the state remains as is and uncles are dropped
+func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, statedb *state.StateDB,
+ body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, mode systemTxMode, blockAccessLists ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
+ var accessList *bal.ConstructionBlockAccessList
+ var tracingState vm.StateDB = statedb
+ var finalizationState vm.StateDB = statedb
+ if p.chainConfig.IsAmsterdam(header.Number, header.Time) {
+ if len(blockAccessLists) != 1 || blockAccessLists[0] == nil {
+ return nil, nil, errors.New("missing block access list for Amsterdam assembly")
+ }
+ accessList = blockAccessLists[0]
+ if tracer != nil {
+ tracingState = state.NewHookedState(statedb, tracer)
+ }
+ finalizationState = core.NewSystemTxStateDB(tracingState, accessList)
+ }
cx := chainContext{ChainHeaderReader: chain, parlia: p}
if body.Transactions == nil {
@@ -1565,17 +1578,17 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
return nil, nil, errors.New("parent not found")
}
- systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false)
+ systemcontracts.TryUpdateBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, finalizationState, false)
if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
- err := p.initializeFeynmanContract(state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
+ err := p.initializeFeynmanContract(finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
return nil, nil, fmt.Errorf("init feynman contract failed: %v", err)
}
}
if header.Number.Cmp(common.Big1) == 0 {
- err := p.initContract(state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
+ err := p.initContract(finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
return nil, nil, errors.New("init contract failed")
}
@@ -1599,20 +1612,20 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
}
}
if !signedRecently {
- err = p.slash(spoiledVal, state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
+ err = p.slash(spoiledVal, finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
log.Error("slash validator failed", "block hash", header.Hash(), "address", spoiledVal)
}
}
}
- err := p.distributeIncoming(header.Coinbase, state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
+ err := p.distributeIncoming(header.Coinbase, finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer)
if err != nil {
return nil, nil, err
}
if p.chainConfig.IsPlato(header.Number) {
- if err := p.distributeFinalityReward(chain, state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer); err != nil {
+ if err := p.distributeFinalityReward(chain, finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer); err != nil {
return nil, nil, err
}
}
@@ -1621,7 +1634,7 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
if p.chainConfig.IsFeynman(header.Number, header.Time) && isBreatheBlock(parent.Time, header.Time) {
// we should avoid update validators in the Feynman upgrade block
if !p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
- if err := p.updateValidatorSetV2(state, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer); err != nil {
+ if err := p.updateValidatorSetV2(finalizationState, header, cx, &body.Transactions, &receipts, nil, &header.GasUsed, mode, tracer); err != nil {
return nil, nil, err
}
}
@@ -1632,24 +1645,32 @@ func (p *Parlia) finalizeAndAssemble(chain consensus.ChainHeaderReader, header *
for _, receipt := range receipts {
allLogs = append(allLogs, receipt.Logs...)
}
- evm := vm.NewEVM(core.NewEVMBlockContext(header, cx, nil), state, p.chainConfig, vm.Config{Tracer: tracer})
- _, _, err := core.PostExecution(context.Background(), p.chainConfig, header.Number, header.Time, allLogs, body.Withdrawals, evm, uint32(len(body.Transactions)+1))
+ evm := vm.NewEVM(core.NewEVMBlockContext(header, cx, nil), tracingState, p.chainConfig, vm.Config{Tracer: tracer})
+ _, postBAL, err := core.PostExecution(context.Background(), p.chainConfig, header.Number, header.Time, allLogs, body.Withdrawals, evm, uint32(len(body.Transactions)+1))
evm.Release()
if err != nil {
return nil, nil, err
}
+ accessList.Merge(postBAL)
}
// should not happen. Once happen, stop the node is better than broadcast the block
if header.GasLimit < header.GasUsed {
return nil, nil, errors.New("gas consumption of system txs exceed the gas limit")
}
header.UncleHash = types.EmptyUncleHash
+ if accessList != nil {
+ header.Root = statedb.IntermediateRoot(chain.Config().Rules(header.Number, false, header.Time))
+ list := accessList.ToEncodingObj()
+ hash := list.Hash()
+ header.BlockAccessListHash = &hash
+ return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)).WithAccessListUnsafe(list), receipts, nil
+ }
var blk *types.Block
var rootHash common.Hash
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
- rootHash = state.IntermediateRoot(chain.Config().Rules(header.Number, false, header.Time))
+ rootHash = statedb.IntermediateRoot(chain.Config().Rules(header.Number, false, header.Time))
wg.Done()
}()
go func() {
diff --git a/consensus/parlia/parlia_test.go b/consensus/parlia/parlia_test.go
index f08f3c183d..a345334bf7 100644
--- a/consensus/parlia/parlia_test.go
+++ b/consensus/parlia/parlia_test.go
@@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
@@ -1458,7 +1459,7 @@ func (c *mockParlia) Finalize(chain consensus.ChainHeaderReader, header *types.H
return
}
-func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
+func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, _ ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
// Finalize block
c.Finalize(chain, header, state, body, nil, nil, nil, tracer)
diff --git a/core/blockchain_test.go b/core/blockchain_test.go
index ecfa377af1..15f73ec4eb 100644
--- a/core/blockchain_test.go
+++ b/core/blockchain_test.go
@@ -46,6 +46,7 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/vm/program"
"github.com/ethereum/go-ethereum/crypto"
@@ -4105,6 +4106,8 @@ type mockParlia struct {
consensus.Engine
}
+var _ blockAssembler = (*mockParlia)(nil)
+
func (c *mockParlia) Author(header *types.Header) (common.Address, error) {
return header.Coinbase, nil
}
@@ -4135,7 +4138,7 @@ func (c *mockParlia) Finalize(chain consensus.ChainHeaderReader, header *types.H
return
}
-func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
+func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, _ ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
// Finalize block
c.Finalize(chain, header, state, body, nil, nil, nil, tracer)
diff --git a/core/state_processor.go b/core/state_processor.go
index 80e599500a..d526443351 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -199,7 +199,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
body := &types.Body{Transactions: commonTxs, Uncles: block.Uncles(), Withdrawals: block.Withdrawals()}
finalizeState := tracingStateDB
if recordSystemBAL {
- finalizeState = &systemTxStateDB{StateDB: tracingStateDB, accessList: blockAccessList}
+ finalizeState = NewSystemTxStateDB(tracingStateDB, blockAccessList)
}
err = p.chain.Engine().Finalize(p.chain, header, finalizeState, body, &receipts, &systemTxs, &gasUsed, cfg.Tracer)
if err != nil {
@@ -656,20 +656,14 @@ func onSystemCallStart(tracer *tracing.Hooks, ctx *tracing.VMContext) {
// blockAssembler is implemented by consensus engines that support FinalizeAndAssemble.
type blockAssembler interface {
- FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error)
+ FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, blockAccessLists ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error)
}
// AssembleBlock finalizes the state and assembles the block with provided
// body and receipts.
func AssembleBlock(engine consensus.Engine, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, blockAccessLists ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
if p, ok := engine.(blockAssembler); ok {
- // TODO: Include Parlia finalization in BAL construction and attach the
- // completed access list here before enabling Amsterdam on BSC.
- block, receipts, err := p.FinalizeAndAssemble(chain, header, state, body, receipts, nil)
- if err != nil {
- return nil, nil, err
- }
- return block, receipts, nil
+ return p.FinalizeAndAssemble(chain, header, state, body, receipts, nil, blockAccessLists...)
}
if err := engine.Finalize(chain, header, state, body, &receipts, nil, &header.GasUsed, nil); err != nil {
return nil, nil, err
diff --git a/core/state_processor_bsc.go b/core/state_processor_bsc.go
index e6bd57aae1..b0d8fe8167 100644
--- a/core/state_processor_bsc.go
+++ b/core/state_processor_bsc.go
@@ -31,6 +31,11 @@ type systemTxStateDB struct {
accessList *bal.ConstructionBlockAccessList
}
+// NewSystemTxStateDB collects the BALs returned by Parlia system transactions.
+func NewSystemTxStateDB(statedb vm.StateDB, accessList *bal.ConstructionBlockAccessList) vm.StateDB {
+ return &systemTxStateDB{StateDB: statedb, accessList: accessList}
+}
+
func (s *systemTxStateDB) Finalise(rules params.Rules) *bal.ConstructionBlockAccessList {
list := s.StateDB.Finalise(rules)
s.accessList.Merge(list)
diff --git a/eth/handler_test.go b/eth/handler_test.go
index 487558cd77..50c26ae9d3 100644
--- a/eth/handler_test.go
+++ b/eth/handler_test.go
@@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/blobpool"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/types/bal"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
@@ -376,7 +377,7 @@ func (c *mockParlia) Finalize(chain consensus.ChainHeaderReader, header *types.H
return
}
-func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, []*types.Receipt, error) {
+func (c *mockParlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks, _ ...*bal.ConstructionBlockAccessList) (*types.Block, []*types.Receipt, error) {
// Finalize block
c.Finalize(chain, header, state, body, nil, nil, nil, tracer)
diff --git a/miner/worker.go b/miner/worker.go
index dd3e44137f..0ddf09fbb9 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -1707,8 +1707,6 @@ func (w *worker) commit(env *environment, interval func(), start time.Time) erro
if env.header.EmptyWithdrawalsHash() {
body.Withdrawals = make([]*types.Withdrawal, 0)
}
- // TODO: Collect Parlia finalization changes into the BAL and attach it
- // to the block before enabling Amsterdam on BSC.
block, receipts, err := core.AssembleBlock(w.engine, w.chain, types.CopyHeader(env.header), env.state, &body, env.receipts, env.bal)
env.committed = true
if err != nil {
From 29bcdde14e106d9cee1a11b45fe09ab37eabe88c Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 20:21:06 +0800
Subject: [PATCH 09/14] eth, internal/ethapi: fix system transaction replay gas
budget
---
eth/api_backend_test.go | 54 +++++++++++++++++++++++++
eth/state_accessor.go | 2 +-
eth/tracers/api.go | 9 +++--
eth/tracers/api_test.go | 90 +++++++++++++++++++++++++++++++++++++++++
internal/ethapi/api.go | 2 +-
5 files changed, 151 insertions(+), 6 deletions(-)
diff --git a/eth/api_backend_test.go b/eth/api_backend_test.go
index aa0539511b..57c2793d73 100644
--- a/eth/api_backend_test.go
+++ b/eth/api_backend_test.go
@@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
+ "github.com/ethereum/go-ethereum/consensus/parlia"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/txpool"
@@ -180,3 +181,56 @@ func testSendTx(t *testing.T, withLocal bool) {
}
}
}
+
+func TestStateAtSystemTransactionGasBudget(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ config.Parlia = ¶ms.ParliaConfig{}
+ config.HaberTime, config.HaberFixTime, config.BohrTime, config.PascalTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.PragueTime, config.LorentzTime, config.MaxwellTime, config.FermiTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ config.OsakaTime, config.MendelTime, config.PasteurTime = new(uint64), new(uint64), new(uint64)
+ forkTime := uint64(2)
+ config.AmsterdamTime = &forkTime
+ config.BlobScheduleConfig = params.BSCChainConfig.BlobScheduleConfig
+ to := common.HexToAddress("0x1000")
+ genesis := &core.Genesis{Config: &config, Alloc: types.GenesisAlloc{
+ address: {Balance: big.NewInt(100)},
+ // The first transaction records whether more than 16M execution gas is available.
+ to: {Code: common.FromHex("0x63010000005a1160005500")},
+ }}
+ chain := newTestBlockChain(t, 0, genesis, nil)
+ defer chain.Stop()
+ parent := chain.GetBlockByNumber(0)
+ engine := parlia.New(&config, nil, nil, parent.Hash())
+ defer engine.Close()
+ backend := &Ethereum{blockchain: chain, engine: engine}
+ var txs []*types.Transaction
+ for nonce := uint64(0); nonce < 2; nonce++ {
+ txs = append(txs, types.MustSignNewTx(key, types.LatestSigner(&config), &types.LegacyTx{
+ Nonce: nonce, To: &to, Gas: math.MaxUint64 / 2, GasPrice: common.Big0,
+ }))
+ }
+ for _, timestamp := range []uint64{1, 2} {
+ t.Run(new(big.Int).SetUint64(timestamp).String(), func(t *testing.T) {
+ header := parent.Header()
+ header.Number, header.ParentHash = big.NewInt(1), parent.Hash()
+ header.Time, header.Coinbase, header.GasLimit = timestamp, address, 30_000_000
+ header.BaseFee, header.Difficulty = common.Big0, common.Big1
+ if timestamp >= forkTime {
+ slot := uint64(1)
+ header.SlotNumber = &slot
+ }
+ block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
+ tx, _, statedb, release, err := backend.stateAtTransaction(context.Background(), block, 1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer release()
+ if tx.Hash() != txs[1].Hash() || statedb.GetNonce(address) != 1 {
+ t.Fatal("incorrect transaction or nonce after system transaction replay")
+ }
+ if have := statedb.GetState(to, common.Hash{}); have != common.BigToHash(common.Big1) {
+ t.Fatalf("incorrect system transaction prestate: have %s, want 1", have)
+ }
+ })
+ }
+}
diff --git a/eth/state_accessor.go b/eth/state_accessor.go
index 05db81b634..ff212281ca 100644
--- a/eth/state_accessor.go
+++ b/eth/state_accessor.go
@@ -291,7 +291,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
// Assemble the transaction call message and return if the requested offset
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
if !beforeSystemTx {
- msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
}
// Not yet the searched for transaction, execute on top of the current state
diff --git a/eth/tracers/api.go b/eth/tracers/api.go
index 29b5146e8b..b95c20b541 100644
--- a/eth/tracers/api.go
+++ b/eth/tracers/api.go
@@ -572,7 +572,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
if !beforeSystemTx {
- msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
}
statedb.SetTxContext(tx.Hash(), i, uint32(i+1))
if _, err := core.ApplyMessage(evm, msg, nil); err != nil {
@@ -768,7 +768,7 @@ txloop:
// Generate the next state snapshot fast without tracing
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
if !beforeSystemTx {
- msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
}
statedb.SetTxContext(tx.Hash(), i, uint32(i+1))
if _, err := core.ApplyMessage(evm, msg, nil); err != nil {
@@ -866,7 +866,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
// Prepare the transaction for un-traced execution
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
if !beforeSystemTx {
- msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
}
if txHash != (common.Hash{}) && tx.Hash() != txHash {
// Process the tx to update state, but don't trace it.
@@ -1149,7 +1149,8 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor
// Run the transaction with tracing enabled.
if isSystemTx {
intrinsicGas, _ = core.IntrinsicGas(message.Data, message.AccessList, message.SetCodeAuthorizations, message.From, message.To, message.Value, evm.GetRules())
- message.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ // Parlia system transactions are not subject to the execution gas cap.
+ message.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
}
// Call Prepare to clear out the statedb access list
diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go
index e32af4a899..34b79c5670 100644
--- a/eth/tracers/api_test.go
+++ b/eth/tracers/api_test.go
@@ -23,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "math"
"math/big"
"os"
"reflect"
@@ -49,6 +50,7 @@ import (
"github.com/ethereum/go-ethereum/internal/ethapi/override"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
+ "github.com/holiman/uint256"
)
var (
@@ -1990,3 +1992,91 @@ func TestTraceCallDefaultsToLatest(t *testing.T) {
t.Errorf("omitted-block result %s != latest result %s", omitted, latest)
}
}
+
+func TestTraceSystemTransactionGasBudget(t *testing.T) {
+ config := *params.MergedTestChainConfig
+ config.Parlia = ¶ms.ParliaConfig{}
+ config.Ethash = nil
+ forkTime := uint64(2)
+ config.AmsterdamTime = &forkTime
+ accounts := newAccounts(1)
+ from, to := accounts[0].addr, common.HexToAddress("0x1000")
+ for _, timestamp := range []uint64{1, 2} {
+ for _, tc := range []struct {
+ name string
+ code []byte
+ system bool
+ }{
+ // Memory expansion needs more gas than an ordinary transaction can execute with.
+ {"system-memory", common.FromHex("0x600063003000005200"), true},
+ {"system-gas-branch", common.FromHex("0x63010000005a1160005500"), true},
+ {"ordinary-memory", common.FromHex("0x600063003000005200"), false},
+ } {
+ t.Run(fmt.Sprintf("%d/%s", timestamp, tc.name), func(t *testing.T) {
+ blockCtx := vm.BlockContext{
+ CanTransfer: core.CanTransfer, Transfer: core.Transfer,
+ GetHash: func(uint64) common.Hash { return common.Hash{} },
+ Coinbase: from, BlockNumber: big.NewInt(1), Time: timestamp,
+ Difficulty: common.Big1, GasLimit: 30_000_000,
+ BaseFee: new(big.Int), BlobBaseFee: new(big.Int),
+ CostPerStateByte: params.CostPerStateByte,
+ }
+ if timestamp >= forkTime {
+ blockCtx.SlotNum = blockCtx.BlockNumber.Uint64()
+ }
+ statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
+ if err != nil {
+ t.Fatal(err)
+ }
+ statedb.SetCode(to, tc.code, tracing.CodeChangeUnspecified)
+ rules := config.Rules(blockCtx.BlockNumber, false, timestamp)
+ statedb.Finalise(rules)
+ gasLimit := uint64(math.MaxUint64 / 2)
+ if !tc.system {
+ gasLimit = params.MaxTxGas
+ if timestamp >= forkTime {
+ gasLimit = blockCtx.GasLimit
+ }
+ }
+ tx := types.MustSignNewTx(accounts[0].key, types.LatestSigner(&config), &types.LegacyTx{
+ To: &to, Gas: gasLimit, GasPrice: common.Big0,
+ })
+ msg, err := core.TransactionToMessage(tx, types.MakeSigner(&config, blockCtx.BlockNumber, timestamp), common.Big0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ actual := statedb.Copy()
+ result, err := NewAPI(&testBackend{chainConfig: &config}).traceTx(context.Background(), tx, msg,
+ &Context{TxHash: tx.Hash()}, blockCtx, statedb, nil, tc.system, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var trace logger.ExecutionResult
+ if err := json.Unmarshal(result.(json.RawMessage), &trace); err != nil {
+ t.Fatal(err)
+ }
+ if !tc.system {
+ if !trace.Failed || trace.Gas != params.MaxTxGas {
+ t.Fatalf("ordinary transaction execution cap changed: failed=%v gas=%d", trace.Failed, trace.Gas)
+ }
+ return
+ }
+ evm := vm.NewEVM(blockCtx, actual, &config, vm.Config{})
+ defer evm.Release()
+ actual.Prepare(rules, from, from, &to, vm.ActivePrecompiles(rules), nil)
+ budget := vm.NewGasBudget(gasLimit, 0)
+ _, remaining, err := evm.Call(from, to, nil, budget, new(uint256.Int))
+ if err != nil {
+ t.Fatal(err)
+ }
+ actual.SetNonce(from, 1, tracing.NonceChangeEoACall)
+ if trace.Failed || trace.Gas != remaining.Used(budget) {
+ t.Fatalf("system transaction gas mismatch: failed=%v have=%d want=%d", trace.Failed, trace.Gas, remaining.Used(budget))
+ }
+ if statedb.IntermediateRoot(rules) != actual.IntermediateRoot(rules) {
+ t.Fatal("traced system transaction state differs from execution")
+ }
+ })
+ }
+ }
+}
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go
index 4926abb50a..4c006a56a4 100644
--- a/internal/ethapi/api.go
+++ b/internal/ethapi/api.go
@@ -1437,7 +1437,7 @@ func (api *BlockChainAPI) replay(ctx context.Context, block *types.Block, accoun
if posa, ok := api.b.Engine().(consensus.PoSA); ok {
if isSystem, _ := posa.IsSystemTransaction(tx, block.Header()); isSystem {
- msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck()
+ msg.SkipNonceCheck().SkipEOACheck().SkipGasLimitCapCheck().SkipExecutionGasCapCheck()
balance := statedb.GetBalance(consensus.SystemAddress)
if balance.Cmp(common.U2560) > 0 {
statedb.SetBalance(consensus.SystemAddress, uint256.NewInt(0), tracing.BalanceChangeUnspecified)
From 9554f18144e226aa307e89fcf5a44c61692384dd Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 21:33:46 +0800
Subject: [PATCH 10/14] core, miner: fix payment lane gas accounting
---
core/chain_makers.go | 9 +-
core/payment_lane.go | 113 ++++++++++++---
core/payment_lane_test.go | 238 +++++++++++++++++++++++++++++++-
core/paymentlane/paymentlane.go | 8 +-
core/state_processor.go | 4 +-
miner/bid_simulator.go | 4 +-
miner/lane_regression_test.go | 27 ++++
miner/worker.go | 21 ++-
8 files changed, 378 insertions(+), 46 deletions(-)
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 2d1f6efe40..f4fdde9a38 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -125,7 +125,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti
evm = vm.NewEVM(blockContext, b.statedb, b.cm.config, vmConfig)
)
laneType := b.lane.Classify(tx)
- usedBefore := b.gasPool.Used()
+ usedBefore := b.gasPool.Snapshot()
b.statedb.SetTxContext(tx.Hash(), len(b.txs), uint32(len(b.txs)+1))
receipt, bal, err := ApplyTransaction(context.Background(), evm, b.gasPool, b.statedb, b.header, tx)
if err != nil {
@@ -481,8 +481,13 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
panic(fmt.Sprintf("failed to assemble block: %v", err))
}
+ // Empty generated blocks have no transaction gas pool.
+ if b.gasPool == nil {
+ b.gasPool = NewGasPool(b.header.GasLimit)
+ }
+
// The same self-check the miner runs.
- if err := b.lane.Verify(block.GasUsed()); err != nil {
+ if err := b.lane.Verify(block.GasUsed(), b.gasPool); err != nil {
panic(fmt.Sprintf("payment lane check failed: %v", err))
}
if config.IsCancun(block.Number(), block.Time()) {
diff --git a/core/payment_lane.go b/core/payment_lane.go
index e3c03dde36..e383e217be 100644
--- a/core/payment_lane.go
+++ b/core/payment_lane.go
@@ -3,6 +3,7 @@ package core
import (
"errors"
"fmt"
+ "math/bits"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/paymentlane"
@@ -35,10 +36,13 @@ func laneReject(err error) error {
// LaneState is one block's lane: the quota derived from the parent post-state, plus the payment
// total accumulated as the block executes.
type LaneState struct {
- Budget paymentlane.Budget
- classifier *paymentlane.Classifier
- state laneStateDB
- gasLimit uint64
+ Budget paymentlane.Budget // quota and pre-Amsterdam usage
+ classifier *paymentlane.Classifier
+ state laneStateDB
+ gasLimit uint64
+ amsterdam bool
+ paymentExecution uint64
+ paymentState uint64
}
// laneStateDB is the live state: what the classifier reads, and whether reading it worked.
@@ -62,6 +66,7 @@ func ResolveLaneState(config *params.ChainConfig, engine consensus.Engine, paren
classifier: meta.NewClassifier(systemTxOracle(engine, header), statedb),
state: statedb,
gasLimit: header.GasLimit,
+ amsterdam: config.IsAmsterdam(header.Number, header.Time),
}, nil
}
@@ -87,27 +92,81 @@ func (ls *LaneState) Classify(tx *types.Transaction) paymentlane.LaneType {
return ls.classifier.Classify(tx)
}
-// RecordUsedFrom books the gas the pool consumed since usedBefore.
-func (ls *LaneState) RecordUsedFrom(laneType paymentlane.LaneType, gp *GasPool, usedBefore uint64) {
- if used := gp.Used(); ls.On() && used > usedBefore {
- ls.Budget.RecordUsed(laneType, used-usedBefore)
+// RecordUsedFrom books payment resources consumed since the pool snapshot.
+func (ls *LaneState) RecordUsedFrom(laneType paymentlane.LaneType, gp, before *GasPool) {
+ if !ls.On() || laneType != paymentlane.PaymentLane {
+ return
+ }
+ if !ls.amsterdam {
+ if used, previous := gp.Used(), before.Used(); used > previous {
+ ls.Budget.RecordUsed(laneType, used-previous)
+ }
+ return
+ }
+ execution, state := gp.CumulativeExecution(), gp.CumulativeState()
+ if execution < before.CumulativeExecution() || state < before.CumulativeState() {
+ return
+ }
+ ls.paymentExecution += execution - before.CumulativeExecution()
+ ls.paymentState += state - before.CumulativeState()
+}
+
+func (ls *LaneState) idle() (execution, state uint64) {
+ budget := ls.Budget
+ if !ls.amsterdam {
+ idle := budget.IdleLane()
+ return idle, idle
+ }
+ budget.PaymentLaneUsed = ls.paymentExecution
+ execution = budget.IdleLane()
+ budget.PaymentLaneUsed = ls.paymentState
+ return execution, budget.IdleLane()
+}
+
+// PaymentGasUsed returns the larger payment resource total after Amsterdam.
+func (ls *LaneState) PaymentGasUsed() uint64 {
+ if ls.amsterdam {
+ return max(ls.paymentExecution, ls.paymentState)
}
+ return ls.Budget.PaymentLaneUsed
}
-// Admits takes shared as the shared remainder, i.e. gasPool.Gas().
-func (ls *LaneState) Admits(shared uint64, laneType paymentlane.LaneType, txGasLimit uint64) bool {
+// IdleGas reports the larger unused reservation; validation checks both dimensions.
+func (ls *LaneState) IdleGas() uint64 {
+ execution, state := ls.idle()
+ return max(execution, state)
+}
+
+// Admits leaves each resource's unused payment quota available for payments.
+func (ls *LaneState) Admits(gp *GasPool, laneType paymentlane.LaneType, txGasLimit uint64) bool {
if !ls.On() {
return true
}
- return ls.Budget.Admits(shared, laneType, txGasLimit)
+ if !ls.amsterdam {
+ return ls.Budget.Admits(gp.Available(false), laneType, txGasLimit)
+ }
+ var idleExecution, idleState uint64
+ if laneType != paymentlane.PaymentLane {
+ idleExecution, idleState = ls.idle()
+ }
+ execution, executionCarry := bits.Add64(min(txGasLimit, params.MaxTxGas), idleExecution, 0)
+ state, stateCarry := bits.Add64(txGasLimit, idleState, 0)
+ return executionCarry == 0 && stateCarry == 0 && gp.CheckGasAmsterdam(execution, state) == nil
}
-// VerifyPackedBid is the bid path's verdict on an environment it did not pack itself.
-func (ls *LaneState) VerifyPackedBid(shared uint64) error {
+// VerifyPackedBid checks that the re-executed bid leaves the unused quota in the pool.
+func (ls *LaneState) VerifyPackedBid(gp *GasPool) error {
if !ls.On() {
return nil
}
- if idle := ls.Budget.IdleLane(); idle > shared {
+ if ls.amsterdam {
+ execution, state := ls.idle()
+ if err := gp.CheckGasAmsterdam(execution, state); err != nil {
+ return fmt.Errorf("%w: idle payment quota (execution %d, state %d): %v", paymentlane.ErrViolated, execution, state, err)
+ }
+ return nil
+ }
+ if idle, shared := ls.Budget.IdleLane(), gp.Available(false); idle > shared {
return fmt.Errorf("%w: idle lane %d exceeds the %d gas left in the pool", paymentlane.ErrViolated, idle, shared)
}
return nil
@@ -117,14 +176,32 @@ func (ls *LaneState) VerifyPackedBid(shared uint64) error {
// verdict alike. A failed state read is the local fault it is, not the peer's: StateDB answers
// such a read with the zero code hash - which classifies as payment - and holds the error until
// Commit, after every verdict here.
-func (ls *LaneState) Verify(totalGasUsed uint64) error {
+func (ls *LaneState) Verify(totalGasUsed uint64, gp *GasPool) error {
if !ls.On() {
return nil
}
if err := ls.state.Error(); err != nil {
return fmt.Errorf("%w: %w", paymentlane.ErrStateUnavailable, err)
}
- return ls.Budget.Verify(ls.gasLimit, totalGasUsed)
+ if !ls.amsterdam {
+ return ls.Budget.Verify(ls.gasLimit, totalGasUsed)
+ }
+ userGas := max(gp.CumulativeExecution(), gp.CumulativeState())
+ if totalGasUsed < userGas || totalGasUsed > ls.gasLimit {
+ return fmt.Errorf("%w: total gas %d user gas %d limit %d", paymentlane.ErrViolated, totalGasUsed, userGas, ls.gasLimit)
+ }
+ // Parlia adds system gas after the user pool's maximum, so it reduces both budgets.
+ limit := ls.gasLimit - (totalGasUsed - userGas)
+ budget := ls.Budget
+ budget.PaymentLaneUsed = ls.paymentExecution
+ if err := budget.Verify(limit, gp.CumulativeExecution()); err != nil {
+ return fmt.Errorf("execution: %w", err)
+ }
+ budget.PaymentLaneUsed = ls.paymentState
+ if err := budget.Verify(limit, gp.CumulativeState()); err != nil {
+ return fmt.Errorf("state: %w", err)
+ }
+ return nil
}
func (ls *LaneState) recordImported() {
@@ -132,6 +209,6 @@ func (ls *LaneState) recordImported() {
return
}
paymentLaneImportedQuotaGauge.Update(int64(ls.Budget.PaymentLaneQuota))
- paymentLaneImportedGasUsedGauge.Update(int64(ls.Budget.PaymentLaneUsed))
- paymentLaneImportedIdleGauge.Update(int64(ls.Budget.IdleLane()))
+ paymentLaneImportedGasUsedGauge.Update(int64(ls.PaymentGasUsed()))
+ paymentLaneImportedIdleGauge.Update(int64(ls.IdleGas()))
}
diff --git a/core/payment_lane_test.go b/core/payment_lane_test.go
index dc8081d4c6..889883d3a1 100644
--- a/core/payment_lane_test.go
+++ b/core/payment_lane_test.go
@@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"encoding/hex"
"errors"
+ "math"
"math/big"
"strings"
"testing"
@@ -14,7 +15,9 @@ import (
"github.com/ethereum/go-ethereum/core/paymentlane"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/systemcontracts/jenner"
+ "github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
@@ -83,7 +86,7 @@ func recordLanes(into map[uint64]laneRecord, gen func(int, *BlockGen)) func(int,
into[b.header.Number.Uint64()] = laneRecord{
on: b.lane.On(),
quota: b.lane.Budget.PaymentLaneQuota,
- paymentGas: b.lane.Budget.PaymentLaneUsed,
+ paymentGas: b.lane.PaymentGasUsed(),
}
}
}
@@ -291,12 +294,12 @@ func TestPaymentLaneVerifyPackedBidChecksTheIdleQuota(t *testing.T) {
ls := &LaneState{classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil), state: liveState{}}
ls.Budget.PaymentLaneQuota = 100
- require.NoError(t, ls.VerifyPackedBid(100), "a quota that exactly fits is the accepting case")
- require.ErrorIs(t, ls.VerifyPackedBid(99), paymentlane.ErrViolated,
+ require.NoError(t, ls.VerifyPackedBid(NewGasPool(100)), "a quota that exactly fits is the accepting case")
+ require.ErrorIs(t, ls.VerifyPackedBid(NewGasPool(99)), paymentlane.ErrViolated,
"a bid that leaves less than the idle quota must be rejected")
ls.Budget.PaymentLaneUsed = 40
- require.NoError(t, ls.VerifyPackedBid(60), "payment gas already booked shrinks the idle quota one for one")
+ require.NoError(t, ls.VerifyPackedBid(NewGasPool(60)), "payment gas already booked shrinks the idle quota one for one")
}
// TestPaymentLaneReportsAFailedReadAsLocal pins the error category, not the rejection: a failed
@@ -312,7 +315,7 @@ func TestPaymentLaneReportsAFailedReadAsLocal(t *testing.T) {
gasLimit: laneTestGasLimit,
}
- err := ls.Verify(params.TxGas)
+ err := ls.Verify(params.TxGas, NewGasPool(laneTestGasLimit))
require.ErrorIs(t, err, paymentlane.ErrStateUnavailable, "a failed read is this node's fault, not the block's")
require.ErrorIs(t, err, broken, "the cause has to survive for whoever reads the log")
require.NotErrorIs(t, err, paymentlane.ErrViolated, "calling a good block invalid is what costs peers")
@@ -389,7 +392,7 @@ func TestRecordUsedFromIgnoresARolledBackPool(t *testing.T) {
snapshot := *gp
require.NoError(t, gp.CheckGasLegacy(100))
require.NoError(t, gp.ChargeGasLegacy(0, 100))
- usedBefore := gp.Used()
+ usedBefore := gp.Snapshot()
require.NoError(t, gp.CheckGasLegacy(40))
require.NoError(t, gp.ChargeGasLegacy(0, 40))
@@ -397,7 +400,228 @@ func TestRecordUsedFromIgnoresARolledBackPool(t *testing.T) {
require.EqualValues(t, 40, ls.Budget.PaymentLaneUsed)
*gp = snapshot // back past usedBefore
- require.Less(t, gp.Used(), usedBefore)
+ require.Less(t, gp.Used(), usedBefore.Used())
ls.RecordUsedFrom(paymentlane.PaymentLane, gp, usedBefore)
require.EqualValues(t, 40, ls.Budget.PaymentLaneUsed, "a rolled-back pool books nothing rather than wrapping")
}
+
+func TestPaymentLaneAmsterdamAccounting(t *testing.T) {
+ for name, paymentFirst := range map[string]bool{"payment-first": true, "general-first": false} {
+ t.Run(name, func(t *testing.T) {
+ ls := &LaneState{amsterdam: true, Budget: paymentlane.Budget{PaymentLaneQuota: 10}, classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil), state: liveState{}}
+ gp := NewGasPool(100)
+ charge := func(lane paymentlane.LaneType, e, s uint64) {
+ before := gp.Snapshot()
+ require.NoError(t, gp.ChargeGasAmsterdam(e, s, 1))
+ ls.RecordUsedFrom(lane, gp, before)
+ }
+ if paymentFirst {
+ charge(paymentlane.PaymentLane, 3, 7)
+ charge(paymentlane.GeneralLane, 6, 0)
+ } else {
+ charge(paymentlane.GeneralLane, 6, 0)
+ charge(paymentlane.PaymentLane, 3, 7)
+ }
+ require.EqualValues(t, 3, ls.paymentExecution)
+ require.EqualValues(t, 7, ls.paymentState)
+ require.EqualValues(t, 7, ls.PaymentGasUsed())
+ e, s := ls.idle()
+ require.EqualValues(t, 7, e)
+ require.EqualValues(t, 3, s)
+ before := gp.Snapshot()
+ require.NoError(t, gp.ChargeGasAmsterdam(4, 2, 1))
+ gp.Set(before)
+ ls.RecordUsedFrom(paymentlane.PaymentLane, gp, before)
+ require.EqualValues(t, 3, ls.paymentExecution)
+ require.EqualValues(t, 7, ls.paymentState)
+ gp.Set(NewGasPool(100)) // Rewind past the snapshot as well.
+ ls.RecordUsedFrom(paymentlane.PaymentLane, gp, before)
+ require.EqualValues(t, 3, ls.paymentExecution)
+ require.EqualValues(t, 7, ls.paymentState)
+ })
+ }
+}
+
+func TestPaymentLaneAmsterdamAdmission(t *testing.T) {
+ const limit, quota = 55_000_000, 2_750_000
+ ls := &LaneState{amsterdam: true, Budget: paymentlane.Budget{PaymentLaneQuota: quota}, classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil)}
+ gp := NewGasPool(limit)
+ require.NoError(t, gp.ChargeGasAmsterdam(38_000_000, quota, 0))
+ require.Greater(t, gp.Available(true), uint64(30_000_000))
+ require.False(t, ls.Admits(gp, paymentlane.GeneralLane, 30_000_000), "execution must retain its idle quota despite ample state budget")
+ require.True(t, ls.Admits(gp, paymentlane.PaymentLane, 30_000_000))
+ ls.paymentState = quota
+ require.False(t, ls.Admits(gp, paymentlane.GeneralLane, 30_000_000), "state payments cannot release execution quota")
+ ls.paymentState = 0
+ ls.paymentExecution = quota
+ require.True(t, ls.Admits(gp, paymentlane.GeneralLane, 30_000_000))
+ gp = NewGasPool(limit)
+ require.NoError(t, gp.ChargeGasAmsterdam(quota, 38_000_000, 0))
+ require.False(t, ls.Admits(gp, paymentlane.GeneralLane, 15_000_000), "execution payments cannot release state quota")
+ require.True(t, ls.Admits(gp, paymentlane.PaymentLane, 15_000_000))
+ ls.paymentState = quota
+ require.True(t, ls.Admits(gp, paymentlane.GeneralLane, 15_000_000))
+ for _, statePaid := range []uint64{0, math.MaxUint64} {
+ ls.Budget.PaymentLaneQuota = math.MaxUint64
+ ls.paymentExecution = 0
+ ls.paymentState = statePaid
+ require.False(t, ls.Admits(NewGasPool(math.MaxUint64), paymentlane.GeneralLane, 1), "each reservation addition must reject overflow")
+ }
+}
+
+func TestPaymentLaneAmsterdamVerify(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ e, s, pe, ps, system uint64
+ reject bool
+ }{
+ {"empty", 0, 0, 0, 0, 0, false},
+ {"execution boundary", 90, 0, 0, 0, 0, false},
+ {"state boundary", 0, 90, 0, 0, 0, false},
+ {"one dimension cannot pay both quotas", 100, 100, 5, 5, 0, true},
+ {"both quotas paid", 100, 100, 10, 10, 0, false},
+ {"payments can exceed quota", 100, 100, 100, 100, 0, false},
+ {"execution system boundary", 85, 0, 0, 0, 5, false},
+ {"execution system overflow", 85, 0, 0, 0, 6, true},
+ {"state system boundary", 0, 85, 0, 0, 5, false},
+ {"state system overflow", 0, 85, 0, 0, 6, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ ls := &LaneState{amsterdam: true, gasLimit: 100, Budget: paymentlane.Budget{PaymentLaneQuota: 10}, paymentExecution: tc.pe, paymentState: tc.ps, classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil), state: liveState{}}
+ gp := NewGasPool(100)
+ require.NoError(t, gp.ChargeGasAmsterdam(tc.e, tc.s, 0))
+ err := ls.Verify(gp.Used()+tc.system, gp)
+ if tc.reject {
+ require.ErrorIs(t, err, paymentlane.ErrViolated)
+ } else {
+ require.NoError(t, err)
+ }
+ })
+ }
+ ls := &LaneState{amsterdam: true, gasLimit: 100, classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil), state: liveState{}}
+ gp := NewGasPool(100)
+ require.NoError(t, gp.ChargeGasAmsterdam(50, 0, 0))
+ require.ErrorIs(t, ls.Verify(49, gp), paymentlane.ErrViolated)
+ require.ErrorIs(t, ls.Verify(101, gp), paymentlane.ErrViolated)
+}
+
+func TestPaymentLaneAmsterdamPackedBid(t *testing.T) {
+ ls := &LaneState{amsterdam: true, Budget: paymentlane.Budget{PaymentLaneQuota: 20_000_000}, classifier: paymentlane.NewClassifier(paymentlane.NoSystemTxs, liveState{}, nil)}
+ gp := NewGasPool(400_000_000)
+ require.NoError(t, gp.ChargeGasAmsterdam(382_000_000, 20_000_000, 0))
+ require.EqualValues(t, 380_000_000, gp.Available(true))
+ require.ErrorIs(t, ls.VerifyPackedBid(gp), paymentlane.ErrViolated)
+ ls.paymentState = 20_000_000
+ require.ErrorIs(t, ls.VerifyPackedBid(gp), paymentlane.ErrViolated)
+ ls.paymentExecution = 2_000_000
+ require.NoError(t, ls.VerifyPackedBid(gp))
+ gp = NewGasPool(400_000_000)
+ require.NoError(t, gp.ChargeGasAmsterdam(20_000_000, 382_000_000, 0))
+ ls.paymentExecution = 20_000_000
+ ls.paymentState = 0
+ require.ErrorIs(t, ls.VerifyPackedBid(gp), paymentlane.ErrViolated)
+ ls.paymentState = 2_000_000
+ require.NoError(t, ls.VerifyPackedBid(gp))
+}
+
+// laneTestEngine omits PoW rewards from the BSC chain fixture.
+type laneTestEngine struct{ consensus.Engine }
+
+func (*laneTestEngine) Finalize(consensus.ChainHeaderReader, *types.Header, vm.StateDB, *types.Body, *[]*types.Receipt, *[]*types.Transaction, *uint64, *tracing.Hooks) error {
+ return nil
+}
+
+func TestPaymentLaneAmsterdamImport(t *testing.T) {
+ config, gspec, key := laneGenesis(t, laneTestGasLimit)
+ amsterdam := uint64(25)
+ config.AmsterdamTime = &amsterdam
+ require.NoError(t, config.CheckConfigForkOrder())
+ clear, revert, create, floor, refill := common.Address{0xc1}, common.Address{0xc2}, common.Address{0xc3}, common.Address{0xc4}, common.Address{0xc5}
+ gspec.Alloc[clear] = types.Account{Code: common.FromHex("0x600060005500"), Storage: map[common.Hash]common.Hash{{}: common.BigToHash(common.Big1)}, Balance: common.Big0}
+ gspec.Alloc[revert] = types.Account{Code: common.FromHex("0x600060005560006000fd"), Storage: map[common.Hash]common.Hash{{}: common.BigToHash(common.Big1)}, Balance: common.Big0}
+ gspec.Alloc[create] = types.Account{Code: common.FromHex("0x600160005500"), Balance: common.Big0}
+ gspec.Alloc[floor] = types.Account{Code: common.FromHex("0x600160005500"), Balance: common.Big0}
+ gspec.Alloc[refill] = types.Account{Code: common.FromHex("0x6001600055600060005500"), Balance: common.Big0}
+ contract := gspec.Alloc[paymentlane.ContractAddress]
+ contract.Storage = map[common.Hash]common.Hash{laneListedLenSlot: common.BigToHash(big.NewInt(5))}
+ for i, addr := range []common.Address{clear, revert, create, floor, refill} {
+ contract.Storage[enumerableSetSlot(laneListedLenSlot, uint64(i))] = common.BytesToHash(addr[:])
+ }
+ gspec.Alloc[paymentlane.ContractAddress] = contract
+ engine := &laneTestEngine{ethash.NewFullFaker()}
+ signer := types.LatestSigner(config)
+ _, blocks, _ := GenerateChainWithGenesis(gspec, engine, 4, func(i int, b *BlockGen) {
+ if i != 2 {
+ return
+ }
+ require.True(t, b.lane.On())
+ require.True(t, b.lane.amsterdam)
+ b.SetCoinbase(common.Address{})
+ for n, addr := range []common.Address{clear, revert, create, floor, refill} {
+ before := b.gasPool.Snapshot()
+ var data []byte
+ if n == 3 {
+ data = make([]byte, 4096)
+ }
+ b.AddTx(key.sign(t, signer, uint64(n), addr, common.Big0, 400_000, data))
+ receipt := b.receipts[n]
+ execution := b.gasPool.CumulativeExecution() - before.CumulativeExecution()
+ switch n {
+ case 0:
+ require.Less(t, receipt.GasUsed, execution, "refund reduces fees, not the lane's execution contribution")
+ case 1:
+ require.EqualValues(t, types.ReceiptStatusFailed, receipt.Status)
+ require.Positive(t, execution)
+ case 2:
+ require.Positive(t, b.gasPool.CumulativeState()-before.CumulativeState())
+ case 3:
+ rules := config.Rules(b.header.Number, false, b.header.Time)
+ floorGas, err := FloorDataGas(rules, key.addr, &addr, new(uint256.Int), data, nil)
+ require.NoError(t, err)
+ require.Equal(t, floorGas, execution)
+ require.Positive(t, b.gasPool.CumulativeState()-before.CumulativeState())
+ case 4:
+ require.EqualValues(t, types.ReceiptStatusSuccessful, receipt.Status)
+ require.Zero(t, b.gasPool.CumulativeState()-before.CumulativeState(), "state refill reverses temporary creation, unlike a transaction refund")
+ require.Equal(t, common.Hash{}, b.statedb.GetState(addr, common.Hash{}))
+ }
+ }
+ require.Equal(t, b.gasPool.CumulativeExecution(), b.lane.paymentExecution)
+ require.Equal(t, b.gasPool.CumulativeState(), b.lane.paymentState)
+ })
+ chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), gspec, engine, DefaultConfig())
+ require.NoError(t, err)
+ defer chain.Stop()
+ n, err := chain.InsertChain(blocks)
+ require.NoError(t, err, "block index %d", n)
+ require.Empty(t, blocks[3].Transactions())
+ require.Equal(t, blocks[3].Root(), chain.CurrentBlock().Root)
+}
+
+func TestPaymentLaneAmsterdamRejectsStateOverflow(t *testing.T) {
+ const gasLimit = 2_000_000
+ config, gspec, key := laneGenesis(t, gasLimit)
+ amsterdam := uint64(25)
+ config.AmsterdamTime = &amsterdam
+ contract := common.Address{0xc1}
+ gspec.Alloc[contract] = types.Account{Code: common.FromHex("0x60016000355500"), Balance: common.Big0}
+ engine := &laneTestEngine{ethash.NewFullFaker()}
+ signer := types.LatestSigner(config)
+ _, blocks, _ := GenerateChainWithGenesis(gspec, engine, 3, func(i int, b *BlockGen) {
+ if i != 2 {
+ return
+ }
+ // Build a block that ignores the quota, but still obeys the ordinary gas limits.
+ b.lane = &LaneState{}
+ for n := uint64(0); n < 20; n++ {
+ b.AddTx(key.sign(t, signer, n, contract, common.Big0, 130_000, []byte{byte(n)}))
+ }
+ })
+ require.Greater(t, blocks[2].GasUsed(), uint64(gasLimit-paymentlane.Quota(500, gasLimit)))
+ require.LessOrEqual(t, blocks[2].GasUsed(), uint64(gasLimit))
+ chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), gspec, engine, DefaultConfig())
+ require.NoError(t, err)
+ defer chain.Stop()
+ _, err = chain.InsertChain(blocks)
+ require.ErrorIs(t, err, paymentlane.ErrViolated)
+}
diff --git a/core/paymentlane/paymentlane.go b/core/paymentlane/paymentlane.go
index 71114f198b..beac4e7ea6 100644
--- a/core/paymentlane/paymentlane.go
+++ b/core/paymentlane/paymentlane.go
@@ -1,9 +1,9 @@
-// Package paymentlane implements the BEP-703 payment lane rules. One inequality per block:
+// Package paymentlane implements the BEP-703 scalar payment lane budget:
//
-// header.GasUsed + max(0, paymentLaneQuota - paymentGasUsed) <= GasLimit
+// gasUsed + max(0, paymentLaneQuota - paymentGasUsed) <= gasLimit
//
-// Section 3.3's rule with generalGasUsed as the header residual, so Parlia's system transactions
-// count as general gas. Nothing is committed to the block: the quota is a pure function of the
+// Before Amsterdam this applies to the block total; afterwards it applies to each resource.
+// Parlia system transactions count as general gas. The quota is a pure function of the
// parent post-state's ratio and this block's gas limit, so every node derives it independently.
package paymentlane
diff --git a/core/state_processor.go b/core/state_processor.go
index d526443351..c294d89989 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -170,7 +170,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
return nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
laneType := lane.Classify(tx)
- usedBefore := gp.Used()
+ usedBefore := gp.Snapshot()
statedb.SetTxContext(tx.Hash(), i, uint32(i+1))
receipt, bal, err := applyTransactionWithEVM(ctx, msg, gp, statedb, blockNumber, blockHash, context.Time, tx, evm, eagerBloom)
if err != nil {
@@ -222,7 +222,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated
blockAccessList.Merge(postBAL)
}
- if err := lane.Verify(gasUsed); err != nil {
+ if err := lane.Verify(gasUsed, gp); err != nil {
return nil, laneReject(err)
}
lane.recordImported()
diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go
index 46fb07a14b..15f178266a 100644
--- a/miner/bid_simulator.go
+++ b/miner/bid_simulator.go
@@ -1214,7 +1214,7 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
return
}
- if err = bidRuntime.env.lane.VerifyPackedBid(bidRuntime.env.gasPool.Available(bidRuntime.env.evm.GetRules().IsAmsterdam)); err != nil {
+ if err = bidRuntime.env.lane.VerifyPackedBid(bidRuntime.env.gasPool); err != nil {
log.Warn("BidSimulator: bid rejected by the payment lane", "builder", bidRuntime.bid.Builder,
"bidHash", bidRuntime.bid.Hash(), "err", err)
return
@@ -1388,7 +1388,7 @@ func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *para
}
laneType := env.lane.Classify(tx)
- usedBefore := env.gasPool.Used()
+ usedBefore := env.gasPool.Snapshot()
receipt, txBAL, err := core.ApplyTransaction(context.Background(), env.evm, env.gasPool, env.state, env.header, tx)
if err != nil {
diff --git a/miner/lane_regression_test.go b/miner/lane_regression_test.go
index cb58611d87..c00dce35e5 100644
--- a/miner/lane_regression_test.go
+++ b/miner/lane_regression_test.go
@@ -119,3 +119,30 @@ func TestBidCommitTransactionBooksNothingForAFailedTransaction(t *testing.T) {
require.NotZero(t, env.gasPool.Used(), "buyGas must have drawn from the pool, or the test proves nothing")
require.Zero(t, env.lane.Budget.PaymentLaneUsed, "a failed transaction must not book lane gas")
}
+
+func TestPaymentLaneWorkerRollback(t *testing.T) {
+ for _, amsterdam := range []bool{false, true} {
+ t.Run(map[bool]string{false: "legacy", true: "amsterdam"}[amsterdam], func(t *testing.T) {
+ w, config, parent, header, key := laneMinerChain(t, false)
+ if amsterdam {
+ config.AmsterdamTime = &header.Time
+ }
+ env, err := w.makeEnv(parent, header, common.Address{}, nil, false)
+ require.NoError(t, err)
+ defer env.discard()
+ require.True(t, env.lane.On())
+ sender := crypto.PubkeyToAddress(key.PublicKey)
+ balance := env.state.GetBalance(sender).ToBig()
+ pool := env.gasPool.String()
+ tx, err := types.SignTx(types.NewTransaction(0, common.Address{0xaa}, big.NewInt(1), 1, big.NewInt(1), nil), types.LatestSigner(config), key)
+ require.NoError(t, err)
+ require.Equal(t, paymentlane.PaymentLane, env.lane.Classify(tx))
+ _, err = w.applyTransaction(env, tx)
+ require.ErrorIs(t, err, core.ErrIntrinsicGas)
+ require.Equal(t, pool, env.gasPool.String())
+ require.Equal(t, balance, env.state.GetBalance(sender).ToBig())
+ require.Zero(t, env.lane.PaymentGasUsed())
+ require.Equal(t, env.lane.Budget.PaymentLaneQuota, env.lane.IdleGas())
+ })
+ }
+}
diff --git a/miner/worker.go b/miner/worker.go
index 0ddf09fbb9..cb366b014c 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -97,7 +97,7 @@ var (
// Producing-side lane metrics; core/payment_lane.go reports the imported side.
paymentLaneQuotaGauge = metrics.NewRegisteredGauge("paymentlane/paymentLaneQuota", nil) // gas, at seal
- paymentLaneIdleGauge = metrics.NewRegisteredGauge("paymentlane/paymentLaneIdle", nil) // gas wasted, at seal
+ paymentLaneIdleGauge = metrics.NewRegisteredGauge("paymentlane/paymentLaneIdle", nil) // largest unused payment reservation, at seal
generalLaneYieldedCounter = metrics.NewRegisteredCounter("paymentlane/generalLaneYielded", nil) // txs dropped for the quota
paymentLaneDeclineCounter = metrics.NewRegisteredCounter("paymentlane/produceDeclined", nil)
@@ -847,9 +847,8 @@ func (w *worker) commitBlobTransaction(env *environment, tx *types.Transaction,
func (w *worker) applyTransaction(env *environment, tx *types.Transaction, receiptProcessors ...core.ReceiptProcessor) (*types.Receipt, error) {
laneType := env.lane.Classify(tx)
var (
- snap = env.state.Snapshot()
- gp = env.gasPool.Snapshot()
- usedBefore = env.gasPool.Used()
+ snap = env.state.Snapshot()
+ gp = env.gasPool.Snapshot()
)
receipt, txBAL, err := core.ApplyTransaction(context.Background(), env.evm, env.gasPool, env.state, env.header, tx)
@@ -865,7 +864,7 @@ func (w *worker) applyTransaction(env *environment, tx *types.Transaction, recei
}
}
env.header.GasUsed = env.gasPool.Used()
- env.lane.RecordUsedFrom(laneType, env.gasPool, usedBefore)
+ env.lane.RecordUsedFrom(laneType, env.gasPool, gp)
return receipt, err
}
@@ -995,10 +994,10 @@ LOOP:
prefetchCurr.Store(tx)
laneType := env.lane.Classify(tx)
- if !env.lane.Admits(env.gasPool.Available(env.evm.GetRules().IsAmsterdam), laneType, tx.Gas()) {
+ if !env.lane.Admits(env.gasPool, laneType, tx.Gas()) {
generalLaneYieldedCounter.Inc(1)
env.generalLaneYielded++
- log.Trace("Yielding idle payment lane", "hash", ltx.Hash, "left", env.gasPool.Available(env.evm.GetRules().IsAmsterdam), "paymentLaneIdle", env.lane.Budget.IdleLane())
+ log.Trace("Yielding idle payment lane", "hash", ltx.Hash, "left", env.gasPool.Available(env.evm.GetRules().IsAmsterdam), "paymentLaneIdle", env.lane.IdleGas())
txs.Pop()
continue
}
@@ -1341,7 +1340,7 @@ func (w *worker) generateWork(genParam *generateParams, witness bool) *newPayloa
if err != nil {
return &newPayloadResult{err: err}
}
- if err := work.lane.Verify(block.GasUsed()); err != nil {
+ if err := work.lane.Verify(block.GasUsed(), work.gasPool); err != nil {
paymentLaneDeclineCounter.Inc(1)
log.Error("Payment lane refused to seal", "number", block.Number(), "err", err)
return &newPayloadResult{err: err}
@@ -1719,14 +1718,14 @@ func (w *worker) commit(env *environment, interval func(), start time.Time) erro
env.receipts = receipts
finalizeBlockTimer.UpdateSince(finalizeStart)
- if err := env.lane.Verify(block.GasUsed()); err != nil {
+ if err := env.lane.Verify(block.GasUsed(), env.gasPool); err != nil {
paymentLaneDeclineCounter.Inc(1)
log.Error("Payment lane refused to seal", "number", block.Number(), "err", err)
return err
}
if env.lane.On() {
paymentLaneQuotaGauge.Update(int64(env.lane.Budget.PaymentLaneQuota))
- paymentLaneIdleGauge.Update(int64(env.lane.Budget.IdleLane()))
+ paymentLaneIdleGauge.Update(int64(env.lane.IdleGas()))
}
// If Cancun enabled, sidecars can't be nil then.
@@ -1739,7 +1738,7 @@ func (w *worker) commit(env *environment, interval func(), start time.Time) erro
case w.taskCh <- &task{receipts: receipts, state: env.state, block: block, createdAt: time.Now(), miningStartAt: start}:
log.Info("Commit new sealing work", "number", block.Number(), "sealhash", w.engine.SealHash(block.Header()),
"txs", len(env.txs), "blobs", env.blobs, "gas", block.GasUsed(), "paymentLaneQuota", env.lane.Budget.PaymentLaneQuota,
- "paymentLaneIdle", env.lane.Budget.IdleLane(), "generalLaneYielded", env.generalLaneYielded,
+ "paymentLaneIdle", env.lane.IdleGas(), "generalLaneYielded", env.generalLaneYielded,
"fees", feesInEther, "elapsed", common.PrettyDuration(time.Since(start)))
case <-w.exitCh:
From 6552d382ef3b6a503d2d19f45afa9b2b0862eade Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:20:43 +0800
Subject: [PATCH 11/14] core/vm: fix cas20 gas accounting
---
core/payment_lane_test.go | 42 ++++-
core/vm/cas20_access_gas_bytecode_test.go | 19 ++-
core/vm/cas20_factory_test.go | 96 +++++++++++
core/vm/cas20_gas.go | 28 +++-
core/vm/cas20_gas_bytecode_test.go | 188 ++++++++++++++++++++--
core/vm/contracts_stateful.go | 9 +-
core/vm/gas_table.go | 24 ++-
7 files changed, 370 insertions(+), 36 deletions(-)
diff --git a/core/payment_lane_test.go b/core/payment_lane_test.go
index 889883d3a1..6552d0632d 100644
--- a/core/payment_lane_test.go
+++ b/core/payment_lane_test.go
@@ -541,29 +541,45 @@ func TestPaymentLaneAmsterdamImport(t *testing.T) {
gspec.Alloc[revert] = types.Account{Code: common.FromHex("0x600060005560006000fd"), Storage: map[common.Hash]common.Hash{{}: common.BigToHash(common.Big1)}, Balance: common.Big0}
gspec.Alloc[create] = types.Account{Code: common.FromHex("0x600160005500"), Balance: common.Big0}
gspec.Alloc[floor] = types.Account{Code: common.FromHex("0x600160005500"), Balance: common.Big0}
+ token := common.Address{0: 0xca, 1: 0x52, 19: 1}
+ gspec.Alloc[token] = types.Account{Code: vm.CAS20MarkerCode, Balance: common.Big0}
gspec.Alloc[refill] = types.Account{Code: common.FromHex("0x6001600055600060005500"), Balance: common.Big0}
contract := gspec.Alloc[paymentlane.ContractAddress]
- contract.Storage = map[common.Hash]common.Hash{laneListedLenSlot: common.BigToHash(big.NewInt(5))}
- for i, addr := range []common.Address{clear, revert, create, floor, refill} {
+ contract.Storage = map[common.Hash]common.Hash{laneListedLenSlot: common.BigToHash(big.NewInt(6))}
+ for i, addr := range []common.Address{clear, revert, create, floor, refill, token} {
contract.Storage[enumerableSetSlot(laneListedLenSlot, uint64(i))] = common.BytesToHash(addr[:])
}
gspec.Alloc[paymentlane.ContractAddress] = contract
engine := &laneTestEngine{ethash.NewFullFaker()}
signer := types.LatestSigner(config)
+ approve := func(spender common.Address) []byte {
+ data := append(common.FromHex("0x095ea7b3"), common.LeftPadBytes(spender[:], 32)...)
+ return append(data, common.LeftPadBytes([]byte{1}, 32)...)
+ }
_, blocks, _ := GenerateChainWithGenesis(gspec, engine, 4, func(i int, b *BlockGen) {
+ if i == 1 {
+ require.True(t, config.IsJenner(b.header.Number, b.header.Time))
+ require.False(t, config.IsAmsterdam(b.header.Number, b.header.Time))
+ b.AddTx(key.sign(t, signer, 0, token, common.Big0, 400_000, approve(common.Address{0xcd})))
+ require.EqualValues(t, types.ReceiptStatusSuccessful, b.receipts[0].Status)
+ require.Zero(t, b.gasPool.CumulativeState())
+ return
+ }
if i != 2 {
return
}
require.True(t, b.lane.On())
require.True(t, b.lane.amsterdam)
b.SetCoinbase(common.Address{})
- for n, addr := range []common.Address{clear, revert, create, floor, refill} {
+ for n, addr := range []common.Address{clear, revert, create, floor, refill, token} {
before := b.gasPool.Snapshot()
var data []byte
if n == 3 {
data = make([]byte, 4096)
+ } else if n == 5 {
+ data = approve(common.Address{0xcc})
}
- b.AddTx(key.sign(t, signer, uint64(n), addr, common.Big0, 400_000, data))
+ b.AddTx(key.sign(t, signer, uint64(n+1), addr, common.Big0, 400_000, data))
receipt := b.receipts[n]
execution := b.gasPool.CumulativeExecution() - before.CumulativeExecution()
switch n {
@@ -584,6 +600,11 @@ func TestPaymentLaneAmsterdamImport(t *testing.T) {
require.EqualValues(t, types.ReceiptStatusSuccessful, receipt.Status)
require.Zero(t, b.gasPool.CumulativeState()-before.CumulativeState(), "state refill reverses temporary creation, unlike a transaction refund")
require.Equal(t, common.Hash{}, b.statedb.GetState(addr, common.Hash{}))
+ case 5:
+ stateGas := b.gasPool.CumulativeState() - before.CumulativeState()
+ require.EqualValues(t, types.ReceiptStatusSuccessful, receipt.Status)
+ require.EqualValues(t, params.StorageCreationSize*params.CostPerStateByte, stateGas)
+ require.Equal(t, execution+stateGas, receipt.GasUsed)
}
}
require.Equal(t, b.gasPool.CumulativeExecution(), b.lane.paymentExecution)
@@ -596,6 +617,19 @@ func TestPaymentLaneAmsterdamImport(t *testing.T) {
require.NoError(t, err, "block index %d", n)
require.Empty(t, blocks[3].Transactions())
require.Equal(t, blocks[3].Root(), chain.CurrentBlock().Root)
+ header := chain.CurrentBlock()
+ st, err := chain.StateAt(header.Root, header.Number, header.Time)
+ require.NoError(t, err)
+ evm := vm.NewEVM(NewEVMBlockContext(header, chain, nil), st, config, vm.Config{})
+ defer evm.Release()
+ for _, spender := range []common.Address{{0xcd}, {0xcc}} {
+ input := append(common.FromHex("0xdd62ed3e"), common.LeftPadBytes(key.addr[:], 32)...)
+ input = append(input, common.LeftPadBytes(spender[:], 32)...)
+ ret, _, err := evm.StaticCall(key.addr, token, input, vm.NewGasBudget(100_000, 0))
+ require.NoError(t, err)
+ require.Equal(t, common.Big1, new(big.Int).SetBytes(ret), "allowance must survive the fork")
+ }
+
}
func TestPaymentLaneAmsterdamRejectsStateOverflow(t *testing.T) {
diff --git a/core/vm/cas20_access_gas_bytecode_test.go b/core/vm/cas20_access_gas_bytecode_test.go
index 075efb7b5c..822b98c55a 100644
--- a/core/vm/cas20_access_gas_bytecode_test.go
+++ b/core/vm/cas20_access_gas_bytecode_test.go
@@ -79,6 +79,22 @@ func (p cas20AccessProgram) RunStateful(ctx *PrecompileContext, _ []byte) ([]byt
}
func TestCAS20AccessGasAgainstBytecode(t *testing.T) {
+ for _, amsterdam := range []bool{false, true} {
+ for _, table := range []bool{false, true} {
+ t.Run(fmt.Sprintf("amsterdam=%t/table=%t", amsterdam, table), func(t *testing.T) {
+ testCAS20AccessGasAgainstBytecode(t, cas20GasChainConfig(amsterdam), table)
+ })
+ }
+ }
+}
+
+func testCAS20AccessGasAgainstBytecode(t *testing.T, cfg *params.ChainConfig, table bool) {
+ if err := cfg.CheckConfigForkOrder(); err != nil {
+ t.Fatal(err)
+ }
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+
self := common.HexToAddress("0x1234")
slot := common.Hash{31: 1}
for _, target := range []struct {
@@ -143,7 +159,8 @@ func TestCAS20AccessGasAgainstBytecode(t *testing.T) {
}
run := func(native bool, budget uint64) result {
sdb := initial.Copy()
- evm := NewEVM(cas20BlockContext(1), sdb, cas20TestChainConfig(), Config{})
+ evm := NewEVM(bc, sdb, cfg, Config{})
+ evm.forceTableLoop = table
defer evm.Release()
callee := self
if native {
diff --git a/core/vm/cas20_factory_test.go b/core/vm/cas20_factory_test.go
index 5977d83ddb..d725c46647 100644
--- a/core/vm/cas20_factory_test.go
+++ b/core/vm/cas20_factory_test.go
@@ -3,11 +3,14 @@ package vm
import (
"bytes"
"errors"
+ "fmt"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)
@@ -489,3 +492,96 @@ func TestCAS20BootstrapReachesTheVariant(t *testing.T) {
t.Errorf("totalSupply = %d, want 30", got)
}
}
+
+func TestCAS20CodeWriteGas(t *testing.T) {
+ for _, amsterdam := range []bool{false, true} {
+ for _, prefunded := range []bool{false, true} {
+ t.Run(fmt.Sprintf("amsterdam=%t/prefunded=%t", amsterdam, prefunded), func(t *testing.T) {
+ st, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
+ addr := cas20Addr(cas20VariantAsset, 1)
+ if prefunded {
+ st.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
+ }
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+ evm := NewEVM(bc, st, cas20GasChainConfig(amsterdam), Config{})
+ defer evm.Release()
+ gas := NewGasBudget(1_000_000, 1_000_000)
+ ctx := &PrecompileContext{evm: evm, StateDB: st, Self: CAS20FactoryAddress, gas: &gas}
+ if !ctx.chargeCodeWrite(addr, CAS20MarkerCode) {
+ t.Fatal("code write charge failed")
+ }
+ size := uint64(len(CAS20MarkerCode))
+ execution := params.CreateGas + size*params.CreateDataGas
+ var stateGas uint64
+ if amsterdam {
+ execution = params.CreateAccessAmsterdam
+ stateGas = (params.AccountCreationSize + size) * params.CostPerStateByte
+ }
+ execution += params.Keccak256Gas + toWordSize(size)*params.Keccak256WordGas
+ if gas.UsedExecutionGas != execution || gas.UsedStateGas != int64(stateGas) {
+ t.Fatalf("charged %v, want execution=%d state=%d", gas, execution, stateGas)
+ }
+ })
+ }
+ }
+}
+
+func TestCAS20FactoryGas(t *testing.T) {
+ for _, variant := range []byte{cas20VariantAsset, cas20VariantStablecoin} {
+ for _, amsterdam := range []bool{false, true} {
+ for _, prefunded := range []bool{false, true} {
+ for _, reservoir := range []uint64{0, 1_000_000} {
+ t.Run(fmt.Sprintf("variant=%d/amsterdam=%t/prefunded=%t/state=%d", variant, amsterdam, prefunded, reservoir), func(t *testing.T) {
+ cfg := cas20GasChainConfig(amsterdam)
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+ st, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
+ seedActivation(st, cas20TestCaller)
+ creator, salt := cas20Alice, common.Hash{31: 1}
+ token := cas20DeriveAddress(variant, creator, salt)
+ if prefunded {
+ st.SetBalance(token, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
+ }
+ rules := cfg.Rules(bc.BlockNumber, true, bc.Time)
+ before := st.IntermediateRoot(rules)
+ evm := NewEVM(bc, st, cfg, Config{})
+ defer evm.Release()
+ input := encodeCreateCAS20(variant, salt, creator, nil)
+ ret, left, err := evm.Call(creator, CAS20FactoryAddress, input, NewGasBudget(300_000, reservoir), new(uint256.Int))
+ if amsterdam && reservoir == 0 {
+ if !errors.Is(err, ErrOutOfGas) || left.UsedStateGas != 0 || st.IntermediateRoot(rules) != before {
+ t.Fatalf("failed factory did not restore state: gas=%v err=%v", left, err)
+ }
+ return
+ }
+ if err != nil || common.BytesToAddress(ret) != token || !bytes.Equal(st.GetCode(token), CAS20MarkerCode) {
+ t.Fatalf("factory failed: gas=%v err=%v", left, err)
+ }
+ if amsterdam {
+ if left.UsedStateGas <= int64((params.AccountCreationSize+len(CAS20MarkerCode))*params.CostPerStateByte) {
+ t.Fatalf("factory storage was not charged: %v", left)
+ }
+ } else if left.UsedStateGas != 0 {
+ t.Fatalf("state gas charged before Amsterdam: %v", left)
+ }
+ if prefunded && st.GetBalance(token).Uint64() != 1 {
+ t.Fatal("factory changed the prefunded balance")
+ }
+ root := st.IntermediateRoot(rules)
+ _, _, err = evm.Call(creator, CAS20FactoryAddress, input, NewGasBudget(1_000_000, 1_000_000), new(uint256.Int))
+ if !errors.Is(err, ErrExecutionReverted) || st.IntermediateRoot(rules) != root {
+ t.Fatalf("duplicate creation did not preserve the token: %v", err)
+ }
+ // A failing init call must also undo the code and state already created.
+ input = encodeCreateCAS20(variant, common.Hash{31: 2}, creator, [][]byte{{0xff, 0xff, 0xff, 0xff}})
+ _, left, err = evm.Call(creator, CAS20FactoryAddress, input, NewGasBudget(1_000_000, 2_000_000), new(uint256.Int))
+ if !errors.Is(err, ErrExecutionReverted) || left.UsedStateGas != 0 || left.StateGas != 2_000_000 || st.IntermediateRoot(rules) != root {
+ t.Fatalf("failed bootstrap did not restore state and reservoir: gas=%v err=%v", left, err)
+ }
+ })
+ }
+ }
+ }
+ }
+}
diff --git a/core/vm/cas20_gas.go b/core/vm/cas20_gas.go
index 2439df22ec..a16a160669 100644
--- a/core/vm/cas20_gas.go
+++ b/core/vm/cas20_gas.go
@@ -2,6 +2,7 @@ package vm
import (
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/params"
)
@@ -50,6 +51,9 @@ func (ctx *PrecompileContext) chargeAccountAccess(addr common.Address) bool {
}
// Warmed before the charge, in the interpreter's order; the addition is journalled.
ctx.StateDB.AddAddressToAccessList(addr)
+ if ctx.evm != nil && ctx.evm.chainRules.IsAmsterdam {
+ return ctx.chargeGas(params.ColdAccountAccessAmsterdam)
+ }
return ctx.chargeGas(params.ColdAccountAccessCostEIP2929)
}
@@ -60,6 +64,15 @@ func (ctx *PrecompileContext) chargeCodeWrite(addr common.Address, code []byte)
ctx.markWriteProtected()
return false
}
+ if ctx.evm != nil && ctx.evm.chainRules.IsAmsterdam {
+ cost := GasCosts{StateGas: uint64(len(code)) * ctx.evm.Context.CostPerStateByte}
+ if hadNoCode(ctx.StateDB, addr) {
+ cost.ExecutionGas = params.CreateAccessAmsterdam
+ cost.StateGas += params.AccountCreationSize * ctx.evm.Context.CostPerStateByte
+ }
+ // Code hashing keeps its existing explicit keccak charge.
+ return ctx.chargeCosts(cost) && ctx.chargeKeccak(len(code))
+ }
cost := params.CreateDataGas * uint64(len(code))
if hadNoCode(ctx.StateDB, addr) {
cost += params.CreateGas
@@ -73,7 +86,7 @@ func (ctx *PrecompileContext) sstoreSentry() bool {
return ctx.gas.ExecutionGas > params.SstoreSentryGasEIP2200
}
-// Mirrors makeGasSStoreFunc, arms in the same order with the same clause numbers.
+// chargeStorageWrite follows the active SSTORE schedule, including refunds and refills.
func (s cas20Storage) chargeStorageWrite(slot, value common.Hash) bool {
if s.ctx == nil {
return true
@@ -87,6 +100,19 @@ func (s cas20Storage) chargeStorageWrite(slot, value common.Hash) bool {
return false
}
s.ctx.traceStorageRead(s.token, slot)
+ if s.ctx.evm != nil && s.ctx.evm.chainRules.IsAmsterdam {
+ // Cost calculation can refill state gas before the next charge.
+ prior := s.ctx.gas.AsTracing()
+ cost, err := gasSStoreCostAmsterdam(s.ctx.evm, s.ctx.gas, s.token, slot, value)
+ if err != nil {
+ s.ctx.markOutOfGas()
+ return false
+ }
+ if hooks := s.ctx.evm.Config.Tracer; hooks.HasGasHook() && prior != s.ctx.gas.AsTracing() {
+ hooks.EmitGasChange(prior, s.ctx.gas.AsTracing(), tracing.GasChangeCallPrecompiledContract)
+ }
+ return s.ctx.chargeCosts(cost)
+ }
var (
current, original = s.state.GetStateAndCommittedState(s.token, slot)
clearingRefund = params.SstoreClearsScheduleRefundEIP3529
diff --git a/core/vm/cas20_gas_bytecode_test.go b/core/vm/cas20_gas_bytecode_test.go
index a07696d399..e5ca90e964 100644
--- a/core/vm/cas20_gas_bytecode_test.go
+++ b/core/vm/cas20_gas_bytecode_test.go
@@ -1,6 +1,7 @@
package vm
import (
+ "bytes"
"errors"
"fmt"
"testing"
@@ -13,8 +14,7 @@ import (
"github.com/holiman/uint256"
)
-// Each operation accesses slot 1. Values fit PUSH1 so the reference needs only
-// PUSH1, SLOAD, SSTORE and POP, with no Solidity compiler or ABI overhead.
+// Storage operations use slot 1, without Solidity compiler or ABI overhead.
type cas20StorageOp struct {
write bool
value byte
@@ -22,7 +22,9 @@ type cas20StorageOp struct {
type cas20StorageProgram struct {
cas20StatefulBase
- ops []cas20StorageOp
+ ops []cas20StorageOp
+ valueFromInput bool
+ revert bool
}
func (p cas20StorageProgram) Name() string { return "CAS20 storage test" }
@@ -31,15 +33,23 @@ func (p cas20StorageProgram) bytecode() []byte {
var code []byte
for _, op := range p.ops {
if op.write {
- code = append(code, byte(PUSH1), op.value, byte(PUSH1), 1, byte(SSTORE))
+ if p.valueFromInput {
+ code = append(code, byte(PUSH1), 0, byte(CALLDATALOAD))
+ } else {
+ code = append(code, byte(PUSH1), op.value)
+ }
+ code = append(code, byte(PUSH1), 1, byte(SSTORE))
} else {
code = append(code, byte(PUSH1), 1, byte(SLOAD), byte(POP))
}
}
+ if p.revert {
+ code = append(code, byte(PUSH1), 0, byte(PUSH1), 0, byte(REVERT))
+ }
return code
}
-func (p cas20StorageProgram) RunStateful(ctx *PrecompileContext, _ []byte) ([]byte, error) {
+func (p cas20StorageProgram) RunStateful(ctx *PrecompileContext, input []byte) ([]byte, error) {
s := newMeteredCAS20Storage(ctx)
for _, op := range p.ops {
// Pay only the reference's stack scaffolding here. Storage gas and refunds
@@ -53,7 +63,15 @@ func (p cas20StorageProgram) RunStateful(ctx *PrecompileContext, _ []byte) ([]by
return nil, ErrOutOfGas
}
if op.write {
- if !s.setWord(common.Hash{31: 1}, common.Hash{31: op.value}) {
+ value := common.Hash{31: op.value}
+ if p.valueFromInput {
+ if !ctx.chargeGas(ctx.evm.table[CALLDATALOAD].constantGas) {
+ return nil, ErrOutOfGas
+ }
+ value = common.Hash{}
+ copy(value[:], input)
+ }
+ if !s.setWord(common.Hash{31: 1}, value) {
return nil, ErrOutOfGas
}
} else {
@@ -65,13 +83,49 @@ func (p cas20StorageProgram) RunStateful(ctx *PrecompileContext, _ []byte) ([]by
}
}
}
+ if p.revert {
+ if !ctx.chargeGas(2 * ctx.evm.table[PUSH1].constantGas) {
+ return nil, ErrOutOfGas
+ }
+ return finishCAS20Metered(ctx, nil, ErrExecutionReverted)
+ }
return finishCAS20Metered(ctx, nil, nil)
}
+func cas20GasChainConfig(amsterdam bool) *params.ChainConfig {
+ cfg := *params.ParliaTestChainConfig
+ cfg.HaberTime, cfg.HaberFixTime, cfg.BohrTime, cfg.PascalTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ cfg.PragueTime, cfg.LorentzTime, cfg.MaxwellTime, cfg.FermiTime = new(uint64), new(uint64), new(uint64), new(uint64)
+ cfg.OsakaTime, cfg.MendelTime, cfg.PasteurTime = new(uint64), new(uint64), new(uint64)
+ cfg.JennerTime = new(uint64)
+ cfg.AmsterdamTime = nil
+ if amsterdam {
+ cfg.AmsterdamTime = new(uint64)
+ }
+ cfg.BlobScheduleConfig = ¶ms.BlobScheduleConfig{Cancun: params.DefaultCancunBlobConfig, Prague: params.DefaultPragueBlobConfigBSC}
+ return &cfg
+}
+
// Compare the native storage layer against the fork-selected bytecode engine,
// including refunds and EVM.Call rollback. Both paths start with identical code
// and state at the same address, so their complete state roots are comparable.
func TestCAS20StorageGasAgainstBytecode(t *testing.T) {
+ for _, amsterdam := range []bool{false, true} {
+ for _, table := range []bool{false, true} {
+ t.Run(fmt.Sprintf("amsterdam=%t/table=%t", amsterdam, table), func(t *testing.T) {
+ testCAS20StorageGasAgainstBytecode(t, cas20GasChainConfig(amsterdam), table)
+ })
+ }
+ }
+}
+
+func testCAS20StorageGasAgainstBytecode(t *testing.T, cfg *params.ChainConfig, table bool) {
+ if err := cfg.CheckConfigForkOrder(); err != nil {
+ t.Fatal(err)
+ }
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+
read := cas20StorageOp{}
write := func(v byte) cas20StorageOp { return cas20StorageOp{write: true, value: v} }
for _, tc := range []struct {
@@ -122,28 +176,33 @@ func TestCAS20StorageGasAgainstBytecode(t *testing.T) {
root common.Hash
err error
}
- run := func(native bool, budget uint64) result {
+ run := func(native bool, budget GasBudget) result {
sdb := initial.Copy()
- evm := NewEVM(cas20BlockContext(1), sdb, cas20TestChainConfig(), Config{})
+ evm := NewEVM(bc, sdb, cfg, Config{})
+ evm.forceTableLoop = table
defer evm.Release()
if native {
evm.SetPrecompiles(PrecompiledContracts{addr: program})
}
- _, left, err := evm.Call(cas20Alice, addr, nil, NewGasBudget(budget, 0), new(uint256.Int))
+ _, left, err := evm.Call(cas20Alice, addr, nil, budget, new(uint256.Int))
refund := sdb.GetRefund() // IntermediateRoot finalises and clears refunds.
return result{left, refund, sdb.IntermediateRoot(params.Rules{IsEIP158: true}), err}
}
- const ample = uint64(1_000_000)
+ ample := NewGasBudget(1_000_000, 0)
oracle := run(false, ample)
if oracle.err != nil {
t.Fatalf("reference execution failed: %v", oracle.err)
}
- used := ample - oracle.gas.ExecutionGas
- // The last three budgets leave 2299, 2300 and 2301 gas after
+ used := ample.ExecutionGas - oracle.gas.ExecutionGas
+ // Budgets 2305-2307 leave 2299, 2300 and 2301 gas after
// the two PUSH1s preceding a first SSTORE. Exact/short budgets
// also exercise failures after earlier writes and refund changes.
- for _, budget := range []uint64{ample, used, used - 1, 2305, 2306, 2307} {
- t.Run(fmt.Sprintf("gas=%d", budget), func(t *testing.T) {
+ budgets := []GasBudget{ample, NewGasBudget(used, 0), NewGasBudget(used-1, 0), NewGasBudget(2305, 0), NewGasBudget(2306, 0), NewGasBudget(2307, 0)}
+ if cfg.IsAmsterdam(bc.BlockNumber, bc.Time) {
+ budgets = append(budgets, NewGasBudget(20_000, 100_000), NewGasBudget(30_000, 0), NewGasBudget(1_000_000, 100_000), NewGasBudget(20_000, 10_000))
+ }
+ for _, budget := range budgets {
+ t.Run(fmt.Sprintf("execution=%d/state=%d", budget.ExecutionGas, budget.StateGas), func(t *testing.T) {
want, got := run(false, budget), run(true, budget)
for _, err := range []error{want.err, got.err} {
if err != nil && !errors.Is(err, ErrOutOfGas) {
@@ -160,3 +219,104 @@ func TestCAS20StorageGasAgainstBytecode(t *testing.T) {
}
}
}
+
+func TestCAS20StorageFramesAgainstBytecode(t *testing.T) {
+ for _, amsterdam := range []bool{false, true} {
+ for _, table := range []bool{false, true} {
+ for _, tc := range []struct {
+ name string
+ values []byte
+ forward, state uint64
+ revert bool
+ }{
+ {"limited-forward", []byte{1}, 20_000, 100_000, false},
+ {"refill", []byte{1, 0}, 200_000, 200_000, false},
+ {"spill-refill", []byte{1, 0}, 200_000, 0, false},
+ {"revert", []byte{1}, 200_000, 100_000, true},
+ {"oog", []byte{1}, 3_000, 100_000, false},
+ } {
+ t.Run(fmt.Sprintf("amsterdam=%t/table=%t/%s", amsterdam, table, tc.name), func(t *testing.T) {
+ cfg := cas20GasChainConfig(amsterdam)
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+ target, wrapper := common.HexToAddress("0x1234"), common.HexToAddress("0x5678")
+ program := cas20StorageProgram{ops: []cas20StorageOp{{write: true}}, valueFromInput: true, revert: tc.revert}
+ var code []byte
+ for i, v := range tc.values {
+ code = append(code, byte(PUSH1), v, byte(PUSH1), 0, byte(MSTORE))
+ code = append(code, byte(PUSH1), 0, byte(PUSH1), 0, byte(PUSH1), 32, byte(PUSH1), 0, byte(PUSH1), 0, byte(PUSH20))
+ code = append(code, target[:]...)
+ code = append(code, byte(PUSH3), byte(tc.forward>>16), byte(tc.forward>>8), byte(tc.forward), byte(CALL), byte(PUSH1), byte((i+1)*32), byte(MSTORE))
+ }
+ code = append(code, byte(PUSH1), byte(len(tc.values)*32), byte(PUSH1), 32, byte(RETURN))
+ initial, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
+ initial.SetCode(target, program.bytecode(), tracing.CodeChangeUnspecified)
+ initial.SetCode(wrapper, code, tracing.CodeChangeUnspecified)
+ initial.Finalise(cfg.Rules(bc.BlockNumber, true, bc.Time))
+ type result struct {
+ output []byte
+ gas GasBudget
+ refund uint64
+ root common.Hash
+ }
+ run := func(native bool) result {
+ st := initial.Copy()
+ evm := NewEVM(bc, st, cfg, Config{})
+ defer evm.Release()
+ evm.forceTableLoop = table
+ if native {
+ evm.SetPrecompiles(PrecompiledContracts{target: program})
+ }
+ reservoir := tc.state
+ if !amsterdam {
+ reservoir = 0
+ }
+ ret, left, err := evm.Call(cas20Alice, wrapper, nil, NewGasBudget(1_000_000, reservoir), new(uint256.Int))
+ if err != nil {
+ t.Fatal(err)
+ }
+ refund := st.GetRefund()
+ return result{ret, left, refund, st.IntermediateRoot(evm.GetRules())}
+ }
+ want, got := run(false), run(true)
+ if !bytes.Equal(want.output, got.output) || want.gas != got.gas || want.refund != got.refund || want.root != got.root {
+ t.Fatalf("native %+v; bytecode %+v", got, want)
+ }
+ if amsterdam && tc.name == "limited-forward" && got.output[31] != 1 {
+ t.Fatal("state reservoir did not fund the forwarded call")
+ }
+ })
+ }
+ }
+ }
+}
+
+func TestCAS20StateRefillGasTrace(t *testing.T) {
+ for _, reservoir := range []uint64{0, 200_000} {
+ cfg := cas20GasChainConfig(true)
+ bc := cas20BlockContext(1)
+ bc.CostPerStateByte = params.CostPerStateByte
+ st, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
+ addr := common.HexToAddress("0x1234")
+ program := cas20StorageProgram{ops: []cas20StorageOp{{write: true, value: 1}, {write: true, value: 0}}}
+ st.SetCode(addr, program.bytecode(), tracing.CodeChangeUnspecified)
+ st.Finalise(cfg.Rules(bc.BlockNumber, true, bc.Time))
+ gas := NewGasBudget(200_000, reservoir)
+ last := gas.AsTracing()
+ hooks := &tracing.Hooks{OnGasChangeV2: func(before, after tracing.Gas, _ tracing.GasChangeReason) {
+ if before != last {
+ t.Errorf("gas event gap: previous %v, next %v", last, before)
+ }
+ last = after
+ }}
+ evm := NewEVM(bc, st, cfg, Config{Tracer: hooks})
+ defer evm.Release()
+ ctx := &PrecompileContext{evm: evm, StateDB: st, Self: addr, gas: &gas}
+ if _, err := program.RunStateful(ctx, nil); err != nil {
+ t.Fatal(err)
+ }
+ if gas.UsedStateGas != 0 || last != gas.AsTracing() {
+ t.Fatalf("refill gas=%v, final event=%v", gas, last)
+ }
+ }
+}
diff --git a/core/vm/contracts_stateful.go b/core/vm/contracts_stateful.go
index 032503ca82..34ed4ca73c 100644
--- a/core/vm/contracts_stateful.go
+++ b/core/vm/contracts_stateful.go
@@ -108,13 +108,16 @@ func (ctx *PrecompileContext) traceAccountRead(addr common.Address) {
}
}
-// chargeGas is where every CAS20 charge arrives. False means stop before the
-// operation the charge pays for, as the interpreter checks gas before an opcode.
func (ctx *PrecompileContext) chargeGas(cost uint64) bool {
+ return ctx.chargeCosts(GasCosts{ExecutionGas: cost})
+}
+
+// chargeCosts stops the frame before an operation whose cost it cannot cover.
+func (ctx *PrecompileContext) chargeCosts(cost GasCosts) bool {
if ctx.OutOfGas() {
return false
}
- if !ctx.UseGas(GasCosts{ExecutionGas: cost}) {
+ if !ctx.UseGas(cost) {
ctx.markOutOfGas()
return false
}
diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go
index e8590814d3..3ce1fafde7 100644
--- a/core/vm/gas_table.go
+++ b/core/vm/gas_table.go
@@ -685,29 +685,27 @@ func gasSStore8037And8038(evm *EVM, contract *Contract, stack *Stack, mem *Memor
if contract.Gas.ExecutionGas <= params.SstoreSentryGasEIP2200 {
return GasCosts{}, errors.New("not enough gas for reentrancy sentry")
}
- var (
- y, x = stack.back(1), stack.peek()
- slot = common.Hash(x.Bytes32())
- stateSet = params.StorageCreationSize * evm.Context.CostPerStateByte
- )
+ return gasSStoreCostAmsterdam(evm, &contract.Gas, contract.Address(), stack.peek().Bytes32(), stack.back(1).Bytes32())
+}
+
+// gasSStoreCostAmsterdam also prices native writes; callers enforce write protection and the sentry.
+func gasSStoreCostAmsterdam(evm *EVM, budget *GasBudget, addr common.Address, slot, value common.Hash) (GasCosts, error) {
+ stateSet := params.StorageCreationSize * evm.Context.CostPerStateByte
// Check slot presence in the access list
access := params.WarmStorageAccessAmsterdam
- _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot)
+ _, slotPresent := evm.StateDB.SlotInAccessList(addr, slot)
if !slotPresent {
access = params.ColdStorageAccessAmsterdam
}
// Check access cost affordability before reading slot
- if contract.Gas.ExecutionGas < access {
+ if budget.ExecutionGas < access {
return GasCosts{}, errors.New("not enough gas for slot access")
}
if !slotPresent {
- evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
+ evm.StateDB.AddSlotToAccessList(addr, slot)
}
// Read the slot value for gas cost measurement
- var (
- value = common.Hash(y.Bytes32())
- current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot)
- )
+ current, original := evm.StateDB.GetStateAndCommittedState(addr, slot)
if current == value { // noop (1)
return GasCosts{ExecutionGas: access}, nil
}
@@ -732,7 +730,7 @@ func gasSStore8037And8038(evm *EVM, contract *Contract, stack *Stack, mem *Memor
}
if original == value { // reset to original value (2.2.2)
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
- contract.Gas.RefundState(stateSet)
+ budget.RefundState(stateSet)
}
evm.StateDB.AddRefund(params.StorageWriteAmsterdam)
}
From f3c95c856f45dea782738b8718bdefed169a2567 Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:51:19 +0800
Subject: [PATCH 12/14] miner: build Amsterdam activation blocks locally
---
miner/bid_simulator.go | 13 ++++++++++++
miner/miner_mev.go | 9 +++++++++
miner/miner_mev_test.go | 44 +++++++++++++++++++++++++++++++++++++++++
miner/worker.go | 5 ++++-
4 files changed, 70 insertions(+), 1 deletion(-)
diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go
index 15f178266a..2d596a7761 100644
--- a/miner/bid_simulator.go
+++ b/miner/bid_simulator.go
@@ -734,6 +734,9 @@ func (b *bidSimulator) preSealVerifyBidBlock(decoded *buildertypes.DecodedBidBlo
if parent == nil {
return consensus.ErrUnknownAncestor
}
+ if isAmsterdamActivation(b.chainConfig, parent, header) {
+ return errors.New("builder bids disabled on Amsterdam activation block")
+ }
expectedGasLimit := core.CalcGasLimit(parent.GasLimit, b.bidWorker.getGasCeil())
if header.GasLimit != expectedGasLimit {
return fmt.Errorf("invalid gasLimit: got %d, want %d", header.GasLimit, expectedGasLimit)
@@ -1051,6 +1054,16 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
return
}
+ parent := b.chain.GetHeaderByHash(bidRuntime.env.header.ParentHash)
+ if parent == nil {
+ err = consensus.ErrUnknownAncestor
+ return
+ }
+ if isAmsterdamActivation(b.chainConfig, parent, bidRuntime.env.header) {
+ err = errors.New("builder bids disabled on Amsterdam activation block")
+ return
+ }
+
// TODO: Adapt this reservation to both gas dimensions before enabling
// Amsterdam on BSC.
// Reserve gas for the payBidTx appended at the end of the block so the
diff --git a/miner/miner_mev.go b/miner/miner_mev.go
index 3a69c67a3f..879e86bb6d 100644
--- a/miner/miner_mev.go
+++ b/miner/miner_mev.go
@@ -14,6 +14,7 @@ import (
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
"github.com/ethereum/go-ethereum/internal/version"
"github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/params"
)
const (
@@ -21,6 +22,11 @@ const (
maxBlobTxPerBlock = 6
)
+// isAmsterdamActivation identifies the block validators must build locally.
+func isAmsterdamActivation(config *params.ChainConfig, parent, header *types.Header) bool {
+ return !config.IsAmsterdam(parent.Number, parent.Time) && config.IsAmsterdam(header.Number, header.Time)
+}
+
// MevRunning return true if mev is running.
func (miner *Miner) MevRunning() bool {
return miner.bidSimulator.isRunning() && miner.bidSimulator.receivingBid()
@@ -114,6 +120,9 @@ func (miner *Miner) SendBidBlock(ctx context.Context, args *buildertypes.BidBloc
return common.Hash{}, buildertypes.NewInvalidBidError(fmt.Sprintf("parent not found: %s, bidHash=%s", parentHash.Hex(), bidHash))
}
+ if isAmsterdamActivation(miner.worker.chainConfig, parent, bb.Header) {
+ return common.Hash{}, buildertypes.NewInvalidBidError("builder bids disabled on Amsterdam activation block")
+ }
// Security: validators must self-produce hard-fork activation blocks.
if miner.worker.chainConfig.IsOnPasteur(bb.Header.Number, parent.Time, bb.Header.Time) ||
miner.worker.chainConfig.IsOnJenner(bb.Header.Number, parent.Time, bb.Header.Time) {
diff --git a/miner/miner_mev_test.go b/miner/miner_mev_test.go
index 96f425d004..996f0b28ab 100644
--- a/miner/miner_mev_test.go
+++ b/miner/miner_mev_test.go
@@ -3,11 +3,15 @@ package miner
import (
"crypto/rand"
"errors"
+ "math/big"
"strings"
"testing"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
gokzg4844 "github.com/crate-crypto/go-eth-kzg"
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/consensus/parlia"
+ "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
"github.com/ethereum/go-ethereum/crypto"
@@ -107,3 +111,43 @@ func TestValidateBidBlockBlobTxs_InvalidProof(t *testing.T) {
t.Fatalf("unexpected error message: %v", err)
}
}
+
+func TestAmsterdamActivation(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ fork := uint64(100)
+ config.AmsterdamTime = &fork
+ for _, tc := range []struct {
+ name string
+ parent, time uint64
+ want bool
+ }{
+ {"before", 98, 99, false}, {"activation", 99, 100, true}, {"skipped-timestamp", 99, 101, true}, {"after", 100, 101, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ parent := &types.Header{Number: big.NewInt(1), Time: tc.parent}
+ header := &types.Header{Number: big.NewInt(2), Time: tc.time}
+ if got := isAmsterdamActivation(&config, parent, header); got != tc.want {
+ t.Fatalf("activation=%v, want %v", got, tc.want)
+ }
+ })
+ }
+ config.AmsterdamTime = nil
+ if isAmsterdamActivation(&config, &types.Header{Number: big.NewInt(1)}, &types.Header{Number: big.NewInt(2), Time: 100}) {
+ t.Fatal("disabled Amsterdam detected as activation")
+ }
+}
+
+func TestBidBlockRejectsAmsterdamActivation(t *testing.T) {
+ w, config, _, header, _ := laneMinerChain(t, false)
+ config.AmsterdamTime = &header.Time
+ header.Root = common.Hash{1}
+ db := rawdb.NewMemoryDatabase()
+ defer db.Close()
+ engine := parlia.New(config, db, nil, common.Hash{})
+ defer engine.Close()
+ simulator := &bidSimulator{chain: w.chain, chainConfig: config, engine: engine, bidWorker: w}
+ err := simulator.preSealVerifyBidBlock(&buildertypes.DecodedBidBlock{Header: header})
+ if err == nil || err.Error() != "builder bids disabled on Amsterdam activation block" {
+ t.Fatalf("activation bid error: %v", err)
+ }
+}
diff --git a/miner/worker.go b/miner/worker.go
index cb366b014c..7b156acf76 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -1579,7 +1579,10 @@ LOOP:
var simBidBlockReward *uint256.Int
var simBidValidatorReward *uint256.Int
var localValidatorReward *uint256.Int
- if w.bidFetcher != nil && bestWork.header.Difficulty.Cmp(diffInTurn) == 0 {
+ parent := w.chain.GetHeaderByHash(bestWork.header.ParentHash)
+ // Cached builder bids must not replace the validator's Amsterdam activation block.
+ if w.bidFetcher != nil && bestWork.header.Difficulty.Cmp(diffInTurn) == 0 &&
+ parent != nil && !isAmsterdamActivation(w.chainConfig, parent, bestWork.header) {
inturnBlocksGauge.Inc(1)
localValidatorReward = new(uint256.Int).Mul(bestReward, uint256.NewInt(*w.config.Mev.ValidatorCommission))
localValidatorReward.Div(localValidatorReward, uint256.NewInt(10000))
From 22ceb5e7a014964ca736fd919fb0637b685ab13e Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:51:24 +0800
Subject: [PATCH 13/14] core, miner: fix bid gas accounting
---
core/gaspool.go | 38 +++++++++++++---
core/gaspool_test.go | 64 +++++++++++++++++++++++++++
miner/bid_simulator.go | 74 +++++++++++++++++--------------
miner/lane_regression_test.go | 81 ++++++++++++++++++++++++++++++++++
miner/miner_mev_test.go | 83 +++++++++++++++++++++++++++++++++++
5 files changed, 302 insertions(+), 38 deletions(-)
diff --git a/core/gaspool.go b/core/gaspool.go
index ec5aa38ba9..dcf0c54bf8 100644
--- a/core/gaspool.go
+++ b/core/gaspool.go
@@ -38,6 +38,10 @@ type GasPool struct {
// the larger of the two.
cumulativeExecution uint64
cumulativeState uint64
+
+ // Local reservations leave room for transactions appended later.
+ reservedExecution uint64
+ reservedState uint64
}
// NewGasPool initializes the gasPool with the given amount.
@@ -70,15 +74,35 @@ func (gp *GasPool) CheckGasLegacy(amount uint64) error {
// the worst-case execution contribution must fit in the execution dimension and
// the worst-case state contribution must fit in the state dimension
func (gp *GasPool) CheckGasAmsterdam(executionReservation, stateReservation uint64) error {
- if gp.initial-gp.cumulativeExecution < executionReservation {
+ if gp.initial-gp.cumulativeExecution-gp.reservedExecution < executionReservation {
return ErrGasLimitReached
}
- if gp.initial-gp.cumulativeState < stateReservation {
+ if gp.initial-gp.cumulativeState-gp.reservedState < stateReservation {
return ErrGasLimitReached
}
return nil
}
+// ReserveGasAmsterdam leaves room for a later transaction without charging gas used.
+func (gp *GasPool) ReserveGasAmsterdam(execution, state uint64) error {
+ if err := gp.CheckGasAmsterdam(execution, state); err != nil {
+ return err
+ }
+ gp.reservedExecution += execution
+ gp.reservedState += state
+ return nil
+}
+
+// ReleaseGasAmsterdam returns a reservation without changing cumulative gas used.
+func (gp *GasPool) ReleaseGasAmsterdam(execution, state uint64) error {
+ if execution > gp.reservedExecution || state > gp.reservedState {
+ return fmt.Errorf("gas reservation underflow: execution %d, state %d", execution, state)
+ }
+ gp.reservedExecution -= execution
+ gp.reservedState -= state
+ return nil
+}
+
// ChargeGasLegacy adds the refunded gas back to the pool and updates
// the cumulative gas usage accordingly. Used before Amsterdam.
func (gp *GasPool) ChargeGasLegacy(returned uint64, gasUsed uint64) error {
@@ -101,7 +125,7 @@ func (gp *GasPool) ChargeGasAmsterdam(txExecution, txState, receiptGasUsed uint6
cumulativeExecution := gp.cumulativeExecution + txExecution
cumulativeState := gp.cumulativeState + txState
blockUsed := max(cumulativeExecution, cumulativeState)
- if gp.initial < blockUsed {
+ if gp.initial-gp.reservedExecution < cumulativeExecution || gp.initial-gp.reservedState < cumulativeState {
return fmt.Errorf("%w: block gas overflow: initial %d, used %d (execution: %d, state: %d)",
ErrGasLimitReached, gp.initial, blockUsed, cumulativeExecution, cumulativeState)
}
@@ -119,8 +143,8 @@ func (gp *GasPool) Available(amsterdam bool) uint64 {
if !amsterdam {
return gp.remaining
}
- exec := gp.initial - gp.cumulativeExecution
- state := gp.initial - gp.cumulativeState
+ exec := gp.initial - gp.cumulativeExecution - gp.reservedExecution
+ state := gp.initial - gp.cumulativeState - gp.reservedState
if exec >= params.MaxTxGas {
return state
}
@@ -166,6 +190,8 @@ func (gp *GasPool) Snapshot() *GasPool {
cumulativeUsed: gp.cumulativeUsed,
cumulativeExecution: gp.cumulativeExecution,
cumulativeState: gp.cumulativeState,
+ reservedExecution: gp.reservedExecution,
+ reservedState: gp.reservedState,
}
}
@@ -176,6 +202,8 @@ func (gp *GasPool) Set(other *GasPool) {
gp.cumulativeUsed = other.cumulativeUsed
gp.cumulativeExecution = other.cumulativeExecution
gp.cumulativeState = other.cumulativeState
+ gp.reservedExecution = other.reservedExecution
+ gp.reservedState = other.reservedState
}
func (gp *GasPool) String() string {
diff --git a/core/gaspool_test.go b/core/gaspool_test.go
index 67b5aa8ea1..d3a1cae50b 100644
--- a/core/gaspool_test.go
+++ b/core/gaspool_test.go
@@ -17,6 +17,9 @@
package core
import (
+ "errors"
+ "fmt"
+ "math"
"testing"
"github.com/ethereum/go-ethereum/params"
@@ -72,3 +75,64 @@ func TestGasPoolAvailable(t *testing.T) {
}
}
}
+
+func TestGasPoolAmsterdamReservation(t *testing.T) {
+ const limit uint64 = 60_000_000
+ for _, gas := range []uint64{25_000, 300_000, params.MaxTxGas + 1, 30_000_000} {
+ t.Run(fmt.Sprint(gas), func(t *testing.T) {
+ gp := NewGasPool(limit)
+ execution, state := min(gas, params.MaxTxGas), gas
+ if err := gp.ReserveGasAmsterdam(execution, state); err != nil {
+ t.Fatal(err)
+ }
+ if gp.Used() != 0 || gp.CumulativeUsed() != 0 {
+ t.Fatal("reservation counted as consumption")
+ }
+ if gp.Available(false) != limit {
+ t.Fatal("reservation changed legacy budget")
+ }
+ for _, usage := range [][2]uint64{{limit - execution + 1, 0}, {0, limit - state + 1}} {
+ if err := gp.ChargeGasAmsterdam(usage[0], usage[1], 1); !errors.Is(err, ErrGasLimitReached) {
+ t.Fatalf("reserved budget consumed: %v", err)
+ }
+ }
+ before := gp.Snapshot()
+ if err := gp.ReserveGasAmsterdam(math.MaxUint64, 0); !errors.Is(err, ErrGasLimitReached) {
+ t.Fatal(err)
+ }
+ if *gp != *before {
+ t.Fatal("failed reservation changed pool")
+ }
+ if err := gp.ReleaseGasAmsterdam(execution+1, state); err == nil {
+ t.Fatal("invalid release succeeded")
+ }
+ if *gp != *before {
+ t.Fatal("failed release changed pool")
+ }
+ gp.Set(NewGasPool(limit))
+ gp.Set(before)
+ if err := gp.ChargeGasAmsterdam(limit-execution, limit-state, 123); err != nil {
+ t.Fatal(err)
+ }
+ if gp.Available(true) != 0 {
+ t.Fatal("reserved capacity exposed")
+ }
+ consumed := gp.Snapshot()
+ if err := gp.ReleaseGasAmsterdam(execution, state); err != nil {
+ t.Fatal(err)
+ }
+ if gp.Used() != consumed.Used() || gp.CumulativeUsed() != 123 {
+ t.Fatal("release changed consumption")
+ }
+ if err := gp.CheckGasAmsterdam(execution, state); err != nil {
+ t.Fatalf("payment does not fit: %v", err)
+ }
+ if err := gp.ChargeGasAmsterdam(execution, state, 456); err != nil {
+ t.Fatal(err)
+ }
+ if gp.CumulativeExecution() != limit || gp.CumulativeState() != limit || gp.CumulativeUsed() != 579 {
+ t.Fatal("incorrect cumulative accounting")
+ }
+ })
+ }
+}
diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go
index 2d596a7761..fe8c02d3aa 100644
--- a/miner/bid_simulator.go
+++ b/miner/bid_simulator.go
@@ -478,11 +478,16 @@ func (b *bidSimulator) newBidLoop() {
continue
}
- // Pre-check before simulation for fast feedback to the builder;
- // simulation would reject these txs anyway via state_transition preCheck.
- if errCap := b.checkIfBidExceedsTxGasLimit(newBid.bid); errCap != nil {
+ // Builders cannot produce the Amsterdam activation block, so parent rules suffice here.
+ parent := b.chain.GetHeaderByHash(newBid.bid.ParentHash)
+ if parent == nil {
+ err = consensus.ErrUnknownAncestor
+ } else {
+ err = b.checkIfBidExceedsTxGasLimit(newBid.bid, parent)
+ }
+ if err != nil {
if newBid.feedback != nil {
- newBid.feedback <- errCap
+ newBid.feedback <- err
}
continue
}
@@ -578,21 +583,23 @@ func (b *bidSimulator) getBlockInterval(parentHeader *types.Header) uint64 {
}
// checkIfBidExceedsTxGasLimit checks whether any transaction in the bid exceeds the max txn gas.
-func (b *bidSimulator) checkIfBidExceedsTxGasLimit(bid *buildertypes.Bid) error {
- currentHeader := b.chain.CurrentBlock()
- if !b.chainConfig.IsOsaka(currentHeader.Number, currentHeader.Time) {
+func (b *bidSimulator) checkIfBidExceedsTxGasLimit(bid *buildertypes.Bid, header *types.Header) error {
+ cap := params.MaxTxGas
+ if b.chainConfig.IsAmsterdam(header.Number, header.Time) {
+ cap = params.MaxTxTotalGas
+ } else if !b.chainConfig.IsOsaka(header.Number, header.Time) {
return nil
}
for _, tx := range bid.Txs {
- if tx.Gas() > params.MaxTxGas {
+ if tx.Gas() > cap {
log.Debug("discard bid due to per-tx gas limit",
"block", bid.BlockNumber,
"bidHash", bid.Hash().TerminalString(),
"txHash", tx.Hash().TerminalString(),
"txGas", tx.Gas(),
- "txGasLimit", params.MaxTxGas,
+ "txGasLimit", cap,
)
- return fmt.Errorf("bid rejected: %w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, params.MaxTxGas, tx.Gas())
+ return fmt.Errorf("bid rejected: %w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, cap, tx.Gas())
}
}
return nil
@@ -767,9 +774,13 @@ func (b *bidSimulator) preSealVerifyBidBlock(decoded *buildertypes.DecodedBidBlo
}
// Per-tx gas cap over user txs only, mirroring SendBid.
+ cap := params.MaxTxGas
+ if b.chainConfig.IsAmsterdam(header.Number, header.Time) {
+ cap = params.MaxTxTotalGas
+ }
for _, tx := range decoded.Txs[:decoded.SystemTxStart] {
- if tx.Gas() > params.MaxTxGas {
- return fmt.Errorf("%w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, params.MaxTxGas, tx.Gas())
+ if tx.Gas() > cap {
+ return fmt.Errorf("%w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, cap, tx.Gas())
}
}
@@ -1063,28 +1074,18 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
err = errors.New("builder bids disabled on Amsterdam activation block")
return
}
-
- // TODO: Adapt this reservation to both gas dimensions before enabling
- // Amsterdam on BSC.
- // Reserve gas for the payBidTx appended at the end of the block so the
- // admission check and greedy merge leave room for it; returned right before
- // it is committed.
- if err = bidRuntime.env.gasPool.CheckGasLegacy(params.PayBidTxGasLimit); err != nil {
+ amsterdam := bidRuntime.env.evm.GetRules().IsAmsterdam
+ // Leave both budgets available for the payment appended after greedy merge.
+ if amsterdam {
+ err = bidRuntime.env.gasPool.ReserveGasAmsterdam(min(payBidTx.Gas(), params.MaxTxGas), payBidTx.Gas())
+ } else {
+ err = bidRuntime.env.gasPool.CheckGasLegacy(params.PayBidTxGasLimit)
+ }
+ if err != nil {
return
}
-
- // error log:
- // simulation failed blockNumber=47630147 parentHash=0x2476bcc93db4c924a2c8079c6d5d783441a72d6ff70c5850b1afd778102e175e builder=0x48a5Ed9abC1a8FBe86ceC4900483f43a7f2dBB48
- // gasUsed=136807406 gasLimit=137816878 err="gas used exceeds gas limit"
- // error tracing:
- // left: b.RawBid.GasUsed + b.PayBidTxGasUsed => (136782406 + 25000 = 136807406)
- // right: headerGasLimit - b.PayBidTxGasLimit - systemGasReserved => (137816878 - 25000 - 1000000 = 136791878)
- // cause: 136807406 > 136791878 => true
- // error reason:
- // left should not be added with PayBidTxGasUsed, Or right should be not be subtracted with PayBidTxGasLimit
- // error fix:
- // 136782406 > 136791878 => false, Or 136807406 > 136816878 => false
- if bidRuntime.bid.GasUsed > bidRuntime.env.gasPool.Available(bidRuntime.env.evm.GetRules().IsAmsterdam) {
+ // Amsterdam validates capacity per transaction in both dimensions, not by receipt gas sums.
+ if !amsterdam && bidRuntime.bid.GasUsed > bidRuntime.env.gasPool.Available(false) {
err = errors.New("gas used exceeds gas limit")
return
}
@@ -1218,7 +1219,14 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
}
// commit payBidTx at the end of the block
- bidRuntime.env.gasPool.AddGas(params.PayBidTxGasLimit)
+ if amsterdam {
+ err = bidRuntime.env.gasPool.ReleaseGasAmsterdam(min(payBidTx.Gas(), params.MaxTxGas), payBidTx.Gas())
+ } else {
+ err = bidRuntime.env.gasPool.AddGas(params.PayBidTxGasLimit)
+ }
+ if err != nil {
+ return
+ }
err = bidRuntime.commitTransaction(b.chain, b.chainConfig, payBidTx, true)
if err != nil {
log.Error("BidSimulator: failed to commit tx", "builder", bidRuntime.bid.Builder,
diff --git a/miner/lane_regression_test.go b/miner/lane_regression_test.go
index c00dce35e5..8191269f40 100644
--- a/miner/lane_regression_test.go
+++ b/miner/lane_regression_test.go
@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/paymentlane"
"github.com/ethereum/go-ethereum/core/systemcontracts/jenner"
"github.com/ethereum/go-ethereum/core/types"
+ buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
@@ -133,6 +134,10 @@ func TestPaymentLaneWorkerRollback(t *testing.T) {
require.True(t, env.lane.On())
sender := crypto.PubkeyToAddress(key.PublicKey)
balance := env.state.GetBalance(sender).ToBig()
+ if amsterdam {
+ require.NoError(t, env.gasPool.ReserveGasAmsterdam(25_000, 300_000))
+ }
+ before := env.gasPool.Snapshot()
pool := env.gasPool.String()
tx, err := types.SignTx(types.NewTransaction(0, common.Address{0xaa}, big.NewInt(1), 1, big.NewInt(1), nil), types.LatestSigner(config), key)
require.NoError(t, err)
@@ -140,9 +145,85 @@ func TestPaymentLaneWorkerRollback(t *testing.T) {
_, err = w.applyTransaction(env, tx)
require.ErrorIs(t, err, core.ErrIntrinsicGas)
require.Equal(t, pool, env.gasPool.String())
+ require.Equal(t, before, env.gasPool)
require.Equal(t, balance, env.state.GetBalance(sender).ToBig())
require.Zero(t, env.lane.PaymentGasUsed())
require.Equal(t, env.lane.Budget.PaymentLaneQuota, env.lane.IdleGas())
})
}
}
+
+func TestBidPaymentGasReservation(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ amsterdam bool
+ gas uint64
+ }{
+ {"legacy", false, params.PayBidTxGasLimit},
+ {"amsterdam", true, params.PayBidTxGasLimit},
+ {"amsterdam-state-creation", true, 300_000},
+ {"amsterdam-above-execution-cap", true, 30_000_000},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ w, config, parent, header, key := laneMinerChain(t, false)
+ if tc.amsterdam {
+ zero := uint64(0)
+ config.AmsterdamTime = &zero
+ slot := header.Number.Uint64()
+ header.SlotNumber = &slot
+ }
+ header.ExcessBlobGas = new(uint64)
+ header.BlobGasUsed = new(uint64)
+ env, err := w.makeEnv(parent, header, common.Address{}, nil, false)
+ require.NoError(t, err)
+ defer env.discard()
+ sender := crypto.PubkeyToAddress(key.PublicKey)
+ recipient := common.Address{0xaa}
+ payee := sender
+ if tc.gas == 300_000 {
+ payee = recipient
+ }
+ pay, err := types.SignTx(types.NewTransaction(1, payee, big.NewInt(1), tc.gas, common.Big0, nil), types.LatestSigner(config), key)
+ require.NoError(t, err)
+ if tc.amsterdam {
+ require.NoError(t, env.gasPool.ReserveGasAmsterdam(min(pay.Gas(), params.MaxTxGas), pay.Gas()))
+ } else {
+ require.NoError(t, env.gasPool.CheckGasLegacy(params.PayBidTxGasLimit))
+ }
+ if tc.amsterdam {
+ require.Zero(t, env.gasPool.Used())
+ require.Zero(t, env.lane.PaymentGasUsed())
+ }
+ // The preceding payment must be booked even while the final payment has a reservation.
+ tx, err := types.SignTx(types.NewTransaction(0, sender, big.NewInt(1), 25_000, common.Big0, nil), types.LatestSigner(config), key)
+ require.NoError(t, err)
+ r := &BidRuntime{env: env, bid: &buildertypes.Bid{}}
+ require.NoError(t, r.commitTransaction(w.chain, config, tx, true))
+ if tc.amsterdam {
+ require.Equal(t, uint64(12_000), env.lane.PaymentGasUsed())
+ } else {
+ require.Equal(t, uint64(21_000), env.lane.PaymentGasUsed())
+ }
+ before := env.gasPool.Snapshot()
+ if tc.amsterdam {
+ require.NoError(t, env.gasPool.ReleaseGasAmsterdam(min(pay.Gas(), params.MaxTxGas), pay.Gas()))
+ } else {
+ require.NoError(t, env.gasPool.AddGas(params.PayBidTxGasLimit))
+ }
+ require.Equal(t, before.CumulativeUsed(), env.gasPool.CumulativeUsed())
+ require.NoError(t, r.commitTransaction(w.chain, config, pay, true))
+ require.Equal(t, types.ReceiptStatusSuccessful, env.receipts[1].Status)
+ require.NoError(t, env.lane.VerifyPackedBid(env.gasPool))
+ if tc.amsterdam {
+ require.Equal(t, max(env.gasPool.CumulativeExecution(), env.gasPool.CumulativeState()), env.lane.PaymentGasUsed())
+ require.Equal(t, env.receipts[0].GasUsed+env.receipts[1].GasUsed, env.gasPool.CumulativeUsed())
+ if payee == recipient {
+ require.Equal(t, uint64(params.AccountCreationSize)*env.evm.Context.CostPerStateByte, env.gasPool.CumulativeState())
+ }
+ } else {
+ require.Equal(t, uint64(42_000), env.gasPool.Used())
+ require.Equal(t, uint64(42_000), env.lane.PaymentGasUsed())
+ }
+ })
+ }
+}
diff --git a/miner/miner_mev_test.go b/miner/miner_mev_test.go
index 996f0b28ab..2c195a0f70 100644
--- a/miner/miner_mev_test.go
+++ b/miner/miner_mev_test.go
@@ -6,16 +6,19 @@ import (
"math/big"
"strings"
"testing"
+ "time"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
gokzg4844 "github.com/crate-crypto/go-eth-kzg"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/parlia"
+ "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
buildertypes "github.com/ethereum/go-ethereum/core/types/builder"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
+ "github.com/ethereum/go-ethereum/miner/minerconfig"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)
@@ -112,6 +115,35 @@ func TestValidateBidBlockBlobTxs_InvalidProof(t *testing.T) {
}
}
+func TestBidTransactionGasCap(t *testing.T) {
+ config := *params.ParliaTestChainConfig
+ osaka, amsterdam := uint64(100), uint64(200)
+ config.OsakaTime = &osaka
+ config.AmsterdamTime = &amsterdam
+ simulator := &bidSimulator{chainConfig: &config}
+ for _, tc := range []struct {
+ name string
+ time, gas uint64
+ rejected bool
+ }{
+ {"before-osaka", 99, params.MaxTxGas + 1, false},
+ {"osaka-cap", 100, params.MaxTxGas, false},
+ {"before-amsterdam", 199, params.MaxTxGas + 1, true},
+ {"amsterdam", 200, params.MaxTxGas + 1, false},
+ {"amsterdam-total-cap", 201, params.MaxTxTotalGas, false},
+ {"amsterdam-over-cap", 201, params.MaxTxTotalGas + 1, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ header := &types.Header{Number: big.NewInt(2), Time: tc.time}
+ bid := &buildertypes.Bid{Txs: types.Transactions{types.NewTx(&types.LegacyTx{Gas: tc.gas})}}
+ err := simulator.checkIfBidExceedsTxGasLimit(bid, header)
+ if errors.Is(err, core.ErrGasLimitTooHigh) != tc.rejected {
+ t.Fatalf("gas cap error: %v", err)
+ }
+ })
+ }
+}
+
func TestAmsterdamActivation(t *testing.T) {
config := *params.ParliaTestChainConfig
fork := uint64(100)
@@ -151,3 +183,54 @@ func TestBidBlockRejectsAmsterdamActivation(t *testing.T) {
t.Fatalf("activation bid error: %v", err)
}
}
+
+func TestBidGasCapBeforeSelection(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ amsterdam bool
+ gas uint64
+ reject bool
+ }{
+ {"legacy-over-cap", false, params.MaxTxGas + 1, true},
+ {"amsterdam-over-cap", true, params.MaxTxTotalGas + 1, true},
+ {"amsterdam-large-tx", true, 30_000_000, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ w, config, parent, _, _ := laneMinerChain(t, false)
+ if tc.amsterdam {
+ zero := uint64(0)
+ config.AmsterdamTime = &zero
+ }
+ commission := uint64(100)
+ previous := &buildertypes.Bid{GasFee: big.NewInt(1), BuilderFee: common.Big0}
+ simulator := &bidSimulator{chain: w.chain, chainConfig: config,
+ config: &minerconfig.MevConfig{ValidatorCommission: &commission},
+ exitCh: make(chan struct{}), newBidCh: make(chan newBidPackage, 1), simBidCh: make(chan *simBidReq, 1),
+ bestBidToRun: map[common.Hash]*buildertypes.Bid{parent.Hash(): previous},
+ }
+ simulator.running.Store(true)
+ done := make(chan struct{})
+ go func() { defer close(done); simulator.newBidLoop() }()
+ defer func() { close(simulator.exitCh); <-done }()
+ bid := &buildertypes.Bid{BlockNumber: parent.Number.Uint64() + 1, ParentHash: parent.Hash(), GasFee: big.NewInt(100), BuilderFee: common.Big0,
+ Txs: types.Transactions{types.NewTx(&types.LegacyTx{Gas: tc.gas})}}
+ feedback := make(chan error, 1)
+ simulator.newBidCh <- newBidPackage{bid: bid, feedback: feedback}
+ select {
+ case err := <-feedback:
+ if errors.Is(err, core.ErrGasLimitTooHigh) != tc.reject {
+ t.Fatalf("admission error: %v", err)
+ }
+ case <-time.After(3 * time.Second):
+ t.Fatal("no admission feedback")
+ }
+ if tc.reject {
+ if simulator.GetBestBidToRun(parent.Hash()) != previous || len(simulator.simBidCh) != 0 {
+ t.Fatal("oversized bid replaced or interrupted the previous candidate")
+ }
+ } else if simulator.GetBestBidToRun(parent.Hash()) != bid || len(simulator.simBidCh) != 1 {
+ t.Fatal("legal Amsterdam bid was not scheduled")
+ }
+ })
+ }
+}
From 0deb96184a254e9962243afe058e5f5699eea580 Mon Sep 17 00:00:00 2001
From: will-2012 <117156346+will-2012@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:53:34 +0800
Subject: [PATCH 14/14] core: fix payment lane test lint
---
core/payment_lane_test.go | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/core/payment_lane_test.go b/core/payment_lane_test.go
index 6552d0632d..96d70102bb 100644
--- a/core/payment_lane_test.go
+++ b/core/payment_lane_test.go
@@ -629,7 +629,6 @@ func TestPaymentLaneAmsterdamImport(t *testing.T) {
require.NoError(t, err)
require.Equal(t, common.Big1, new(big.Int).SetBytes(ret), "allowance must survive the fork")
}
-
}
func TestPaymentLaneAmsterdamRejectsStateOverflow(t *testing.T) {
@@ -651,7 +650,7 @@ func TestPaymentLaneAmsterdamRejectsStateOverflow(t *testing.T) {
b.AddTx(key.sign(t, signer, n, contract, common.Big0, 130_000, []byte{byte(n)}))
}
})
- require.Greater(t, blocks[2].GasUsed(), uint64(gasLimit-paymentlane.Quota(500, gasLimit)))
+ require.Greater(t, blocks[2].GasUsed(), gasLimit-paymentlane.Quota(500, gasLimit))
require.LessOrEqual(t, blocks[2].GasUsed(), uint64(gasLimit))
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), gspec, engine, DefaultConfig())
require.NoError(t, err)