mirror of
https://github.com/luxfi/node.git
synced 2026-07-27 03:39:39 +00:00
Pure zap tx: kind dispatch + AdvanceTimeTx template (struct IS the wire)
kind.go: 1-byte discriminator @ object offset 0 = the whole dispatch. No codec, no version, no slot map. AdvanceTimeTx converted to the pure model: holds *zap.Message, Time() is an offset read, NewAdvanceTimeTx builds once, Bytes() returns the buffer. No marshal/unmarshal anywhere. Template for the remaining 21 types. Package migrates atomically (codec.go + all types + Parse/Sign together), so it compiles green again only when the whole set + concentrated consumers (executor/builder/fee/api, ~63 New sites) are converted. WIP. Co-authored-by: Hanzo Dev <dev@hanzo.ai>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
// Copyright (C) 2019-2026, Lux Industries Inc. All rights reserved.
|
||||||
// See the file LICENSE for licensing terms.
|
// See the file LICENSE for licensing terms.
|
||||||
|
|
||||||
package txs
|
package txs
|
||||||
@@ -7,61 +7,52 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/luxfi/runtime"
|
|
||||||
|
|
||||||
"github.com/luxfi/ids"
|
"github.com/luxfi/ids"
|
||||||
"github.com/luxfi/math/set"
|
"github.com/luxfi/math/set"
|
||||||
|
"github.com/luxfi/runtime"
|
||||||
lux "github.com/luxfi/utxo"
|
lux "github.com/luxfi/utxo"
|
||||||
|
"github.com/luxfi/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ UnsignedTx = (*AdvanceTimeTx)(nil)
|
var _ UnsignedTx = (*AdvanceTimeTx)(nil)
|
||||||
|
|
||||||
// AdvanceTimeTx is a transaction to increase the chain's timestamp.
|
// AdvanceTimeTx increases the chain's timestamp. The struct IS the wire: it
|
||||||
// When the chain's timestamp is updated (a AdvanceTimeTx is accepted and
|
// holds the zap buffer and reads its field by offset. No codec, no marshal.
|
||||||
// followed by a commit block) the staker set is also updated accordingly.
|
//
|
||||||
// It must be that:
|
// Wire: zap header + object{ kind:u8@0, Time:u64@1 }.
|
||||||
// - proposed timestamp > [current chain time]
|
|
||||||
// - proposed timestamp <= [time for next staker set change]
|
|
||||||
type AdvanceTimeTx struct {
|
type AdvanceTimeTx struct {
|
||||||
// Unix time this block proposes increasing the timestamp to
|
msg *zap.Message
|
||||||
Time uint64 `serialize:"true" json:"time"`
|
|
||||||
|
|
||||||
unsignedBytes []byte // Unsigned byte representation of this data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *AdvanceTimeTx) SetBytes(unsignedBytes []byte) {
|
const offAdvanceTimeTime = 1 // u64, after kind@0
|
||||||
tx.unsignedBytes = unsignedBytes
|
|
||||||
|
// NewAdvanceTimeTx builds the tx into a fresh zap buffer — the one place a
|
||||||
|
// field becomes bytes (construction, not serialization).
|
||||||
|
func NewAdvanceTimeTx(t uint64) *AdvanceTimeTx {
|
||||||
|
b := zap.NewBuilder(zap.HeaderSize + 16 + 9)
|
||||||
|
ob := b.StartObject(9)
|
||||||
|
ob.SetUint8(offKind, uint8(kindAdvanceTime))
|
||||||
|
ob.SetUint64(offAdvanceTimeTime, t)
|
||||||
|
ob.FinishAsRoot()
|
||||||
|
msg, _ := zap.Parse(b.Finish())
|
||||||
|
return &AdvanceTimeTx{msg: msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *AdvanceTimeTx) Bytes() []byte {
|
// Time is the unix time this tx proposes advancing the chain to (offset read).
|
||||||
return tx.unsignedBytes
|
func (tx *AdvanceTimeTx) Time() uint64 { return tx.msg.Root().Uint64(offAdvanceTimeTime) }
|
||||||
}
|
|
||||||
|
|
||||||
func (*AdvanceTimeTx) InitRuntime(*runtime.Runtime) {}
|
// Timestamp returns Time as a time.Time.
|
||||||
|
func (tx *AdvanceTimeTx) Timestamp() time.Time { return time.Unix(int64(tx.Time()), 0) }
|
||||||
|
|
||||||
// Timestamp returns the time this block is proposing the chain should be set to
|
// Bytes returns the underlying zap buffer literally — no encode.
|
||||||
func (tx *AdvanceTimeTx) Timestamp() time.Time {
|
func (tx *AdvanceTimeTx) Bytes() []byte { return tx.msg.Bytes() }
|
||||||
return time.Unix(int64(tx.Time), 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*AdvanceTimeTx) InputIDs() set.Set[ids.ID] {
|
// SetBytes wraps b as this tx's buffer (zero copy); the Parse dispatch uses it.
|
||||||
return nil
|
func (tx *AdvanceTimeTx) SetBytes(b []byte) { tx.msg, _ = zap.Parse(b) }
|
||||||
}
|
|
||||||
|
|
||||||
func (*AdvanceTimeTx) Outputs() []*lux.TransferableOutput {
|
func (*AdvanceTimeTx) InitRuntime(*runtime.Runtime) {}
|
||||||
return nil
|
func (*AdvanceTimeTx) InputIDs() set.Set[ids.ID] { return nil }
|
||||||
}
|
func (*AdvanceTimeTx) Outputs() []*lux.TransferableOutput { return nil }
|
||||||
|
func (*AdvanceTimeTx) SyntacticVerify(*runtime.Runtime) error { return nil }
|
||||||
func (*AdvanceTimeTx) SyntacticVerify(*runtime.Runtime) error {
|
func (tx *AdvanceTimeTx) Visit(visitor Visitor) error { return visitor.AdvanceTimeTx(tx) }
|
||||||
return nil
|
func (tx *AdvanceTimeTx) Initialize(context.Context) error { return nil }
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *AdvanceTimeTx) Visit(visitor Visitor) error {
|
|
||||||
return visitor.AdvanceTimeTx(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// InitializeWithRuntime initializes the transaction with Runtime
|
|
||||||
func (tx *AdvanceTimeTx) Initialize(ctx context.Context) error {
|
|
||||||
// Initialize any context-dependent fields here
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
// Copyright (C) 2019-2026, Lux Industries Inc. All rights reserved.
|
||||||
|
// See the file LICENSE for licensing terms.
|
||||||
|
|
||||||
|
package txs
|
||||||
|
|
||||||
|
import "github.com/luxfi/zap"
|
||||||
|
|
||||||
|
// kind is the 1-byte tx discriminator at object offset 0 of every P-chain tx
|
||||||
|
// buffer. It is the whole dispatch: Parse reads it and returns the typed
|
||||||
|
// accessor. There is no codec, no version, no slot map.
|
||||||
|
type kind uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
kindReserved kind = iota
|
||||||
|
kindAdvanceTime
|
||||||
|
kindRewardValidator
|
||||||
|
kindBase
|
||||||
|
kindImport
|
||||||
|
kindExport
|
||||||
|
kindCreateNetwork
|
||||||
|
kindCreateChain
|
||||||
|
kindTransferChainOwnership
|
||||||
|
kindRemoveChainValidator
|
||||||
|
kindTransformChain
|
||||||
|
kindAddValidator
|
||||||
|
kindAddChainValidator
|
||||||
|
kindAddDelegator
|
||||||
|
kindAddPermissionlessValidator
|
||||||
|
kindAddPermissionlessDelegator
|
||||||
|
kindConvertNetworkToL1
|
||||||
|
kindCreateSovereignL1
|
||||||
|
kindRegisterL1Validator
|
||||||
|
kindSetL1ValidatorWeight
|
||||||
|
kindIncreaseL1ValidatorBalance
|
||||||
|
kindDisableL1Validator
|
||||||
|
kindSlashValidator
|
||||||
|
kindCreateAsset
|
||||||
|
kindOperation
|
||||||
|
)
|
||||||
|
|
||||||
|
// offKind is the fixed wire position of the discriminator (object offset 0).
|
||||||
|
const offKind = 0
|
||||||
|
|
||||||
|
// kindOf reads the discriminator from a parsed tx buffer.
|
||||||
|
func kindOf(msg *zap.Message) kind {
|
||||||
|
return kind(msg.Root().Uint8(offKind))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user