Skip to content

Commit

Permalink
improve deepCopy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Jan 5, 2024
1 parent 5f4a2a5 commit 18a4355
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,11 +1608,66 @@ func (ms *InMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) MarkO
return errs
}

func (ms *InMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) deepCopyTx(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE] {
// TODO: COMPLETE DEEP COPY WORK
etx := *tx
etx.TxAttempts = make([]txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], len(tx.TxAttempts))
copy(etx.TxAttempts, tx.TxAttempts)
func (ms *InMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) deepCopyTx(
tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
) *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE] {
copyTx := txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]{
ID: tx.ID,
IdempotencyKey: tx.IdempotencyKey,
Sequence: tx.Sequence,
FromAddress: tx.FromAddress,
ToAddress: tx.ToAddress,
EncodedPayload: make([]byte, len(tx.EncodedPayload)),
Value: *new(big.Int).Set(&tx.Value),
FeeLimit: tx.FeeLimit,
Error: tx.Error,
BroadcastAt: tx.BroadcastAt,
InitialBroadcastAt: tx.InitialBroadcastAt,
CreatedAt: tx.CreatedAt,
State: tx.State,
TxAttempts: make([]txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], len(tx.TxAttempts)),
Meta: tx.Meta,
Subject: tx.Subject,
ChainID: tx.ChainID,
PipelineTaskRunID: tx.PipelineTaskRunID,
MinConfirmations: tx.MinConfirmations,
TransmitChecker: tx.TransmitChecker,
SignalCallback: tx.SignalCallback,
CallbackCompleted: tx.CallbackCompleted,
}

// Copy the EncodedPayload
copy(copyTx.EncodedPayload, tx.EncodedPayload)

// Copy the TxAttempts
for i, attempt := range tx.TxAttempts {
copyTx.TxAttempts[i] = ms.deepCopyTxAttempt(copyTx, attempt)
}

return &copyTx
}

return &etx
func (ms *InMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) deepCopyTxAttempt(
tx txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
attempt txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
) txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE] {
copyAttempt := txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]{
ID: attempt.ID,
TxID: attempt.TxID,
Tx: tx,
TxFee: attempt.TxFee,
ChainSpecificFeeLimit: attempt.ChainSpecificFeeLimit,
SignedRawTx: make([]byte, len(attempt.SignedRawTx)),
Hash: attempt.Hash,
CreatedAt: attempt.CreatedAt,
BroadcastBeforeBlockNum: attempt.BroadcastBeforeBlockNum,
State: attempt.State,
Receipts: make([]txmgrtypes.ChainReceipt[TX_HASH, BLOCK_HASH], len(attempt.Receipts)),
TxType: attempt.TxType,
}

copy(copyAttempt.SignedRawTx, attempt.SignedRawTx)
copy(copyAttempt.Receipts, attempt.Receipts)

return copyAttempt
}

0 comments on commit 18a4355

Please sign in to comment.