package state import ( "time" "ripple/config" "ripple/crypto" "ripple/types" ) type Counterpart struct { Identifier types.UserIdentifier Port int SecretKey [32]byte CounterIn uint32 CounterOut uint32 } type Pathfinding struct { Amount uint64 Incoming types.UserIdentifier Outgoing types.UserIdentifier Counterpart types.UserIdentifier InOrOut bool PenaltyRate uint32 Fee uint64 Hops byte Depth uint8 Commit bool Timeout int64 } type Memory struct { Commit map[types.UserIdentifier]map[[32]byte]struct{} Pathfinding map[[32]byte]Pathfinding Preimage [32]byte Counterpart Counterpart } func (m *Memory) GetPaymentID() [32]byte { return crypto.Sha256(m.Preimage[:]) } func (m *Memory) AddAccount(id types.UserIdentifier) { m.Commit[id] = make(map[[32]byte]struct{}) } func (m *Memory) RemoveAccount(id types.UserIdentifier) { for paymentID := range m.Commit[id] { pf, ok := m.GetPathfinding(paymentID) if !ok || pf.PathFound() { continue } delete(m.Pathfinding, paymentID) } delete(m.Commit, id) } func (m *Memory) removePathfinding(paymentID [32]byte, incoming, outgoing types.UserIdentifier) { delete(m.Pathfinding, paymentID) if incoming.IsSet() { delete(m.Commit[incoming], paymentID) } if outgoing.IsSet() { delete(m.Commit[outgoing], paymentID) } } func (m *Memory) GetPathfinding(paymentID [32]byte) (Pathfinding, bool) { pf, ok := m.Pathfinding[paymentID] if !ok { return Pathfinding{}, false } if pf.Timeout < time.Now().Unix() { m.removePathfinding(paymentID, pf.Incoming, pf.Outgoing) return Pathfinding{}, false } return pf, true } func (m *Memory) RemovePathfinding(paymentID [32]byte) { pf, ok := m.Pathfinding[paymentID] if !ok { return } m.removePathfinding(paymentID, pf.Incoming, pf.Outgoing) } func (pf *Pathfinding) PathFound() bool { return pf.Incoming.IsSet() && (pf.Outgoing.IsSet() || pf.Counterpart.IsSet()) } func (pf *Pathfinding) SetCommit() { pf.Timeout = time.Now().Add(config.Timeout).Unix() pf.Commit = true } func (pf *Pathfinding) FeeIn() uint64 { return uint64(pf.Hops+1) * pf.Fee } func (pf *Pathfinding) FeeOut() uint64 { return uint64(pf.Hops) * pf.Fee } func (pf *Pathfinding) IsIncoming() bool { return !pf.InOrOut } func (pf *Pathfinding) IsOutgoing() bool { return pf.InOrOut }