package services import ( "time" "ripple/bug" "ripple/config" "ripple/crypto" "ripple/lockstep" "ripple/state" "ripple/transport" "ripple/types" ) type PaymentManager struct { st *state.State d *transport.Data lockstep *lockstep.Lockstep accSender *transport.AccountTransport FailsafeCh chan struct{} } func NewPaymentManager( st *state.State, d *transport.Data, lockstep *lockstep.Lockstep, accSender *transport.AccountTransport, ) *PaymentManager { m := &PaymentManager{ st: st, d: d, lockstep: lockstep, accSender: accSender, FailsafeCh: make(chan struct{}), } return m } func (m *PaymentManager) InitPayment( identifier types.UserIdentifier, port int, secretKey [32]byte, amount int64, ) { delete(m.st.Memory.Pathfinding, m.st.Memory.Payment) m.d.AddCounterpart(identifier, port, secretKey) timeout := time.Now().Add(config.Timeout).Unix() paymentID := crypto.Sha256(secretKey[:]) m.st.Memory.Pathfinding[paymentID] = state.Pathfinding{ Amount: amount, Timeout: timeout, Counterpart: identifier, } m.st.Memory.Payment = paymentID } func (m *PaymentManager) AddPathfinding(paymentID [32]byte, amount int64, id types.UserIdentifier) 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, Timeout: time.Now().Add(config.Timeout).Unix(), } if amount < 0 { newPath.Incoming = id } else { newPath.Outgoing = id } m.st.Memory.Pathfinding[paymentID] = newPath return true } //func (m *PaymentManager) RemovePath(paymentID [32]byte) { // m.st.Memory.RemovePathfinding(paymentID) //}