package state import ( "time" "ripple/config" "ripple/types" ) type Pathfinding struct { Amount int64 Incoming types.UserIdentifier Outgoing types.UserIdentifier Counterpart types.UserIdentifier Depth uint8 Commit bool Timeout int64 } type Memory struct { Commit map[types.UserIdentifier]map[[32]byte]struct{} Pathfinding map[[32]byte]Pathfinding Payment [32]byte } 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) GetPathfinding(paymentID [32]byte) (Pathfinding, bool) { pf, ok := m.Pathfinding[paymentID] if !ok { return Pathfinding{}, false } if pf.Timeout < time.Now().Unix() { delete(m.Pathfinding, paymentID) if pf.Incoming != (types.UserIdentifier{}) { delete(m.Commit[pf.Incoming], paymentID) } if pf.Outgoing != (types.UserIdentifier{}) { delete(m.Commit[pf.Outgoing], paymentID) } return Pathfinding{}, false } return pf, true } func (pf *Pathfinding) PathFound() bool { if pf.Incoming == (types.UserIdentifier{}) || pf.Outgoing == (types.UserIdentifier{}) { return false } return true } func (pf *Pathfinding) SetCommit() { pf.Timeout = time.Now().Add(config.Timeout).Unix() pf.Commit = true }