ceremonyclient/node/execution/intrinsics/token/token_intrinsic_transaction_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

148 lines
6.4 KiB
Go

package token
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"math/big"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"source.quilibrium.com/quilibrium/monorepo/types/crypto"
"source.quilibrium.com/quilibrium/monorepo/types/keys"
"source.quilibrium.com/quilibrium/monorepo/types/mocks"
"source.quilibrium.com/quilibrium/monorepo/types/schema"
qcrypto "source.quilibrium.com/quilibrium/monorepo/types/tries"
)
func TestValidTransactionWithMocks(t *testing.T) {
dc := &mocks.MockDecafConstructor{}
vk, _ := dc.New()
sk, _ := dc.New()
out1, err := NewTransactionOutput(big.NewInt(7), vk.Public(), sk.Public())
if err != nil {
t.Fatal(err)
}
out2, err := NewTransactionOutput(big.NewInt(3), vk.Public(), sk.Public())
if err != nil {
t.Fatal(err)
}
total := new(big.Int).Add(out1.value, out2.value)
total.Add(total, big.NewInt(2))
hg := &mocks.MockHypergraph{}
bp := &mocks.MockBulletproofProver{}
ip := &mocks.MockInclusionProver{}
ve := &mocks.MockVerifiableEncryptor{}
km := &mocks.MockKeyRing{}
bp.On("GenerateRangeProofFromBig", mock.Anything, mock.Anything, mock.Anything).Return(
crypto.RangeProofResult{
Proof: []byte("valid-proof"),
Commitment: []byte("valid-commitment" + string(bytes.Repeat([]byte{0x00}, 56*2-16))),
Blinding: []byte("valid-blinding" + string(bytes.Repeat([]byte{0x00}, 56*2-14))),
},
nil,
)
bp.On("VerifyRangeProof", mock.Anything, mock.Anything, mock.Anything).Return(true)
bp.On("SumCheck", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true)
km.On("GetRawKey", "q-verenc-key").Return(&keys.Key{
Id: "q-verenc-key",
Type: crypto.KeyTypeEd448,
PrivateKey: []byte("valid-user-specific-verifiable-encryption-key"),
PublicKey: []byte("valid-user-specific-verifiable-encryption-pubkey"),
}, nil)
inputViewKey, _ := dc.New()
inputSpendKey, _ := dc.New()
km.On("GetAgreementKey", "q-view-key", mock.Anything, mock.Anything).Return(inputViewKey, nil)
km.On("GetAgreementKey", "q-spend-key", mock.Anything, mock.Anything).Return(inputSpendKey, nil)
address := [64]byte{}
copy(address[:32], QUIL_TOKEN_ADDRESS)
rand.Read(address[32:])
// Used only for non-deletion check
hg.On("GetVertex", address).Return(nil, nil)
hg.On("GetVertex", mock.MatchedBy(func(addr [64]byte) bool { return !bytes.Equal(addr[:], address[:]) })).Return(nil, errors.New("not found"))
bp.On("SignHidden", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(bytes.Repeat([]byte("valid-commitment"+string(bytes.Repeat([]byte{0x00}, 56-16))), 6))
ip.On("ProveRaw", mock.Anything, mock.Anything, mock.Anything).Return([]byte("valid-proof"+string(bytes.Repeat([]byte{0x00}, 74-11))), nil)
ip.On("CommitRaw", mock.Anything, mock.Anything).Return([]byte("valid-commit"+string(bytes.Repeat([]byte{0x00}, 74-12))), nil)
mp := &mocks.MockMultiproof{}
ip.On("NewMultiproof").Return(mp)
mp.On("ToBytes").Return([]byte("multiproof"), nil)
mp.On("GetMulticommitment").Return([]byte("multicommitment"))
mp.On("GetProof").Return([]byte("proof"))
mp.On("FromBytes", mock.Anything).Return(nil)
ip.On("ProveMultiple", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mp, nil)
ip.On("VerifyMultiple", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil)
hg.On("GetShardCommits", mock.Anything, mock.Anything).Return([][]byte{make([]byte, 64), make([]byte, 64), make([]byte, 64), make([]byte, 64)}, nil)
bp.On("GenerateInputCommitmentsFromBig", mock.Anything, mock.Anything).Return([]byte("input-commit" + string(bytes.Repeat([]byte{0x00}, 56-12))))
hg.On("CreateTraversalProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&qcrypto.TraversalProof{
Multiproof: &mocks.MockMultiproof{},
SubProofs: []qcrypto.TraversalSubProof{{
Commits: [][]byte{[]byte("valid-hg-commit" + string(bytes.Repeat([]byte{0x00}, 74-15)))},
Ys: [][]byte{{0x00}},
Paths: [][]uint64{{0}},
}},
}, nil)
hg.On("VerifyTraversalProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil)
hg.On("GetProver").Return(ip)
ip.On("VerifyRaw", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil)
bp.On("VerifyHidden", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true)
tree := &qcrypto.VectorCommitmentTree{}
tree.Insert([]byte{0}, binary.BigEndian.AppendUint64(nil, FRAME_2_1_EXTENDED_ENROLL_CONFIRM_END+1), nil, big.NewInt(55))
tree.Insert([]byte{1 << 2}, []byte("valid-commitment"+string(bytes.Repeat([]byte{0x00}, 56-16))), nil, big.NewInt(56))
tree.Insert([]byte{2 << 2}, []byte("one-time-key"+string(bytes.Repeat([]byte{0x00}, 56-12))), nil, big.NewInt(56))
tree.Insert([]byte{3 << 2}, []byte("verification-key"+string(bytes.Repeat([]byte{0x00}, 56-16))), nil, big.NewInt(56))
tree.Insert([]byte{4 << 2}, []byte("coin-balance-enc"+string(bytes.Repeat([]byte{0x00}, 56-16))), nil, big.NewInt(56))
tree.Insert([]byte{5 << 2}, []byte("mask-enc1"+string(bytes.Repeat([]byte{0x00}, 56-9))), nil, big.NewInt(56))
tree.Insert([]byte{6 << 2}, []byte("mask-enc2"+string(bytes.Repeat([]byte{0x00}, 56-9))), nil, big.NewInt(56))
typeAddr, _ := hex.DecodeString("096de9a09f693f92cfa9cf3349bab2b3baee09f3e4f9c596514ecb3e8b0dff8f")
tree.Insert(bytes.Repeat([]byte{0xff}, 32), typeAddr, nil, big.NewInt(32))
hg.On("GetVertexData", address).Return(tree, nil)
// Create RDF multiprover for testing
rdfSchema := getTestRDFSchema()
parser := &schema.TurtleRDFParser{}
rdfMultiprover := schema.NewRDFMultiprover(parser, ip)
// simulate input as commitment to total
input, _ := NewTransactionInput(address[:])
tx := NewTransaction(
[32]byte(QUIL_TOKEN_ADDRESS),
[]*TransactionInput{input},
[]*TransactionOutput{out1, out2},
[]*big.Int{big.NewInt(1), big.NewInt(1)},
&TokenIntrinsicConfiguration{
Behavior: Mintable | Burnable | Divisible | Tenderable,
MintStrategy: &TokenMintStrategy{
MintBehavior: MintWithProof,
ProofBasis: ProofOfMeaningfulWork,
},
Units: big.NewInt(8000000000),
Name: "QUIL",
Symbol: "QUIL",
},
hg,
bp,
ip,
ve,
dc,
km,
rdfSchema,
rdfMultiprover,
)
if err := tx.Prove(FRAME_2_1_EXTENDED_ENROLL_CONFIRM_END + 1); err != nil {
t.Fatal(err)
}
if valid, err := tx.Verify(FRAME_2_1_EXTENDED_ENROLL_CONFIRM_END + 2); !valid {
t.Fatal("Expected transaction to verify but it failed", err)
}
}