ceremonyclient/node/execution/intrinsics/global/global_prover_update_test.go
Cassandra Heart 53f7c2b5c9
v2.1.0.2 (#442)
* v2.1.0.2

* restore tweaks to simlibp2p

* fix: nil ref on size calc

* fix: panic should induce shutdown from event_distributor

* fix: friendlier initialization that requires less manual kickstarting for test/devnets

* fix: fewer available shards than provers should choose shard length

* fix: update stored worker registry, improve logging for debug mode

* fix: shut the fuck up, peer log

* qol: log value should be snake cased

* fix:non-archive snap sync issues

* fix: separate X448/Decaf448 signed keys, add onion key to registry

* fix: overflow arithmetic on frame number comparison

* fix: worker registration should be idempotent if inputs are same, otherwise permit updated records

* fix: remove global prover state from size calculation

* fix: divide by zero case

* fix: eager prover

* fix: broadcast listener default

* qol: diagnostic data for peer authenticator

* fix: master/worker connectivity issue in sparse networks

tight coupling of peer and workers can sometimes interfere if mesh is sparse, so give workers a pseudoidentity but publish messages with the proper peer key

* fix: reorder steps of join creation

* fix: join verify frame source + ensure domain is properly padded (unnecessary but good for consistency)

* fix: add delegate to protobuf <-> reified join conversion

* fix: preempt prover from planning with no workers

* fix: use the unallocated workers to generate a proof

* qol: underflow causes join fail in first ten frames on test/devnets

* qol: small logging tweaks for easier log correlation in debug mode

* qol: use fisher-yates shuffle to ensure prover allocations are evenly distributed when scores are equal

* qol: separate decisional logic on post-enrollment confirmation into consensus engine, proposer, and worker manager where relevant, refactor out scoring

* reuse shard descriptors for both join planning and confirm/reject decisions

* fix: add missing interface method and amend test blossomsub to use new peer id basis

* fix: only check allocations if they exist

* fix: pomw mint proof data needs to be hierarchically under global intrinsic domain

* staging temporary state under diagnostics

* fix: first phase of distributed lock refactoring

* fix: compute intrinsic locking

* fix: hypergraph intrinsic locking

* fix: token intrinsic locking

* fix: update execution engines to support new locking model

* fix: adjust tests with new execution shape

* fix: weave in lock/unlock semantics to liveness provider

* fix lock fallthrough, add missing allocation update

* qol: additional logging for diagnostics, also testnet/devnet handling for confirmations

* fix: establish grace period on halt scenario to permit recovery

* fix: support test/devnet defaults for coverage scenarios

* fix: nil ref on consensus halts for non-archive nodes

* fix: remove unnecessary prefix from prover ref

* add test coverage for fork choice behaviors and replay – once passing, blocker (2) is resolved

* fix: no fork replay on repeat for non-archive nodes, snap now behaves correctly

* rollup of pre-liveness check lock interactions

* ahead of tests, get the protobuf/metrics-related changes out so teams can prepare

* add test coverage for distributed lock behaviors – once passing, blocker (3) is resolved

* fix: blocker (3)

* Dev docs improvements (#445)

* Make install deps script more robust

* Improve testing instructions

* Worker node should stop upon OS SIGINT/SIGTERM signal (#447)

* move pebble close to Stop()

* move deferred Stop() to Start()

* add core id to worker stop log message

* create done os signal channel and stop worker upon message to it

---------

Co-authored-by: Cassandra Heart <7929478+CassOnMars@users.noreply.github.com>

---------

Co-authored-by: Daz <daz_the_corgi@proton.me>
Co-authored-by: Black Swan <3999712+blacks1ne@users.noreply.github.com>
2025-10-23 01:03:06 -05:00

310 lines
9.6 KiB
Go

package global_test
import (
"bytes"
"crypto/rand"
"encoding/binary"
"slices"
"testing"
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/global"
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/token"
hgstate "source.quilibrium.com/quilibrium/monorepo/node/execution/state/hypergraph"
"source.quilibrium.com/quilibrium/monorepo/types/crypto"
"source.quilibrium.com/quilibrium/monorepo/types/execution/intrinsics"
"source.quilibrium.com/quilibrium/monorepo/types/mocks"
qcrypto "source.quilibrium.com/quilibrium/monorepo/types/tries"
)
func TestProverUpdate_Prove(t *testing.T) {
mockKM := new(mocks.MockKeyManager)
mockSigner := new(mocks.MockBLSSigner)
mockHG := new(mocks.MockHypergraph)
mockHG.On("GetCoveredPrefix").Return([]int{}, nil)
delegate := make([]byte, 32)
for i := range delegate {
delegate[i] = byte(0xAB)
}
// Fake BLS48-581 G1 pubkey bytes
pubKey := make([]byte, 585)
for i := range pubKey {
pubKey[i] = byte(i % 251)
}
// Expected domain = H(GLOBAL || "PROVER_UPDATE")
updateDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_UPDATE"))
updateDomain, err := poseidon.HashBytes(updateDomainPreimage)
require.NoError(t, err)
// Signer behavior
mockSigner.On("Public").Return(pubKey)
mockSigner.On("SignWithDomain", delegate, updateDomain.Bytes()).
Return([]byte("upd_sig"), nil)
// KM behavior
mockKM.On("GetSigningKey", "q-prover-key").Return(mockSigner, nil)
mockHG.On("GetProver").Return(nil) // unused by Prove
op := global.NewProverUpdate(delegate, nil, mockHG, &mocks.MockBLSSigner{}, createMockRDFMultiprover(), mockKM)
require.NoError(t, op.Prove(0))
require.NotNil(t, op.PublicKeySignatureBLS48581)
assert.Equal(t, []byte("upd_sig"), op.PublicKeySignatureBLS48581.Signature)
// Address must be poseidon(pubKey)
addrBI, _ := poseidon.HashBytes(pubKey)
expectedAddr := addrBI.FillBytes(make([]byte, 32))
assert.Equal(t, expectedAddr, op.PublicKeySignatureBLS48581.Address)
mockSigner.AssertExpectations(t)
mockKM.AssertExpectations(t)
}
func TestProverUpdate_Verify_Succeeds(t *testing.T) {
mockKM := new(mocks.MockKeyManager)
mockHG := new(mocks.MockHypergraph)
mockHG.On("GetCoveredPrefix").Return([]int{}, nil).Maybe()
// Setup pubkey and its address
pubKey := make([]byte, 585)
for i := range pubKey {
pubKey[i] = byte(i % 251)
}
addrBI, _ := poseidon.HashBytes(pubKey)
addr := addrBI.FillBytes(make([]byte, 32))
// Prover vertex data includes PublicKey (order 0 in our RDF schema mapping)
proverTree := &qcrypto.VectorCommitmentTree{}
// Using RDF helper to set PublicKey into the tree
rdf := createMockRDFMultiprover()
require.NoError(t, rdf.Set(
global.GLOBAL_RDF_SCHEMA, intrinsics.GLOBAL_INTRINSIC_ADDRESS[:],
"prover:Prover", "PublicKey", pubKey, proverTree,
))
// hypergraph.GetVertexData(GLOBAL||addr) -> proverTree
full := [64]byte{}
copy(full[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(full[32:], addr)
mockHG.On("GetVertexData", full).Return(proverTree, nil)
delegate := make([]byte, 32)
for i := range delegate {
delegate[i] = byte(0xCD)
}
// Domain for update
updateDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_UPDATE"))
updateDomain, _ := poseidon.HashBytes(updateDomainPreimage)
// Signature validates
mockKM.On("ValidateSignature",
crypto.KeyTypeBLS48581G1,
pubKey,
delegate,
[]byte("sig"),
updateDomain.Bytes(),
).Return(true, nil)
op := global.NewProverUpdate(delegate, &global.BLS48581AddressedSignature{
Signature: []byte("sig"),
Address: addr,
}, mockHG, &mocks.MockBLSSigner{}, rdf, mockKM)
ok, err := op.Verify(0)
require.NoError(t, err)
assert.True(t, ok)
mockKM.AssertExpectations(t)
mockHG.AssertExpectations(t)
}
func TestProverUpdate_Verify_FailsOnBadSignature(t *testing.T) {
mockKM := new(mocks.MockKeyManager)
mockHG := new(mocks.MockHypergraph)
mockHG.On("GetCoveredPrefix").Return([]int{}, nil)
// Prover tree with pubkey
pubKey := make([]byte, 585)
for i := range pubKey {
pubKey[i] = byte(i % 251)
}
addrBI, _ := poseidon.HashBytes(pubKey)
addr := addrBI.FillBytes(make([]byte, 32))
rdf := createMockRDFMultiprover()
tree := &qcrypto.VectorCommitmentTree{}
require.NoError(t, rdf.Set(
global.GLOBAL_RDF_SCHEMA, intrinsics.GLOBAL_INTRINSIC_ADDRESS[:],
"prover:Prover", "PublicKey", pubKey, tree,
))
full := [64]byte{}
copy(full[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(full[32:], addr)
mockHG.On("GetVertexData", full).Return(tree, nil)
delegate := make([]byte, 32)
for i := range delegate {
delegate[i] = byte(0xEE)
}
updateDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_UPDATE"))
updateDomain, _ := poseidon.HashBytes(updateDomainPreimage)
mockKM.On("ValidateSignature",
crypto.KeyTypeBLS48581G1,
pubKey,
delegate,
[]byte("bad"),
updateDomain.Bytes(),
).Return(false, nil)
op := global.NewProverUpdate(delegate, &global.BLS48581AddressedSignature{
Signature: []byte("bad"),
Address: addr,
}, mockHG, nil, rdf, mockKM)
ok, err := op.Verify(0)
require.Error(t, err)
assert.False(t, ok)
}
func TestProverUpdate_Verify_FailsOnAddressMismatch(t *testing.T) {
mockKM := new(mocks.MockKeyManager)
mockHG := new(mocks.MockHypergraph)
mockHG.On("GetCoveredPrefix").Return([]int{}, nil)
// Prover tree with pubkey
pubKey := make([]byte, 585)
rand.Read(pubKey)
// Address in signature is WRONG
wrongAddr := make([]byte, 32)
for i := range wrongAddr {
wrongAddr[i] = 0xFF
}
rdf := createMockRDFMultiprover()
tree := &qcrypto.VectorCommitmentTree{}
require.NoError(t, rdf.Set(
global.GLOBAL_RDF_SCHEMA, intrinsics.GLOBAL_INTRINSIC_ADDRESS[:],
"prover:Prover", "PublicKey", pubKey, tree,
))
full := [64]byte{}
copy(full[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(full[32:], wrongAddr) // lookup by wrong address
mockHG.On("GetVertexData", full).Return(tree, nil)
delegate := make([]byte, 32)
for i := range delegate {
delegate[i] = 0xAA
}
// We don't even need to reach signature check; address mismatch should fail.
op := global.NewProverUpdate(delegate, &global.BLS48581AddressedSignature{
Signature: []byte("sig"),
Address: wrongAddr,
}, mockHG, nil, rdf, mockKM)
ok, err := op.Verify(0)
require.Error(t, err)
assert.False(t, ok)
}
func TestProverUpdate_Materialize_PreservesBalance(t *testing.T) {
mockKM := new(mocks.MockKeyManager)
mockHG := new(mocks.MockHypergraph)
mockHG.On("GetCoveredPrefix").Return([]int{}, nil)
hypergraphState := hgstate.NewHypergraphState(mockHG)
// Prover exists with PublicKey
pubKey := make([]byte, 585)
for i := range pubKey {
pubKey[i] = byte(i % 251)
}
addrBI, _ := poseidon.HashBytes(pubKey)
addr := addrBI.FillBytes(make([]byte, 32))
rdf := createMockRDFMultiprover()
proverTree := &qcrypto.VectorCommitmentTree{}
require.NoError(t, rdf.Set(
global.GLOBAL_RDF_SCHEMA, intrinsics.GLOBAL_INTRINSIC_ADDRESS[:],
"prover:Prover", "PublicKey", pubKey, proverTree,
))
// Existing reward vertex with non-zero balance
rewardPrior := &qcrypto.VectorCommitmentTree{}
nonZero := make([]byte, 32)
binary.BigEndian.PutUint64(nonZero[24:], 12345)
require.NoError(t, rdf.Set(
global.GLOBAL_RDF_SCHEMA, intrinsics.GLOBAL_INTRINSIC_ADDRESS[:],
"reward:ProverReward", "Balance", nonZero, rewardPrior,
))
fullProver := [64]byte{}
fullReward := [64]byte{}
rewardAddr, err := poseidon.HashBytes(slices.Concat(token.QUIL_TOKEN_ADDRESS[:], addr))
copy(fullProver[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(fullReward[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(fullProver[32:], addr)
copy(fullReward[32:], rewardAddr.FillBytes(make([]byte, 32)))
mockHG.On("GetVertex", fullProver).Return(nil, nil)
mockHG.On("GetVertexData", fullProver).Return(proverTree, nil)
mockHG.On("GetVertex", fullReward).Return(nil, nil)
mockHG.On("GetVertexData", fullReward).Return(rewardPrior, nil)
// Hypergraph lookups
mockHG.On("Get", intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], rewardAddr.FillBytes(make([]byte, 32)), hgstate.VertexAddsDiscriminator).Return(rewardPrior, nil)
mockHG.On("Get", intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], addr, hgstate.HyperedgeAddsDiscriminator).Return(nil, assert.AnError)
delegate := make([]byte, 32)
for i := range delegate {
delegate[i] = 0x42
}
// Expect Set of reward vertex with preserved balance
mockHG.On("AddVertex", mock.Anything, mock.Anything).Return(nil)
mockHG.
On("SetVertexData",
mock.Anything,
mock.MatchedBy(func(id [64]byte) bool {
return bytes.Equal(id[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:]) &&
bytes.Equal(id[32:], rewardAddr.FillBytes(make([]byte, 32)))
}),
mock.MatchedBy(func(tree *qcrypto.VectorCommitmentTree) bool {
d, err := rdf.Get(global.GLOBAL_RDF_SCHEMA, "reward:ProverReward", "DelegateAddress", tree)
if err != nil || len(d) == 0 || !bytes.Equal(d, delegate) {
return false
}
b, err := rdf.Get(global.GLOBAL_RDF_SCHEMA, "reward:ProverReward", "Balance", tree)
return err == nil && len(b) > 0 && bytes.Equal(b, make([]byte, 32))
}),
).
Return(nil)
op := global.NewProverUpdate(delegate, &global.BLS48581AddressedSignature{
Signature: []byte("s"),
Address: addr,
}, mockHG, nil, rdf, mockKM)
newState, err := op.Materialize(99, hypergraphState)
require.NoError(t, err)
require.NotNil(t, newState)
}
func TestProverUpdate_GetCost(t *testing.T) {
op := global.NewProverUpdate(make([]byte, 32), nil, nil, nil, nil, nil)
cost, err := op.GetCost()
require.NoError(t, err)
assert.Equal(t, int64(0), cost.Int64())
}