tx binary with block time

This commit is contained in:
thloyi
2026-06-05 10:35:49 +08:00
parent 9f17ffce61
commit 44eecac087
2 changed files with 175 additions and 15 deletions

View File

@@ -19,6 +19,7 @@ func TestTxBinaryRoundTrip(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 123456789,
BlockIndex: 42,
BlockAt: 1710000000,
TxHash: &txHash,
CuFee: decimal.NewFromInt(5000),
CUPrice: decimal.RequireFromString("0.123456"),
@@ -118,6 +119,9 @@ func TestTxBinaryRoundTrip(t *testing.T) {
if decoded.BlockIndex != original.BlockIndex {
t.Fatalf("BlockIndex = %d, want %d", decoded.BlockIndex, original.BlockIndex)
}
if decoded.BlockAt != original.BlockAt {
t.Fatalf("BlockAt = %d, want %d", decoded.BlockAt, original.BlockAt)
}
if decoded.TxHash == nil {
t.Fatal("TxHash = nil, want non-nil")
}
@@ -510,6 +514,7 @@ func TestTxsBinaryRoundTripWithSharedAddressTable(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 1,
BlockIndex: 1,
BlockAt: 1710000001,
CuFee: decimal.NewFromInt(1000),
CUPrice: decimal.RequireFromString("0.123456"),
BeforeSolBalance: decimal.RequireFromString("1.000000000"),
@@ -560,6 +565,7 @@ func TestTxsBinaryRoundTripWithSharedAddressTable(t *testing.T) {
tx2 := tx1
tx2.Block = 2
tx2.BlockIndex = 2
tx2.BlockAt = 1710000002
tx2.CuFee = decimal.NewFromInt(2000)
tx2.AfterSOLBalance = decimal.RequireFromString("0.700000000")
tx2.Swaps = []Swap{tx1.Swaps[0]}
@@ -581,6 +587,9 @@ func TestTxsBinaryRoundTripWithSharedAddressTable(t *testing.T) {
if decoded[0].Signer != tx1.Signer || decoded[1].Signer != tx2.Signer {
t.Fatalf("decoded signer mismatch")
}
if decoded[0].BlockAt != tx1.BlockAt || decoded[1].BlockAt != tx2.BlockAt {
t.Fatalf("decoded block_at mismatch")
}
if decoded[0].Swaps[0].Pool != tx1.Swaps[0].Pool || decoded[1].Swaps[0].Pool != tx2.Swaps[0].Pool {
t.Fatalf("decoded shared address mismatch")
}
@@ -603,6 +612,7 @@ func TestDecodeTxsBinaryReader(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 100,
BlockIndex: 7,
BlockAt: 1710000100,
CuFee: decimal.NewFromInt(111),
CUPrice: decimal.RequireFromString("0.123456"),
BeforeSolBalance: decimal.RequireFromString("1.000000000"),
@@ -649,6 +659,7 @@ func TestDecodeTxsBinaryReader(t *testing.T) {
tx2 := tx1
tx2.Block = 101
tx2.BlockIndex = 8
tx2.BlockAt = 1710000101
tx2.CuFee = decimal.NewFromInt(222)
tx2.AfterSOLBalance = decimal.RequireFromString("0.300000000")
tx2.Swaps = []Swap{tx1.Swaps[0]}
@@ -677,6 +688,9 @@ func TestDecodeTxsBinaryReader(t *testing.T) {
if decoded[0].Block != tx1.Block || decoded[1].Block != tx2.Block {
t.Fatalf("decoded block mismatch")
}
if decoded[0].BlockAt != tx1.BlockAt || decoded[1].BlockAt != tx2.BlockAt {
t.Fatalf("decoded block_at mismatch")
}
if decoded[0].Swaps[0].BaseAmount.Cmp(tx1.Swaps[0].BaseAmount) != 0 {
t.Fatalf("decoded tx1 swap base amount = %s, want %s", decoded[0].Swaps[0].BaseAmount, tx1.Swaps[0].BaseAmount)
}
@@ -724,6 +738,7 @@ func TestMergeTxsBinaryBytes(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 11,
BlockIndex: 1,
BlockAt: 1710000011,
CuFee: decimal.NewFromInt(10),
CUPrice: decimal.RequireFromString("0.000123"),
BeforeSolBalance: decimal.RequireFromString("1.100000000"),
@@ -755,6 +770,7 @@ func TestMergeTxsBinaryBytes(t *testing.T) {
Signer: mustPubKey("SysvarRent111111111111111111111111111111111"),
Block: 12,
BlockIndex: 2,
BlockAt: 1710000012,
CuFee: decimal.NewFromInt(20),
CUPrice: decimal.RequireFromString("0.000456"),
BeforeSolBalance: decimal.RequireFromString("2.200000000"),
@@ -818,6 +834,9 @@ func TestMergeTxsBinaryBytes(t *testing.T) {
if decoded[0].Block != tx1.Block || decoded[1].Block != tx2.Block {
t.Fatalf("decoded block mismatch")
}
if decoded[0].BlockAt != tx1.BlockAt || decoded[1].BlockAt != tx2.BlockAt {
t.Fatalf("decoded block_at mismatch")
}
}
func TestMergeTxsBinarySourcesToWriterWithConcatenatedBatches(t *testing.T) {
@@ -825,6 +844,7 @@ func TestMergeTxsBinarySourcesToWriterWithConcatenatedBatches(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 21,
BlockIndex: 1,
BlockAt: 1710000021,
CuFee: decimal.NewFromInt(1),
CUPrice: decimal.RequireFromString("0.000001"),
BeforeSolBalance: decimal.RequireFromString("1.000000000"),
@@ -835,9 +855,11 @@ func TestMergeTxsBinarySourcesToWriterWithConcatenatedBatches(t *testing.T) {
tx2 := tx1
tx2.Block = 22
tx2.BlockIndex = 2
tx2.BlockAt = 1710000022
tx2.Signer = mustPubKey("SysvarRent111111111111111111111111111111111")
tx3 := tx1
tx3.Block = 23
tx3.BlockAt = 1710000023
tx3.BlockIndex = 3
tx3.Signer = mustPubKey("ComputeBudget111111111111111111111111111111")
@@ -880,6 +902,9 @@ func TestMergeTxsBinarySourcesToWriterWithConcatenatedBatches(t *testing.T) {
if decoded[0].Block != tx1.Block || decoded[1].Block != tx2.Block || decoded[2].Block != tx3.Block {
t.Fatalf("decoded block order mismatch")
}
if decoded[0].BlockAt != tx1.BlockAt || decoded[1].BlockAt != tx2.BlockAt || decoded[2].BlockAt != tx3.BlockAt {
t.Fatalf("decoded block_at order mismatch")
}
}
func TestMergeTxsBinarySourcesToWriterWithBatchHeaderFuncSkip(t *testing.T) {
@@ -887,6 +912,7 @@ func TestMergeTxsBinarySourcesToWriterWithBatchHeaderFuncSkip(t *testing.T) {
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 31,
BlockIndex: 1,
BlockAt: 1710000031,
CuFee: decimal.NewFromInt(1),
CUPrice: decimal.RequireFromString("0.000001"),
BeforeSolBalance: decimal.RequireFromString("1.000000000"),
@@ -897,10 +923,12 @@ func TestMergeTxsBinarySourcesToWriterWithBatchHeaderFuncSkip(t *testing.T) {
tx2 := tx1
tx2.Block = 32
tx2.BlockIndex = 2
tx2.BlockAt = 1710000032
tx2.Signer = mustPubKey("SysvarRent111111111111111111111111111111111")
tx3 := tx1
tx3.Block = 33
tx3.BlockIndex = 3
tx3.BlockAt = 1710000033
tx3.Signer = mustPubKey("ComputeBudget111111111111111111111111111111")
batch1, err := EncodeTxsBinary([]Tx{tx1})
@@ -960,15 +988,127 @@ func TestMergeTxsBinarySourcesToWriterWithBatchHeaderFuncSkip(t *testing.T) {
if decoded[0].Block != tx1.Block || decoded[1].Block != tx3.Block {
t.Fatalf("decoded block order mismatch after skip")
}
if decoded[0].BlockAt != tx1.BlockAt || decoded[1].BlockAt != tx3.BlockAt {
t.Fatalf("decoded block_at order mismatch after skip")
}
if source.opens != 2 {
t.Fatalf("source.opens = %d, want 2", source.opens)
}
}
func TestTxBinaryDecodeSchemaV3LeavesBlockAtZero(t *testing.T) {
original := &Tx{
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 41,
BlockIndex: 1,
BlockAt: 1710000041,
}
encoded := mustEncodeTxBinaryV3(t, original)
decoded, err := DecodeTxBinary(encoded)
if err != nil {
t.Fatalf("DecodeTxBinary(v3) error = %v", err)
}
if decoded.Block != original.Block || decoded.BlockIndex != original.BlockIndex {
t.Fatalf("decoded block mismatch: got (%d,%d), want (%d,%d)", decoded.Block, decoded.BlockIndex, original.Block, original.BlockIndex)
}
if decoded.BlockAt != 0 {
t.Fatalf("BlockAt = %d, want 0 for legacy v3", decoded.BlockAt)
}
}
func TestMergeTxsBinaryBytesUpgradesSchemaV3AndPreservesV4BlockAt(t *testing.T) {
legacyTx := Tx{
Signer: mustPubKey("So11111111111111111111111111111111111111112"),
Block: 51,
BlockIndex: 1,
BlockAt: 1710000051,
}
currentTx := Tx{
Signer: mustPubKey("SysvarRent111111111111111111111111111111111"),
Block: 52,
BlockIndex: 2,
BlockAt: 1710000052,
}
merged, err := MergeTxsBinaryBytes([][]byte{
mustEncodeTxsBinaryV3(t, []Tx{legacyTx}),
mustEncodeTxsBinary(t, []Tx{currentTx}),
})
if err != nil {
t.Fatalf("MergeTxsBinaryBytes(v3,v4) error = %v", err)
}
var mergedBinary TxsBinary
if err := mergedBinary.UnmarshalBinary(merged); err != nil {
t.Fatalf("UnmarshalBinary(merged) error = %v", err)
}
if mergedBinary.SchemaVersion != txBinarySchemaVersionCurrent {
t.Fatalf("merged schema version = %d, want %d", mergedBinary.SchemaVersion, txBinarySchemaVersionCurrent)
}
decoded, err := DecodeTxsBinary(merged)
if err != nil {
t.Fatalf("DecodeTxsBinary(merged) error = %v", err)
}
if len(decoded) != 2 {
t.Fatalf("decoded len = %d, want 2", len(decoded))
}
if decoded[0].BlockAt != 0 {
t.Fatalf("legacy BlockAt = %d, want 0", decoded[0].BlockAt)
}
if decoded[1].BlockAt != currentTx.BlockAt {
t.Fatalf("current BlockAt = %d, want %d", decoded[1].BlockAt, currentTx.BlockAt)
}
}
func mustPubKey(value string) solana.PublicKey {
return solana.MustPublicKeyFromBase58(value)
}
func mustEncodeTxBinaryV3(t *testing.T, tx *Tx) []byte {
t.Helper()
binaryTx, err := NewTxBinary(tx)
if err != nil {
t.Fatalf("NewTxBinary() error = %v", err)
}
binaryTx.SchemaVersion = txBinarySchemaVersionV3
encoded, err := binaryTx.MarshalBinary()
if err != nil {
t.Fatalf("MarshalBinary(v3) error = %v", err)
}
return encoded
}
func mustEncodeTxsBinary(t *testing.T, txs []Tx) []byte {
t.Helper()
encoded, err := EncodeTxsBinary(txs)
if err != nil {
t.Fatalf("EncodeTxsBinary() error = %v", err)
}
return encoded
}
func mustEncodeTxsBinaryV3(t *testing.T, txs []Tx) []byte {
t.Helper()
binaryTxs, err := NewTxsBinary(txs)
if err != nil {
t.Fatalf("NewTxsBinary() error = %v", err)
}
binaryTxs.SchemaVersion = txBinarySchemaVersionV3
for i := range binaryTxs.Txs {
binaryTxs.Txs[i].SchemaVersion = txBinarySchemaVersionV3
}
encoded, err := binaryTxs.MarshalBinary()
if err != nil {
t.Fatalf("MarshalBinary(v3) error = %v", err)
}
return encoded
}
func mustTxBinary(t *testing.T, data []byte) *TxsBinary {
t.Helper()