ceremonyclient/node/execution/intrinsics/global/global_prover_leave_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

513 lines
18 KiB
Go

package global_test
import (
"bytes"
"encoding/binary"
"math/big"
"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/hypergraph"
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/global"
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"
thypergraph "source.quilibrium.com/quilibrium/monorepo/types/hypergraph"
"source.quilibrium.com/quilibrium/monorepo/types/mocks"
qcrypto "source.quilibrium.com/quilibrium/monorepo/types/tries"
)
func TestProverLeave_Prove(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockSigner := new(mocks.MockBLSSigner)
// Test data
filters := [][]byte{[]byte("testfilter1"), []byte("testfilter2")}
frameNumber := uint64(12345)
pubKey := make([]byte, 585) // Simulate a BLS48581G1 public key
for i := range pubKey {
pubKey[i] = byte(i % 256)
}
// Compute expected address
addressBI, err := poseidon.HashBytes(pubKey)
require.NoError(t, err)
address := addressBI.FillBytes(make([]byte, 32))
// Create expected domain for leave signature
leaveDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_LEAVE"))
leaveDomain, err := poseidon.HashBytes(leaveDomainPreimage)
require.NoError(t, err)
// Create expected message
expectedMessage := bytes.Buffer{}
// Add number of filters
numFiltersBytes := make([]byte, 4)
binary.BigEndian.PutUint32(numFiltersBytes, uint32(len(filters)))
expectedMessage.Write(numFiltersBytes)
// Add each filter
for _, filter := range filters {
filterLenBytes := make([]byte, 4)
binary.BigEndian.PutUint32(filterLenBytes, uint32(len(filter)))
expectedMessage.Write(filterLenBytes)
expectedMessage.Write(filter)
}
// Add frame number
frameBytes := make([]byte, 8)
binary.BigEndian.PutUint64(frameBytes, frameNumber)
expectedMessage.Write(frameBytes)
// Configure mock signer
mockSigner.On("Public").Return(pubKey)
mockSigner.On("SignWithDomain", expectedMessage.Bytes(), leaveDomain.Bytes()).
Return([]byte("signature"), nil)
// Configure mock key manager
mockKeyManager.On("GetSigningKey", "q-prover-key").Return(mockSigner, nil)
// Create the prover leave operation
rdfMultiprover := createMockRDFMultiprover()
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, nil, rdfMultiprover)
require.NoError(t, err)
// Call the prove function
err = proverLeave.Prove(0) // Frame number parameter is not used in the Prove method
require.NoError(t, err)
// Verify the results
assert.NotNil(t, proverLeave.PublicKeySignatureBLS48581)
assert.Equal(t, []byte("signature"), proverLeave.PublicKeySignatureBLS48581.Signature)
assert.Equal(t, address, proverLeave.PublicKeySignatureBLS48581.Address)
// Verify that all expected methods were called
mockSigner.AssertExpectations(t)
mockKeyManager.AssertExpectations(t)
}
func TestProverLeave_Verify(t *testing.T) {
t.Run("Can leave when active allocation exists", func(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockHypergraph := new(mocks.MockHypergraph)
mockHypergraph.On("GetCoveredPrefix").Return([]int{}, nil)
// Test data
filters := [][]byte{[]byte("testfilter1"), []byte("testfilter2")}
frameNumber := uint64(12345)
address := make([]byte, 32)
for i := range address {
address[i] = byte(i % 256)
}
pubkey := make([]byte, 585)
for i := range pubkey {
pubkey[i] = byte(i % 256)
}
// Setup prover tree
proverTree := &qcrypto.VectorCommitmentTree{}
proverTree.Insert([]byte{0}, pubkey, nil, big.NewInt(585))
// Setup allocation tree for first filter (active)
allocationAddressBI1, err := poseidon.HashBytes(slices.Concat([]byte("PROVER_ALLOCATION"), pubkey, filters[0]))
require.NoError(t, err)
allocationAddress1 := allocationAddressBI1.FillBytes(make([]byte, 32))
allocationFullAddress1 := [64]byte{}
copy(allocationFullAddress1[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(allocationFullAddress1[32:], allocationAddress1)
allocationTree1 := &qcrypto.VectorCommitmentTree{}
allocationTree1.Insert([]byte{1 << 2}, []byte{1}, nil, big.NewInt(1)) // status = active
// Setup allocation tree for second filter (also active)
allocationAddressBI2, err := poseidon.HashBytes(slices.Concat([]byte("PROVER_ALLOCATION"), pubkey, filters[1]))
require.NoError(t, err)
allocationAddress2 := allocationAddressBI2.FillBytes(make([]byte, 32))
allocationFullAddress2 := [64]byte{}
copy(allocationFullAddress2[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(allocationFullAddress2[32:], allocationAddress2)
allocationTree2 := &qcrypto.VectorCommitmentTree{}
allocationTree2.Insert([]byte{1 << 2}, []byte{1}, nil, big.NewInt(1)) // status = active
mockHypergraph.On("GetVertex", mock.Anything).Return(hypergraph.NewVertex([32]byte(intrinsics.GLOBAL_INTRINSIC_ADDRESS), [32]byte(address), make([]byte, 74), big.NewInt(0)), nil)
mockHypergraph.On("GetVertexData", [64]byte(slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], address))).Return(proverTree, nil)
mockHypergraph.On("GetVertexData", allocationFullAddress1).Return(allocationTree1, nil).Once()
mockHypergraph.On("GetVertexData", allocationFullAddress2).Return(allocationTree2, nil).Once()
mockHypergraph.On("GetHyperedge", [64]byte(slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], address))).Return(&mockHyperedge{}, nil)
// Create the prover leave operation
rdfMultiprover := createMockRDFMultiprover()
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, mockHypergraph, rdfMultiprover)
require.NoError(t, err)
// Set up the signature data manually
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Create expected domain for leave signature
leaveDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_LEAVE"))
leaveDomain, err := poseidon.HashBytes(leaveDomainPreimage)
require.NoError(t, err)
// Create expected message
expectedMessage := bytes.Buffer{}
// Add number of filters
numFiltersBytes := make([]byte, 4)
binary.BigEndian.PutUint32(numFiltersBytes, uint32(len(filters)))
expectedMessage.Write(numFiltersBytes)
// Add each filter
for _, filter := range filters {
filterLenBytes := make([]byte, 4)
binary.BigEndian.PutUint32(filterLenBytes, uint32(len(filter)))
expectedMessage.Write(filterLenBytes)
expectedMessage.Write(filter)
}
// Add frame number
frameBytes := make([]byte, 8)
binary.BigEndian.PutUint64(frameBytes, frameNumber)
expectedMessage.Write(frameBytes)
// Configure mock key manager
mockKeyManager.On("ValidateSignature",
crypto.KeyTypeBLS48581G1,
pubkey,
expectedMessage.Bytes(),
[]byte("signature"),
leaveDomain.Bytes(),
).Return(true, nil)
// Call the verify function
valid, err := proverLeave.Verify(frameNumber)
require.NoError(t, err)
assert.True(t, valid)
// Verify that all expected methods were called
mockKeyManager.AssertExpectations(t)
})
t.Run("Cannot leave when no active allocations", func(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockHypergraph := new(mocks.MockHypergraph)
mockHypergraph.On("GetCoveredPrefix").Return([]int{}, nil)
// Test data
filters := [][]byte{[]byte("testfilter")}
frameNumber := uint64(12345)
address := make([]byte, 32)
for i := range address {
address[i] = byte(i % 256)
}
pubkey := make([]byte, 585)
for i := range pubkey {
pubkey[i] = byte(i % 256)
}
// Setup prover tree
proverTree := &qcrypto.VectorCommitmentTree{}
proverTree.Insert([]byte{0}, pubkey, nil, big.NewInt(585))
// Setup allocation tree with joining status (0)
allocationAddressBI, err := poseidon.HashBytes(slices.Concat([]byte("PROVER_ALLOCATION"), pubkey, filters[0]))
require.NoError(t, err)
allocationAddress := allocationAddressBI.FillBytes(make([]byte, 32))
allocationFullAddress := [64]byte{}
copy(allocationFullAddress[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(allocationFullAddress[32:], allocationAddress)
allocationTree := &qcrypto.VectorCommitmentTree{}
allocationTree.Insert([]byte{1 << 2}, []byte{0}, nil, big.NewInt(1)) // status = joining
mockHypergraph.On("GetVertex", mock.Anything).Return(hypergraph.NewVertex([32]byte(intrinsics.GLOBAL_INTRINSIC_ADDRESS), [32]byte(address), make([]byte, 74), big.NewInt(0)), nil)
mockHypergraph.On("GetVertexData", [64]byte(slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], address))).Return(proverTree, nil)
mockHypergraph.On("GetVertexData", allocationFullAddress).Return(allocationTree, nil)
// Create the prover leave operation
rdfMultiprover := createMockRDFMultiprover()
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, mockHypergraph, rdfMultiprover)
require.NoError(t, err)
// Set up the signature data manually
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Call the verify function
valid, err := proverLeave.Verify(frameNumber)
require.Error(t, err)
assert.Contains(t, err.Error(), "no active allocations found")
assert.False(t, valid)
})
t.Run("Invalid signature fails", func(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockHypergraph := new(mocks.MockHypergraph)
mockHypergraph.On("GetCoveredPrefix").Return([]int{}, nil)
// Test data
filters := [][]byte{[]byte("testfilter")}
frameNumber := uint64(12345)
address := make([]byte, 32)
for i := range address {
address[i] = byte(i % 256)
}
pubkey := make([]byte, 585)
for i := range pubkey {
pubkey[i] = byte(i % 256)
}
// Setup prover tree
proverTree := &qcrypto.VectorCommitmentTree{}
proverTree.Insert([]byte{0}, pubkey, nil, big.NewInt(585))
// Setup allocation tree with active status
allocationAddressBI, err := poseidon.HashBytes(slices.Concat([]byte("PROVER_ALLOCATION"), pubkey, filters[0]))
require.NoError(t, err)
allocationAddress := allocationAddressBI.FillBytes(make([]byte, 32))
allocationFullAddress := [64]byte{}
copy(allocationFullAddress[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(allocationFullAddress[32:], allocationAddress)
allocationTree := &qcrypto.VectorCommitmentTree{}
allocationTree.Insert([]byte{1 << 2}, []byte{1}, nil, big.NewInt(1)) // status = active
mockHypergraph.On("GetVertex", mock.Anything).Return(hypergraph.NewVertex([32]byte(intrinsics.GLOBAL_INTRINSIC_ADDRESS), [32]byte(address), make([]byte, 74), big.NewInt(0)), nil)
mockHypergraph.On("GetVertexData", [64]byte(slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], address))).Return(proverTree, nil)
mockHypergraph.On("GetVertexData", allocationFullAddress).Return(allocationTree, nil)
// Create expected domain for leave signature
leaveDomainPreimage := slices.Concat(intrinsics.GLOBAL_INTRINSIC_ADDRESS[:], []byte("PROVER_LEAVE"))
leaveDomain, err := poseidon.HashBytes(leaveDomainPreimage)
require.NoError(t, err)
// Create expected message
expectedMessage := bytes.Buffer{}
// Add number of filters
numFiltersBytes := make([]byte, 4)
binary.BigEndian.PutUint32(numFiltersBytes, uint32(len(filters)))
expectedMessage.Write(numFiltersBytes)
// Add each filter
for _, filter := range filters {
filterLenBytes := make([]byte, 4)
binary.BigEndian.PutUint32(filterLenBytes, uint32(len(filter)))
expectedMessage.Write(filterLenBytes)
expectedMessage.Write(filter)
}
// Add frame number
frameBytes := make([]byte, 8)
binary.BigEndian.PutUint64(frameBytes, frameNumber)
expectedMessage.Write(frameBytes)
// Configure mock key manager to return false
mockKeyManager.On("ValidateSignature",
crypto.KeyTypeBLS48581G1,
pubkey,
expectedMessage.Bytes(),
[]byte("signature"),
leaveDomain.Bytes(),
).Return(false, nil)
// Create the prover leave operation
rdfMultiprover := createMockRDFMultiprover()
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, mockHypergraph, rdfMultiprover)
require.NoError(t, err)
// Set up the signature data manually
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Call the verify function
valid, err := proverLeave.Verify(frameNumber)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid signature")
assert.False(t, valid)
})
}
func TestProverLeave_Materialize(t *testing.T) {
t.Run("Materialize leave - updates allocation status to leaving", func(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockHypergraph := new(mocks.MockHypergraph)
mockHypergraph.On("GetCoveredPrefix").Return([]int{}, nil)
hypergraphState := hgstate.NewHypergraphState(mockHypergraph)
// Test data
filters := [][]byte{[]byte("testfilter")}
frameNumber := uint64(253000)
address := make([]byte, 32)
for i := range address {
address[i] = byte(i % 256)
}
pubkey := make([]byte, 585)
for i := range pubkey {
pubkey[i] = byte(i % 256)
}
// Create full addresses
proverFullAddress := [64]byte{}
copy(proverFullAddress[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(proverFullAddress[32:], address)
// Setup existing prover tree
proverTree := &qcrypto.VectorCommitmentTree{}
proverTree.Insert([]byte{0}, pubkey, nil, big.NewInt(585))
// Calculate allocation address
allocationAddressBI, err := poseidon.HashBytes(slices.Concat([]byte("PROVER_ALLOCATION"), pubkey, filters[0]))
require.NoError(t, err)
allocationAddress := allocationAddressBI.FillBytes(make([]byte, 32))
allocationFullAddress := [64]byte{}
copy(allocationFullAddress[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(allocationFullAddress[32:], allocationAddress)
// Setup existing allocation tree with active status
allocationTree := &qcrypto.VectorCommitmentTree{}
allocationTree.Insert([]byte{1 << 2}, []byte{1}, nil, big.NewInt(1)) // status = active
// Create existing vertices
proverVertex := hypergraph.NewVertex(
[32]byte(proverFullAddress[:32]),
[32]byte(proverFullAddress[32:]),
make([]byte, 74),
big.NewInt(7),
)
allocationVertex := hypergraph.NewVertex(
[32]byte(allocationFullAddress[:32]),
[32]byte(allocationFullAddress[32:]),
make([]byte, 74),
big.NewInt(7),
)
// Configure mock to return existing vertices and trees
mockHypergraph.On("GetVertex", proverFullAddress).Return(proverVertex, nil)
mockHypergraph.On("GetVertexData", proverFullAddress).Return(proverTree, nil)
mockHypergraph.On("GetVertex", allocationFullAddress).Return(allocationVertex, nil)
mockHypergraph.On("GetVertexData", allocationFullAddress).Return(allocationTree, nil)
mockHypergraph.On("GetHyperedge", proverFullAddress).Return(&mockHyperedge{}, nil)
// Expect the allocation tree to be updated with leaving status and leave frame
mockHypergraph.On("AddVertex",
mock.MatchedBy(func(v thypergraph.Vertex) bool {
id := v.GetID()
return bytes.Equal(id[:], allocationFullAddress[:])
}),
mock.MatchedBy(func(tree *qcrypto.VectorCommitmentTree) bool {
// Check that status was updated to leaving (3)
statusBytes, _ := tree.Get([]byte{1 << 2}) // status (order 1)
if len(statusBytes) != 1 || statusBytes[0] != 3 {
return false
}
// Check that leave frame was stored
frameBytes, _ := tree.Get([]byte{5 << 2}) // leave frame number (order 5)
if len(frameBytes) != 8 {
return false
}
storedFrame := binary.BigEndian.Uint64(frameBytes)
return storedFrame == frameNumber
}),
).Return(nil)
// Create the prover leave operation
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, nil, createMockRDFMultiprover())
require.NoError(t, err)
// Set up the signature data
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Call Materialize
newState, err := proverLeave.Materialize(frameNumber, hypergraphState)
require.NoError(t, err)
assert.NotNil(t, newState)
assert.Equal(t, hypergraphState, newState)
})
t.Run("Materialize prover not found - returns error", func(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
mockHypergraph := new(mocks.MockHypergraph)
mockHypergraph.On("GetCoveredPrefix").Return([]int{}, nil)
hypergraphState := hgstate.NewHypergraphState(mockHypergraph)
// Test data
filters := [][]byte{[]byte("testfilter")}
frameNumber := uint64(253000)
address := make([]byte, 32)
for i := range address {
address[i] = byte(i % 256)
}
// Create full address
fullAddress := [64]byte{}
copy(fullAddress[:32], intrinsics.GLOBAL_INTRINSIC_ADDRESS[:])
copy(fullAddress[32:], address)
// Configure mock to return nil vertex (prover not found)
mockHypergraph.On("GetVertex", fullAddress).Return(nil, assert.AnError)
mockHypergraph.On("GetVertexData", fullAddress).Return(nil, assert.AnError)
// Create the prover leave operation
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, nil, createMockRDFMultiprover())
require.NoError(t, err)
// Set up the signature data
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Call Materialize
newState, err := proverLeave.Materialize(frameNumber, hypergraphState)
require.Error(t, err)
assert.Contains(t, err.Error(), "prover not found")
assert.Nil(t, newState)
})
}
func TestProverLeave_GetCost(t *testing.T) {
// Setup
mockKeyManager := new(mocks.MockKeyManager)
// Test data
filters := [][]byte{[]byte("testfilter")}
frameNumber := uint64(12345)
address := make([]byte, 32) // Simulate an address derived from the public key
for i := range address {
address[i] = byte(i % 256)
}
// Create the prover leave operation
rdfMultiprover := createMockRDFMultiprover()
proverLeave, err := global.NewProverLeave(filters, frameNumber, mockKeyManager, nil, rdfMultiprover)
require.NoError(t, err)
// Set up the signature data manually
proverLeave.PublicKeySignatureBLS48581 = global.BLS48581AddressedSignature{
Signature: []byte("signature"),
Address: address,
}
// Call the GetCost function
cost, err := proverLeave.GetCost()
require.NoError(t, err)
assert.Equal(t, int64(0), cost.Int64())
}