package services import ( "time" "ripple/bug" "ripple/config" "ripple/state" "ripple/transport" "ripple/types" ) type PaymentManager struct { st *state.State accSender *transport.AccountTransport SyncRoutineCh chan struct{} AutoCancelCh chan struct{} CleanupPaymentCh chan struct{} } func NewPaymentManager( st *state.State, accSender *transport.AccountTransport, ) *PaymentManager { m := &PaymentManager{ st: st, accSender: accSender, SyncRoutineCh: make(chan struct{}), AutoCancelCh: make(chan struct{}), CleanupPaymentCh: make(chan struct{}), } go m.RunSyncRoutine() go m.RunAutoCancelRoutine() go m.RunCleanupPaymentRoutine() return m } func (m *PaymentManager) InitPayment( identifier types.UserIdentifier, port int, secretKey [32]byte, amount uint64, inOrOut bool, penaltyRate uint32, fee uint64, tax uint64, ) { delete(m.st.Memory.Pathfinding, m.st.Memory.GetPaymentID()) m.st.AddCounterpart(identifier, port, secretKey) timeout := time.Now().Add(config.Timeout).Unix() m.st.Memory.Preimage = secretKey m.st.Memory.Pathfinding[m.st.Memory.GetPaymentID()] = state.Pathfinding{ Amount: amount, InOrOut: inOrOut, Counterpart: identifier, PenaltyRate: penaltyRate, Fee: fee, Timeout: timeout, Tax: tax, } } func (m *PaymentManager) AddPathfinding(paymentID [32]byte, amount uint64, inOrOut bool, penaltyRate uint32, fee uint64, hops byte, id types.UserIdentifier, tax uint64) bool { if _, ok := m.st.Memory.Pathfinding[paymentID]; ok { panic(bug.BugStateViolated) } if len(m.st.Memory.Pathfinding) >= config.BufferSize { return false } newPath := state.Pathfinding{ Amount: amount, InOrOut: inOrOut, PenaltyRate: penaltyRate, Fee: fee, Timeout: time.Now().Add(config.Timeout).Unix(), Tax: tax, } if !inOrOut { newPath.Incoming = id } else { newPath.Outgoing = id newPath.Hops = hops+1 } m.st.Memory.Pathfinding[paymentID] = newPath return true }