mirror of
https://github.com/luxfi/chains.git
synced 2026-09-24 00:26:00 +00:00
Completes the "one and only one way" wire migration: every VM in this repo now
serializes blocks and txs through luxfi/zap struct-is-wire at fixed field
offsets — no encoding/json, no hand-rolled big-endian, no cursor codec.
- aivm: Block + AIVertex + nested CIntent native-encoded. Fixes a latent
data-loss bug — AIVertex had unexported fields, so encoding/json silently
marshalled it as {}; the native encoder round-trips every field.
- keyvm: Transaction (signing preimage = SigningBytes(), excludes Auth/Sig;
full wire = signing || sig object, so the signed bytes stay a genuine prefix
and authenticate() re-derives a byte-identical preimage) + Block.
- quantumvm: Block + BaseTransaction. Block wire is the quantum-signature
preimage (deterministic fixed offsets, ordered tx list).
- dexvm: block envelope (txs were already ZAP). Preserves UnixNano timestamps,
deterministic tx order, and content-addressed carried fills.
Hardening from the adversarial crypto review (must-holds all verified: no
keyvm signature malleability/replay, no aivm CIntent id-forgery/escrow tamper,
no dexvm ordering/fill tamper, all parsers fail closed):
- keyvm (M1): make the wire strictly canonical. zap follows the root offset
and ignores unreferenced padding inside a message's declared size, so a twin
buffer could decode to identical fields with a different hash (id-malleability).
ParseTransaction now binds the id to the re-serialized (canonical) form and
rejects any non-canonical input at the door — exactly one byte-string
authenticates per logical tx. Regression test: the padded twin is rejected.
- quantumvm (M2): derive block + tx ids as sha256(Bytes()), matching the other
three VMs. The prior ids.ToID(parent||height) over 40 bytes (and ids.ToID over
the >=32-byte tx wire) silently yielded ids.Empty for every block/tx,
collapsing the block store and tx pool to a single slot. The block id is no
longer stored in the wire, so the invariant id == sha256(Bytes()) holds
unconditionally. Regression tests: ids are non-Empty and collision-distinct.
Not in scope (tracked separately, fail-closed today): aivm RewardPerOperator is
escrowed but not bound by ComputeIntentID — a pre-existing design gap that needs
a coordinated A-side + C-side intent-id preimage bump before any non-fail-closed
CCommitVerifier ships.
Builds + tests green under the canonical CGO=0 -mod=readonly recipe; go.mod/go.sum
are the go mod tidy canonical set (indirect deps resolved forward, all within
major). Cryptographer verdict: SAFE TO PUBLISH.
Co-authored-by: Hanzo Dev <[email protected]>
176 lines
4.1 KiB
Go
176 lines
4.1 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package quantumvm
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/luxfi/chains/quantumvm/quantum"
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
var (
|
|
errBlockVerificationFailed = errors.New("block verification failed")
|
|
errInvalidBlockHeight = errors.New("invalid block height")
|
|
errInvalidParentID = errors.New("invalid parent ID")
|
|
)
|
|
|
|
// Block represents a QVM block with quantum features
|
|
type Block struct {
|
|
id ids.ID
|
|
timestamp time.Time
|
|
height uint64
|
|
parentID ids.ID
|
|
transactions []Transaction
|
|
quantumSignature *quantum.QuantumSignature
|
|
vm *VM
|
|
bytes []byte
|
|
}
|
|
|
|
// ID returns the block ID
|
|
func (b *Block) ID() ids.ID {
|
|
return b.id
|
|
}
|
|
|
|
// Accept marks the block as accepted
|
|
func (b *Block) Accept(context.Context) error {
|
|
b.vm.lock.Lock()
|
|
defer b.vm.lock.Unlock()
|
|
|
|
// Store block in database
|
|
if err := b.vm.state.Put(b.id[:], b.Bytes()); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Update last accepted
|
|
if err := b.vm.state.Put([]byte("lastAccepted"), b.id[:]); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Update height
|
|
heightBytes := make([]byte, 8)
|
|
binary.BigEndian.PutUint64(heightBytes, b.height)
|
|
if err := b.vm.state.Put([]byte("height"), heightBytes); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Process transactions
|
|
for _, tx := range b.transactions {
|
|
if err := b.vm.txPool.RemoveTransaction(tx.ID()); err != nil {
|
|
b.vm.log.Error("failed to remove tx from pool", "txID", tx.ID(), "error", err)
|
|
}
|
|
}
|
|
|
|
b.vm.log.Info("block accepted",
|
|
"blockID", b.id,
|
|
"height", b.height,
|
|
"txCount", len(b.transactions),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Reject marks the block as rejected
|
|
func (b *Block) Reject(context.Context) error {
|
|
b.vm.lock.Lock()
|
|
defer b.vm.lock.Unlock()
|
|
|
|
// Return transactions to pool
|
|
for _, tx := range b.transactions {
|
|
if err := b.vm.txPool.AddTransaction(tx); err != nil {
|
|
b.vm.log.Error("failed to return tx to pool", "txID", tx.ID(), "error", err)
|
|
}
|
|
}
|
|
|
|
b.vm.log.Debug("block rejected", "blockID", b.id, "height", b.height)
|
|
return nil
|
|
}
|
|
|
|
// Verify verifies the block validity
|
|
func (b *Block) Verify(ctx context.Context) error {
|
|
b.vm.lock.RLock()
|
|
defer b.vm.lock.RUnlock()
|
|
|
|
// Verify height
|
|
if b.height == 0 {
|
|
return errInvalidBlockHeight
|
|
}
|
|
|
|
// Verify parent exists (except for genesis)
|
|
if b.height > 1 {
|
|
if _, err := b.vm.GetBlock(ctx, b.parentID); err != nil {
|
|
return errInvalidParentID
|
|
}
|
|
}
|
|
|
|
// Verify quantum signature if enabled
|
|
if b.vm.Config.QuantumStampEnabled {
|
|
if b.quantumSignature == nil {
|
|
return errInvalidQuantumStamp
|
|
}
|
|
if err := b.vm.quantumSigner.Verify(b.Bytes(), b.quantumSignature); err != nil {
|
|
return errBlockVerificationFailed
|
|
}
|
|
}
|
|
|
|
// Verify transactions in parallel
|
|
if len(b.transactions) > 0 {
|
|
msgs := make([][]byte, len(b.transactions))
|
|
sigs := make([]*quantum.QuantumSignature, len(b.transactions))
|
|
|
|
for i, tx := range b.transactions {
|
|
msgs[i] = tx.Bytes()
|
|
sigs[i] = tx.GetQuantumSignature()
|
|
}
|
|
|
|
if b.vm.Config.CoronaEnabled {
|
|
if err := b.vm.quantumSigner.ParallelVerify(msgs, sigs); err != nil {
|
|
return errBlockVerificationFailed
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Parent returns the parent block ID
|
|
func (b *Block) Parent() ids.ID {
|
|
return b.parentID
|
|
}
|
|
|
|
// ParentID returns the parent block ID (implements consensus Block interface)
|
|
func (b *Block) ParentID() ids.ID {
|
|
return b.parentID
|
|
}
|
|
|
|
// Height returns the block height
|
|
func (b *Block) Height() uint64 {
|
|
return b.height
|
|
}
|
|
|
|
// Timestamp returns the block timestamp (implements block.Block interface)
|
|
func (b *Block) Timestamp() time.Time {
|
|
return b.timestamp
|
|
}
|
|
|
|
// TimestampUnix returns the block timestamp as Unix seconds.
|
|
func (b *Block) TimestampUnix() int64 {
|
|
return b.timestamp.Unix()
|
|
}
|
|
|
|
// Status returns the block status. Q-Chain does not yet track per-block
|
|
// status separately (all blocks reaching here are processing or accepted),
|
|
// so we return 0 (Unknown) until a status field is added.
|
|
func (b *Block) Status() uint8 {
|
|
return 0
|
|
}
|
|
|
|
// String returns a string representation of the block
|
|
func (b *Block) String() string {
|
|
return b.id.String()
|
|
}
|